【问题标题】:How to withdraw all tokens form my bsc contract如何从我的 bsc 合约中提取所有代币
【发布时间】:2022-01-02 02:14:00
【问题描述】:

我真的对合同缺乏经验,需要您的帮助。我用 remix 创建了一个合约,并发送了一些 bnb 给它。我想找回它,但我似乎无法做到。

pragma solidity ^0.8;

interface IERC20 {
    function transfer(address _to, uint256 _amount) external returns (bool);
}

contract MyContract {
    function withdrawToken(address _tokenContract, uint256 _amount) external {
        IERC20 tokenContract = IERC20(_tokenContract);
        
        // transfer the token from address of this contract
        // to address of the user (executing the withdrawToken() function)
        tokenContract.transfer(msg.sender, _amount);
    }
}

这是我在另一篇文章中使用的代码,但我不明白。我必须用数字更改“_to”“和“_amount”还是我只是复制代码并编译它?

我真的很抱歉,但我不知道我做了什么,所以我只想拿回令牌。

谢谢

【问题讨论】:

    标签: solidity smartcontracts remix erc20


    【解决方案1】:

    抱歉,你的bnb不能提现,bnb不是代币,bnb就像以太坊中的以太币,原生链币,合约没有让你提现的功能,唯一的办法就是如果你发送wbnb,在这种情况下你可以查看wbnb的合约地址并调用你所做的合约的函数withdraw

    【讨论】:

      【解决方案2】:

      正如 Jhonny 所说,BNB 不是一个实际的代币,因此您实施的逻辑将无法提取 BNB。但只是为了让您知道,您可以创建一个只允许提取 BNB(这是本币)的功能。应该是这样的:

      function withdraw(uint _amount) external {
          payable(msg.sender).transfer(_amount);    
      }
      

      希望你觉得这很有用。

      【讨论】:

        猜你喜欢
        • 2023-04-10
        • 2022-08-02
        • 2021-08-28
        • 2023-01-05
        • 2021-12-18
        • 2021-05-17
        • 1970-01-01
        • 2022-07-30
        • 1970-01-01
        相关资源
        最近更新 更多