1

firstly, I have a code in a contract as below:

    receive() external payable {
        // function enter() external payable {
        require(msg.value > 0.1 ether, "require minimum 0.1 ether! ");
        players.push(payable(msg.sender));
    }

then I test it in the ganache with mocha and web3.js, the js code as below:

        await mycontract.send({
            from: accounts[1],
            value: web3.utils.toWei('2', 'ether')
        });

but got the error TypeError: mycontract.send is not a function

then I update the contract as function enter() external payable , so js works as below:

        await mycontract.methods.enter().send({
            from: accounts[2],
            value: web3.utils.toWei('2', 'ether')
        });

But I'm wondering is there a solution as I wrote as the first type?

1 Answer 1

1

The receive() function does not exist but the fallback function exists, it will be triggered when a contract is sent Ether without any data. To interact with this receive() function in web3.js, you don't call it directly, you simply send Ether to the contract address.

In the code below just replace contract.address with the address of your deployed contract

web3.eth.sendTransaction({
    from: accounts[1],
    to: contract.address,
    value: web3.utils.toWei('2', 'ether')
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.