Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

发行token时精度问题 #46

Closed
jiangying000 opened this issue Jul 28, 2022 · 2 comments · Fixed by #65
Closed

发行token时精度问题 #46

jiangying000 opened this issue Jul 28, 2022 · 2 comments · Fixed by #65
Labels
bug Something isn't working

Comments

@jiangying000
Copy link
Member

在页面:https://starcoinorg.github.io/dapps/#/stc/issue/token
发行:1152921504606846976 个Token
发行成功后实际上钱包里是:1,152,921,504,606,847,000 个

猜测和js整数数据精度有关

number超过2 ** 53 - 1就可能损失数据

@nangongamo
Copy link
Contributor

定位到问题,都是因为 js 大数的问题(超过2^53 -1)丢失精度。
涉及的地方有两点:

  1. movetool 项目 前端未处理 js 大数的问题。
  2. starmask-extension 钱包 显示数量的时候,也同样是未处理 js 大数的问题。

如解决了第1点,这个 issue 就可以关闭了。因为链上数量是正确的,剩下就是钱包显示问题,另外的 issue。

@nangongamo nangongamo mentioned this issue Sep 2, 2022
@jiangying000
Copy link
Member Author

定位到问题,都是因为 js 大数的问题(超过2^53 -1)丢失精度。
涉及的地方有两点:

  1. movetool 项目 前端未处理 js 大数的问题。
  2. starmask-extension 钱包 显示数量的时候,也同样是未处理 js 大数的问题。

如解决了第1点,这个 issue 就可以关闭了。因为链上数量是正确的,剩下就是钱包显示问题,另外的 issue。

是的,现在前端和js的除法、大数相关的地方有类似问题

jiangying000 pushed a commit that referenced this issue Sep 8, 2022
修复 issue #61

现状:

This Banker Amount: 37.3 , max input amount 3.7299999999999995
修复后:

This Banker Amount: 37.3 , max input amount 3.73
建议:
在 web3 应用中,涉及的金融数字居多,对于浮点数运算的精度以及大数溢出的问题,建议采用 bignumber.js

例如,类似的 issue (issue #46 :发行 token 时精度问题)

在页面:https://starcoinorg.github.io/dapps/#/stc/issue/token
发行:1152921504606846976 个Token
发行成功后实际上钱包里是:1,152,921,504,606,847,000 个

例如:

console.log(1152921504606846976); // 结果:1152921504606847000 【精度丢失】
console.log(new BigNumber(1152921504606846976).toString()); // 结果:1152921504606847000 【精度丢失】
console.log(new BigNumber('1152921504606846976').toString()); // 结果:1152921504606846976  【正确】
js 中的数超过 2^53 - 1 的会丢失精度,可以使用 bignumber.js 将大数以字符串的形式传入,如上述例子的第三行所示。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants