Skip to content

Instantly share code, notes, and snippets.

@basileos
Created June 17, 2022 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basileos/ece52f22ffc560b2978c4786685c3f06 to your computer and use it in GitHub Desktop.
Save basileos/ece52f22ffc560b2978c4786685c3f06 to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
const BigNumber = require('bignumber.js');
const ERC20ABI = require('./abis/ERC20.json');
const RPC_URL = 'https://evm-cronos.crypto.org';
const calcRemoveLiquidityAmounts = async (lpAmount, tokenAAddress, tokenBAddress, lpAddress) => {
const slippage = 0.001; // 0.1% slippage
const web3 = new Web3(RPC_URL);
const tokenA = new web3.eth.Contract(ERC20ABI, tokenAAddress);
const tokenB = new web3.eth.Contract(ERC20ABI, tokenBAddress);
const tokenABalance = await
tokenA.methods.balanceOf(lpAddress).call();
const tokenBBalance = await tokenB.methods.balanceOf(lpAddress).call()
const pair = await new web3.eth.Contract(ERC20ABI, lpAddress);
const totalSupply = await pair.methods.totalSupply().call();
const amountA = new BigNumber(lpAmount).div(totalSupply).multipliedBy(tokenABalance).multipliedBy(1 - slippage);
const amountB = new BigNumber(lpAmount).div(totalSupply).multipliedBy(tokenBBalance).multipliedBy(1 - slippage);
console.log(amountA.integerValue().toFixed(), amountB.integerValue().toFixed());
};
calcRemoveLiquidityAmounts(
"701540069209",
"0xc21223249CA28397B4B6541dfFaEcC539BfF0c59",
"0x2D03bECE6747ADC00E1a131BBA1469C15fD11e03",
"0x814920d1b8007207db6cb5a2dd92bf0b082bdba1"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment