Skip to content

feat: custom ERC20 destination and source tokens - #23

Open
soos3d wants to merge 13 commits into
mainfrom
feat/custom-erc20-tokens
Open

feat: custom ERC20 destination and source tokens#23
soos3d wants to merge 13 commits into
mainfrom
feat/custom-erc20-tokens

Conversation

@soos3d

@soos3d soos3d commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Any ERC20 can now be used on either side of a deposit, riding the Universal Account's buy/sell primitives:

  • Custom destination token — deposits are bought into the configured token via createBuyTransaction, then the full intermediary balance is transferred to the destination (src/sweep/custom-delivery.ts). Full-balance transfer makes delivery self-healing: anything stranded by a failed run is picked up by the next delivery.
  • Custom source tokensCustomSourceWatcher polls balanceOf for each configured token, sells arrivals into USDC (preferTokenType: USD), and the proceeds are detected and swept by the existing pipeline (src/sweep/custom-source.ts).

Key design points (established by a live spike with LINK on Base and verified end to end):

  • expectTokens/createUniversalTransaction is primary-tokens-only, so custom delivery must be buy → transfer (two UA transactions).
  • The buy is sized fee-aware: depositUSD − buyFee − transferReserve, where the reserve covers the follow-up transfer's fee — without it the bought tokens strand in the intermediary.
  • Unknown tokens fail with UA code -32686 ("Token metadata not found") — surfaced as a typed error, no retries; the source watcher marks such tokens unsellable.
  • A ConfigurationError rejects a token configured as both destination and source on the same chain (found live: the watcher sold the freshly bought destination balance).
  • Onramp is gated off for custom destinations (no RampNow route); the widget shows the custom symbol; the React provider passes sourceTokens through.

Known v1 limitations (documented in sdk/docs/CUSTOM-ERC20-PLAN.md)

  • EVM chains only on both sides (no SPL tokens).
  • Custom source tokens are excluded from refund-to-source.
  • No USD price feed for custom tokens — the per-token minAmount is the dust gate instead of minValueUSD.

Related

  • chore/remove-arbitrum is stacked on this branch (Arbitrum removal touched this branch's test files); it will be PR'd after this merges.

Test plan

  • 717 unit tests green (npm run test), including new suites: custom-token-config, custom-delivery, custom-source
  • npm run typecheck and npm run build pass
  • Live end-to-end: SOL deposit → LINK on Base delivered to destination (~14 s)
  • Live: destination/source conflict guard reproduced and fixed
  • Live source-token direction: send AERO on Base (≥ 0.01) to the deposit address; expect [CustomSource] sell logs, USDC proceeds, normal sweep (unit-tested; live run pending)

soos3d added 12 commits July 8, 2026 15:52
- CustomErc20Token / CustomSourceToken / DestinationToken types
- DestinationConfig.token accepts any ERC20 (EVM chains only)
- DepositClientConfig.sourceTokens for custom deposit sources
- resolveDestinationTokenSpec discriminated seam + isCustomToken guard
- Onramp gated off for custom destination tokens (no RampNow route)
- Sweeper throws a clear not-yet-implemented error for custom delivery
  (replaced by the buy->transfer pipeline in the next commit)
- src/sweep/custom-delivery.ts: buy the deposits' aggregate USD value into
  the custom token, wait for settlement + balance arrival, then transfer
  the UA's FULL token balance to the destination (self-healing: stranded
  balances from failed runs are recovered on the next delivery)
- buy amount ladder (100/97/94/90%) until fee-calibrated post-spike
- src/sweep/erc20-rpc.ts: dependency-free balanceOf reads over public RPC
- fromSmallestUnit added to erc20.ts
- Sweeper routes custom destination specs through the new flow
  (readTokenBalance + customDelivery tuning seams injectable for tests)
- src/sweep/custom-source.ts: CustomSourceWatcher polls balanceOf for each
  configured sourceTokens entry on the intermediary UA; at/above minAmount
  it sells the full balance via createSellTransaction (preferTokenType USD);
  proceeds are a primary asset the existing pipeline detects and delivers
- in-flight guard holds each token until the sell settles (no double-sell)
- src/sweep/ua-transaction.ts: shared sign+send with sweep error semantics
  (extracted from custom-delivery)
- DepositClient wires the watcher into startWatching/stopWatching/destroy
- DepositConfig.sourceTokens pass-through to DepositClient
- react entry re-exports DestinationToken / CustomErc20Token /
  CustomSourceToken types
(onramp gating and widget symbol display for custom destination tokens
landed with the type-union commit)
Live spike results (LINK on Base, 2026-07-08):
- buy quotes carry the standard feeQuotes shape -> reuse extractFeeBreakdown
- buy fees are charged ON TOP of amountInUSD from primary assets, and the
  post-buy transfer pays its own fee (~$0.14 on Base) from primary assets
  too -> executed buy amount is now baseUSD − buyFee − transferReserve
  (reserve = max(quoted gas fee, $0.05)); quote-size ladder kept only as
  a fallback for obtaining fee data
- unknown tokens fail with code -32686 'Token metadata not found' ->
  typed immediately (no pointless retries); the source watcher marks such
  tokens unsellable for the session
- buy result tokenChanges.incr matches balanceOf exactly (full-balance
  transfer approach confirmed)
- DestinationPanel: custom ERC20 editor (address/decimals/symbol, prefilled
  LINK on Base) applying via setDestination; custom-aware chain/token chips,
  onramp-unavailable notice, and dev-note snippet
- providers.tsx: sourceTokens configured with LINK on Base for source-flow
  testing
- BuyDemo/DepositDemo render via destinationTokenSymbol (custom token
  objects are not directly renderable)
- SDK reference: spike-verified fee overhead + -32686 error documented
- demo README: 'Testing custom ERC20 tokens' walkthrough
Live test found the two flows fighting: with LINK on Base as BOTH the
custom destination and a sourceTokens entry, the delivery bought ~$1.1 of
LINK and the source watcher immediately sold the balance back into USDC
(~$0.78 kept in the UA, sell fee lost), leaving the transfer only ~$0.08.

- assertDestinationNotSourceToken: ConfigurationError on same chain+address
  overlap, enforced at construction and setDestination (case-insensitive)
- demo: sourceTokens switched to AERO on Base so both directions are
  testable side by side; README + SDK reference document the limitation
The spike page served its purpose: it live-verified UA buy/sell/transfer
semantics for custom ERC20 tokens (documented in docs/CUSTOM-ERC20-PLAN.md).
The real flows are now covered by the SDK implementation and the demo's
custom-token wiring.
@vercel

vercel Bot commented Jul 14, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
universal-deposit-u53q Ready Ready Preview, Comment Jul 14, 2026 4:27pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant