Uniswap/docs

SDK V2 - TypeError: Cannot convert large reserve value to a BigInt

elier opened this issue · 1 comments

Took from SDK V2 documentation:

const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18)

async function createPair(): Promise<Pair> {
    const pairAddress = Pair.getAddress(DAI, WETH9[DAI.chainId])
    const pairContract = new ethers.Contract(pairAddress, IUniswapV2Pair.abi, provider)

    const reserves = await pairContract["getReserves"]()
    const [reserve0, reserve1] = reserves

    const tokens = [DAI, WETH9[DAI.chainId]]
    const [token0, token1] = tokens[0].sortsBefore(tokens[1]) ? tokens : [tokens[1], tokens[0]]

    const pair = new Pair(CurrencyAmount.fromRawAmount(token0, reserve0), CurrencyAmount.fromRawAmount(token1, reserve1))
    return pair
}

Through the following error:

TypeError: Cannot convert 7442959731171455700483124 to a BigInt
at Function.fromRawAmount (/.../node_modules/@uniswap/sdk-core/src/entities/fractions/currencyAmount.ts:23:12)

Versions:

"dependencies": {
    "@uniswap/v2-core": "^1.0.1",
    "@uniswap/v2-sdk": "^4.3.0",
    "ethers": "^6.11.1",
    "web3": "^4.7.0"
  },
  "devDependencies": {
    "typescript": "^5.4.3"
  }

Casting to string did the trick:

const pair = new Pair(CurrencyAmount.fromRawAmount(token0, String(reserve0)), CurrencyAmount.fromRawAmount(token1, String(reserve1)))