abi.map not a function error trying to connect to uniswap

2.5k Views Asked by At

I'm new to working with blockchain and I'm having a problem trying to get the contract from Uniswap. I've been following their docs on V3 but I can't get past this "abi.map is not a function" error. When I output the ABI to the console, it looks like I get the ABI back correctly but when I try to use it to initialize the contract I get this error.

import { ethers } from 'ethers'
const ABI = require('@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json')
console.log(ABI)

const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/Your Address Here;p')
const poolAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const poolContract = new ethers.Contract(poolAddress, ABI, provider)

interface Immutables {
  factory: string 
  token0: string
  token1: string 
  fee: number
  tickSpacing: number
  maxLiquidityPerTick: number
}

async function getPoolImmutables() {
  const [factory, token0, token1, fee, tickSpacing, maxLiquidityPerTick] = await Promise.all([
    poolContract.factory(),
    poolContract.token0(),
    poolContract.token1(),
    poolContract.fee(),
    poolContract.tickSpacing(),
    poolContract.maxLiquidityPerTick(),
  ])

  const immutables: Immutables = {
    factory,
    token0,
    token1,
    fee,
    tickSpacing,
    maxLiquidityPerTick,
  }
  return immutables
}

getPoolImmutables().then((result) => {
  console.log(result)
})

2

There are 2 best solutions below

0
On

This error will go away if you delete everything in the abi up to the first square bracket after "abi". So for IUniswapV3Pool.json, instead of:

{
  "_format": "hh-sol-artifact-1",
  "contractName": "IUniswapV3Pool",
  "sourceName": "contracts/interfaces/IUniswapV3Pool.sol",
  "abi": [
    {
      "anonymous": false,
      "inputs": [
...

It should be:

[
    {
      "anonymous": false,
      "inputs": [

And don't forget to delete everything up to the last square bracket at the end of the file as well.

0
On

To build on @jaspers answer you don't actually need to delete anything from the original file. you just need to pass the abi property from the original.

const poolContract = new ethers.Contract(poolAddress, ABI.abi, provider)