units.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseEther = exports.formatEther = exports.parseUnits = exports.formatUnits = void 0;
  4. /**
  5. * Most interactions with Ethereum requires integer values, which use
  6. * the smallest magnitude unit.
  7. *
  8. * For example, imagine dealing with dollars and cents. Since dollars
  9. * are divisible, non-integer values are possible, such as ``$10.77``.
  10. * By using the smallest indivisible unit (i.e. cents), the value can
  11. * be kept as the integer ``1077``.
  12. *
  13. * When receiving decimal input from the user (as a decimal string),
  14. * the value should be converted to an integer and when showing a user
  15. * a value, the integer value should be converted to a decimal string.
  16. *
  17. * This creates a clear distinction, between values to be used by code
  18. * (integers) and values used for display logic to users (decimals).
  19. *
  20. * The native unit in Ethereum, //ether// is divisible to 18 decimal places,
  21. * where each individual unit is called a //wei//.
  22. *
  23. * @_subsection api/utils:Unit Conversion [about-units]
  24. */
  25. const errors_js_1 = require("./errors.js");
  26. const fixednumber_js_1 = require("./fixednumber.js");
  27. const maths_js_1 = require("./maths.js");
  28. const names = [
  29. "wei",
  30. "kwei",
  31. "mwei",
  32. "gwei",
  33. "szabo",
  34. "finney",
  35. "ether",
  36. ];
  37. /**
  38. * Converts %%value%% into a //decimal string//, assuming %%unit%% decimal
  39. * places. The %%unit%% may be the number of decimal places or the name of
  40. * a unit (e.g. ``"gwei"`` for 9 decimal places).
  41. *
  42. */
  43. function formatUnits(value, unit) {
  44. let decimals = 18;
  45. if (typeof (unit) === "string") {
  46. const index = names.indexOf(unit);
  47. (0, errors_js_1.assertArgument)(index >= 0, "invalid unit", "unit", unit);
  48. decimals = 3 * index;
  49. }
  50. else if (unit != null) {
  51. decimals = (0, maths_js_1.getNumber)(unit, "unit");
  52. }
  53. return fixednumber_js_1.FixedNumber.fromValue(value, decimals, { decimals, width: 512 }).toString();
  54. }
  55. exports.formatUnits = formatUnits;
  56. /**
  57. * Converts the //decimal string// %%value%% to a BigInt, assuming
  58. * %%unit%% decimal places. The %%unit%% may the number of decimal places
  59. * or the name of a unit (e.g. ``"gwei"`` for 9 decimal places).
  60. */
  61. function parseUnits(value, unit) {
  62. (0, errors_js_1.assertArgument)(typeof (value) === "string", "value must be a string", "value", value);
  63. let decimals = 18;
  64. if (typeof (unit) === "string") {
  65. const index = names.indexOf(unit);
  66. (0, errors_js_1.assertArgument)(index >= 0, "invalid unit", "unit", unit);
  67. decimals = 3 * index;
  68. }
  69. else if (unit != null) {
  70. decimals = (0, maths_js_1.getNumber)(unit, "unit");
  71. }
  72. return fixednumber_js_1.FixedNumber.fromString(value, { decimals, width: 512 }).value;
  73. }
  74. exports.parseUnits = parseUnits;
  75. /**
  76. * Converts %%value%% into a //decimal string// using 18 decimal places.
  77. */
  78. function formatEther(wei) {
  79. return formatUnits(wei, 18);
  80. }
  81. exports.formatEther = formatEther;
  82. /**
  83. * Converts the //decimal string// %%ether%% to a BigInt, using 18
  84. * decimal places.
  85. */
  86. function parseEther(ether) {
  87. return parseUnits(ether, 18);
  88. }
  89. exports.parseEther = parseEther;
  90. //# sourceMappingURL=units.js.map