// remove bsv and token from liquidity pool
public function removeLiquidity(PubKey sender, int lpAmount, Sig senderSig, int oldTokenBalance, int senderKeyIndex, int senderBalance,
int lpSenderBalance, int lpSenderKeyIndex, SigHashPreimage txPreimage) {
require(checkSig(senderSig, sender));
int oldBsvBalance = SigHash.value(txPreimage);
// withdraw amount
int bsvAmount = oldBsvBalance * lpAmount / this.lpToken.totalSupply();
int tokenAmount = oldTokenBalance * lpAmount / this.lpToken.totalSupply();
// burn the lp tokens
require(this.lpToken.burn(sender, lpSenderBalance, lpAmount, lpSenderKeyIndex));
// transfer tokens from pool to the sender
require(this.token.transferFrom(this.poolPubkey, sender, tokenAmount, oldTokenBalance, senderKeyIndex, senderBalance, senderKeyIndex));
// transfer bsvs to the sender
int newBsvBalance = oldBsvBalance - bsvAmount;
require(this.propagateState(newBsvBalance, txPreimage));
流动性提供者拥有的 LP Token 的数量来提取流动性(第 9 行和第 10 行)。提款后,LP Token 在第 13 行被烧毁。第 16 行将 Token 从池子中转移到流动性提供者。第 18 行和第 20 行对 BSV 做同样的事情。
请注意,除了合约输出之外,还需要在同一交易中的另一个输出将 BSV 返回给流动性提供者。