Ready. This template is published in uzolabs/templates and has been deployed and verified on testnet. It’s a learning template and hasn’t been audited. Use test funds only.
What you’ll build
Your own swap scripts and widget on top of BDEX. Each change below is independent. The scripts and the widget sharefrontend/src/dex/, so most changes apply to both at once.
Prerequisites
- A copy of the template that quotes, from Use a template.
- The walkthrough, so you know how routes and approvals work.
Options per run
These need no code changes:Terminal
--slippage 1accepts up to 1% less than the quote, for this run only.--fee 500,--fee 3000or--fee 10000picks a V3 pool. Without it,swap-v3uses the pool with the best quote.
Change the code
1
Make your change
- Slippage and deadline
- More tokens
- Multi-hop routes
- Exact output
- Remove liquidity
Edit the defaults in Raising the maximum lets users accept worse prices by mistake. Keep it low unless you have a reason.
frontend/src/dex/math.ts. Values are in basis points, so 50 means 0.5%.frontend/src/dex/math.ts
2
Run the tests
Terminal
npm test covers the slippage maths and the failure reasons. Add a test for any new rule.3
Try it on testnet
Terminal
Embed the widget in your app
SwapCard in frontend/src/App.tsx is self-contained. To use it in your own wagmi app, copy:
SwapCardfromApp.tsx, withSwapResult.tsx.- The whole
frontend/src/dex/folder. frontend/src/styles.css, and the variables fromtheme.cssif you want the same look.
WagmiProvider and a QueryClientProvider. See wagmi setup.
Change the look
Edit the variables at the top offrontend/src/theme.css for colours and fonts. The two background glows switch off for users who prefer reduced motion.
Mainnet
Terminal
MAINNET. For the widget, set VITE_CHAIN_ID=677 in frontend/.env, run npm --prefix frontend run build, and host the frontend/dist folder on any static host.
Verify
There’s no contract to verify. After a swap, open the BOTScan link the script printed and check the token transfers match the “After” balances.Troubleshooting
EXPIRED or Transaction too old
EXPIRED or Transaction too old
More than 20 minutes passed between building the swap and mining it, usually because the wallet popup stayed open. Swap again for a fresh deadline.
Too little received or INSUFFICIENT_OUTPUT_AMOUNT
Too little received or INSUFFICIENT_OUTPUT_AMOUNT
The price moved past your slippage. Nothing was swapped and only gas was spent. Quote again, or raise slippage a little with
--slippage 1.No pool for a fee tier
No pool for a fee tier
Not every tier has a pool. On testnet there was no 0.01% WBOT/USDT pool when the template was tested. Run
npm run find-pools to see which exist.BOT and WBOT are 1:1 and need no pool
BOT and WBOT are 1:1 and need no pool
Use
npm run wrap -- 1 or npm run wrap -- 1 --unwrap. In the widget, the button says Wrap or Unwrap.The widget quotes nothing
The widget quotes nothing
Quotes appear about half a second after you stop typing. If they never do, check the browser console. A slow or blocked RPC is the usual cause. Set
VITE_RPC_URL in frontend/.env and restart npm run frontend.A new token's amounts are wildly wrong
A new token's amounts are wildly wrong
Its
decimals in getTokens doesn’t match the contract. Read decimals() on BOTScan and fix the entry.Next steps
BDEX V3 swaps
Fee tiers and the SwapRouter by hand.
Token template
Make a token, then add a pool for it.