developer-content icon indicating copy to clipboard operation
developer-content copied to clipboard

CPIs Lab code: Confusing amount of tokens minted

Open toblich opened this issue 1 year ago • 0 comments

The following lines (program and test) actually yields 98 instead of the expected 10 million.

https://github.com/solana-foundation/developer-content/blob/3a4505ef09649ed12092ac56930613211c9cb524/content/courses/onchain-development/anchor-cpi.md?plain=1#L557

https://github.com/solana-foundation/developer-content/blob/3a4505ef09649ed12092ac56930613211c9cb524/content/courses/onchain-development/anchor-cpi.md?plain=1#L676

This is actually doing a bit-XOR between 10 * 10 = 100 and 6. In the provided test code, you can just print it and see. It actually does

100 ---> 0b1100100
6   ---> 0b0000110
----------------------
98  <--- 0b1100010  (result of XOR'ing the bits)

The math should be fixed (i.e. use the proper operators/methods) to actually yield 10 million, that is 10 tokens using 6 decimal places. As this stackoverflow answer points out, the ^ in Rust is doing a bitwise XOR and not exponentiation.

toblich avatar Aug 08 '24 19:08 toblich