Hardhat TypeError:etherWallet.ownerAddress 不是函数

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

我正在制作一个简单的以太钱包智能合约。当我尝试在安全帽中使用部署脚本部署合同时,我收到此错误。以下是我的deploy.js代码

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

async function main() {
  const etherWalletFactory = await ethers.getContractFactory("wallet");
  console.log("Deploying, Please wait...");
  const etherWallet = etherWalletFactory.deploy();
  (await etherWallet).waitForDeployment();
  console.log("Contract Deployed");
  console.log(`Contract address: ${etherWallet.address}`);
  const owner = await etherWallet.ownerAddress();
  console.log(owner);
  const balance = await etherWallet.getBalance();
  console.log(`Current Balance: ${balance}`);
}

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

这是我的 Solidity 代码 (wallet.sol) :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract wallet {
    address payable private owner;

    constructor() {
        owner = payable(msg.sender);
    }

    receive() external payable {}

    function ownerAddress() public view returns (address) {
        return owner;
    }

    function getBalance() public view returns (uint) {
        return address(this).balance;
    }

    function sendEth(uint _amount, address payable _recipient) public payable {
        require(_amount < address(this).balance, "Insufficient funds");
        _recipient.transfer(_amount);
    }

    function withdraw(uint _amount) public {
        require(msg.sender == owner, "You are not the owner");
        payable(msg.sender).transfer(_amount);
    }
}

这是我运行deploy.js时遇到的错误

yarn run v1.22.19
warning package.json: No license field
$ /home/yash/Codes/ether-wallet/node_modules/.bin/hardhat run scripts/deploy.js
Deploying, Please wait...
Contract Deployed
Contract address: undefined
TypeError: etherWallet.ownerAddress is not a function
    at main (/home/yash/Codes/ether-wallet/scripts/deploy.js:10:35)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
error Command failed with exit code 1.

我使用

yarn hardhat run scripts/deploy.js
命令来运行脚本

我尝试多次使用

yarn hardhat clean
命令手动删除工件和缓存文件,但我仍然收到此错误。

javascript typeerror solidity smartcontracts hardhat
1个回答
0
投票

您可以尝试以下步骤:

  • 在部署脚本中,您正在部署合约,然后立即尝试与其交互。但是,部署是异步的,您需要等待部署过程。 改变这个:
const etherWallet = etherWalletFactory.deploy();

至:

const etherWallet = await etherWalletFactory.deploy();
  • 您正在呼叫
    (await etherWallet).waitForDeployment();
    。这不是合同部署的标准功能。这个功能很可能不存在。

更改此:

(await etherWallet).waitForDeployment();

至:

await etherWallet.deployed();
© www.soinside.com 2019 - 2024. All rights reserved.