0

I'm making a token with ERC777 standard.

I've got my decimals at 18, and I want whole tokens, so I set granularity to 10^18.

when I try to mint 20 tokens, I get granularity error. So I made a function to see what the calculations are

function cal_Multiple(uint256 _amount) public pure returns(uint, uint) {
  uint cal =  (_amount.div(10^18).mul(10^18));
  return (_amount, cal);
}

I'm running tests via Truffle. I want to make 20 tokens, with 18 decimals, and granularity of 10^18

let cal_mul = await Campaign_Deployer_inst.cal_Multiple(new web3.utils.BN('20000000000000000000'))

The result is

20000000000000000000 //value I sent in
19999999999999999992 //calculated value

This shouldn't be a JavaScript rounding error, the math is being some with the Solidity code.

Thanks for any help!!

UPDATE: I changed it to 10**18, and the test returns the correct value thank you!

1

The calculation looks correct to me. 20000000000000000000 can't be evenly divided by 18 (the result is 1111111111111111111.11111...), so doing an integer division results in truncation.

Did you maybe mean to divide and multiply by 10**18?

EDIT

Your updated calculation divides and multiplies by 10^18 (10 xor 18), which is 24. A similar problem ensues... 20000000000000000000 can't be evenly divided by 24. I assume you really do want 10**18.

  • oops sorry its supposed to be 10^18, updated, still getting rounding error – Barrard Jan 20 '19 at 23:55
  • See my edit. 10^18=24 – user19510 Jan 20 '19 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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