Home > Enterprise >  How to test a solidity payable function with hardhat
How to test a solidity payable function with hardhat

Time:09-15

I have the following smart contract function:

 function safeMint(address to, uint256 tokenId) public onlyOwner payable {
    require(msg.value >= mintPrice, "Not enough ETH to purchase NFT; check price!"); 
    _safeMint(to, tokenId);
}

and the following test function in chai to test it.

describe("mint", () => {
  it("should return true when 0.5 ethers are sent with transaction", async function () {
    await contract.deployed();
    const cost = ethers.utils.parseEther("0.1");
    await contract.safeMint("0x65.....",1,cost
  }); 

However the test function is not working and gives me an error on cost. Error: "Type 'BigNumber' has no properties in common with type 'Overrides & { from?: PromiseOrValue; }'." I fail to understand where the error lies.

CodePudding user response:

Try this, it's the valid syntax to send value with the call:

await contract.safeMint("0x65.....", 1, {value: cost});
  • Related