Skip to content

Commit 0e4a4f6

Browse files
kriampogo
authored andcommitted
sdk/node: accept amounts as strings
Closes #3680 Author: Keith Rarick <kr@xph.us> Date: Fri May 11 13:14:14 2018 -0700 upstream:77f78350bc59db23b041987c645428082ff8e21a
1 parent dea25db commit 0e4a4f6

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/api/transactions.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,21 @@ export class TransactionBuilder {
6161
*
6262
* @param {Object} params - Action parameters.
6363
* @param {String} params.flavorId - ID of flavor to be issued.
64-
* @param {Number|BigNumber} params.amount - Amount of the flavor to be issued.
64+
* @param {Number|String|BigNumber} params.amount - Amount of the flavor to be issued.
6565
* @param {String} params.destinationAccountId - Account ID specifying the
6666
* account controlling the flavor.
6767
* @param {Object} params.tokenTags - Tags to add to the receiving tokens.
6868
* @param {Object} params.actionTags - Tags to add to the action.
6969
*/
7070
public issue(params: {
7171
flavorId: string
72-
amount: number | BigNumber
72+
amount: number | string | BigNumber
7373
destinationAccountId: string
7474
tokenTags?: object
7575
actionTags?: object
7676
}) {
7777
validate(params, 'IssueActionSchema')
78+
params.amount = new BigNumber(params.amount)
7879
this.actions.push(Object.assign({}, params, { type: 'issue' }))
7980
}
8081

@@ -85,20 +86,21 @@ export class TransactionBuilder {
8586
* @param {String} params.sourceAccountId - Account ID specifying the account
8687
* controlling the flavor.
8788
* @param {String} params.flavorId - ID of flavor to be retired.
88-
* @param {Number|BigNumber} params.amount - Amount of the flavor to be retired.
89+
* @param {Number|String|BigNumber} params.amount - Amount of the flavor to be retired.
8990
* @param {String} params.filter - Token filter string, see {@link https://dashboard.seq.com/docs/filters}.
9091
* @param {Array<String|Number>} params.filterParams - Parameter values for filter string (if needed).
9192
* @param {Object} params.actionTags - Tags to add to the action.
9293
*/
9394
public retire(params: {
9495
sourceAccountId: string
9596
flavorId: string
96-
amount: number | BigNumber
97+
amount: number | string | BigNumber
9798
filter?: string
9899
filterParams?: object
99100
actionTags?: object
100101
}) {
101102
validate(params, 'RetireActionSchema')
103+
params.amount = new BigNumber(params.amount)
102104
this.actions.push(Object.assign({}, params, { type: 'retire' }))
103105
}
104106

@@ -110,7 +112,7 @@ export class TransactionBuilder {
110112
* @param {String} params.sourceAccountId - Account ID specifying the account
111113
* controlling the flavor.
112114
* @param {String} params.flavorId - ID of flavor to be transferred.
113-
* @param {Number|BigNumber} params.amount - Amount of the flavor to be transferred.
115+
* @param {Number|String|BigNumber} params.amount - Amount of the flavor to be transferred.
114116
* @param {String} params.destinationAccountId - Account ID specifying the
115117
* account controlling the flavor.
116118
* @param {String} params.filter - Token filter string, see {@link https://dashboard.seq.com/docs/filters}.
@@ -121,14 +123,15 @@ export class TransactionBuilder {
121123
public transfer(params: {
122124
sourceAccountId: string
123125
flavorId: string
124-
amount: number | BigNumber
126+
amount: number | string | BigNumber
125127
destinationAccountId: string
126128
filter?: string
127129
filterParams?: object
128130
tokenTags?: object
129131
actionTags?: object
130132
}) {
131133
validate(params, 'TransferActionSchema')
134+
params.amount = new BigNumber(params.amount)
132135
this.actions.push(Object.assign({}, params, { type: 'transfer' }))
133136
}
134137
}

src/validate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ ajv.addSchema(
7979
minimum: 0,
8080
maximum: Number.MAX_SAFE_INTEGER,
8181
},
82+
{ type: 'string' },
8283
{ type: 'object' },
8384
],
8485
},
@@ -105,6 +106,7 @@ ajv.addSchema(
105106
minimum: 0,
106107
maximum: Number.MAX_SAFE_INTEGER,
107108
},
109+
{ type: 'string' },
108110
{ type: 'object' },
109111
],
110112
},
@@ -128,6 +130,7 @@ ajv.addSchema(
128130
minimum: 0,
129131
maximum: Number.MAX_SAFE_INTEGER,
130132
},
133+
{ type: 'string' },
131134
{ type: 'object' },
132135
],
133136
},

test/transactions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ describe('Transaction', () => {
125125
assert.equal(amount, tx.actions[0].amount.toString())
126126
})
127127

128+
it('handles large numbers as strings', async () => {
129+
const gold = await createFlavor('gold')
130+
const alice = await createAccount('alice')
131+
const amount = '9223372036854775807'
132+
133+
const tx = await client.transactions.transact(builder => {
134+
builder.issue({
135+
flavorId: gold.id,
136+
amount: amount,
137+
destinationAccountId: alice.id,
138+
})
139+
})
140+
141+
assert.equal(amount, tx.actions[0].amount.toString())
142+
})
143+
128144
it('throws an error at the amount boundary', async () => {
129145
const gold = await createFlavor('gold')
130146
const alice = await createAccount('alice')

0 commit comments

Comments
 (0)