Skip to content

Commit de941c2

Browse files
committed
CodeRabbit suggestions: Refactor parseAmountValue function to use bigint, instead of number type. Include unit tests for comprehensive coverage of this function.
1 parent 6b79898 commit de941c2

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

packages/xrpl/src/models/transactions/NFTokenAcceptOffer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ export interface NFTokenAcceptOfferMetadata extends TransactionMetadataBase {
7171
}
7272

7373
function validateNFTokenBrokerFee(tx: Record<string, unknown>): void {
74-
const value = parseAmountValue(tx.NFTokenBrokerFee)
75-
if (Number.isNaN(value)) {
76-
throw new ValidationError('NFTokenAcceptOffer: invalid NFTokenBrokerFee')
74+
let value: bigint
75+
try {
76+
value = parseAmountValue(tx.NFTokenBrokerFee)
77+
} catch {
78+
throw new ValidationError(
79+
'NFTokenAcceptOffer: invalid NFTokenBrokerFee. BigInt constructor could not parse NFTokenBrokerFee',
80+
)
7781
}
7882

7983
if (value <= 0) {

packages/xrpl/src/models/transactions/NFTokenCreateOffer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,16 @@ function validateNFTokenBuyOfferCases(tx: Record<string, unknown>): void {
107107
)
108108
}
109109

110-
if (parseAmountValue(tx.Amount) <= 0) {
110+
let parsedAmount: bigint
111+
try {
112+
parsedAmount = parseAmountValue(tx.Amount)
113+
} catch {
114+
throw new ValidationError(
115+
'NFTokenCreateOffer: Invalid Amount, Amount field could not be parsed by BigInt constructor',
116+
)
117+
}
118+
119+
if (parsedAmount <= 0) {
111120
throw new ValidationError(
112121
'NFTokenCreateOffer: Amount must be greater than 0 for buy offers',
113122
)

packages/xrpl/src/models/transactions/common.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,17 @@ export function validateBaseTransaction(common: Record<string, unknown>): void {
356356
*
357357
* @param amount - An Amount to parse for its value.
358358
* @returns The parsed amount value, or NaN if the amount count not be parsed.
359+
* @throws ValidationError, if the input Amount is invalid
360+
* @throws SyntaxError, if Amount cannot be parsed by BigInt constructor
359361
*/
360-
export function parseAmountValue(amount: unknown): number {
362+
export function parseAmountValue(amount: unknown): bigint {
361363
if (!isAmount(amount)) {
362-
return NaN
364+
throw new ValidationError(
365+
'parseAmountValue: Specified input Amount is invalid',
366+
)
363367
}
364368
if (typeof amount === 'string') {
365-
return parseFloat(amount)
369+
return BigInt(amount)
366370
}
367-
return parseFloat(amount.value)
371+
return BigInt(amount.value)
368372
}

packages/xrpl/test/models/NFTokenCreateOffer.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
import { assert } from 'chai'
22

3-
import { validate, ValidationError, NFTokenCreateOfferFlags } from '../../src'
3+
import {
4+
validate,
5+
ValidationError,
6+
NFTokenCreateOfferFlags,
7+
IssuedCurrencyAmount,
8+
} from '../../src'
9+
import { parseAmountValue } from '../../src/models/transactions/common'
410

511
const NFTOKEN_ID =
612
'00090032B5F762798A53D543A014CAF8B297CFF8F2F937E844B17C9E00000003'
713

14+
describe('parseAmountValue', function () {
15+
it(`validate large amount values`, function () {
16+
// (Upper bound of created XRP tokens) minus 12 drops
17+
assert.equal(
18+
parseAmountValue('99999999999999988'),
19+
BigInt('99999999999999988'),
20+
)
21+
22+
// Token Amounts or Issued Currencies are represented using 54 bits of precision in the XRP Ledger
23+
// Docs: https://xrpl.org/docs/references/protocol/binary-format#token-amount-format
24+
const highest_iou_amount: IssuedCurrencyAmount = {
25+
currency: 'ABC',
26+
issuer: 'rIssuerAddress',
27+
// 54 bits can be used to safely represent a value of (2**54 - 1)
28+
value: '18014398509481983',
29+
}
30+
31+
assert.equal(
32+
parseAmountValue(highest_iou_amount),
33+
BigInt('18014398509481983'),
34+
)
35+
})
36+
37+
it(`validate non-positive amount values`, function () {
38+
assert.equal(parseAmountValue('0'), BigInt(0))
39+
assert.equal(parseAmountValue('-1234'), BigInt(-1234))
40+
})
41+
42+
it(`validate invalid amount values`, function () {
43+
assert.throws(
44+
() => parseAmountValue(1234),
45+
ValidationError,
46+
'parseAmountValue: Specified input Amount is invalid',
47+
)
48+
49+
assert.throws(
50+
() => parseAmountValue('abcd'),
51+
SyntaxError,
52+
'Cannot convert abcd to a BigInt',
53+
)
54+
})
55+
})
56+
857
/**
958
* NFTokenCreateOffer Transaction Verification Testing.
1059
*

0 commit comments

Comments
 (0)