Skip to content

Instantly share code, notes, and snippets.

@shikanime
Last active December 16, 2021 20:34
Show Gist options
  • Save shikanime/41257b258c4e2113970e28122c74c07d to your computer and use it in GitHub Desktop.
Save shikanime/41257b258c4e2113970e28122c74c07d to your computer and use it in GitHub Desktop.
Get MATIC/USDC pair via Uniwap
import { Token } from "@uniswap/sdk-core";
import { abi as IUniswapV3PoolABI } from "@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json";
import { Pool } from "@uniswap/v3-sdk";
import { ethers } from "ethers";
async function main() {
const provider = new ethers.providers.InfuraProvider();
const poolAddress = "0x07a6e955ba4345bae83ac2a6faa771fddd8a2011";
const poolContract = new ethers.Contract(
poolAddress,
IUniswapV3PoolABI,
provider
);
const token0 = await poolContract.token0();
const token1 = await poolContract.token1();
const fee = await poolContract.fee();
const MATIC = new Token(1, token0, 18, "Matic");
const USDC = new Token(1, token1, 6, "USDC");
const slot = await poolContract.slot0();
const state = {
liquidity: await poolContract.liquidity(),
sqrtPriceX96: slot[0],
tick: slot[1],
observationIndex: slot[2],
observationCardinality: slot[3],
observationCardinalityNext: slot[4],
feeProtocol: slot[5],
unlocked: slot[6],
};
const MATIC_USDC_POOL = new Pool(
MATIC,
USDC,
fee,
state.sqrtPriceX96.toString(),
state.liquidity.toString(),
state.tick
);
const price = MATIC_USDC_POOL.token0Price.toSignificant();
console.log("MATIC:", price);
}
if (require.main === module) {
main();
}
{
"name": "uniswap-pricing",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@types/node": "^17.0.0",
"@uniswap/sdk-core": "^3.0.1",
"@uniswap/v3-core": "^1.0.0",
"@uniswap/v3-sdk": "^3.6.3",
"ethers": "^5.5.2",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment