# 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

``````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 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 approve(address spender, uint256 amount) external returns (bool);

}

contract XXX is IERC20 {
using SafeMath for uint256;

mapping (address => uint256) private _balances;

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;

}

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

return _allowances[owner][spender];
}

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

_transfer(sender, recipient, amount);
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
return true;
}

return true;
}

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

uint256 tokensToBurn = _tokenToBurn(amount);
uint256 tokensToTransfer = amount.sub(tokensToBurn);

_balances[sender] = _balances[sender].sub(amount);

_totalSupply = _totalSupply.sub(tokensToBurn);

emit Transfer(sender, recipient, tokensToTransfer);
}

_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 !

Transection after below 48,000 total supply

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