units.js 2.8 KB

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