我试着在混合中逐个运行以下示例:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract Payable {
// Payable address can receive Ether
address payable public owner;
// Payable constructor can receive Ether
constructor() payable {
owner = payable(msg.sender);
}
// Function to deposit Ether into this contract.
// Call this function along with some Ether.
// The balance of this contract will be automatically updated.
function deposit() public payable {}
// Call this function along with some Ether.
// The function will throw an error since this function is not payable.
function notPayable() public {}
// Function to withdraw all Ether from this contract.
function withdraw() public {
// get the amount of Ether stored in this contract
uint amount = address(this).balance;
// send all Ether to owner
// Owner can receive Ether since the address of owner is payable
(bool success, ) = owner.call{value: amount}("");
require(success, "Failed to send Ether");
}
// Function to transfer Ether from this contract to address from input
function transfer(address payable _to, uint _amount) public {
// Note that "to" is declared as payable
(bool success, ) = _to.call{value: _amount}("");
require(success, "Failed to send Ether");
}}
部署之后,当我使用任意一个混合的javascript VM地址作为_to,使用number 1作为_amount调用"transfer“函数时,它将使用以下方式进行还原:
还原事务已恢复到初始状态。合同提供的理由是:“未能发送以太”。
我认为这与访问无关,而是与我如何引入属性有关。有人能给我指点正确的方法吗?提前谢谢你!
发布于 2022-02-10 23:01:20
一个简单的解决方案是将此函数添加到代码中,以便正确地接收契约的令牌:
receive() external payable { }我发现将ETH发送到合同中会造成错误,不管使用何种气体,如果没有上述功能,这是可以预料的。
其次,您应该考虑到,当您输入发送的ETH数量时,除非代码(即_amount * 10**18)中另有规定,否则发送的ETH数量没有十进制。这意味着"1“的输入实际上意味着"0.000000000000000001 ETH”。
发布于 2022-11-11 06:19:22
测试了这个智能混合协议(0.8.11+)能够
另外,_to地址不需要支付,
function transfer(address _to, uint _amount) public {}由于调用调用不需要支付地址
发布于 2022-02-10 18:23:34
您的合同可能没有足够的乙醚发送到另一个帐户。在部署时,确保它收到了足够的数量,以便以后调用传输函数时,它可以执行传输。
https://ethereum.stackexchange.com/questions/121338
复制相似问题