简体   繁体   中英

how to transfer fund from msg.sender(amount) to recipient address without using setting msg.value

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM