[英]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?
但是有没有办法将资金直接转移到 msg.sender 钱包给收件人? I can not set msg.value at the time of invoking payoutBonus call.
我无法在调用 payoutBonus 时设置 msg.value。 Because I can get the amount only inside payoutBonus method.
因为我只能在 payoutBonus 方法中获得金额。
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. msg.value
是一个只读属性,反映传入事务的值。 If you want to send an ETH amount, you can do it in one of two ways.如果您想发送 ETH 金额,您可以通过以下两种方式之一进行。
Notes:笔记:
send()
method that is now deprecated, don't require the payable
type, and have slightly different syntax for the call()
method.send()
方法,不需要payable
类型,并且call()
方法的语法略有不同。bonus
is amount of wei (1 ETH is 10^18 wei) bonus
是 wei 的数量(1 ETH 是 10^18 wei)transfer()
method transfer()
方法uint256 bonus = calculateBonus();
payable(msg.sender).transfer(bonus);
The transfer()
method only allows consuming 2300 gas, which is enough for non-contract recipients. transfer()
方法只允许消耗 2300 gas,这对于非合约接收者来说已经足够了。 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.如果接收方是需要超过 2300 个 gas 才能接收 ETH 的合约(例如,它设置了一些局部变量),则交易将恢复。
call()
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.使用低级
call()
方法,您还可以指定要调用的气体限制 function,它是 arguments。 Use this method only if you know what you're doing.仅当您知道自己在做什么时才使用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.