1

I want to specifically use solidity's low level transfer function to send ether.

I tried something like this:

contract.methods.transfer()

But transfer is not a function. I would send a transaction to the receive function of the contract but it's also not available like you would for a normal function,

contract.functionName(params)

How would I go about solving this?

2 Answers 2

2

You could try this https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#sendtransaction The simple explanation is:

  • Send Transaction by web3
sendTransaction({
from: 'your_address'
to: 'contract_address'
data: '0x'
value: web3.utils.toWei('1', 'ether')
})
  • Above tx has data is '0x', none function, so it will access into receive or fallback function in your smart contract, so you should put one of them inside your contract, Read here

Additional simple explanation about receive & fallback:

send ether? 
 => have msg.data?
     => Yes: 
        => receive exist? 
            => Yes: receive()
            => No: fallback()
     => No: fallback()
5
  • But is the sendTransaction function solidity's transfer's function equivalent? Or is this the send function equivalent? There are 3 different ways to transfer ETH; transfer, call, and send. I want to use the transfer function. Which one does sendTransaction use?
    – Domini
    Commented Mar 6, 2023 at 14:33
  • transfer, call, and send and web3.sendTransaction are different. And transfer, call, and send, different each other too. - transfer: use to transfer eth and throw error - send: use to transfer eth and return bool - call: use to execute a function from another contract, it can be used to transfer eth because we can include value when executing addressA.call{value: msg.value}("");
    – CT95
    Commented Mar 6, 2023 at 15:42
  • Web3.sendTransaction, it's used to execute a transaction based on data, you can see the docs here: ethereum.org/en/developers/docs/transactions/#the-data-field. Like The first four bytes specify which function to call and so on...
    – CT95
    Commented Mar 6, 2023 at 15:45
  • If you ask Which one does sendTransaction use? I think the suitable answer is call. The tx web3.sendTransaction({from:..., to:..., data: '0x', value: 1ether } is the same with addressA.call{value: 1 ether}('0x'). But the transfer, send is work as well as too, so I can't ensure about it ("._.)
    – CT95
    Commented Mar 6, 2023 at 15:47
  • Hmm, I suppose I'll opt to go your and Safi's route of using sendTransaction. I'll admit, I am trying to use a transfer function for an Ethernaut's challenge on exploiting a receive() function to make it unusable. It seems I can't use the transfer function to send ETH through a contract to another contract. However, I will use sendTransaction to take advantage of the gas stipend receive() requires to execute (trying not to spoil the answer too much for whoever reads this and I don't know if my hypothesis will work yet). Thank you for your thoroughness.
    – Domini
    Commented Mar 6, 2023 at 22:31
2

To send ETH to the contract having receive() external payable{} you just send transaction with value to:contractAddress as you do to send ETH to EOA wallets. Every contract is also an ethereum address so, the contract having function receive can receive ETH as normal transfer. Eg.

web3.eth.sendTransaction({
    from: 'YourETHAddress',
    to: 'contractAddress',
    value: '1000000000000000'
})
.then(function(receipt){
    ...
});

Your Answer

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

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