UnitConvert.js 671 B

1234567891011121314151617181920212223242526272829
  1. "use strict";
  2. var _ethers = require("ethers");
  3. class UnitConverter {
  4. static ETH(value) {
  5. return _ethers.ethers.utils.parseEther(`${value}`).toString();
  6. }
  7. static Token(value, decimals = 18) {
  8. return _ethers.ethers.utils.parseUnits(`${value}`, +decimals).toString();
  9. }
  10. static FromWei(value, decimals = 18) {
  11. const result = _ethers.ethers.utils.formatUnits(value, decimals); // formatUnits will always add a trailing 0, remove this as we want to return "1" instead of "1.0"
  12. const splitResult = result.split('.');
  13. if (splitResult[1] === '0') {
  14. return splitResult[0];
  15. }
  16. return result;
  17. }
  18. }
  19. module.exports = UnitConverter;