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

使用Infura部署ERC20交互合约时遇构造函数参数缺失错误求助

部署ERC20交互合约时的参数缺失问题

我正在使用Infura开发一款可查询钱包内Token总余额并实现Token转账的智能合约,合约编译成功但部署时报错,相关代码及问题如下:

Transactions.sol

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Transactions {
    uint256 transactionCount;
    event Transfer(address from, address receiver, uint amount, string message, uint256 timestamp, string keyword);

    mapping(address => uint256) public balances; // 用于追踪每个地址的余额

    IERC20 private token;

    constructor(address _tokenAddress) {
        token = IERC20(_tokenAddress);
    }

    struct TransferStruct {
        address sender;
        address receiver;
        uint amount;
        string message;
        uint256 timestamp;
        string keyword;
    }

    TransferStruct[] transactions;

    function addToBlockchain(address payable receiver, uint amount, string memory message, string memory keyword) public {
        transactionCount += 1;
        transactions.push(TransferStruct(msg.sender, receiver, amount, message, block.timestamp, keyword));

        // 更新发送方和接收方的余额
        balances[msg.sender] -= amount;
        balances[receiver] += amount;

        emit Transfer(msg.sender, receiver, amount, message, block.timestamp, keyword);
    }

    function getAllTransactions() public view returns (TransferStruct[] memory) {
        return transactions;
    }

    function getTransactionCount() public view returns (uint256) {
        return transactionCount;
    }
    
    function getBalance() public view returns (uint256) {
        return token.balanceOf(msg.sender);
    }

    function balanceOf(address account) public view returns (uint256) {
        return token.balanceOf(account);
    }

    function sendTokens(address to, uint256 amount) public {
        require(token.allowance(msg.sender, address(this)) >= amount, "合约未获得转账授权");
        require(token.transferFrom(msg.sender, to, amount), "Token转账失败");
    }
}

Hardhat.config.js

const path = require('path');
require('@nomiclabs/hardhat-waffle');

module.exports = {
  solidity: '0.8.0',
  networks: {
    sepolia: {
      url: 'https://sepolia.infura.io/v3/f37bc94c8c524217b597e4f291784097',
      accounts: ['MY API KEY IS HERE'],
    },
    mainnet: {
      url: 'https://mainnet.infura.io/v3/f37bc94c8c524217b597e4f291784097',
      accounts: ['MY API KEY IS HERE'],
    },
  },
  paths: {
    sources: './contracts',
    tests: './test',
    cache: './cache',
    artifacts: './artifacts',
    deploy: './deploy',
  },
  solidity: {
    compilers: [
      {
        version: '0.8.0',
      },
    ],
  },
  namedAccounts: {
    deployer: 0,
  },
  mocha: {
    timeout: 20000,
  },
  // 指定deploy.js的路径
  scripts: {
    deploy: 'npx hardhat run scripts/deploy.js --network sepolia',
  },
};

deploy.js

const { ethers } = require('hardhat');

const main = async () => {
  const TransactionsFactory = await hre.ethers.getContractFactory("Transactions");
  const transactionsContract = await TransactionsFactory.deploy();

  await transactionsContract.deployed();

  console.log("Transactions合约地址: ", transactionsContract.address);
};

const runMain = async () => {
  try {
    await main();
    process.exit(0);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};

runMain();

部署错误信息

Error: missing argument:  in Contract constructor (count=0, expectedCount=1, code=MISSING_ARGUMENT, version=contracts/5.7.0)
    at Logger.makeError (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\logger\src.ts\index.ts:269:28)
    at Logger.throwError (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\logger\src.ts\index.ts:281:20)
    at Logger.checkArgumentCount (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\logger\src.ts\index.ts:340:18)
    at ContractFactory.<anonymous> (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\src.ts\index.ts:1237:16)
    at step (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:48:23)
    at Object.next (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:29:53)
    at C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:23:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:19:12)
    at ContractFactory.deploy (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:1138:16)
    at main (C:\Users\ABE\Desktop\test\smart_contract\scripts\deploy.js:5:58)
    at runMain (C:\Users\ABE\Desktop\test\smart_contract\scripts\deploy.js:14:5) {
  reason: 'missing argument:  in Contract constructor',
  code: 'MISSING_ARGUMENT',
  count: 0,
  expectedCount: 1
}

问题原因及解决方案

问题原因

Transactions合约的构造函数要求传入一个ERC20代币地址_tokenAddress,但部署脚本deploy.js中调用TransactionsFactory.deploy()时没有传递该参数,导致参数缺失报错。

解决方案

修改deploy.js,在deploy()方法中传入目标ERC20代币的地址(以下用Sepolia测试网USDC地址举例,你可以替换为自己需要交互的代币地址):

const { ethers } = require('hardhat');

// 替换为你要交互的ERC20代币地址,示例为Sepolia测试网USDC
const TOKEN_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";

const main = async () => {
  const TransactionsFactory = await hre.ethers.getContractFactory("Transactions");
  // 传入构造函数所需的代币地址参数
  const transactionsContract = await TransactionsFactory.deploy(TOKEN_ADDRESS);

  await transactionsContract.deployed();

  console.log("Transactions合约地址: ", transactionsContract.address);
};

const runMain = async () => {
  try {
    await main();
    process.exit(0);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};

runMain();

额外注意事项:

  • 确保使用的代币地址在目标网络(如Sepolia)上是有效的ERC20合约地址
  • 部署账户需要持有足够的测试网ETH,用于支付合约部署的Gas费

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 17:28:08