You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地部署Solidity智能合约时遭遇ethers.js TypeError问题求助

Solidity本地部署脚本报错解决

问题说明

尝试在本地部署Solidity智能合约,运行JavaScript部署脚本时触发错误,需要协助解决。

错误信息

Deploying, please wait...
TypeError: no matching function (argument="key", value="deployTransaction", code=INVALID_ARGUMENT, version=6.2.3)
    at makeError (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:114:21)
    at assert (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:138:15)
    at assertArgument (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:150:5)
    at Interface.getFunctionName (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/abi/interface.js:337:39)
    at buildWrappedMethod (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/contract/contract.js:308:34)
    at BaseContract.getFunction (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/contract/contract.js:686:22)
    at Object.get (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/node_modules/ethers/lib.commonjs/contract/contract.js:627:39)
    at main (/home/sanju/Solidity Tutorial/hh-fcc/ethers-simple-storage/deploy.js:32:45)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'INVALID_ARGUMENT',
  argument: 'key',
  value: 'deployTransaction'
}

部署脚本代码(Node v18.17.1)

const ethers = require("ethers");
const fs = require("fs");

// we can only use await keyword inside async functions

async function main() {
  const rpcURL = "http://127.0.0.1:7545";
  const provider = new ethers.JsonRpcProvider(rpcURL);

  const wallet = new ethers.Wallet(
    "0xdae2a8f600d25556f9863f2a5f5134241fc860f5469e71e6205f5b1dc517c576",
    provider
  );

  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf-8");

  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf-8"
  );

  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("Deploying, please wait...");
  const contract = await contractFactory.deploy(); // STOP here, wait for the contract to deploy
  const transactionReceipt = await contract.deployTransaction.wait(1);
  console.log(
    `Here is the deployment transaction: ${contract.deployTransaction}`
  );
  console.log(`Here is the transaction receipt: ${transactionReceipt}`);
}

main()
  .then(() => process.exit(0))
  .catch((err) => {
    console.error(err);
    process.exit(1);
  });

Solidity合约代码(0.8.7-fixed)

// SPDX-License-Identifier: MIT

pragma solidity 0.8.7;

contract SimpleStorage {
    uint256 favoriteNumber;

    struct People {
        uint256 favoriteNumber;
        string name;
    }
    // uint256[] public anArray;
    People[] public people;

    mapping(string => uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;
    }

    function retrieve() public view returns (uint256) {
        return favoriteNumber;
    }

    function addPerson(string memory _name, uint256 _favoriteNumber) public {
        people.push(People(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }
}

环境配置

  • ethers.js - 6.2.3
  • solc - 0.8.7
  • ganache
  • node - v18.17.1

解决方案

问题根源是ethers.js v6的API变更:v6版本移除了contract.deployTransaction属性,改用contract.deploymentTransaction()方法获取部署交易对象。

修改部署脚本中的对应代码:

// 原代码
const transactionReceipt = await contract.deployTransaction.wait(1);
console.log(`Here is the deployment transaction: ${contract.deployTransaction}`);

// 修改后代码
const deploymentTx = contract.deploymentTransaction();
const transactionReceipt = await deploymentTx.wait(1);
console.log(`Here is the deployment transaction: ${JSON.stringify(deploymentTx, null, 2)}`);

修改后重新运行脚本即可完成正常部署。


内容的提问来源于stack exchange,提问作者Sanju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 15:37:16