Reputation: 21
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
Upvotes: 1
Views: 4267
Reputation: 65
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.
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:
While deploying the burning contract, make sure to add the token address as the constructor parameter.
Upvotes: -1