简体   繁体   English

如何在不使用设置 msg.value 的情况下将资金从 msg.sender(amount) 转移到收件人地址

[英]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:笔记:

  • These examples work on Solidity 0.8.这些示例适用于 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.一些以前的版本还允许现在不推荐使用的send()方法,不需要payable类型,并且call()方法的语法略有不同。
  • bonus is amount of wei (1 ETH is 10^18 wei) bonus是 wei 的数量(1 ETH 是 10^18 wei)

The 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 的合约(例如,它设置了一些局部变量),则交易将恢复。

Low-level 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.

相关问题 如何通过 msg.value 将以太币转入智能合约? - How to transfer ether to smart contract by msg.value? 为什么 msg.sender 有部署者的地址(地址不是调用者) - Why msg.sender has the address of the deployer (address isnt the caller) 在传递给另一个合约作为参数时,msg.sender地址是否已更改? - Is the msg.sender address changed while passing to another contract as a parameter? 下面代码中的 msg.sender 和 address(this) 有什么区别? - what is the differnce between msg.sender and address(this) in below code? 如何在solidity 0.5.0中将etherenum发送到msg.sender - How to send etherenum to msg.sender in solidity 0.5.0 调用super.owner()。transfer(msg.value)给出错误 - Call to super.owner().transfer(msg.value) giving errors 无法从智能合约向 msg.sender 发送以太币 - Not able to send ether to msg.sender from smart contract function 调用中的参数类型无效。 从地址到请求的应付地址的隐式转换无效。对于 msg.sender 和地址 0 - Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested.for msg.sender and adress 0 检查 msg.sender 是否是特定类型的合约 - Check if msg.sender is a specific type of contract Solidity 基础知识:“msg.sender”代表什么 - Solidity basics: what "msg.sender" stands for
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM