Skip to content

Instantly share code, notes, and snippets.

@talegift
talegift / calculateUniswapTWAP.js
Created November 17, 2020 16:59 — forked from l3wi/calculateUniswapTWAP.js
Off-chain Uniswap V2 TWAP calculator in Javascript
import { ethers } from 'ethers'
// To use this you need to read the Uniswap v2 contract for a pair/
// PRICE pulled from priceXCumulativeLast
// TIMESTAMPS pulled from _blockTimestampLast in getReserves()
// Mock Data
// In a real scenario you would fetch and store price & timestamp at an interval
// to mirror the contract calculating the TWAP on chain
const price0 = '529527205677379158060966860839'