100000000050000000 instead of 1050000000 when sending ERC20 token

User A has initial balance - 1000000000 (1000 ERC20 balance with 6 decimal).
User A received 50000000 (50 ERC20 balance) from User B.
So the final balance of User A should be 1050000000 (1050 ERC20 balance).

But IERC20Upgradeable(ERC20_CONTRACT_ADDRESS).balanceOf(userA_Adress) returns
100000000050000000 instead of 1050000000
How could I solve it?

Looks like you are adding 2 strings together and not numbers.

1 Like

I give contract token approval and contract use transfer of openzeppling ERC20 library.
With 0 decimal,it worked but 6 or 12 decimal it is not working as expected.
IERC20Upgradeable(ERC20_CONTRACT_ADDRESS).transfer(userA_Adress, 50000000);

The issue is converting to WEI right before calling and pass as a parameter mint function.

await erc20.mint(deployer.address, 2000000000);

function mint(address to, uint256 amount) external {
      _mint(to, amount);
  }

is corrected to following code:

await erc20.mint(deployer.address, 2000);

function mint(address to, uint256 amount) external {
      _mint(to, amount * uint256(10 ** decimals()) );
  }

1 token with 18 decimals = 1000000000000000000 WEI
1 token with 6 decimals = 1000000 WEI

I suggest you pass the amount in WEI instead or doing math with decimals.

1 Like