简体   繁体   中英

How to invoke a payable solidity function using HardHat?

I have a solidity function called adopt a dog as below which is bascically a payable function in the contract.

// THIS IS FAILING AS I DONT KNOW HOW TO PASS ETHERS IN HARDHAT/ETHER.JS

Hardhart

 const Adopt = await ethers.getContractFactory("Adopt");
    const adopt = await Adopt.deploy();
    await adopt.deployed();
    await adopt.adopt("Hachiko"); 

Contract

 function adopt(string calldata dog_breed) external payable {
             require(msg.value >= 1 ether ,"Min 1 ether needs to be transfered");
            require(user_list[msg.sender].user_allowed_to_adopt,"User not 
            allowed to participate for adoption");
            require(!user_list[msg.sender].adopted,"User has already 
            adopted the dog");
            
        User memory user=user_list[msg.sender];
        user.adopted=true;
        user_list[msg.sender]=user;
    }

You can use the overrides param.

await adopt.adopt("Hachiko", {
    value: ethers.utils.parseEther("1.0")
}); 

Docs: https://docs.ethers.io/v5/api/contract/contract/#Contract-functionsCall

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM