Search code examples
blockchainsoliditytruffleweb3jshardhat

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;
    }

Solution

  • 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