How do I burn the token with each transfer? Deflationary Token

I am aware of the problems found in deflationary tokens, and I want to understand a little more how everything works.

Initially I created this code in a simple way, and I wanted to add a 5% burn for each transfer made, so I made the following code.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

contract Token {

mapping(address => uint) public balances;
mapping(address => mapping(address => uint)) public allowance;

uint public totalSupply = 10000 * 10 ** 9;
string public name = "BitTom";
string public symbol = "BTO";
uint public decimals = 9;

event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);

constructor() {
    balances[msg.sender] = totalSupply;
}

function balanceOf(address owner) public view returns(uint) {
    return balances[owner];
}

function transfer(address to, uint value) public returns(bool) {
    require(balanceOf(msg.sender) >= value, 'Saldo insuficiente (balance too low)');
    balances[from] -= value;
    totalSupply -=  value * 5 / 100;
    value -=  value * 95 / 100;
    balances[to] += value;
    emit Transfer(msg.sender, to, value);
    return true;
}

function transferFrom(address from, address to, uint value) public returns(bool) {
    require(balanceOf(from) >= value, 'Saldo insuficiente (balance too low)');
    require(allowance[from][msg.sender] >= value, 'Sem permissao (allowance too low)');
    balances[from] -= value;
    totalSupply -=  value * 5 / 100;
    value -=  value * 95 / 100;
    balances[to] += value;
    
    emit Transfer(from, to, value);
    return true;
}

function approve(address spender, uint value) public returns(bool) {
    allowance[msg.sender][spender] = value;
    emit Approval(msg.sender, spender, value);
    return true;
}

}

Would you know to inform me if my thinking is right?

Does it work? In your test environment transfer the token. See if it burns 5%. If it does then yes your thinking is correct.

My doubt is if this is really the best way.

Check out the Burnable contract from Open Zeppelin

https://docs.openzeppelin.com/contracts/4.x/api/token/erc20#ERC20Burnable

You could do a transfer, then call a burn with a specific amount of tokens (the percentage of tokens you want burned)

1 Like