How do I include burn function as per the total supply?

@abcoathup I try to code my token to burn each transection as per the total supply, but when the total supply 48,000 or below it’s not burning 4% the burning percentage is same 2% please check my code :point_down:

pragma solidity 0.5.10;

library SafeMath {
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
  return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
  }

  function div(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a / b;
return c;
  }

  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
  }

  function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
  }

  function ceil(uint256 a, uint256 m) internal pure returns (uint256) {
uint256 c = add(a,m);
uint256 d = sub(c,1);
return mul(div(d,m),m);
  }
}


interface IERC20 {

function totalSupply() external view returns (uint256);

function balanceOf(address account) external view returns (uint256);

function transfer(address recipient, uint256 amount) external returns (bool);

function allowance(address owner, address spender) external view returns (uint256);

function approve(address spender, uint256 amount) external returns (bool);

function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

event Transfer(address indexed from, address indexed to, uint256 value);

event Approval(address indexed owner, address indexed spender, uint256 value);
}


contract XXX is IERC20 {
using SafeMath for uint256;

mapping (address => uint256) private _balances;

mapping (address => mapping (address => uint256)) private _allowances;

uint256 public _burnRate;
uint256 private _totalSupply;


string public constant name = "TRIPLX";
string public constant symbol = "XXX";
uint256 public constant decimals = 18;

uint256 public constant INITIAL_SUPPLY = 50000 * 10**18;

  /**
   * @dev Contructor
   */
  constructor() public {
_totalSupply = INITIAL_SUPPLY;
_balances[0xdddFE120F803b18159EcB807274fb37340a12dd4] = INITIAL_SUPPLY;
emit Transfer(address(0), 0xdddFE120F803b18159EcB807274fb37340a12dd4,_totalSupply);

  }


function totalSupply() public view returns (uint256) {
    return _totalSupply;
}


function balanceOf(address account) public view returns (uint256) {
    return _balances[account];
}

function transfer(address recipient, uint256 amount) public returns (bool) {
    _transfer(msg.sender, recipient, amount);
    return true;
}


function allowance(address owner, address spender) public view returns (uint256) {
    return _allowances[owner][spender];
}


function approve(address spender, uint256 value) public returns (bool) {
    _approve(msg.sender, spender, value);
    return true;
}

function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
    _transfer(sender, recipient, amount);
    _approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
    return true;
}


function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
    _approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
    return true;
}


function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
    _approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue));
    return true;
}


function _transfer(address sender, address recipient, uint256 amount) internal {
    require(sender != address(0), "ERC20: transfer from the zero address");
    require(recipient != address(0), "ERC20: transfer to the zero address");
    
    
    uint256 tokensToBurn = _tokenToBurn(amount);
    uint256 tokensToTransfer = amount.sub(tokensToBurn);
    
    _balances[sender] = _balances[sender].sub(amount);
    _balances[recipient] = _balances[recipient].add(tokensToTransfer);

    _totalSupply = _totalSupply.sub(tokensToBurn);
    
    emit Transfer(sender, recipient, tokensToTransfer);
    emit Transfer(sender, address(0), tokensToBurn);
}


function _approve(address owner, address spender, uint256 value) internal {
    require(owner != address(0), "ERC20: approve from the zero address");
    require(spender != address(0), "ERC20: approve to the zero address");

    _allowances[owner][spender] = value;
    emit Approval(owner, spender, value);
}


function burnRate() public returns(uint256) {
    if (_totalSupply > 48000) {
        _burnRate = 2;
    } else if(_totalSupply <= 48000 && _totalSupply > 46000) {
        _burnRate = 4;
    } else if(_totalSupply <= 46000 && _totalSupply > 44000) {
        _burnRate = 6;
    } else if(_totalSupply <= 44000 && _totalSupply > 42000) {
        _burnRate = 8;
    } else if(_totalSupply <= 42000 && _totalSupply > 0) {
        _burnRate = 10;
    }
    
    return _burnRate;
}


function _tokenToBurn(uint256 value) public returns(uint256){ 
    uint256 _burnerRate = burnRate();
    uint256 roundValue = value.ceil(_burnerRate);
    uint256 _myTokensToBurn = roundValue.mul(_burnerRate).div(100);
    return _myTokensToBurn;
}

}

If you see here you will understand what I want !

function burnRate() public returns(uint256) {
    if (_totalSupply > 48000) {
        _burnRate = 2;
    } else if(_totalSupply <= 48000 && _totalSupply > 46000) {
        _burnRate = 4;
    } else if(_totalSupply <= 46000 && _totalSupply > 44000) {
        _burnRate = 6;
    } else if(_totalSupply <= 44000 && _totalSupply > 42000) {
        _burnRate = 8;
    } else if(_totalSupply <= 42000 && _totalSupply > 0) {
        _burnRate = 10;
    }
    
    return _burnRate;
}

I’m confused why the code is not working ? and what’s wrong with me ? Please help me !
Thanks in advenced ! @abcoathup :two_hearts:

Transection after below 48,000 total supply :point_down:

1 Like

Hi @Rahaman,

I copied your function into a contract to manually test in Remix and it looks ok:

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

contract MyContract {
    
    function burnRate(uint256 supply) public pure returns(uint256) {
        uint256 rate = 2;
        if (supply > 48000) {
            rate = 2;
        } else if(supply <= 48000 && supply > 46000) {
            rate = 4;
        } else if(supply <= 46000 && supply > 44000) {
            rate = 6;
        } else if(supply <= 44000 && supply > 42000) {
            rate = 8;
        } else if(supply <= 42000 && supply > 0) {
            rate = 10;
       }
        return rate;
    }
}

I recommend breaking your contract down and testing the functions and when you are happy with the logic, write unit tests for your contract to test the functionality.

As an aside, I recommend not using a flattened contract. It is more difficult to read, it is hard to tell if anything that you imported has been modified, the information about imports is lost, so are any licenses.

You can use npm imports with Truffle/Hardhat or GitHub imports with Remix.

1 Like

Thanks for your time !

1 Like