使用 ethers.js 调用智能合约函数。尝试使用 etherscan api 请求合约的 ABI 并调用其方法

问题描述 投票:0回答:0

我正在尝试使用我在 goerli 上测试智能合约的 ABI 调用

mint
函数 - (https://goerli.etherscan.io/address/0x636c6090348b03a46d73ecc40f005e43662f515d#code) 。我正在使用
axios
和 etherscan api 发送请求,如下所示:

const url = `https://api-goerli.etherscan.io/api
?module=contract
&action=getabi
&address=${contractAddress}
&apikey=${apiKey}`;

然后我得到了abi:

async function getABI() {
  try {
    const response = await axios.get(url);
    const abi = response.data.result;
    return abi;
  } catch (e) {
    console.log(error);
  }
}

它在工作,我可以得到 abi,检查过。但是当我试图在我的函数中调用

mint
方法时:

async function mint() {
  try {
    const promises = privateKeys.map(async (privateKey) => {
      const abi = await getABI();
      const signer = new ethers.Wallet(privateKey, provider);
      const contract = new ethers.Contract(contractAddress, abi, signer);
      const encodeMint = contract.methods[method](
        ...Object.values(params)
      ).encodeABI();
      const gasLim = await contract.methods[method](
        ...Object.values(params)
      ).estimateGas();
      const gasPrice = gas * Math.pow(10, 9);
      const tx = {
        to: contractAddress,
        data: encodeMint,
        gasPrice: gasPrice,
        gasLimit: gasLim,
      };
      let txHash;
      while (true) {
        try {
          const signedTx = await signer.sign(tx);
          txHash = await provider.sendTransaction(signedTx);
          break;
        } catch (e) {
          console.log(`Failed to send transaction, trying again...`);
          await new Promise((resolve) => setTimeout(resolve, 1000));
        }
      }

它给我一个错误:

发送交易时发生错误 TypeError: no matching function (argument="key", value="methods", code=INVALID_ARGUMENT, version=6.1.0) 在 makeError (C:\Users\dmitr\OneDrive\Рабочий стол\Node-js ode_modules thers\lib.commonjs\utils rrors.js:114:21) 在断言(C:\Users\dmitr\OneDrive\Рабочий стол\Node-js ode_modules thers\lib.commonjs\utils rrors.js:138:15) 在 assertArgument (C:\Users\dmitr\OneDrive\Рабочий стол\Node-js ode_modules thers\lib.commonjs\utils rrors.js:150:5) 在 Interface.getFunctionName(C:\Users\dmitr\OneDrive\Рабочий стол\Node-js ode_modules thers\lib.commonjs bi\interface.js:337:39) 在新的 WrappedMethod (C:\Users\dmitr\OneDrive\Рабочий стол\Node-js ode_modules thers\lib.commonj

smartcontracts web3js abi ethers.js
© www.soinside.com 2019 - 2024. All rights reserved.