Skip to content

Instantly share code, notes, and snippets.

@BlockmanCodes
Created June 16, 2023 12:25
Show Gist options
  • Save BlockmanCodes/f3cf80ff900275aec32ab70189724ba7 to your computer and use it in GitHub Desktop.
Save BlockmanCodes/f3cf80ff900275aec32ab70189724ba7 to your computer and use it in GitHub Desktop.
Uni V3: getPriceFromTick
const JSBI = require('jsbi')
const { TickMath, FullMath } = require('@uniswap/v3-sdk')
const baseToken = '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599' // WETH
const quoteToken = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' // WBTC
async function main(
baseToken,
quoteToken,
inputAmount,
currentTick,
baseTokenDecimals,
quoteTokenDecimals
) {
const sqrtRatioX96 = TickMath.getSqrtRatioAtTick(currentTick)
const ratioX192 = JSBI.multiply(sqrtRatioX96, sqrtRatioX96)
const baseAmount = JSBI.BigInt( inputAmount * (10 ** baseTokenDecimals))
const shift = JSBI.leftShift( JSBI.BigInt(1), JSBI.BigInt(192))
const quoteAmount = FullMath.mulDivRoundingUp(ratioX192, baseAmount, shift)
console.log('quoteAmount', quoteAmount.toString() / (10**quoteTokenDecimals))
return quoteAmount
}
main(
baseToken,
quoteToken,
1,
257109,
8,
18,
)
{
"name": "priceFromTick",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@uniswap/v3-periphery": "^1.4.0",
"@uniswap/v3-sdk": "^3.9.0",
"dotenv": "^16.0.3",
"ethers": "^5.7.2",
"jsbi": "^3.2.5"
}
}
@priyankaj1910
Copy link

Hello, thanks for the video. But price from other contract not matching. for example ARB/WETH , USDC/WETH. I doing this on arbitrum and providing valid ticks and token decimals. Actually on Ethereum its giving correct WETH value for all tokens but not on Arbitrum. Whats the issue?

@itxtoledo
Copy link

this code does nothing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment