Skip to content

Instantly share code, notes, and snippets.

@dmihal
Created June 4, 2021 09:39
Show Gist options
  • Save dmihal/9d65b489417a84c078b686e3c78af6d5 to your computer and use it in GitHub Desktop.
Save dmihal/9d65b489417a84c078b686e3c78af6d5 to your computer and use it in GitHub Desktop.
Get Uniswap prices in a subgraph
import { Address, BigInt, BigDecimal, dataSource } from "@graphprotocol/graph-ts"
import { UniswapV2Pair } from '../generated/Pool/UniswapV2Pair'
import { UniswapV2Factory } from '../generated/Pool/UniswapV2Factory'
let UNISWAP_FACTORY = Address.fromString('0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f')
let EIGHTEEN_DECIMALS = BigInt.fromI32(10).pow(18).toBigDecimal()
let wethAddress = new Map<string, Address>()
wethAddress.set('rinkeby', Address.fromString('0xc778417E063141139Fce010982780140Aa0cD5Ab'))
wethAddress.set('mainnet', Address.fromString('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'))
let stablecoinAddress = new Map<string, Address>()
stablecoinAddress.set('rinkeby', Address.fromString('0x5592ec0cfb4dbc12d3ab100b257153436a1f0fea'))
stablecoinAddress.set('mainnet', Address.fromString('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48')) // USDC
function getPair(token0: Address, token1: Address): Address {
let factory = UniswapV2Factory.bind(UNISWAP_FACTORY)
return factory.getPair(token0, token1)
}
export function getPairPrice(pairAddress: Address): BigDecimal {
let pair = UniswapV2Pair.bind(pairAddress)
let reserves = pair.getReserves()
let totalSupply = pair.totalSupply();
if(totalSupply.gt(BigInt.fromI32(0))) {
let valueOfToken0 = getPrice(pair.token0()) * reserves.value0.divDecimal(EIGHTEEN_DECIMALS)
let valueOfToken1 = getPrice(pair.token1()) * reserves.value1.divDecimal(EIGHTEEN_DECIMALS)
let lpTokenPrice = (valueOfToken0 + valueOfToken1) / (pair.totalSupply().divDecimal(EIGHTEEN_DECIMALS))
return lpTokenPrice;
}
return new BigDecimal(BigInt.fromI32(0));
}
export function getPrice(token: Address): BigDecimal {
let network = dataSource.network()
let stablecoin = stablecoinAddress.get(network)
let weth = wethAddress.get(network)
if (token.toHex() == stablecoin.toHex()) {
return BigInt.fromI32(1).toBigDecimal()
}
if (token == weth) {
let pair = UniswapV2Pair.bind(getPair(token, stablecoin))
let reserves = pair.getReserves()
if(reserves.value0 == BigInt.fromI32(0) || reserves.value1 == BigInt.fromI32(0) ) {
return BigInt.fromI32(1).toBigDecimal()
}
let ethPrice = token == pair.token0()
? reserves.value1.toBigDecimal() / reserves.value0.toBigDecimal()
: reserves.value0.toBigDecimal() / reserves.value1.toBigDecimal()
return ethPrice
}
let pair = UniswapV2Pair.bind(getPair(token, weth))
let reserves = pair.getReserves()
if(reserves.value0 == BigInt.fromI32(0) || reserves.value1 == BigInt.fromI32(0) ) {
return BigInt.fromI32(1).toBigDecimal()
}
let priceInETH = token == pair.token0()
? reserves.value1.toBigDecimal() / reserves.value0.toBigDecimal()
: reserves.value0.toBigDecimal() / reserves.value1.toBigDecimal()
return priceInETH * getPrice(weth)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment