Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more clean up for min/max adjustments #873

Merged
merged 5 commits into from
Mar 22, 2018
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions src/transaction/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Amount, Adjustment, MaxAdjustment,
MinAdjustment, Memo} from '../common/types/objects'


export type Payment = {
export interface Payment {
source: Adjustment | MaxAdjustment,
destination: Adjustment | MinAdjustment,
paths?: string,
Expand All @@ -30,11 +30,22 @@ import {Amount, Adjustment, MaxAdjustment,
limitQuality?: boolean
}

function isMaxAdjustment(
source: Adjustment | MaxAdjustment): source is MaxAdjustment {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: why is this on a new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exceeds 80 character max, enforced by our linting config :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Looks strange to me cause I'm used to the "standard" style

return (<MaxAdjustment>source).maxAmount !== undefined
}

function isMinAdjustment(
destination: Adjustment | MinAdjustment): destination is MinAdjustment {
return (<MinAdjustment>destination).minAmount !== undefined
}

function isXRPToXRPPayment(payment: Payment): boolean {
const sourceCurrency = _.get(payment, 'source.maxAmount.currency',
_.get(payment, 'source.amount.currency'))
const destinationCurrency = _.get(payment, 'destination.amount.currency',
_.get(payment, 'destination.minAmount.currency'))
const {source, destination} = payment
const sourceCurrency = isMaxAdjustment(source)
? source.maxAmount.currency : source.amount.currency
const destinationCurrency = isMinAdjustment(destination)
? destination.minAmount.currency : destination.amount.currency
return sourceCurrency === 'XRP' && destinationCurrency === 'XRP'
}

Expand Down Expand Up @@ -74,21 +85,29 @@ function createPaymentTransaction(address: string, paymentArgument: Payment
throw new ValidationError('address must match payment.source.address')
}

if (((<MaxAdjustment>payment.source).maxAmount && (<MinAdjustment>payment.destination).minAmount) ||
((<Adjustment>payment.source).amount && (<Adjustment>payment.destination).amount)) {
if (
(isMaxAdjustment(payment.source) && isMinAdjustment(payment.destination))
||
(!isMaxAdjustment(payment.source) && !isMinAdjustment(payment.destination))
) {
throw new ValidationError('payment must specify either (source.maxAmount '
+ 'and destination.amount) or (source.amount and destination.minAmount)')
}

const destinationAmount = isMinAdjustment(payment.destination)
? payment.destination.minAmount : payment.destination.amount
const sourceAmount = isMaxAdjustment(payment.source)
? payment.source.maxAmount : payment.source.amount

// when using destination.minAmount, rippled still requires that we set
// a destination amount in addition to DeliverMin. the destination amount
// is interpreted as the maximum amount to send. we want to be sure to
// send the whole source amount, so we set the destination amount to the
// maximum possible amount. otherwise it's possible that the destination
// cap could be hit before the source cap.
const amount = (<MinAdjustment>payment.destination).minAmount && !isXRPToXRPPayment(payment) ?
createMaximalAmount((<MinAdjustment>payment.destination).minAmount) :
((<Adjustment>payment.destination).amount || (<MinAdjustment>payment.destination).minAmount)
const amount =
(isMinAdjustment(payment.destination) && !isXRPToXRPPayment(payment))
? createMaximalAmount(destinationAmount) : destinationAmount

const txJSON: any = {
TransactionType: 'Payment',
Expand Down Expand Up @@ -121,16 +140,14 @@ function createPaymentTransaction(address: string, paymentArgument: Payment
// temREDUNDANT_SEND_MAX removed in:
// https://github.com/ripple/rippled/commit/
// c522ffa6db2648f1d8a987843e7feabf1a0b7de8/
if (payment.allowPartialPayment === true
|| (<MinAdjustment>payment.destination).minAmount !== undefined) {
if (payment.allowPartialPayment || isMinAdjustment(payment.destination)) {
txJSON.Flags |= paymentFlags.PartialPayment
}

txJSON.SendMax = toRippledAmount(
(<MaxAdjustment>payment.source).maxAmount || (<Adjustment>payment.source).amount)
txJSON.SendMax = toRippledAmount(sourceAmount)

if ((<MinAdjustment>payment.destination).minAmount !== undefined) {
txJSON.DeliverMin = toRippledAmount((<MinAdjustment>payment.destination).minAmount)
if (isMinAdjustment(payment.destination)) {
txJSON.DeliverMin = toRippledAmount(destinationAmount)
}

if (payment.paths !== undefined) {
Expand Down