Hey everyone,I just need to call a function in smart contract which named withdraw and it is a state changing function..I'm using hardhat to deploy the contract..
here is my smart contract...
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.10;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract ExampleTest{
address public onlyOwner;
address public daiAddress;
constructor(
address onlyowner,
address dai
) {
onlyOwner = onlyowner;
daiAddress = dai;
}
modifier Owner{
require(msg.sender == onlyOwner,"Sorry dear it's ownable");
_;
}
event info(
uint256 amount
) ;
receive() external payable{}
function withdraw () public Owner {
uint256 amount = getERC20Balance(daiAddress);
IERC20(daiAddress).allowance(onlyOwner,address(this));
IERC20(daiAddress).approve(address(this),amount);
IERC20(daiAddress).transferFrom(address(this),onlyOwner,amount);
emit info(amount);
}
function getERC20Balance(address _erc20token) public view returns (uint256){
uint256 amount = IERC20(_erc20token).balanceOf(address(this));
return amount;
}
}
Here is my deploy.js file..
const {ethers} = require("hardhat");
require("dotenv").config();
const fs = require("fs");
const { BigNumber } = require("ethers");
const fspromise = fs.promises;
async function main(){
const provider = await ethers.getSigner();
const contract = await ethers.getContractFactory("ExampleTest");
const deploy = await contract.deploy(provider.address,"0xDF1742fE5b0bFc12331D8EAec6b478DfDbD31464");
const deployed = await deploy.deployed();
const dai_Address = process.env.DAI_ADDRESS;
const Dai_contract = await ethers.getContractAt("IERC20",dai_Address,provider);
const Dai_Wait = await Dai_contract.connect(provider).transfer(deployed.address,ethers.utils.parseUnits("10"));
await Dai_Wait.wait(8);
console.log(Dai_Wait);
await Dai_Wait.wait(8);
**// Here is the error occured....**
const muiop = await deployed.withdraw({gasLimit:ethers.utils.parseUnits("100000000000001","wei")});
muiop.wait(2);
deploy.on("info",(amount)=>{
console.log(amount);
})
console.log(muiop);
}
main().catch((error)=>{
console.log(error),
process.exit(1);
});
I don't know why i'm getting this error...thanks in advance.`