How V2 liquidity works
A V2 pool holds two tokens. When you add liquidity, you deposit both at the pool’s current ratio and receive LP tokens. The pair contract is itself the LP token. Your LP balance divided by the total supply is your share of the reserves. Every swap pays 0.30% into the reserves, so your share grows with trading volume. The price can move while you’re in the pool. When it does, you get back a different mix of the two tokens than you put in, which can be worth less than simply holding them. This is called impermanent loss.What you’ll build
A viem script that reads the pool’s reserves, adds 0.02 USDT plus the matching amount of tBOT, prints the value of your LP tokens, then removes all of it.Prerequisites
- The
bot-defiproject and.envfile from Wrap BOT. - At least 0.02 testnet USDT. Swap tBOT for it with Swap on BDEX V2.
- Enough tBOT for the matching amount plus gas for four transactions. On 2026-10-01 the tBOT side was about 0.0012.
Steps
1
Write the script
liquidity-v2.ts
2
Understand each part
getPairreturns the pair address. Pairs store their tokens sorted by address, so readtoken0to know which reserve is which.quote(amountA, reserveA, reserveB)returns the matching amount of the other token at the current ratio, with no fee. Use it for liquidity, never for swap quotes.addLiquidityETHtakes the token side as an argument and the tBOT side asvalue. The two minimums protect you if the ratio moves before your transaction lands. The router refunds any tBOT it doesn’t use.- Your share is
lpBalance / totalSupplyof each reserve. removeLiquidityETHburns your LP tokens and sends both tokens back, unwrapping WBOT to tBOT. Approve the pair first, because the router moves your LP tokens.
3
Run it
Verify it worked
Output
Troubleshooting
BotDexV2Router: INSUFFICIENT_A_AMOUNT or INSUFFICIENT_B_AMOUNT
BotDexV2Router: INSUFFICIENT_A_AMOUNT or INSUFFICIENT_B_AMOUNT
The pool ratio moved between your reserve read and your transaction, so one side fell below its minimum. Read the reserves again and retry.
TransferHelper::transferFrom: transferFrom failed
TransferHelper::transferFrom: transferFrom failed
The router couldn’t pull your tokens. Check the approval for the right token: USDT when adding, the pair address when removing.
BotDexV2Router: EXPIRED
BotDexV2Router: EXPIRED
The deadline passed. The script builds a fresh deadline for each call, so this only happens if a transaction waits more than 20 minutes.
Next steps
Provide V3 liquidity
Concentrate liquidity in a price range.
BDEX API
List pools and their reserves.