Im making a simple ether wallet smart contract. When i try to deploy the contract with deploy script in hardhat, im getting this error. Following is my deploy.js code
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);
});
And this is my Solidity code (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);
}
}
This is the error im getting on running deploy.js
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.
Im using yarn hardhat run scripts/deploy.js
command to run the script
I tried deleting artifacts and cache files manually and by using yarn hardhat clean
command multiple times but im still getting this error.
>Solution :
You can try with this steps:
- In your deployment script, you’re deploying the contract and then immediately trying to interact with it. However, the deployment is asynchronous, and you need to await the deployment process.
Change this:
const etherWallet = etherWalletFactory.deploy();
to:
const etherWallet = await etherWalletFactory.deploy();
- You’re calling
(await etherWallet).waitForDeployment();
. This is not a standard function for a contract deployment. It’s likely that this function does not exist.
Change this:
(await etherWallet).waitForDeployment();
to:
await etherWallet.deployed();