0

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!

0

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){
    ...
});

2
  • Unfortunately, this does not trigger the receive() func. This just sends from addr to addr. – pr0logas Mar 1 at 21:03
  • @Prologas Why do you say it doesn't work? I've used the same code for many projects under different circumstances from backed, from frontend and it worked every single time. – Ismael Mar 2 at 2:34
0

This example works well. Please take a note if you want to hit SC address receive function you have to pass CONTRACT.options.address

client_account = accounts[0]; // assuming master client addr

function contribute_to_the_project() {
  const amount = "0.0004"; 
  const amountToSend = web3.utils.toWei(amount, "ether"); // Convert to wei value
  web3.eth.sendTransaction({ 
    from: client_account,
    to: CONTRACT.options.address, 
    value: amountToSend 
  }).then( function(tx) { ;
  console.log("Transaction: ", tx); 
  });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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