-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathpayment.ts
278 lines (254 loc) · 7.85 KB
/
payment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* eslint-disable complexity -- Necessary for validatePayment */
import { ValidationError } from '../../errors'
import { Amount, Path } from '../common'
import { isFlagEnabled } from '../utils'
import {
BaseTransaction,
isAmount,
GlobalFlags,
validateBaseTransaction,
} from './common'
/**
* Enum representing values for Payment Transaction Flags.
*
* @category Transaction Flags
*/
export enum PaymentFlags {
/**
* Do not use the default path; only use paths included in the Paths field.
* This is intended to force the transaction to take arbitrage opportunities.
* Most clients do not need this.
*/
tfNoDirectRipple = 0x00010000,
/**
* If the specified Amount cannot be sent without spending more than SendMax,
* reduce the received amount instead of failing outright. See Partial.
* Payments for more details.
*/
tfPartialPayment = 0x00020000,
/**
* Only take paths where all the conversions have an input:output ratio that
* is equal or better than the ratio of Amount:SendMax. See Limit Quality for
* details.
*/
tfLimitQuality = 0x00040000,
}
/**
* Map of flags to boolean values representing {@link Payment} transaction
* flags.
*
* @category Transaction Flags
*
* @example
* ```typescript
* const partialPayment: Payment = {
* TransactionType: 'Payment',
* Account: 'rM9WCfJU6udpFkvKThRaFHDMsp7L8rpgN',
* Amount: {
* currency: 'FOO',
* value: '4000',
* issuer: 'rPzwM2JfCSDjhbesdTCqFjWWdK7eFtTwZz',
* },
* Destination: 'rPzwM2JfCSDjhbesdTCqFjWWdK7eFtTwZz',
* Flags: {
* tfPartialPayment: true
* }
* }
*
* // Autofill the tx to see how flags actually look compared to the interface usage.
* const autofilledTx = await client.autofill(partialPayment)
* console.log(autofilledTx)
* // {
* // TransactionType: 'Payment',
* // Account: 'rM9WCfJU6udpFkvKThRaFHDMsp7L8rpgN',
* // Amount: {
* // currency: 'FOO',
* // value: '4000',
* // issuer: 'rPzwM2JfCSDjhbesdTCqFjWWdK7eFtTwZz'
* // },
* // Destination: 'rPzwM2JfCSDjhbesdTCqFjWWdK7eFtTwZz',
* // Flags: 131072,
* // Sequence: 21970996,
* // Fee: '12',
* // LastLedgerSequence: 21971016
* // }
* ```
*/
export interface PaymentFlagsInterface extends GlobalFlags {
/**
* Do not use the default path; only use paths included in the Paths field.
* This is intended to force the transaction to take arbitrage opportunities.
* Most clients do not need this.
*/
tfNoDirectRipple?: boolean
/**
* If the specified Amount cannot be sent without spending more than SendMax,
* reduce the received amount instead of failing outright. See Partial.
* Payments for more details.
*/
tfPartialPayment?: boolean
/**
* Only take paths where all the conversions have an input:output ratio that
* is equal or better than the ratio of Amount:SendMax. See Limit Quality for
* details.
*/
tfLimitQuality?: boolean
}
/**
* A Payment transaction represents a transfer of value from one account to
* another.
*
* @category Transaction Models
*/
export interface Payment extends BaseTransaction {
TransactionType: 'Payment'
/**
* The amount of currency to deliver. For non-XRP amounts, the nested field
* names MUST be lower-case. If the tfPartialPayment flag is set, deliver up
* to this amount instead.
*/
Amount: Amount
/** The unique address of the account receiving the payment. */
Destination: string
/**
* Arbitrary tag that identifies the reason for the payment to the
* destination, or a hosted recipient to pay.
*/
DestinationTag?: number
/**
* Arbitrary 256-bit hash representing a specific reason or identifier for
* this payment.
*/
InvoiceID?: string
/**
* Array of payment paths to be used for this transaction. Must be omitted
* for XRP-to-XRP transactions.
*/
Paths?: Path[]
/**
* Highest amount of source currency this transaction is allowed to cost,
* including transfer fees, exchange rates, and slippage . Does not include
* the XRP destroyed as a cost for submitting the transaction. For non-XRP
* amounts, the nested field names MUST be lower-case. Must be supplied for
* cross-currency/cross-issue payments. Must be omitted for XRP-to-XRP
* Payments.
*/
SendMax?: Amount
/**
* Minimum amount of destination currency this transaction should deliver.
* Only valid if this is a partial payment. For non-XRP amounts, the nested
* field names are lower-case.
*/
DeliverMin?: Amount
Flags?: number | PaymentFlagsInterface
}
/**
* Verify the form and type of a Payment at runtime.
*
* @param tx - A Payment Transaction.
* @throws When the Payment is malformed.
*/
export function validatePayment(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)
if (tx.Amount === undefined) {
throw new ValidationError('PaymentTransaction: missing field Amount')
}
if (!isAmount(tx.Amount)) {
throw new ValidationError('PaymentTransaction: invalid Amount')
}
if (tx.Destination === undefined) {
throw new ValidationError('PaymentTransaction: missing field Destination')
}
if (!isAmount(tx.Destination)) {
throw new ValidationError('PaymentTransaction: invalid Destination')
}
if (tx.DestinationTag != null && typeof tx.DestinationTag !== 'number') {
throw new ValidationError(
'PaymentTransaction: DestinationTag must be a number',
)
}
if (tx.InvoiceID !== undefined && typeof tx.InvoiceID !== 'string') {
throw new ValidationError('PaymentTransaction: InvoiceID must be a string')
}
if (
tx.Paths !== undefined &&
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Only used by JS
!isPaths(tx.Paths as Array<Array<Record<string, unknown>>>)
) {
throw new ValidationError('PaymentTransaction: invalid Paths')
}
if (tx.SendMax !== undefined && !isAmount(tx.SendMax)) {
throw new ValidationError('PaymentTransaction: invalid SendMax')
}
checkPartialPayment(tx)
}
function checkPartialPayment(tx: Record<string, unknown>): void {
if (tx.DeliverMin != null) {
if (tx.Flags == null) {
throw new ValidationError(
'PaymentTransaction: tfPartialPayment flag required with DeliverMin',
)
}
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Only used by JS
const flags = tx.Flags as number | PaymentFlagsInterface
const isTfPartialPayment =
typeof flags === 'number'
? isFlagEnabled(flags, PaymentFlags.tfPartialPayment)
: flags.tfPartialPayment ?? false
if (!isTfPartialPayment) {
throw new ValidationError(
'PaymentTransaction: tfPartialPayment flag required with DeliverMin',
)
}
if (!isAmount(tx.DeliverMin)) {
throw new ValidationError('PaymentTransaction: invalid DeliverMin')
}
}
}
function isPathStep(pathStep: Record<string, unknown>): boolean {
if (pathStep.account !== undefined && typeof pathStep.account !== 'string') {
return false
}
if (
pathStep.currency !== undefined &&
typeof pathStep.currency !== 'string'
) {
return false
}
if (pathStep.issuer !== undefined && typeof pathStep.issuer !== 'string') {
return false
}
if (
pathStep.account !== undefined &&
pathStep.currency === undefined &&
pathStep.issuer === undefined
) {
return true
}
if (pathStep.currency !== undefined || pathStep.issuer !== undefined) {
return true
}
return false
}
function isPath(path: Array<Record<string, unknown>>): boolean {
for (const pathStep of path) {
if (!isPathStep(pathStep)) {
return false
}
}
return true
}
function isPaths(paths: Array<Array<Record<string, unknown>>>): boolean {
if (!Array.isArray(paths) || paths.length === 0) {
return false
}
for (const path of paths) {
if (!Array.isArray(path) || path.length === 0) {
return false
}
if (!isPath(path)) {
return false
}
}
return true
}