【问题标题】:abi.map not a function error trying to connect to uniswapabi.map 不是尝试连接到 uniswap 的函数错误
【发布时间】:2022-11-13 04:36:04
【问题描述】:

我是使用区块链的新手,在尝试从 Uniswap 获取合同时遇到问题。我一直在关注他们在 V3 上的文档,但我无法克服这个“abi.map 不是函数”错误。当我将 ABI 输出到控制台时,看起来我正确地恢复了 ABI,但是当我尝试使用它来初始化合同时,我得到了这个错误。

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)
})

【问题讨论】:

  • 如果它期望 abi.map 是一个函数,那么它必须期望 abi 是一个数组(它不是)。检查github.com/ethers-io/ethers.js/issues/1238
  • 我已经尝试了他们在那里建议的修复程序,但找不到找回模块。我检查了文件目录并且模块在那里并且我指向正确的位置。

标签: javascript cryptography ethers.js uniswap


【解决方案1】:

如果您删除 abi 中的所有内容,直到“abi”之后的第一个方括号,此错误就会消失。所以对于 IUniswapV3Pool.json,而不是:

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

它应该是:

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

并且不要忘记删除文件末尾最后一个方括号之前的所有内容。

【讨论】:

    【解决方案2】:

    要建立在@jaspers 的答案上,您实际上不需要从原始文件中删除任何内容。您只需要传递原始的 abi 属性。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2015-06-20
      • 2021-08-17
      相关资源
      最近更新 更多