固体类型“发送”和“转移”仅适用于类型的“应付款项”的对象。使用应付的地址

发布于 2025-01-22 12:19:13 字数 806 浏览 0 评论 0原文

因此,我正在写一份关于坚固性的智能合同,我认为我的编译器是错误的,但是我尝试使用混音,松露和硬汉,他们都会给出同样的错误,我不知道我做错了,因为我将“受益人”变量宣布为应付款项,即使在构造函数中,任何人都可以帮助我吗?

    // 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);
    }
}

So, i am writing a smart contract on Solidity, and i thought my compiler was wrong or something, but i tried using Remix, Truffle and Hardhat, and them all give the same error, i don't know that i am doing wrong because i explicit declared the "Beneficiary" variable as payable, even in the constructor, can anyone help me?

    // 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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2025-01-29 12:19:13

2件事是错误的:

当您启动受益人时,您不应该像这样付款时启动他:

address public payable Beneficiary;

fort()中,您应该将地址投入到应付款中:

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

也不建议使用由于气体限制而再转移,我建议您这样使用呼叫:

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

2 Things are wrong:

When you init Beneficiary you didn't init him as payable like this:

address public payable Beneficiary;

And in the Withdraw() you should cast the address into payable like this:

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

Also it is not recommended to use transfer anymore because of gas restrictions, I recommend that you use call instead like this:

(bool success,) = payable(Beneficiary).call{value: address(this).balance}(""); 
require(success, "transaction failed");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文