0
votes

We can transfer funds from address(this) to recipient. But is there any way to transfer funds directly msg.sender wallet to recipient? I can not set msg.value at the time of invoking payoutBonus call. Because I can get the amount only inside payoutBonus method.

function payoutBonus(address recipient) public payable returns (bool) {
    // bonus = calculateBonus();
    //transfer this bonus to recipient from msg.sender;
    return true;
}
1
msg.sender.send()Mikko Ohtamaa

1 Answers

0
votes

msg.value is a read-only property reflecting value of the incoming transaction. If you want to send an ETH amount, you can do it in one of two ways.

Notes:

  • These examples work on Solidity 0.8. Some previous versions also allow the send() method that is now deprecated, don't require the payable type, and have slightly different syntax for the call() method.
  • bonus is amount of wei (1 ETH is 10^18 wei)

The transfer() method

uint256 bonus = calculateBonus();
payable(msg.sender).transfer(bonus);

The transfer() method only allows consuming 2300 gas, which is enough for non-contract recipients. If the recipient is a contract requiring more than 2300 gas to receive the ETH (for instance it sets some local variable), the transaction reverts.

Low-level call()

uint256 bonus = calculateBonus();
msg.sender.call{value: bonus}("");

With the low-level call() method, you can also specify the gas limit, function to call and it's arguments. Use this method only if you know what you're doing.