0

i have erc20 token with 18 decimal, the problem is if i want to transfer 100k token, the javascript can't handle that, because there will lot of 0.

so in smart contract i override all the transfer function into this

function transfer(address _toadr, uint256 _amount) override public returns (bool) {
    uint transferedAmount = _amount*10**18;
    uint balance =address(msg.sender).balance *10**18;
    require(balance >= transferedAmount);
    
    uint256 _teamFee = (transferedAmount / 100) * teamFee; 
    uint256 _marketingFee = (transferedAmount / 100) * marketingFee; 
    uint256 _rewardFee = (transferedAmount / 100) * rewardFee;
    uint256 _finalAmount = transferedAmount-(_teamFee+_marketingFee+_rewardFee);

    _transfer(msg.sender,team,_teamFee);
    _transfer(msg.sender,marketing,_marketingFee);
    _transfer(msg.sender,rewardpool,_rewardFee);
    _transfer(msg.sender,_toadr,_finalAmount);

    return true;
}

is this safe to do? or is there a best practice for this?

i heard we can use some utils from web3.js, but i didn't find a way to compare 2 value if i use web3.js utils.

1 Answer 1

0

You should use some BigNumber library. web3js has BN for that: https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html#bn

With BN you can do calculations and comparisons with big numbers. You definitely should not change your Solidity because of big numbers.

4
  • how to compare 2 big number? can i just do this? var number1 = new BigNumber(10); var number2 = new BigNumber(20); if(number1>number2) ... Commented Mar 1, 2022 at 11:06
  • nevermind i tried and it work Commented Mar 1, 2022 at 11:11
  • You probably shouldn't use direct > comparison. Check here for more info: github.com/indutny/bn.js Commented Mar 1, 2022 at 11:22
  • ah i see, i'll check it Commented Mar 1, 2022 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.