1 year ago
#324148
seems
find percentage of price impact using constant product formula
This is an ethereum question because it has to do with EVM reserve pools and exchange prices. How do I find percentage of price impact using constant product formula, rather than the value of amount and value of tokens? Using typescript, I'm pulling the reserve data from the pool contract using the common getReserves function.
I'm using constant product to find the price between reserve0 and reserve1.
I've referenced these links for help: https://dailydefi.org/articles/price-impact-and-how-to-calculate/ https://ethereum.stackexchange.com/questions/102063/understand-price-impact-and-liquidity-in-pancakeswap
price_impact( ) {
const poolAddress = '0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16';
const poolContract = new ethers.Contract(poolAddress, ['function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast)'], signer);
poolContract['getReserves']()
.then((reserves: { _reserve0: ethers.BigNumberish; _reserve1: ethers.BigNumberish; }) => {
let reserve_a_initial = parseFloat(utils.formatUnits(reserves._reserve0));
let reserve_b_initial = parseFloat(utils.formatUnits(reserves._reserve1));
console.log(`busd in pool: ${reserve_a_initial}`);
console.log(`busd in pool: ${reserve_b_initial}`);
const fee = 0.025;
let max_price_impact = 0.0001;
let amount_traded_bnb = reserve_a_initial * max_price_impact / ((1 - max_price_impact)*(1 - fee));
let amount_traded_busd = reserve_b_initial * max_price_impact / ((1 - max_price_impact)*(1 - fee));
console.log(`Given a max price impact of ${max_price_impact*100}%, the max amount of bnb tradeable is ${amount_traded_bnb}`);
console.log(`Given a max price impact of ${max_price_impact*100}%, the max amount of busd tradeable is ${amount_traded_busd}`);
let amountInBNB = amount_traded_bnb * (1 - fee);
let amountInBUSD = amount_traded_busd * (1 - fee);
let price_impact_trade_cake = amountInBNB / (reserve_a_initial + amountInBNB);
let price_impact_trade_usdt = amountInBUSD / (reserve_b_initial + amountInBUSD);
console.log(`Price impact when trading ${amount_traded_bnb} bnb: ${price_impact_trade_cake*100}%`);
console.log(`Price impact when trading ${amount_traded_busd} busd: ${price_impact_trade_usdt * 100}%`);
let price = amount_traded_busd / amount_traded_bnb;
console.log(price*4);
}).catch(console.error); }
typescript
ethereum
solidity
price
evm
0 Answers
Your Answer