坚固性类型 "send" 和 "transfer" 仅适用于使用应付地址的 "address payable" 类对象



所以,我正在写一个关于Solidity的智能合约,我认为我的编译器错了什么的,但我尝试使用Remix, Truffle和Hardhat,它们都给出了相同的错误,我不知道我做错了,因为我明确声明了"受益人"。变量是可支付的,即使在构造函数中,有人能帮助我吗?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
//This contract uses a "Timelock" function, please note that if you actually use it, you CANNOT withdraw funds until the set date!!!!
contract Banking {
address public Beneficiary;
uint256 public givenTime;
constructor(address payable _Beneficiary, uint256 _givenTime) {
require(_givenTime > block.timestamp); //Make sure the time is in the future.
Beneficiary = _Beneficiary;
givenTime = _givenTime;
}
function Withdraw() public {
require(block.timestamp >= givenTime);
address(Beneficiary).transfer(address(this).balance);
}
}

事情不对劲:

当您初始化受益人时,您没有像这样初始化他的付款:

address public payable Beneficiary;

Withdraw()中,你应该把地址转换成这样:

payable(Beneficiary).transfer(address(this).balance);

也不建议再使用transfer了,因为gas的限制,我建议你用call代替,像这样:

(bool success,) = payable(Beneficiary).call{value: address(this).balance}(""); 
require(success, "transaction failed");

相关内容