[Ethereum] Web3 how to send ether from client Metamask to contract address native receive function

metamaskremixsolidityweb3js

I wonder how can I natively pop-up the MetaMask window to send specific amount of ether as a donation to already defined SC address and ABI?

Short code intro:

pragma solidity >=0.7.0 <0.8.6;
[...]
    receive() external payable minumum_donation_amount(msg.value)  {
        OWNER.transfer(msg.value);
        donated_people[how_many_people_donated+1] = DonatedPeople(msg.sender, block.timestamp, msg.value);
        how_many_people_donated++;
        emit SubscribeDonatedPeople(msg.sender, block.timestamp, msg.value);
    }

    modifier minumum_donation_amount(uint256 amount) {
        require(amount >= 0.0005 ether, "The minumum donation amount is 0.0005 Ether");
        _;
    }
[...]

Successfully getting data from SC to JavaScript console with web3 anonymous function like this:

(function () {
  CONTRACT.methods.donated_people(1).call().then( function( donated ) { 
    console.log(donated);
  });
})();

I can send ether by hard coding the SC address and from/to address. However, I would like to get the ether from contract method directly to native receive function which has modifier to minimum amount. From Remix we are calling "Low level interactions" like this:

enter image description here

enter image description here

Any ideas how to achieve the same results in web3 like in Remix? Thanks in advance!

Best Answer

The function web3.eth.sendTransaction can be used to transfer directly to the contract receive function.

web3.eth.sendTransaction({
    from: USER_ADDRESS, 
    to: CONTRACT_ADDRESS, 
    value: AMOUNT_IN_WEI, 
    gas: GAS_AMOUNT
})
.then(function(receipt){
    ...
});

Related Topic