Skip to content

RemoveLiquidity

class defipy.process.RemoveLiquidity()

Double-sided withdrawal from a pool. The caller specifies one token amount to receive; the matching amount of the other is calculated from current reserves. The user’s LP tokens are burned proportionally to their withdrawal.

Cross-protocol: dispatches to the underlying protocol implementation. Available on Uniswap V2, Uniswap V3, Balancer, and Stableswap.

amounts = RemoveLiquidity().apply(pool, token_out, user, amount_out)

Withdraw liquidity from pool on behalf of user, returning amount_out of token_out. The matching amount of the other token is computed and also returned to the user.

Parameters

NameTypeDescription
poolexchangePool instance to remove liquidity from.
token_outERC20One side of the withdrawal. The other side is sized automatically.
userstrUser address (string identifier) whose LP tokens are burned.
amount_outfloatAmount of token_out to receive.

Returns

NameTypeDescription
amountsdictDictionary mapping each token symbol to the amount the user received (e.g. {'ETH': 999.0, 'TKN': 99900.0}).
from defipy import *
user_nm = 'user'
eth_amount = 1000
tkn_amount = 100000
tkn = ERC20("TKN", "0x111")
eth = ERC20("ETH", "0x09")
exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP", address="0x011")
factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)
Join().apply(lp, user_nm, eth_amount, tkn_amount)
amounts = RemoveLiquidity().apply(lp, eth, user_nm, 999)
print(amounts)
{'ETH': 999.0, 'TKN': 99900.0}
RemoveLiquidity().apply(lp, tkn, user_nm, 100)
lp.summary()
Exchange ETH-TKN (LP) Reserves: ETH = 999.0, TKN = 99900.0 Liquidity: 9990.0
  • The pool’s price doesn’t change. Both sides scale by the same proportion; reserve ratio is preserved.
  • The user must hold enough LP tokens to cover the implied burn. If they don’t, the call raises.
  • For single-sided withdrawals — leaving with only one token — see WithdrawSwap.