如何从另一份智能合约中烧掉智能合约的令牌?

发布于 2025-01-23 10:49:38 字数 101 浏览 0 评论 0 原文

假设在区块链上具有内部功能_BURN和100000000令牌的总供应,我想写一份新的智能合约,可以刻录令牌tokena并从供应中脱颖而出?我尝试了许多解决方案,但仍然无法调用函数_Burn

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? I've tried many solutions but still unable to call the function_burn

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

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

发布评论

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

评论(1

漆黑的白昼 2025-01-30 10:49:38

由于该功能是内部,因此这意味着它只能在合同本身或从其衍生的合同中内部访问。

您可以使用ERC20Burnable扩展名(下面的链接)或与外部/公共修饰符的令牌合同内实现_burn函数。这样,其他合同可以调用该功能。

erc20 burnable burnable版本:

token合同:

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

燃烧合同:

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

您的合同:

令牌合同:

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

燃烧合同:

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

减少供应已在burn功能内实施,因此无需担心。

请记住,燃烧的合同需求需要津贴才能花费所有者代币。您可以通过将令牌合同的批准功能与烧合合同的地址作为参数:

“设置批准”

在部署燃烧合同时,请确保将令牌地址添加为构造函数参数。

Since the function is internal, it means it can only be accessed internally within the contract itself or in contracts deriving from it.

You can either use ERC20Burnable extension (link below) or implement the _burn function inside your token contract with external/public modifier. This way, other contracts can call the function.

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

ERC20Burnable version:

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. 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:

Setting approve

While deploying the burning contract, make sure to add the token address as the constructor parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文