Created
July 7, 2021 14:00
-
-
Save kassandraoftroy/aead97ebf0c98c10d01c0da2789770fb to your computer and use it in GitHub Desktop.
get executive rebalance params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {ethers} from 'ethers'; | |
const x60 = ethers.BigNumber.from('2').pow(ethers.BigNumber.from('60')); | |
const ALCHEMY_ID = 'YOUR_ALCHEMY_ID'; | |
const op = async () => { | |
const guniPoolAddress = "0x288a17459b5d0ecc401f5ECdC166F447ea3Da13a"; | |
const libAddress = "0xc2D8196DbcC92E8C5F372e58B8dbFDF2ac129057"; | |
const provider = new ethers.providers.JsonRpcProvider(`https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_ID}`) | |
const gelatoUniV3Pool = new ethers.Contract( | |
guniPoolAddress, | |
[ | |
'function getUnderlyingBalances() external view returns (uint256 amount0Current, uint256 amount1Current)', | |
'function pool() external view returns (address)' | |
], | |
provider | |
); | |
const pool = new ethers.Contract( | |
await gelatoUniV3Pool.pool(), | |
["function slot0() external view returns (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked)"], | |
provider | |
); | |
const liquidityLib = new ethers.Contract( | |
libAddress, | |
[ | |
'function getAmountsForLiquidity(uint160, uint160, uint160, uint128) external view returns (uint256 amount0, uint256 amount1)', | |
'function getLiquidityForAmounts(uint160, uint160, uint160, uint256, uint256) external view returns (uint128)' | |
], | |
provider | |
); | |
const { sqrtPriceX96 } = await pool.slot0(); | |
const {amount0Current, amount1Current} = await gelatoUniV3Pool.getUnderlyingBalances(); | |
// IF YOU ARE REBALANCING PUT YOUR DESIRED NEW TICKS HERE | |
const lowerTick = -36000; // new lower tick | |
const upperTick = 0; // new upper tick | |
const lowerPrice = 1.0001 ** Number(lowerTick); | |
const upperPrice = 1.0001 ** Number(upperTick); | |
const lowerSqrtPriceX36 = Math.round(Math.sqrt(lowerPrice) * (2**36)); | |
const upperSqrtPriceX36 = Math.round(Math.sqrt(upperPrice) * (2**36)); | |
const lowerSqrtPriceX96 = ethers.BigNumber.from(lowerSqrtPriceX36.toString()).mul(x60); | |
const upperSqrtPriceX96 = ethers.BigNumber.from(upperSqrtPriceX36.toString()).mul(x60); | |
const liquidity = await liquidityLib.getLiquidityForAmounts( | |
sqrtPriceX96, | |
lowerSqrtPriceX96, | |
upperSqrtPriceX96, | |
amount0Current, | |
amount1Current | |
); | |
const {amount0, amount1} = await liquidityLib.getAmountsForLiquidity( | |
sqrtPriceX96, | |
lowerSqrtPriceX96, | |
upperSqrtPriceX96, | |
liquidity | |
); | |
const leftover0 = amount0Current.sub(amount0); | |
const leftover1 = amount1Current.sub(amount1); | |
console.log("holdings token0", ethers.utils.formatEther(amount0Current)); | |
console.log("holdings token1", ethers.utils.formatEther(amount1Current)); | |
console.log("maximum deposit new position token0", ethers.utils.formatEther(amount0)); | |
console.log("maximum deposit new position token1", ethers.utils.formatEther(amount1)); | |
console.log("leftover token0", ethers.utils.formatEther(leftover0)); | |
console.log("leftover token1", ethers.utils.formatEther(leftover1)); | |
}; | |
(async () => { | |
await op(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment