UNPKG

@parity/api

Version:

The Parity Promise-based API library for interfacing with Ethereum over RPC

57 lines (56 loc) 1.58 kB
"use strict"; // Copyright 2015-2019 Parity Technologies (UK) Ltd. // This file is part of Parity. // // SPDX-License-Identifier: MIT Object.defineProperty(exports, "__esModule", { value: true }); var bignumber_js_1 = require("bignumber.js"); var UNITS = [ 'wei', 'ada', 'babbage', 'shannon', 'szabo', 'finney', 'ether', 'kether', 'mether', 'gether', 'tether' ]; /** * Returns the multiplication factor from wei to another ether denomination. * * @param unit - An ether denomiation. * @example * _getUnitMultiplier('wei'); // 1 * _getUnitMultiplier('ether'); // 10^^18 * @ignore */ exports._getUnitMultiplier = function (unit) { var position = UNITS.indexOf(unit.toLowerCase()); if (position === -1) { throw new Error("Unknown unit " + unit + " passed to wei formatter"); } return Math.pow(10, position * 3); }; /** * Convert from wei to another ether denomination. * * @param value - The value in wei. * @param unit - The ether denomination to convert to. */ exports.fromWei = function (value, unit) { if (unit === void 0) { unit = 'ether'; } return new bignumber_js_1.default(value).dividedBy(exports._getUnitMultiplier(unit)); }; /** * Convert a value from an ether denomination to wei. * * @param value - The value in the ether denomination. * @param unit - The ether denomination to convert to. */ exports.toWei = function (value, unit) { if (unit === void 0) { unit = 'ether'; } return new bignumber_js_1.default(value).multipliedBy(exports._getUnitMultiplier(unit)); };