I'm trying to send ETH to a contract in tests using hardhat await owner.sendTransaction({to: greeter.address, value: "1000000"}), but it reverts with "function selector was not recognized" error.
You can reproduce this with a basic project -
npm init -y
npm i -D hardhat
npx hardhat # use defaults
edit test/sample-test.js and add the following test case -
it("Should send ETH to the contract", async function () {
[owner] = await ethers.getSigners();
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, world!");
await greeter.deployed();
await owner.sendTransaction({to: greeter.address, value: "1000000"});
});
run tests npx hardhat test and the newly added test will fail with Error: Transaction reverted: function selector was not recognized and there's no fallback nor receive function.
Full test log -
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'
✔ Should return the new greeting once it's changed (626ms)
Deploying a Greeter with greeting: Hello, world!
1) Should send ETH to the contract
1 passing (745ms)
1 failing
- Greeter
Should send ETH to the contract:
Error: Transaction reverted: function selector was not recognized and there's no fallback nor receive function
at Greeter.<unrecognized-selector> (contracts/Greeter.sol:6)
at EthModule._estimateGasAction (/Users/tal/dev/solidity/SendEthToContract/node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:425:7)
at HardhatNetworkProvider.request (/Users/tal/dev/solidity/SendEthToContract/node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:118:18)
at EthersProviderWrapper.send (/Users/tal/dev/solidity/SendEthToContract/node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)
Any idea how to send ETH to the contract?
receivekeyword. – Kof Apr 29 '22 at 06:58