Solidity Hardhat – How to Test Sending Ether to a Contract with a Payable Receive Function

hardhatsolidity

I am writing Hardhat tests for a Solidity contract I've written. The contract contains a receive() external payable { ... } function. How can I call this from a hardhat test with an Ether amount?

Best Answer

You can make a transaction and send ether to the contract address

const [owner] = await ethers.getSigners();

const transactionHash = await owner.sendTransaction({
  to: "contract address",
  value: ethers.utils.parseEther("1.0"), // Sends exactly 1.0 ether
});

Making a transaction should call the receive function.

Related Topic