Skip to content

Commit

Permalink
CodeRabbit suggestions: Refactor parseAmountValue function to use big…
Browse files Browse the repository at this point in the history
…int, instead of number type. Include unit tests for comprehensive coverage of this function.
  • Loading branch information
ckeshava committed Oct 3, 2024
1 parent cd6c998 commit d53dd5e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
10 changes: 7 additions & 3 deletions packages/xrpl/src/models/transactions/NFTokenAcceptOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ export interface NFTokenAcceptOfferMetadata extends TransactionMetadataBase {
}

function validateNFTokenBrokerFee(tx: Record<string, unknown>): void {
const value = parseAmountValue(tx.NFTokenBrokerFee)
if (Number.isNaN(value)) {
throw new ValidationError('NFTokenAcceptOffer: invalid NFTokenBrokerFee')
let value: bigint
try {
value = parseAmountValue(tx.NFTokenBrokerFee)
} catch {
throw new ValidationError(
'NFTokenAcceptOffer: invalid NFTokenBrokerFee. BigInt constructor could not parse NFTokenBrokerFee',
)
}

if (value <= 0) {
Expand Down
11 changes: 10 additions & 1 deletion packages/xrpl/src/models/transactions/NFTokenCreateOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ function validateNFTokenBuyOfferCases(tx: Record<string, unknown>): void {
)
}

if (parseAmountValue(tx.Amount) <= 0) {
let parsedAmount: bigint
try {
parsedAmount = parseAmountValue(tx.Amount)
} catch {
throw new ValidationError(
'NFTokenCreateOffer: Invalid Amount, Amount field could not be parsed by BigInt constructor',
)
}

if (parsedAmount <= 0) {
throw new ValidationError(
'NFTokenCreateOffer: Amount must be greater than 0 for buy offers',
)
Expand Down
12 changes: 8 additions & 4 deletions packages/xrpl/src/models/transactions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,17 @@ export function validateBaseTransaction(common: Record<string, unknown>): void {
*
* @param amount - An Amount to parse for its value.
* @returns The parsed amount value, or NaN if the amount count not be parsed.
* @throws ValidationError, if the input Amount is invalid
* @throws SyntaxError, if Amount cannot be parsed by BigInt constructor
*/
export function parseAmountValue(amount: unknown): number {
export function parseAmountValue(amount: unknown): bigint {
if (!isAmount(amount)) {
return NaN
throw new ValidationError(
'parseAmountValue: Specified input Amount is invalid',
)
}
if (typeof amount === 'string') {
return parseFloat(amount)
return BigInt(amount)
}
return parseFloat(amount.value)
return BigInt(amount.value)
}
51 changes: 50 additions & 1 deletion packages/xrpl/test/models/NFTokenCreateOffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
import { assert } from 'chai'

import { validate, ValidationError, NFTokenCreateOfferFlags } from '../../src'
import {
validate,
ValidationError,
NFTokenCreateOfferFlags,
IssuedCurrencyAmount,
} from '../../src'
import { parseAmountValue } from '../../src/models/transactions/common'

const NFTOKEN_ID =
'00090032B5F762798A53D543A014CAF8B297CFF8F2F937E844B17C9E00000003'

describe('parseAmountValue', function () {
it(`validate large amount values`, function () {
// (Upper bound of created XRP tokens) minus 12 drops
assert.equal(
parseAmountValue('99999999999999988'),
BigInt('99999999999999988'),
)

// Token Amounts or Issued Currencies are represented using 54 bits of precision in the XRP Ledger
// Docs: https://xrpl.org/docs/references/protocol/binary-format#token-amount-format
const highest_iou_amount: IssuedCurrencyAmount = {
currency: 'ABC',
issuer: 'rIssuerAddress',
// 54 bits can be used to safely represent a value of (2**54 - 1)
value: '18014398509481983',
}

assert.equal(
parseAmountValue(highest_iou_amount),
BigInt('18014398509481983'),
)
})

it(`validate non-positive amount values`, function () {
assert.equal(parseAmountValue('0'), BigInt(0))
assert.equal(parseAmountValue('-1234'), BigInt(-1234))
})

it(`validate invalid amount values`, function () {
assert.throws(
() => parseAmountValue(1234),
ValidationError,
'parseAmountValue: Specified input Amount is invalid',
)

assert.throws(
() => parseAmountValue('abcd'),
SyntaxError,
'Cannot convert abcd to a BigInt',
)
})
})

/**
* NFTokenCreateOffer Transaction Verification Testing.
*
Expand Down

0 comments on commit d53dd5e

Please sign in to comment.