2

I am trying to create a transaction where the users sends "0.5" USDT token to a smart contract. My issue is that i can't convert "0.5" to a BigNumber in order to pass it in the transaction using web3 contract methods as BigNumber.js doesn't accept decimals. for example this method will throw an error saying that bigNumber doesn't support decimals.

Contract.method(web3.utils.toBN("0.5")).send()

I can use the toWei method of web3 but it doesn't work if the decimals are not equal to 18. How can i do this ? is there a workaround ?

5
  • 0.5 * 10^decimal or should I go better sleep?
    – Majd TL
    Oct 16, 2021 at 20:21
  • if it was another decimal number wouldn't that result in an error from the JS Number object ? Oct 16, 2021 at 20:36
  • Yes You need to use the BN library to power, multiply and div.
    – Majd TL
    Oct 16, 2021 at 20:44
  • 1
    I can try it tomorrow for u . It is 22:44 my time😴
    – Majd TL
    Oct 16, 2021 at 20:44
  • 1
    Did you try something that supports arbitrary decimals like mikemcl.github.io/big.js? It is bad having two libraries for handling arbitrary length numbers, but probably unavoidable in javascript yet.
    – Ismael
    Oct 17, 2021 at 17:45

1 Answer 1

1

That is usually how I calculate half of a token. but I don't guarantee that it works correctly :) so please check the code and make sure it works and is correct and use it only on your own responsibility. Web3js (I'm using "web3": "^1.6.0") use this https://github.com/indutny/bn.js/ for BigNumbers

function getHalfCoin() {
    const decimal = 6; // you can get it from the contract;
    var BN = web3.utils.BN;
    const decimalAsBigNumber = new BN(decimal);
    console.log(decimalAsBigNumber); // BN { negative: 0, words: [ 6 ], length: 1, red: null }

    const numberOneAsBigNumber = new BN(1);
    const numberTowAsBigNumber = new BN(2);
    const numberTenAsBigNumber = new BN(10);

    const oneCoin = numberOneAsBigNumber.mul(numberTenAsBigNumber.pow(decimalAsBigNumber));
    console.log(oneCoin); // BN { negative: 0, words: [ 1000000, <1 empty item> ], length: 1, red: null}
    console.log(oneCoin.toString()); // 1000000

    const halfCoin = oneCoin.div(numberTowAsBigNumber);
    console.log(halfCoin); // BN { negative: 0, words: [ 500000 ], length: 1, red: null }
    console.log(halfCoin.toString()); // 500000
}

https://bybit-exchange.github.io/erc20-faucet/

here mint 1 token on rinkeby enter image description here

and here transfer 0.5 on rinkeby enter image description here

4
  • Your solution is not a bad one but using Big.js like Ismael mentioned was much easier and i suggest you do that also. Oct 30, 2021 at 18:18
  • @KakiMasterOfTime but u can’t do Contract.method(new Big(0.5)).send(), can u?
    – Majd TL
    Oct 30, 2021 at 18:47
  • I use something like this, Contract.method(web3.utils.toBN(Big(0.5).mul(decimals))).send(). My issue was to multiply the O.5 with the 10^decimals since that is not taken into consideration by the utils.toBN function. Oct 30, 2021 at 18:52
  • ah cool nice, add it maybe as answer and accept it , much easier than mine @KakiMasterOfTime
    – Majd TL
    Oct 30, 2021 at 18:56

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.