-
Notifications
You must be signed in to change notification settings - Fork 1
feat: validate enter-side amount intent for ERC-4626 vaults (ENG-2418, ENG-2419) #30
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
Merged
+490
−6
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f21788
fix(release): skip Socket Firewall on win32 (sfw breaks pnpm install …
ajag408 e967890
codeowners update
ajag408 ee27571
schema and type changes
ajag408 1307783
Merge branch 'main' into eng-2418-eng-2419-amount-intent-validation
ajag408 7c02227
feat: 4626 amount validation and tests
ajag408 a5f1daf
chore: address feedback
ajag408 b799fb0
fix: lint
ajag408 9e31143
fix: explicit block reason on decimal amount
ajag408 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { MAX_UINT256, matchesDeclaredAmount } from './amount'; | ||
|
|
||
| describe('MAX_UINT256', () => { | ||
| it('equals 2^256 - 1', () => { | ||
| expect(MAX_UINT256).toBe(2n ** 256n - 1n); | ||
| }); | ||
| }); | ||
|
|
||
| describe('matchesDeclaredAmount', () => { | ||
| const DECLARED = '1000000'; // 1 USDC (6 decimals), wei | ||
|
|
||
| it('returns true on exact match', () => { | ||
| expect(matchesDeclaredAmount(1000000n, DECLARED)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false when calldata amount is one above declared', () => { | ||
| expect(matchesDeclaredAmount(1000001n, DECLARED)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when calldata amount is one below declared', () => { | ||
| expect(matchesDeclaredAmount(999999n, DECLARED)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false on gross inflation', () => { | ||
| expect(matchesDeclaredAmount(1000000000000n, DECLARED)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true when declared is undefined (opt-in skip)', () => { | ||
| expect(matchesDeclaredAmount(123456789n, undefined)).toBe(true); | ||
| }); | ||
|
|
||
| it('matches maxUint256 against a declared maxUint256 (sanctioned infinite)', () => { | ||
| expect(matchesDeclaredAmount(MAX_UINT256, MAX_UINT256.toString())).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('rejects maxUint256 against a finite declared amount', () => { | ||
| expect(matchesDeclaredAmount(MAX_UINT256, DECLARED)).toBe(false); | ||
| }); | ||
|
|
||
| it('matches zero against declared "0"', () => { | ||
| expect(matchesDeclaredAmount(0n, '0')).toBe(true); | ||
| }); | ||
|
|
||
| it('throws on a non-integer declared string (defense-in-depth: unreachable via schema pattern + validator guard, which reject non-digit amounts first)', () => { | ||
| expect(() => matchesDeclaredAmount(1000000n, '1.5')).toThrow(); | ||
| expect(() => matchesDeclaredAmount(1000000n, 'abc')).toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export const MAX_UINT256 = (1n << 256n) - 1n; | ||
|
|
||
| // Opt-in: undefined declared amount → skip (returns true). | ||
| export function matchesDeclaredAmount( | ||
| calldataAmount: bigint, | ||
| declared?: string, | ||
| ): boolean { | ||
| if (declared === undefined) return true; | ||
| return calldataAmount === BigInt(declared); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
args.amount is now treated as base-unit bigInt, but the public contract still suggests passing api args like "0.01", since decimals is accepted but unused, this could false block valid 4626 txs...
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.
Great catch...Shield expects base-unit (wei) integer strings, matching calldata units exactly; Shield deliberately doesn't convert (a validator that infers units from decimals becomes ambiguous — "1000000" could be 1 USDC wei or 1M USDC human — and an ambiguous security comparison is gameable).
But you're right that the API surface didn't say that anywhere and decimals invites the wrong inference. Fixed by: (1) schema pattern: '^[0-9]+$' on amount so JSON callers get a clear schema error, (2) an explicit block reason ("Declared amount must be a base-unit integer string (wei)") for library callers instead of the generic BigInt parse failure, (3) JSDoc on ActionArguments.amount/decimals pinning the unit (decimals is informational context only).