-
Notifications
You must be signed in to change notification settings - Fork 548
fix Autofill validation of DeliverMax
and Amount
#2857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
e7d6001
0e54f15
ef8efd7
3bfec9d
0ae2a9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* eslint-disable max-lines -- common utility file */ | ||
import { HEX_REGEX } from '@xrplf/isomorphic/utils' | ||
import BigNumber from 'bignumber.js' | ||
import { isValidClassicAddress, isValidXAddress } from 'ripple-address-codec' | ||
import { TRANSACTION_TYPES } from 'ripple-binary-codec' | ||
|
||
|
@@ -191,6 +192,46 @@ export function isAmount(amount: unknown): amount is Amount { | |
) | ||
} | ||
|
||
/** | ||
* Check if two amounts are equal. | ||
* | ||
* @param amount1 - The first amount to compare. | ||
* @param amount2 - The second amount to compare. | ||
* @returns Whether the two amounts are equal. | ||
* @throws When the amounts are not valid. | ||
*/ | ||
export function areAmountsEqual(amount1: unknown, amount2: unknown): boolean { | ||
const isAmount1Invalid = !isAmount(amount1) | ||
if (isAmount1Invalid || !isAmount(amount2)) { | ||
Comment on lines
+204
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: for cleanliness, I'd prefer if both were created as variables or both were checked inline during the condition |
||
throw new ValidationError( | ||
`Amount: invalid field. Expected Amount but received ${JSON.stringify( | ||
isAmount1Invalid ? amount1 : amount2, | ||
Comment on lines
+207
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for consistency with the other error messaging in the library, we could just leave this one as something along the lines of "invalid Amount", or slightly more descriptive instead of dynamic |
||
)}`, | ||
) | ||
} | ||
|
||
if (isString(amount1) && isString(amount2)) { | ||
return new BigNumber(amount1).eq(amount2) | ||
} | ||
|
||
if (isIssuedCurrency(amount1) && isIssuedCurrency(amount2)) { | ||
return ( | ||
amount1.currency === amount2.currency && | ||
amount1.issuer === amount2.issuer && | ||
new BigNumber(amount1.value).eq(amount2.value) | ||
) | ||
} | ||
|
||
if (isMPTAmount(amount1) && isMPTAmount(amount2)) { | ||
return ( | ||
amount1.mpt_issuance_id === amount2.mpt_issuance_id && | ||
new BigNumber(amount1.value).eq(amount2.value) | ||
) | ||
} | ||
|
||
return false | ||
} | ||
Comment on lines
+195
to
+233
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Missing test coverage for the This utility function handles critical amount comparisons for different currency types (XRP, IssuedCurrency, MPTAmount) and is used in transaction validation, but appears to lack dedicated test coverage. Consider adding comprehensive tests to verify:
🔗 Analysis chainImplementation looks solid and addresses the original bug. The function correctly handles all amount types (XRP, IssuedCurrency, MPTAmount) with proper validation and precise numeric comparisons using BigNumber. The implementation aligns with the PR objectives to fix amount validation issues. Let's verify the test coverage for this new function: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for test cases covering areAmountsEqual
rg -l "areAmountsEqual" "test_.*\.(ts|js)$"
Length of output: 101 Script: #!/bin/bash
# Find test files and search for areAmountsEqual usage
fd -e ts -e js . | rg "test|spec" | xargs rg "areAmountsEqual" -B 2 -A 2
# Also search for import statements
fd -e ts -e js . | rg "test|spec" | xargs rg "import.*areAmountsEqual" -B 1 -A 1
Length of output: 155 Script: #!/bin/bash
# Verify the source file location
fd common.ts
# Find test files in the same directory and parent directories
fd -e ts -e js . packages/xrpl/src/models/transactions/
# Search all files for areAmountsEqual references
rg "areAmountsEqual" -g "!node_modules" -g "!dist" -g "!build"
Length of output: 3762 |
||
|
||
/** | ||
* Verify the form and type of an XChainBridge at runtime. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would describe this under "Fixed" instead