-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathtrustSet.ts
146 lines (137 loc) · 4.05 KB
/
trustSet.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
import { ValidationError } from '../../errors'
import { IssuedCurrencyAmount } from '../common'
import {
BaseTransaction,
GlobalFlags,
isAmount,
validateBaseTransaction,
} from './common'
/**
* Enum representing values of {@link TrustSet} transaction flags.
*
* @category Transaction Flags
*/
export enum TrustSetFlags {
/**
* Authorize the other party to hold currency issued by this account. (No
* effect unless using the asfRequireAuth AccountSet flag.) Cannot be unset.
*/
tfSetfAuth = 0x00010000,
/**
* Enable the No Ripple flag, which blocks rippling between two trust lines.
* of the same currency if this flag is enabled on both.
*/
tfSetNoRipple = 0x00020000,
/** Disable the No Ripple flag, allowing rippling on this trust line. */
tfClearNoRipple = 0x00040000,
/** Freeze the trust line. */
tfSetFreeze = 0x00100000,
/** Unfreeze the trust line. */
tfClearFreeze = 0x00200000,
}
/**
* Map of flags to boolean values representing {@link TrustSet} transaction
* flags.
*
* @category Transaction Flags
*
* @example
* ```typescript
*
* const trustSetTx: TrustSet = {
* TransactionType: 'TrustSet',
* Account: wallet2.getClassicAddress(),
* LimitAmount: {
* currency: 'FOO',
* issuer: wallet1.getClassicAddress(),
* value: '10000000000',
* },
* Flags: {
* tfSetNoRipple: true
* }
* }
*
* // Autofill the tx to see how flags actually look compared to the interface usage.
* const autofilledTx = await client.autofill(trustSetTx)
* console.log(autofilledTx)
* // {
* // TransactionType: 'TrustSet',
* // Account: 'r9dAdQQCBcGajVSeC9CqW3LCugjPDnAkEb',
* // LimitAmount: {
* // currency: 'FOO',
* // issuer: 'rWZzUjo5xGiAoRBqzsndyzonXz47UV8u1',
* // value: '10000000000'
* // },
* // Flags: 131072,
* // Sequence: 21971483,
* // Fee: '12',
* // LastLedgerSequence: 21971503
* // }
* ```
*/
export interface TrustSetFlagsInterface extends GlobalFlags {
/**
* Authorize the other party to hold currency issued by this account. (No
* effect unless using the asfRequireAuth AccountSet flag.) Cannot be unset.
*/
tfSetfAuth?: boolean
/**
* Enable the No Ripple flag, which blocks rippling between two trust lines
* of the same currency if this flag is enabled on both.
*/
tfSetNoRipple?: boolean
/** Disable the No Ripple flag, allowing rippling on this trust line. */
tfClearNoRipple?: boolean
/** Freeze the trust line. */
tfSetFreeze?: boolean
/** Unfreeze the trust line. */
tfClearFreeze?: boolean
}
/**
* Create or modify a trust line linking two accounts.
*
* @category Transaction Models
*/
export interface TrustSet extends BaseTransaction {
TransactionType: 'TrustSet'
/**
* Object defining the trust line to create or modify, in the format of a
* Currency Amount.
*/
LimitAmount: IssuedCurrencyAmount
/**
* Value incoming balances on this trust line at the ratio of this number per
* 1,000,000,000 units. A value of 0 is shorthand for treating balances at
* face value.
*/
QualityIn?: number
/**
* Value outgoing balances on this trust line at the ratio of this number per
* 1,000,000,000 units. A value of 0 is shorthand for treating balances at
* face value.
*/
QualityOut?: number
Flags?: number | TrustSetFlagsInterface
}
/**
* Verify the form and type of a TrustSet at runtime.
*
* @param tx - A TrustSet Transaction.
* @throws When the TrustSet is malformed.
*/
export function validateTrustSet(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)
const { LimitAmount, QualityIn, QualityOut } = tx
if (LimitAmount === undefined) {
throw new ValidationError('TrustSet: missing field LimitAmount')
}
if (!isAmount(LimitAmount)) {
throw new ValidationError('TrustSet: invalid LimitAmount')
}
if (QualityIn !== undefined && typeof QualityIn !== 'number') {
throw new ValidationError('TrustSet: QualityIn must be a number')
}
if (QualityOut !== undefined && typeof QualityOut !== 'number') {
throw new ValidationError('TrustSet: QualityOut must be a number')
}
}