0

How would I implement a 1% burn of tokens from each transaction? This is my code below, but I am struggling to get it to work. Any help would be greatly appreciated.

pragma solidity ^0.8.4;

// SPDX-License-Identifier: UNLICENSED

contract Test_Token {
    mapping(address => uint256) public balances;
    mapping(address => mapping(address => uint256)) public allowance;
    uint256 public _totalSupply = 10 * 10 * 10**9;
    string public _name = "Test Token";
    string public _symbol = "TTT";
    uint256 public _decimals = 9;
    
    event Transfer(address indexed  from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event Burn(address indexed from, uint256 value);
    
    constructor() {
        balances[msg.sender] = _totalSupply;
    }
    
    function balanceOf(address owner) public view returns(uint256) {
        return balances[owner];
    }
    
    function transferandBurn(address to, uint256 value) public returns (bool) {
        uint256 burn_token = (value / 100);
        require(balanceOf(msg.sender) >= value + burn_token, 'Wallet balance is too low');
        balances[to] += value - burn_token;
        balances[msg.sender] -= value - burn_token;
        _totalSupply -= burn_token;
        return true;
    }
    
    function transferFrom(address from, address to, uint256 value) public returns(bool) {
        require(balanceOf(from) >= value, 'Wallet Balance is too low');
        require(allowance[from][msg.sender] >= value, 'Allowance is too low');
        balances[to] += value;
        balances[from] -= value;
        emit Transfer(from, to, value);
        return true;
        
    }
    
    function approve(address spender, uint256 value) public returns(bool) {
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;
    }


}
8
  • What exactly do you struggle with? What does not work? – jjj Jun 7 at 12:50
  • I get an error message every time I try to make a transaction with it. – GandalfTheGrey Jun 7 at 13:01
  • I see another mistake (should have nothing to do with any error): a transferandBurn call does not remove tokens, because you subtract the same amount from sender as you add to the receiver. You should subtract "value + burn_token" and add just "value". (Or subtract value and add value-burn_token and change the require. What you do is your design choice) – jjj Jun 7 at 13:06
  • ahh, I didn't realise that, is there a way I can just burn 1% of the tokens per transaction? – GandalfTheGrey Jun 7 at 13:11
  • Just add 1% less than you subtract, like I said. You can decide if the receiver just gets less than value or if the sender has to pay more. – jjj Jun 7 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.