簡體   English   中英

Solidity Type“send”和“transfer”僅適用於使用支付地址的“address payable”類型的對象

[英]Solidity Type "send" and "transfer" are only available for objects of type "address payable" using a payable address

所以,我正在寫一個關於 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);
    }
}

2 事情是錯誤的:

當您初始化受益人時,您並沒有像這樣將他初始化為應付賬款:

address public payable Beneficiary;

Withdraw()中,您應該將地址轉換為 payable ,如下所示:

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

另外,由於 gas 限制,不建議再使用 transfer,我建議您使用 call 代替,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM