-
Notifications
You must be signed in to change notification settings - Fork 474
feat: support fxUSD and multi-decimal EIP-3009 stablecoins #109
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
Open
huwangtao123
wants to merge
3
commits into
BlockRunAI:main
Choose a base branch
from
huwangtao123:feat/fxusd-multi-asset-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+572
−39
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| DEFAULT_BASE_PAYMENT_ASSET, | ||
| fetchBasePaymentAsset, | ||
| fetchBasePaymentAssets, | ||
| normalizeBasePaymentAsset, | ||
| normalizeBasePaymentAssets, | ||
| } from "./payment-asset.js"; | ||
|
|
||
| describe("payment asset helpers", () => { | ||
| it("normalizes a valid flat response", () => { | ||
| const asset = normalizeBasePaymentAsset({ | ||
| asset: "0x1111111111111111111111111111111111111111", | ||
| symbol: "eurc", | ||
| decimals: 6, | ||
| name: "Euro Coin", | ||
| transferMethod: "eip3009", | ||
| }); | ||
|
|
||
| expect(asset).toEqual({ | ||
| chain: "base", | ||
| asset: "0x1111111111111111111111111111111111111111", | ||
| symbol: "EURC", | ||
| decimals: 6, | ||
| name: "Euro Coin", | ||
| transferMethod: "eip3009", | ||
| }); | ||
| }); | ||
|
|
||
| it("rejects non-eip3009 assets", () => { | ||
| const asset = normalizeBasePaymentAsset({ | ||
| asset: "0x1111111111111111111111111111111111111111", | ||
| symbol: "USDT", | ||
| decimals: 6, | ||
| name: "Tether", | ||
| transferMethod: "permit2", | ||
| }); | ||
|
|
||
| expect(asset).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("parses nested paymentAsset responses", async () => { | ||
| const mockFetch = vi.fn().mockResolvedValue( | ||
| new Response( | ||
| JSON.stringify({ | ||
| paymentAsset: { | ||
| asset: "0x2222222222222222222222222222222222222222", | ||
| symbol: "EURC", | ||
| decimals: 6, | ||
| name: "Euro Coin", | ||
| transferMethod: "eip3009", | ||
| }, | ||
| }), | ||
| { status: 200 }, | ||
| ), | ||
| ); | ||
|
|
||
| const asset = await fetchBasePaymentAsset( | ||
| "https://blockrun.ai/api", | ||
| mockFetch as unknown as typeof fetch, | ||
| ); | ||
| expect(asset?.asset).toBe("0x2222222222222222222222222222222222222222"); | ||
| expect(asset?.symbol).toBe("EURC"); | ||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| "https://blockrun.ai/api/v1/payment-metadata?chain=base", | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to the default asset for invalid metadata responses", async () => { | ||
| const mockFetch = vi.fn().mockResolvedValue( | ||
| new Response(JSON.stringify({ paymentAsset: { symbol: "EURC" } }), { status: 200 }), | ||
| ); | ||
|
|
||
| const asset = await fetchBasePaymentAsset( | ||
| "https://blockrun.ai/api", | ||
| mockFetch as unknown as typeof fetch, | ||
| ); | ||
| expect(asset).toEqual(DEFAULT_BASE_PAYMENT_ASSET); | ||
| }); | ||
|
|
||
| it("parses and sorts multiple assets by priority", () => { | ||
| const assets = normalizeBasePaymentAssets({ | ||
| paymentAssets: [ | ||
| { | ||
| asset: "0x3333333333333333333333333333333333333333", | ||
| symbol: "FXUSD", | ||
| decimals: 18, | ||
| name: "fxUSD", | ||
| transferMethod: "eip3009", | ||
| priority: 2, | ||
| }, | ||
| { | ||
| asset: "0x1111111111111111111111111111111111111111", | ||
| symbol: "USDC", | ||
| decimals: 6, | ||
| name: "USD Coin", | ||
| transferMethod: "eip3009", | ||
| priority: 1, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(assets.map((asset) => asset.symbol)).toEqual(["USDC", "FXUSD"]); | ||
| }); | ||
|
|
||
| it("fetches multiple payment assets", async () => { | ||
| const mockFetch = vi.fn().mockResolvedValue( | ||
| new Response( | ||
| JSON.stringify({ | ||
| paymentAssets: [ | ||
| { | ||
| asset: "0x1111111111111111111111111111111111111111", | ||
| symbol: "USDC", | ||
| decimals: 6, | ||
| name: "USD Coin", | ||
| transferMethod: "eip3009", | ||
| priority: 1, | ||
| }, | ||
| { | ||
| asset: "0x2222222222222222222222222222222222222222", | ||
| symbol: "EURC", | ||
| decimals: 6, | ||
| name: "Euro Coin", | ||
| transferMethod: "eip3009", | ||
| priority: 2, | ||
| }, | ||
| ], | ||
| }), | ||
| { status: 200 }, | ||
| ), | ||
| ); | ||
|
|
||
| const assets = await fetchBasePaymentAssets( | ||
| "https://blockrun.ai/api", | ||
| mockFetch as unknown as typeof fetch, | ||
| ); | ||
| expect(assets).toHaveLength(2); | ||
| expect(assets[0]?.symbol).toBe("USDC"); | ||
| expect(assets[1]?.symbol).toBe("EURC"); | ||
| }); | ||
|
|
||
| it("keeps USDC as the safe default asset", () => { | ||
| expect(DEFAULT_BASE_PAYMENT_ASSET.symbol).toBe("USDC"); | ||
| expect(DEFAULT_BASE_PAYMENT_ASSET.transferMethod).toBe("eip3009"); | ||
| }); | ||
|
|
||
| it("normalizes fxUSD with 18 decimals correctly", () => { | ||
| const asset = normalizeBasePaymentAsset({ | ||
| asset: "0x55380fe7a1910dff29a47b622057ab4139da42c5", | ||
| symbol: "fxusd", | ||
| decimals: 18, | ||
| name: "fxUSD", | ||
| transferMethod: "eip3009", | ||
| }); | ||
|
|
||
| expect(asset).toEqual({ | ||
| chain: "base", | ||
| asset: "0x55380fe7a1910dff29a47b622057ab4139da42c5", | ||
| symbol: "FXUSD", | ||
| decimals: 18, | ||
| name: "fxUSD", | ||
| transferMethod: "eip3009", | ||
| }); | ||
| }); | ||
|
|
||
| it("handles mixed-decimal assets in normalizeBasePaymentAssets", () => { | ||
| const assets = normalizeBasePaymentAssets({ | ||
| paymentAssets: [ | ||
| { | ||
| asset: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", | ||
| symbol: "USDC", | ||
| decimals: 6, | ||
| name: "USD Coin", | ||
| transferMethod: "eip3009", | ||
| priority: 1, | ||
| }, | ||
| { | ||
| asset: "0x55380fe7a1910dff29a47b622057ab4139da42c5", | ||
| symbol: "FXUSD", | ||
| decimals: 18, | ||
| name: "fxUSD", | ||
| transferMethod: "eip3009", | ||
| priority: 2, | ||
| }, | ||
| { | ||
| asset: "0x0000000000000000000000000000000000000000", | ||
| symbol: "DISABLED", | ||
| decimals: 6, | ||
| name: "Disabled Token", | ||
| transferMethod: "eip3009", | ||
| enabled: false, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(assets).toHaveLength(2); | ||
| expect(assets[0]?.symbol).toBe("USDC"); | ||
| expect(assets[0]?.decimals).toBe(6); | ||
| expect(assets[1]?.symbol).toBe("FXUSD"); | ||
| expect(assets[1]?.decimals).toBe(18); | ||
| }); | ||
|
|
||
| it("accepts the real fxUSD Base contract address", () => { | ||
| const asset = normalizeBasePaymentAsset({ | ||
| asset: "0x55380fe7A1910dFf29a47B622057AB4139DA42C5", | ||
| symbol: "FXUSD", | ||
| decimals: 18, | ||
| name: "fxUSD", | ||
| transferMethod: "eip3009", | ||
| }); | ||
|
|
||
| expect(asset).toBeDefined(); | ||
| expect(asset?.asset).toBe("0x55380fe7A1910dFf29a47B622057AB4139DA42C5"); | ||
| }); | ||
| }); |
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.
Potential race condition when
setAsset()is called concurrently with balance checks.Per the context snippets,
setAsset()is called fromonAfterPaymentCreation(async callback during payment creation) whilecheckSufficient()may be executing concurrently for other requests. The check-then-act pattern at lines 164-170 is not atomic with respect to async operations:checkBalance()→ awaitsfetchBalance()with asset XonAfterPaymentCreation→setAsset(Y)invalidates cachefetchBalance()returns balance for asset X, butthis.assetis now YThis could cause balance info to report the wrong
assetSymbolor cache a balance for the wrong asset.Consider either:
BalanceMonitorimmutable (create new instance per asset) — whichsrc/proxy.tsalready does in the asset selection loop at lines 3447-3457setAsset()should only be called when no concurrent operations are in flight🤖 Prompt for AI Agents