简体   繁体   English

如何使用 solidity 调用 function 从合同中发送固定数量的以太币?

[英]How to send fixed amount of ether from contract using call function in solidity?

I am trying to send ether from contract to a wallet, when I use the call function with msg.value it works, however when I try to send 1 ether instead of msg.value it doesn't work.我正在尝试将以太币从合同发送到钱包,当我使用带有 msg.value 的调用 function 时它有效,但是当我尝试发送 1 个以太币而不是 msg.value 时它不起作用。 Why is that and how to overcome this?为什么会这样,如何克服呢? I am using Remix.io Javascript VM London, can that be the reason?我正在使用 Remix.io Javascript VM London,这可能是原因吗?

Here this code below works:下面这段代码有效:

function sendMoney() public payable  {
    address payable receiver = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);

    (bool sent, bytes memory data) = receiver.call{ value: msg.value }("");
    require(sent, "Failed to send Ether");
}

However this code below doesn't work.但是下面的这段代码不起作用。 I have to send fixed amount of ether.我必须发送固定数量的以太币。

function sendMoney() public payable  {
    address payable receiver = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);

    (bool sent, bytes memory data) = receiver.call{ value:  1 ether }("");
    require(sent, "Failed to send Ether");
}
sendMoney() public payable  {
    address payable receiver = 
    payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);

    (bool sent, bytes memory data) = receiver.call{ value:  1 ether }("");
    require(sent, "Failed to send Ether");
}

This works.这行得通。 You just need to pass something in "value" in remix, and after 1 ether will be transfered to 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 and value that you passed in remix will be transfered to contract balance.你只需要在 remix 中传递一些“值”,在 1 个以太币将被转移到 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 之后,你在 remix 中传递的值将被转移到合约余额中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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