-
Notifications
You must be signed in to change notification settings - Fork 71
feat: Add validation for inputs currency and currencyToken. #683
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 all commits
d7d752d
f1af478
49842fd
cced35b
e934afb
aae5bd3
77c1145
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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { | |
| Hex, | ||
| isAddress, | ||
| PublicClient, | ||
| zeroAddress, | ||
| } from "viem"; | ||
|
|
||
| import { aeneid, mainnet } from "./chain"; | ||
|
|
@@ -91,6 +92,62 @@ export const chainStringToViemChain = function (chainId: SupportedChainIds): Cha | |
| } | ||
| }; | ||
|
|
||
| /** | ||
| * White list of allowed currency tokens for each supported chain. | ||
| * Keys are SupportedChainIds (chain name or chain id), values are arrays of addresses (string). | ||
| */ | ||
| const allowedCurrenciesByChain = { | ||
| aeneid: [ | ||
| "0x1514000000000000000000000000000000000000" as Address, // WIP | ||
| "0xF2104833d386a2734a4eB3B8ad6FC6812F29E38E" as Address, // MERC20 | ||
| ], | ||
| 1315: [ | ||
| "0x1514000000000000000000000000000000000000" as Address, // WIP | ||
| "0xF2104833d386a2734a4eB3B8ad6FC6812F29E38E" as Address, // MERC20 | ||
| ], | ||
| mainnet: [ | ||
| "0x1514000000000000000000000000000000000000" as Address, // WIP | ||
| ], | ||
| 1514: [ | ||
| "0x1514000000000000000000000000000000000000" as Address, // WIP | ||
|
Contributor
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 WIP, could you use it from |
||
| ], | ||
| } as const satisfies Record<SupportedChainIds, readonly Address[]>; | ||
|
|
||
yingyangxu2026 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export const getAllowedCurrencies = (chainId: SupportedChainIds): readonly Address[] => { | ||
| return allowedCurrenciesByChain[chainId]; | ||
| }; | ||
|
|
||
| /** | ||
| * Validate that a currency token is allowed on a given chain. | ||
| * | ||
| * - If `currency` is the zero address, it's treated as "no currency" and allowed. | ||
| * - Otherwise, it must exist in the allowed currency whitelist for the chain. | ||
| * | ||
| * Throws an Error with a consistent message used across the SDK. | ||
| */ | ||
| export const assertCurrencyAllowed = (currency: Address, chainId: SupportedChainIds): void => { | ||
| if (currency === zeroAddress) return; | ||
|
Contributor
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. The lint will not pass when you run |
||
| if (!getAllowedCurrencies(chainId).includes(currency)) { | ||
| throw new Error(`Currency token ${currency} is not allowed on chain ${String(chainId)}.`); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Validate that all currency tokens are allowed on a given chain. | ||
| * Throws an Error with the same message shape previously used by callers that | ||
| * validated arrays (e.g. group royalties distribution). | ||
| */ | ||
| export const assertCurrenciesAllowed = ( | ||
|
Contributor
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. Could you rename validateXX?
Contributor
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. And the logic has some duplication with the caller. |
||
| currencies: readonly Address[], | ||
| chainId: SupportedChainIds, | ||
| ): void => { | ||
| if (currencies.length === 0) return; | ||
| // Keep legacy error message behavior (stringify the full list) for compatibility. | ||
| if (currencies.some((c) => c !== zeroAddress && !getAllowedCurrencies(chainId).includes(c))) { | ||
| throw new Error(`Currency token ${currencies.toString()} is not allowed on chain ${String(chainId)}.`); | ||
| } | ||
| }; | ||
|
|
||
| export const chain: Record<SupportedChainIds, ChainIds> = { | ||
| aeneid: 1315, | ||
| 1315: 1315, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| export const txHash = "0x063834efe214f4199b1ad7181ce8c5ced3e15d271c8e866da7c89e86ee629cfb"; | ||
| export const ipId = "0x73fcb515cee99e4991465ef586cfe2b072ebb512"; | ||
| export const aeneid = 1315; | ||
| export const mockERC20 = "0x73fcb515cee99e4991465ef586cfe2b072ebb512"; | ||
| export const mockAddress = "0x73fcb515cee99e4991465ef586cfe2b072ebb513"; | ||
| import { WIP_TOKEN_ADDRESS } from "../../src/constants/common"; | ||
|
|
||
| // Use addresses that are whitelisted for unit tests that exercise currency checks. | ||
| export const mockERC20 = "0xF2104833d386a2734a4eB3B8ad6FC6812F29E38E"; | ||
|
Contributor
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. You should directly refer to the |
||
| export const mockAddress = WIP_TOKEN_ADDRESS; | ||
|
Contributor
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. Please remove |
||
| // Valid address that is NOT in `allowedCurrenciesByChain` (for negative whitelist tests). | ||
| export const nonWhitelistedToken = "0x000000000000000000000000000000000000dEaD"; | ||
| // Intentionally invalid address string (for validateAddress/validateAddresses tests). | ||
| export const invalidAddress = "0x123"; | ||
| export const privateKey = "0x181b92e2017bff630f8d097e94bdd4c020e89b16b9b19c7cac21eb1ed25828a7"; | ||
| export const walletAddress = "0x24E80b7589f709C8eF728903fB66F92d049Db0e3"; | ||
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.
You should put the validation into the last step. It makes the entire logic correct.