简体   繁体   English

如何从另一个智能合约中烧掉智能合约的代币?

[英]How to burn token of Smart contract from another Smart contract?

Let say there is a BEP20 token (tokenA)on the blockchain with an internal function _burn and a total supply of 100000000 token i want to write a new Smart contract that can burn token TokenA and substract from the supply how can i proceed?假设区块链上有一个 BEP20 代币 (tokenA),其内部 function _burn 和总供应量为 100000000 代币 我想编写一个新的智能合约,可以燃烧代币 TokenA 并从供应中减去我该如何继续? I've tried many solutions but still unable to call the function_burn我尝试了很多解决方案,但仍然无法调用 function_burn

Since the function is internal , it means it can only be accessed internally within the contract itself or in contracts deriving from it.由于 function 是internal ,这意味着它只能在合约本身或从它派生的合约中进行内部访问。

You can either use ERC20Burnable extension (link below) or implement the _burn function inside your token contract with external/public modifier.您可以使用 ERC20Burnable 扩展(下面的链接)或使用外部/公共修饰符在您的代币合约中实施 _burn function。 This way, other contracts can call the function.这样其他合约就可以拨打function了。

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC20Burnable.sol https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC20Burnable.sol

ERC20Burnable version: ERC20可燃版本:

Token contract:代币合约:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract BurnMe is ERC20, ERC20Burnable {
    constructor() ERC20("Burn me", "BURN"){}

    function mint() external {
        _mint(msg.sender, 10000);
    }
}

Burning contract:燃烧合约:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract BurnOther{

    ERC20Burnable _token;
    constructor(address token_){ 
        _token = ERC20Burnable(token_); 
    }

    function burnOther(uint256 amount) external {
        _token.burnFrom(msg.sender, amount);
    }
}

Your contract version:您的合同版本:

Token contract:代币合约:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract BurnMe is ERC20 {
    constructor() ERC20("Burn me", "BURN"){}

    function mint() external {
        _mint(msg.sender, 10000);
    }

    function burn(address account, uint256 amount) external {
        _spendAllowance(account, _msgSender(), amount);
        _burn(account, amount);
    }
}

Burning contract:燃烧合约:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./burn.sol";

contract BurnOther{

    BurnMe _token;
    constructor(address token_){ 
        _token = BurnMe(token_); 
    }

    function burnOther(uint256 amount) external {
        _token.burn(msg.sender, amount);
    }
}

Decreasing supply is already implemented inside _burn function, so no need to worry about that. _burn function 内部已经实施了减少供应,因此无需担心。 https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol#L290 https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol#L290

Keep in mind that the burning contract needs allowance to spend the owner's tokens.请记住,燃烧合约需要津贴才能花费所有者的代币。 You can do that by using approve function of the token contract with the burning contract's address as the parameter:你可以通过使用代币合约的 approve function 和燃烧合约的地址作为参数来做到这一点:

设置批准

While deploying the burning contract, make sure to add the token address as the constructor parameter.在部署燃烧合约时,请确保将代币地址添加为构造函数参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM