Socket
Socket
Sign inDemoInstall

@glif/filecoin-number

Package Overview
Dependencies
Maintainers
3
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@glif/filecoin-number - npm Package Compare versions

Comparing version 3.0.0 to 3.0.2

8

./dist/index.js

@@ -16,7 +16,4 @@ "use strict";

exports["default"] = void 0;
var _bignumber = require("bignumber.js");
var _FilecoinNumber = require("./FilecoinNumber");
Object.keys(_FilecoinNumber).forEach(function (key) {

@@ -33,5 +30,3 @@ if (key === "default" || key === "__esModule") return;

});
var _Converter = require("./Converter");
Object.keys(_Converter).forEach(function (key) {

@@ -48,3 +43,3 @@ if (key === "default" || key === "__esModule") return;

});
var _default = {
var _default = exports["default"] = {
BigNumber: _bignumber.BigNumber,

@@ -54,3 +49,2 @@ FilecoinNumber: _FilecoinNumber.FilecoinNumber,

};
exports["default"] = _default;
//# sourceMappingURL=index.js.map

2

dist/Converter.d.ts
import { BigNumber } from 'bignumber.js';
import { FilecoinNumber } from './FilecoinNumber';
export declare type ConverterOptions = {
export type ConverterOptions = {
apiKey?: string;

@@ -5,0 +5,0 @@ apiURL?: string;

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,20 +8,11 @@ value: true

exports.Converter = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bent = _interopRequireDefault(require("bent"));
var _bignumber = require("bignumber.js");
var _FilecoinNumber = require("./FilecoinNumber");
var Converter = /*#__PURE__*/function () {
var Converter = exports.Converter = /*#__PURE__*/function () {
function Converter(currency, options) {

@@ -40,3 +30,2 @@ (0, _classCallCheck2["default"])(this, Converter);

}
(0, _createClass2["default"])(Converter, [{

@@ -47,25 +36,19 @@ key: "cacheConversionRate",

return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.t0 = _bignumber.BigNumber;
_context.next = 3;
return this.convert(1, 'FIL', this.currency);
case 3:
_context.t1 = _context.sent;
this.rate = new _context.t0(_context.t1);
case 5:
case "end":
return _context.stop();
}
while (1) switch (_context.prev = _context.next) {
case 0:
_context.t0 = _bignumber.BigNumber;
_context.next = 3;
return this.convert(1, 'FIL', this.currency);
case 3:
_context.t1 = _context.sent;
this.rate = new _context.t0(_context.t1);
case 5:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function cacheConversionRate() {
return _cacheConversionRate.apply(this, arguments);
}
return cacheConversionRate;

@@ -77,7 +60,5 @@ }()

if (!amount) return this.toFIL('0');
if (!this.rate) {
throw new Error('Call cacheConversionRate() to get the conversion rate before calling .toFIL.');
}
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof _bignumber.BigNumber) {

@@ -87,3 +68,2 @@ var filAmount = new _bignumber.BigNumber(amount).dividedBy(this.rate);

}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');

@@ -96,7 +76,5 @@ }

if (!this.rate) throw new Error('Call cacheConversionRate() to get the conversion rate before calling .fromFIL.');
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof _bignumber.BigNumber) {
return new _bignumber.BigNumber(amount).multipliedBy(this.rate);
}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber.');

@@ -110,36 +88,27 @@ }

return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
get = (0, _bent["default"])('GET', 'json', {
'X-CMC_PRO_API_KEY': this.apiKey
});
_context2.next = 3;
return get("".concat(this.apiURL, "/v1/tools/price-conversion?symbol=").concat(from, "&amount=").concat(amount, "&convert=").concat(to));
case 3:
res = _context2.sent;
if (!(!res.data || !res.data.quote || !res.data.quote[to])) {
_context2.next = 6;
break;
}
throw new Error('No conversion price found.');
case 6:
return _context2.abrupt("return", res.data.quote[to].price);
case 7:
case "end":
return _context2.stop();
}
while (1) switch (_context2.prev = _context2.next) {
case 0:
get = (0, _bent["default"])('GET', 'json', {
'X-CMC_PRO_API_KEY': this.apiKey
});
_context2.next = 3;
return get("".concat(this.apiURL, "/v1/tools/price-conversion?symbol=").concat(from, "&amount=").concat(amount, "&convert=").concat(to));
case 3:
res = _context2.sent;
if (!(!res.data || !res.data.quote || !res.data.quote[to])) {
_context2.next = 6;
break;
}
throw new Error('No conversion price found.');
case 6:
return _context2.abrupt("return", res.data.quote[to].price);
case 7:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function convert(_x, _x2, _x3) {
return _convert.apply(this, arguments);
}
return convert;

@@ -155,4 +124,2 @@ }()

}();
exports.Converter = Converter;
//# sourceMappingURL=Converter.js.map
/// <reference types="node" />
import { BigNumber } from 'bignumber.js';
export declare type FilecoinDenomination = 'fil' | 'picofil' | 'attofil';
export type FilecoinDenomination = 'fil' | 'picofil' | 'attofil';
export declare enum CoinType {

@@ -5,0 +5,0 @@ MAIN = "f",

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,33 +8,17 @@ value: true

exports.FilecoinNumber = exports.CoinType = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty3 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bignumber = require("bignumber.js");
var _bn = _interopRequireDefault(require("bn.js"));
var _defineProperty2;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty3["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var _FilecoinNumber;
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty3["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
// not sure how we want to configure rounding for this

@@ -45,8 +28,10 @@ _bignumber.BigNumber.set({

});
_bignumber.BigNumber.config({
EXPONENTIAL_AT: 1e9
});
var CoinType;
var CoinType = exports.CoinType = /*#__PURE__*/function (CoinType) {
CoinType["MAIN"] = "f";
CoinType["TEST"] = "t";
return CoinType;
}({});
/**

@@ -59,10 +44,2 @@ * @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null`

*/
exports.CoinType = CoinType;
(function (CoinType) {
CoinType["MAIN"] = "f";
CoinType["TEST"] = "t";
})(CoinType || (exports.CoinType = CoinType = {}));
function toBigNumberValue(value, denom) {

@@ -72,9 +49,6 @@ switch (denom) {

return value;
case 'picofil':
return new _bignumber.BigNumber(value).shiftedBy(-12);
case 'attofil':
return new _bignumber.BigNumber(value).shiftedBy(-18);
default:

@@ -84,2 +58,3 @@ throw new Error("Unsupported denomination passed to FilecoinNumber constructor: ".concat(denom, ". Must be fil, picofil or attofil."));

}
/**

@@ -92,18 +67,11 @@ * FilecoinNumber is a BigNumber specialization that

*/
var FilecoinNumber = /*#__PURE__*/function (_BigNumber) {
var FilecoinNumber = exports.FilecoinNumber = /*#__PURE__*/function (_BigNumber) {
(0, _inherits2["default"])(FilecoinNumber, _BigNumber);
var _super = _createSuper(FilecoinNumber);
// Static FilecoinNumber(0, 'fil') instances for each CoinType and unit
// Constructor params
function FilecoinNumber(value, denom) {
var _this;
var coinType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : CoinType.MAIN;
var unit = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : FilecoinNumber.DefaultUnit;
(0, _classCallCheck2["default"])(this, FilecoinNumber);
_this = _super.call(this, toBigNumberValue(value, denom));
_this = _callSuper(this, FilecoinNumber, [toBigNumberValue(value, denom)]);
// Constructor params
(0, _defineProperty3["default"])((0, _assertThisInitialized2["default"])(_this), "coinType", void 0);

@@ -115,2 +83,3 @@ (0, _defineProperty3["default"])((0, _assertThisInitialized2["default"])(_this), "unit", void 0);

}
/**

@@ -120,10 +89,27 @@ * A static method that checks whether the

*/
(0, _createClass2["default"])(FilecoinNumber, [{
key: "getDenomUnit",
key: "displayUnit",
get:
/**
* Returns the unit with a 't' prefix for testnets
*/
function get() {
var addT = this.coinType === CoinType.TEST && this.unit === FilecoinNumber.DefaultUnit;
return "".concat(addT ? 't' : '').concat(this.unit);
}
/**
* Checks whether the unit is the default unit
*/
}, {
key: "isDefaultUnit",
get: function get() {
return this.unit === FilecoinNumber.DefaultUnit;
}
/**
* Returns the denominator specific unit (omits 't' if adding other prefix)
*/
}, {
key: "getDenomUnit",
value: function getDenomUnit(denom) {

@@ -133,6 +119,6 @@ var unitDenom = denom.replace(/fil$/, '');

}
/**
* Expresses this FilecoinNumber as a FIL string
*/
}, {

@@ -143,6 +129,6 @@ key: "toFil",

}
/**
* Expresses this FilecoinNumber as a PicoFIL string
*/
}, {

@@ -153,6 +139,6 @@ key: "toPicoFil",

}
/**
* Expresses this FilecoinNumber as an AttoFIL string
*/
}, {

@@ -163,6 +149,6 @@ key: "toAttoFil",

}
/**
* Expresses this FilecoinNumber as a CBOR Buffer
*/
}, {

@@ -176,6 +162,6 @@ key: "toCBOR",

}
/**
* Expresses this FilecoinNumber as a balance string
*/
}, {

@@ -185,3 +171,2 @@ key: "formatBalance",

var _options$truncate, _options$decimals, _options$padZeros, _options$addUnit;
var truncate = (_options$truncate = options === null || options === void 0 ? void 0 : options.truncate) !== null && _options$truncate !== void 0 ? _options$truncate : true;

@@ -193,8 +178,7 @@ var round = (options === null || options === void 0 ? void 0 : options.decimals) !== null;

var prefix = options === null || options === void 0 ? void 0 : options.prefix;
var toFormat = function toFormat(value, format) {
return padZeros ? value.toFormat(decimals, _bignumber.BigNumber.ROUND_DOWN, format) : value.toFormat(format);
}; // Create format configuration
};
// Create format configuration
var format = {

@@ -206,12 +190,14 @@ decimalSeparator: '.',

prefix: prefix
}; // When not rounding, it doesn't make sense to truncate either.
};
// When not rounding, it doesn't make sense to truncate either.
// Return the original value when it's zero or when not rounding.
if (this.isZero() || !round) return toFormat(this, format);
if (this.isZero() || !round) return toFormat(this, format); // Round down by default to avoid showing higher balance
// Round down by default to avoid showing higher balance
var rounded = this.dp(decimals, _bignumber.BigNumber.ROUND_DOWN);
var rounded = this.dp(decimals, _bignumber.BigNumber.ROUND_DOWN); // Value is zero after rounding
// Value is zero after rounding
if (rounded.isZero()) {
var isNegative = this.isNegative();
if (decimals === 0) {

@@ -221,3 +207,2 @@ // We rounded to 0 decimals, so we show

var _prefix = isNegative ? '< ' : '> ';
return toFormat(rounded, _objectSpread(_objectSpread({}, format), {}, {

@@ -230,3 +215,2 @@ prefix: _prefix

var _prefix2 = isNegative ? '> ' : '< ';
var roundedUp = this.dp(decimals, _bignumber.BigNumber.ROUND_UP);

@@ -237,11 +221,11 @@ return toFormat(roundedUp, _objectSpread(_objectSpread({}, format), {}, {

}
} // Return rounded value between -1000 and 1000 or when not truncating
}
// Return rounded value between -1000 and 1000 or when not truncating
var isLt1K = rounded.isGreaterThan(-1000) && rounded.isLessThan(1000);
if (isLt1K || !truncate) return toFormat(rounded, format); // Truncate values below -1000 or above 1000
if (isLt1K || !truncate) return toFormat(rounded, format);
// Truncate values below -1000 or above 1000
var power = 0;
var units = ['K', 'M', 'B', 'T'];
for (var _i = 0, _units = units; _i < _units.length; _i++) {

@@ -251,5 +235,3 @@ var unit = _units[_i];

var unitVal = unitRaw.dp(1, _bignumber.BigNumber.ROUND_DOWN);
var _isLt1K = unitVal.isGreaterThan(-1000) && unitVal.isLessThan(1000);
if (_isLt1K || unit === 'T') {

@@ -261,11 +243,11 @@ var suffix = "".concat(unit).concat(format.suffix);

}
} // Should never hit here
}
// Should never hit here
throw new Error('Failed to format balance');
}
/**
* Returns a copy of this FilecoinNumber
*/
}, {

@@ -276,6 +258,6 @@ key: "clone",

}
/**
* Returns an copy of this FilecoinNumber that is rounded up
*/
}, {

@@ -288,6 +270,6 @@ key: "roundUp",

}
/**
* Returns an copy of this FilecoinNumber that is rounded down
*/
}, {

@@ -300,6 +282,6 @@ key: "roundDown",

}
/**
* Returns an absolute value copy of this FilecoinNumber
*/
}, {

@@ -310,6 +292,6 @@ key: "abs",

}
/**
* Returns an absolute value copy of this FilecoinNumber
*/
}, {

@@ -320,6 +302,6 @@ key: "absoluteValue",

}
/**
* Returns a negated copy of this FilecoinNumber (multiplied by -1)
*/
}, {

@@ -330,6 +312,6 @@ key: "negated",

}
/**
* Returns a copy of this FilecoinNumber divided by the supplied value n
*/
}, {

@@ -340,6 +322,6 @@ key: "div",

}
/**
* Returns a copy of this FilecoinNumber divided by the supplied value n
*/
}, {

@@ -350,6 +332,6 @@ key: "dividedBy",

}
/**
* Returns a copy of this FilecoinNumber multiplied by the supplied value n
*/
}, {

@@ -360,6 +342,6 @@ key: "times",

}
/**
* Returns a copy of this FilecoinNumber multiplied by the supplied value n
*/
}, {

@@ -370,2 +352,3 @@ key: "multipliedBy",

}
/**

@@ -375,3 +358,2 @@ * Returns a copy of this FilecoinNumber increased by the supplied value n

*/
}, {

@@ -382,2 +364,3 @@ key: "plus",

}
/**

@@ -387,3 +370,2 @@ * Returns a copy of this FilecoinNumber decreased by the supplied value n

*/
}, {

@@ -394,2 +376,3 @@ key: "minus",

}
/**

@@ -399,3 +382,2 @@ * Returns `true` if the value of this BigNumber is equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -406,2 +388,3 @@ key: "eq",

}
/**

@@ -411,3 +394,2 @@ * Returns `true` if the value of this BigNumber is greater than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -418,2 +400,3 @@ key: "gt",

}
/**

@@ -423,3 +406,2 @@ * Returns `true` if the value of this BigNumber is greater than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -430,2 +412,3 @@ key: "gte",

}
/**

@@ -435,3 +418,2 @@ * Returns `true` if the value of this BigNumber is less than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -442,2 +424,3 @@ key: "lt",

}
/**

@@ -447,3 +430,2 @@ * Returns `true` if the value of this BigNumber is less than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -454,2 +436,3 @@ key: "lte",

}
/**

@@ -459,3 +442,2 @@ * Returns `true` if the value of this BigNumber is equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -466,2 +448,3 @@ key: "isEqualTo",

}
/**

@@ -471,3 +454,2 @@ * Returns `true` if the value of this BigNumber is greater than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -478,2 +460,3 @@ key: "isGreaterThan",

}
/**

@@ -483,3 +466,2 @@ * Returns `true` if the value of this BigNumber is greater than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -490,2 +472,3 @@ key: "isGreaterThanOrEqualTo",

}
/**

@@ -495,3 +478,2 @@ * Returns `true` if the value of this BigNumber is less than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -502,2 +484,3 @@ key: "isLessThan",

}
/**

@@ -507,3 +490,2 @@ * Returns `true` if the value of this BigNumber is less than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -514,21 +496,2 @@ key: "isLessThanOrEqualTo",

}
}, {
key: "displayUnit",
/**
* Returns the unit with a 't' prefix for testnets
*/
get: function get() {
var addT = this.coinType === CoinType.TEST && this.unit === FilecoinNumber.DefaultUnit;
return "".concat(addT ? 't' : '').concat(this.unit);
}
/**
* Checks whether the unit is the default unit
*/
}, {
key: "isDefaultUnit",
get: function get() {
return this.unit === FilecoinNumber.DefaultUnit;
}
}], [{

@@ -539,6 +502,6 @@ key: "isFilecoinNumber",

}
/**
* Returns a static zero fil instance for the CoinType and unit
*/
}, {

@@ -558,3 +521,2 @@ key: "zero",

}
return new FilecoinNumber((0, _get2["default"])((0, _getPrototypeOf2["default"])(FilecoinNumber), "min", this).apply(null, n), 'fil', n[0].coinType, n[0].unit);

@@ -568,3 +530,2 @@ }

}
return new FilecoinNumber((0, _get2["default"])((0, _getPrototypeOf2["default"])(FilecoinNumber), "max", this).apply(null, n), 'fil', n[0].coinType, n[0].unit);

@@ -578,3 +539,2 @@ }

}
return this.min.apply(null, n);

@@ -588,3 +548,2 @@ }

}
return this.max.apply(null, n);

@@ -595,6 +554,6 @@ }

}(_bignumber.BigNumber);
exports.FilecoinNumber = FilecoinNumber;
_FilecoinNumber = FilecoinNumber;
(0, _defineProperty3["default"])(FilecoinNumber, "DefaultUnit", 'FIL');
(0, _defineProperty3["default"])(FilecoinNumber, "zeroMap", (_defineProperty2 = {}, (0, _defineProperty3["default"])(_defineProperty2, CoinType.MAIN, {}), (0, _defineProperty3["default"])(_defineProperty2, CoinType.TEST, {}), _defineProperty2));
// Static FilecoinNumber(0, 'fil') instances for each CoinType and unit
(0, _defineProperty3["default"])(FilecoinNumber, "zeroMap", (0, _defineProperty3["default"])((0, _defineProperty3["default"])({}, CoinType.MAIN, {}), CoinType.TEST, {}));
//# sourceMappingURL=FilecoinNumber.js.map

@@ -16,7 +16,4 @@ "use strict";

exports["default"] = void 0;
var _bignumber = require("bignumber.js");
var _FilecoinNumber = require("./FilecoinNumber");
Object.keys(_FilecoinNumber).forEach(function (key) {

@@ -33,5 +30,3 @@ if (key === "default" || key === "__esModule") return;

});
var _Converter = require("./Converter");
Object.keys(_Converter).forEach(function (key) {

@@ -48,3 +43,3 @@ if (key === "default" || key === "__esModule") return;

});
var _default = {
var _default = exports["default"] = {
BigNumber: _bignumber.BigNumber,

@@ -54,3 +49,2 @@ FilecoinNumber: _FilecoinNumber.FilecoinNumber,

};
exports["default"] = _default;
//# sourceMappingURL=index.js.map

@@ -12,11 +12,6 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";

_classCallCheck(this, Converter);
_defineProperty(this, "currency", void 0);
_defineProperty(this, "apiKey", void 0);
_defineProperty(this, "apiURL", void 0);
_defineProperty(this, "rate", void 0);
if (!currency) throw new Error('No currency passed.');

@@ -28,3 +23,2 @@ this.currency = currency;

}
_createClass(Converter, [{

@@ -35,25 +29,19 @@ key: "cacheConversionRate",

return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.t0 = BigNumber;
_context.next = 3;
return this.convert(1, 'FIL', this.currency);
case 3:
_context.t1 = _context.sent;
this.rate = new _context.t0(_context.t1);
case 5:
case "end":
return _context.stop();
}
while (1) switch (_context.prev = _context.next) {
case 0:
_context.t0 = BigNumber;
_context.next = 3;
return this.convert(1, 'FIL', this.currency);
case 3:
_context.t1 = _context.sent;
this.rate = new _context.t0(_context.t1);
case 5:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function cacheConversionRate() {
return _cacheConversionRate.apply(this, arguments);
}
return cacheConversionRate;

@@ -65,7 +53,5 @@ }()

if (!amount) return this.toFIL('0');
if (!this.rate) {
throw new Error('Call cacheConversionRate() to get the conversion rate before calling .toFIL.');
}
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {

@@ -75,3 +61,2 @@ var filAmount = new BigNumber(amount).dividedBy(this.rate);

}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');

@@ -84,7 +69,5 @@ }

if (!this.rate) throw new Error('Call cacheConversionRate() to get the conversion rate before calling .fromFIL.');
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {
return new BigNumber(amount).multipliedBy(this.rate);
}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber.');

@@ -98,36 +81,27 @@ }

return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
get = bent('GET', 'json', {
'X-CMC_PRO_API_KEY': this.apiKey
});
_context2.next = 3;
return get("".concat(this.apiURL, "/v1/tools/price-conversion?symbol=").concat(from, "&amount=").concat(amount, "&convert=").concat(to));
case 3:
res = _context2.sent;
if (!(!res.data || !res.data.quote || !res.data.quote[to])) {
_context2.next = 6;
break;
}
throw new Error('No conversion price found.');
case 6:
return _context2.abrupt("return", res.data.quote[to].price);
case 7:
case "end":
return _context2.stop();
}
while (1) switch (_context2.prev = _context2.next) {
case 0:
get = bent('GET', 'json', {
'X-CMC_PRO_API_KEY': this.apiKey
});
_context2.next = 3;
return get("".concat(this.apiURL, "/v1/tools/price-conversion?symbol=").concat(from, "&amount=").concat(amount, "&convert=").concat(to));
case 3:
res = _context2.sent;
if (!(!res.data || !res.data.quote || !res.data.quote[to])) {
_context2.next = 6;
break;
}
throw new Error('No conversion price found.');
case 6:
return _context2.abrupt("return", res.data.quote[to].price);
case 7:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function convert(_x, _x2, _x3) {
return _convert.apply(this, arguments);
}
return convert;

@@ -141,5 +115,4 @@ }()

}]);
return Converter;
}();
//# sourceMappingURL=Converter.js.map
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _get from "@babel/runtime/helpers/get";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _defineProperty2;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var _FilecoinNumber;
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import { BigNumber } from 'bignumber.js';
import BN from 'bn.js'; // not sure how we want to configure rounding for this
import BN from 'bn.js';
// not sure how we want to configure rounding for this
BigNumber.set({

@@ -29,3 +24,8 @@ ROUNDING_MODE: BigNumber.ROUND_HALF_DOWN

});
export var CoinType;
export var CoinType = /*#__PURE__*/function (CoinType) {
CoinType["MAIN"] = "f";
CoinType["TEST"] = "t";
return CoinType;
}({});
/**

@@ -39,7 +39,2 @@ * @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null`

(function (CoinType) {
CoinType["MAIN"] = "f";
CoinType["TEST"] = "t";
})(CoinType || (CoinType = {}));
function toBigNumberValue(value, denom) {

@@ -49,9 +44,6 @@ switch (denom) {

return value;
case 'picofil':
return new BigNumber(value).shiftedBy(-12);
case 'attofil':
return new BigNumber(value).shiftedBy(-18);
default:

@@ -61,2 +53,3 @@ throw new Error("Unsupported denomination passed to FilecoinNumber constructor: ".concat(denom, ". Must be fil, picofil or attofil."));

}
/**

@@ -69,25 +62,13 @@ * FilecoinNumber is a BigNumber specialization that

*/
export var FilecoinNumber = /*#__PURE__*/function (_BigNumber) {
_inherits(FilecoinNumber, _BigNumber);
var _super = _createSuper(FilecoinNumber);
// Static FilecoinNumber(0, 'fil') instances for each CoinType and unit
// Constructor params
function FilecoinNumber(value, denom) {
var _this;
var coinType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : CoinType.MAIN;
var unit = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : FilecoinNumber.DefaultUnit;
_classCallCheck(this, FilecoinNumber);
_this = _super.call(this, toBigNumberValue(value, denom));
_this = _callSuper(this, FilecoinNumber, [toBigNumberValue(value, denom)]);
// Constructor params
_defineProperty(_assertThisInitialized(_this), "coinType", void 0);
_defineProperty(_assertThisInitialized(_this), "unit", void 0);
_this.coinType = coinType;

@@ -97,2 +78,3 @@ _this.unit = unit;

}
/**

@@ -102,10 +84,27 @@ * A static method that checks whether the

*/
_createClass(FilecoinNumber, [{
key: "getDenomUnit",
key: "displayUnit",
get:
/**
* Returns the unit with a 't' prefix for testnets
*/
function get() {
var addT = this.coinType === CoinType.TEST && this.unit === FilecoinNumber.DefaultUnit;
return "".concat(addT ? 't' : '').concat(this.unit);
}
/**
* Checks whether the unit is the default unit
*/
}, {
key: "isDefaultUnit",
get: function get() {
return this.unit === FilecoinNumber.DefaultUnit;
}
/**
* Returns the denominator specific unit (omits 't' if adding other prefix)
*/
}, {
key: "getDenomUnit",
value: function getDenomUnit(denom) {

@@ -115,6 +114,6 @@ var unitDenom = denom.replace(/fil$/, '');

}
/**
* Expresses this FilecoinNumber as a FIL string
*/
}, {

@@ -125,6 +124,6 @@ key: "toFil",

}
/**
* Expresses this FilecoinNumber as a PicoFIL string
*/
}, {

@@ -135,6 +134,6 @@ key: "toPicoFil",

}
/**
* Expresses this FilecoinNumber as an AttoFIL string
*/
}, {

@@ -145,6 +144,6 @@ key: "toAttoFil",

}
/**
* Expresses this FilecoinNumber as a CBOR Buffer
*/
}, {

@@ -158,6 +157,6 @@ key: "toCBOR",

}
/**
* Expresses this FilecoinNumber as a balance string
*/
}, {

@@ -167,3 +166,2 @@ key: "formatBalance",

var _options$truncate, _options$decimals, _options$padZeros, _options$addUnit;
var truncate = (_options$truncate = options === null || options === void 0 ? void 0 : options.truncate) !== null && _options$truncate !== void 0 ? _options$truncate : true;

@@ -175,8 +173,7 @@ var round = (options === null || options === void 0 ? void 0 : options.decimals) !== null;

var prefix = options === null || options === void 0 ? void 0 : options.prefix;
var toFormat = function toFormat(value, format) {
return padZeros ? value.toFormat(decimals, BigNumber.ROUND_DOWN, format) : value.toFormat(format);
}; // Create format configuration
};
// Create format configuration
var format = {

@@ -188,12 +185,14 @@ decimalSeparator: '.',

prefix: prefix
}; // When not rounding, it doesn't make sense to truncate either.
};
// When not rounding, it doesn't make sense to truncate either.
// Return the original value when it's zero or when not rounding.
if (this.isZero() || !round) return toFormat(this, format);
if (this.isZero() || !round) return toFormat(this, format); // Round down by default to avoid showing higher balance
// Round down by default to avoid showing higher balance
var rounded = this.dp(decimals, BigNumber.ROUND_DOWN);
var rounded = this.dp(decimals, BigNumber.ROUND_DOWN); // Value is zero after rounding
// Value is zero after rounding
if (rounded.isZero()) {
var isNegative = this.isNegative();
if (decimals === 0) {

@@ -203,3 +202,2 @@ // We rounded to 0 decimals, so we show

var _prefix = isNegative ? '< ' : '> ';
return toFormat(rounded, _objectSpread(_objectSpread({}, format), {}, {

@@ -212,3 +210,2 @@ prefix: _prefix

var _prefix2 = isNegative ? '> ' : '< ';
var roundedUp = this.dp(decimals, BigNumber.ROUND_UP);

@@ -219,11 +216,11 @@ return toFormat(roundedUp, _objectSpread(_objectSpread({}, format), {}, {

}
} // Return rounded value between -1000 and 1000 or when not truncating
}
// Return rounded value between -1000 and 1000 or when not truncating
var isLt1K = rounded.isGreaterThan(-1000) && rounded.isLessThan(1000);
if (isLt1K || !truncate) return toFormat(rounded, format); // Truncate values below -1000 or above 1000
if (isLt1K || !truncate) return toFormat(rounded, format);
// Truncate values below -1000 or above 1000
var power = 0;
var units = ['K', 'M', 'B', 'T'];
for (var _i = 0, _units = units; _i < _units.length; _i++) {

@@ -233,5 +230,3 @@ var unit = _units[_i];

var unitVal = unitRaw.dp(1, BigNumber.ROUND_DOWN);
var _isLt1K = unitVal.isGreaterThan(-1000) && unitVal.isLessThan(1000);
if (_isLt1K || unit === 'T') {

@@ -243,11 +238,11 @@ var suffix = "".concat(unit).concat(format.suffix);

}
} // Should never hit here
}
// Should never hit here
throw new Error('Failed to format balance');
}
/**
* Returns a copy of this FilecoinNumber
*/
}, {

@@ -258,6 +253,6 @@ key: "clone",

}
/**
* Returns an copy of this FilecoinNumber that is rounded up
*/
}, {

@@ -267,11 +262,9 @@ key: "roundUp",

var decimalPlaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var bigNr = _get(_getPrototypeOf(FilecoinNumber.prototype), "dp", this).call(this, decimalPlaces, BigNumber.ROUND_UP);
return new FilecoinNumber(bigNr, 'fil', this.coinType, this.unit);
}
/**
* Returns an copy of this FilecoinNumber that is rounded down
*/
}, {

@@ -281,11 +274,9 @@ key: "roundDown",

var decimalPlaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var bigNr = _get(_getPrototypeOf(FilecoinNumber.prototype), "dp", this).call(this, decimalPlaces, BigNumber.ROUND_DOWN);
return new FilecoinNumber(bigNr, 'fil', this.coinType, this.unit);
}
/**
* Returns an absolute value copy of this FilecoinNumber
*/
}, {

@@ -296,6 +287,6 @@ key: "abs",

}
/**
* Returns an absolute value copy of this FilecoinNumber
*/
}, {

@@ -306,6 +297,6 @@ key: "absoluteValue",

}
/**
* Returns a negated copy of this FilecoinNumber (multiplied by -1)
*/
}, {

@@ -316,6 +307,6 @@ key: "negated",

}
/**
* Returns a copy of this FilecoinNumber divided by the supplied value n
*/
}, {

@@ -326,6 +317,6 @@ key: "div",

}
/**
* Returns a copy of this FilecoinNumber divided by the supplied value n
*/
}, {

@@ -336,6 +327,6 @@ key: "dividedBy",

}
/**
* Returns a copy of this FilecoinNumber multiplied by the supplied value n
*/
}, {

@@ -346,6 +337,6 @@ key: "times",

}
/**
* Returns a copy of this FilecoinNumber multiplied by the supplied value n
*/
}, {

@@ -356,2 +347,3 @@ key: "multipliedBy",

}
/**

@@ -361,3 +353,2 @@ * Returns a copy of this FilecoinNumber increased by the supplied value n

*/
}, {

@@ -368,2 +359,3 @@ key: "plus",

}
/**

@@ -373,3 +365,2 @@ * Returns a copy of this FilecoinNumber decreased by the supplied value n

*/
}, {

@@ -380,2 +371,3 @@ key: "minus",

}
/**

@@ -385,3 +377,2 @@ * Returns `true` if the value of this BigNumber is equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -392,2 +383,3 @@ key: "eq",

}
/**

@@ -397,3 +389,2 @@ * Returns `true` if the value of this BigNumber is greater than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -404,2 +395,3 @@ key: "gt",

}
/**

@@ -409,3 +401,2 @@ * Returns `true` if the value of this BigNumber is greater than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -416,2 +407,3 @@ key: "gte",

}
/**

@@ -421,3 +413,2 @@ * Returns `true` if the value of this BigNumber is less than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -428,2 +419,3 @@ key: "lt",

}
/**

@@ -433,3 +425,2 @@ * Returns `true` if the value of this BigNumber is less than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -440,2 +431,3 @@ key: "lte",

}
/**

@@ -445,3 +437,2 @@ * Returns `true` if the value of this BigNumber is equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -452,2 +443,3 @@ key: "isEqualTo",

}
/**

@@ -457,3 +449,2 @@ * Returns `true` if the value of this BigNumber is greater than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -464,2 +455,3 @@ key: "isGreaterThan",

}
/**

@@ -469,3 +461,2 @@ * Returns `true` if the value of this BigNumber is greater than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -476,2 +467,3 @@ key: "isGreaterThanOrEqualTo",

}
/**

@@ -481,3 +473,2 @@ * Returns `true` if the value of this BigNumber is less than the value of `n`, otherwise returns `false`.

*/
}, {

@@ -488,2 +479,3 @@ key: "isLessThan",

}
/**

@@ -493,3 +485,2 @@ * Returns `true` if the value of this BigNumber is less than or equal to the value of `n`, otherwise returns `false`.

*/
}, {

@@ -500,21 +491,2 @@ key: "isLessThanOrEqualTo",

}
}, {
key: "displayUnit",
/**
* Returns the unit with a 't' prefix for testnets
*/
get: function get() {
var addT = this.coinType === CoinType.TEST && this.unit === FilecoinNumber.DefaultUnit;
return "".concat(addT ? 't' : '').concat(this.unit);
}
/**
* Checks whether the unit is the default unit
*/
}, {
key: "isDefaultUnit",
get: function get() {
return this.unit === FilecoinNumber.DefaultUnit;
}
}], [{

@@ -525,6 +497,6 @@ key: "isFilecoinNumber",

}
/**
* Returns a static zero fil instance for the CoinType and unit
*/
}, {

@@ -544,3 +516,2 @@ key: "zero",

}
return new FilecoinNumber(_get(_getPrototypeOf(FilecoinNumber), "min", this).apply(null, n), 'fil', n[0].coinType, n[0].unit);

@@ -554,3 +525,2 @@ }

}
return new FilecoinNumber(_get(_getPrototypeOf(FilecoinNumber), "max", this).apply(null, n), 'fil', n[0].coinType, n[0].unit);

@@ -564,3 +534,2 @@ }

}
return this.min.apply(null, n);

@@ -574,13 +543,11 @@ }

}
return this.max.apply(null, n);
}
}]);
return FilecoinNumber;
}(BigNumber);
_FilecoinNumber = FilecoinNumber;
_defineProperty(FilecoinNumber, "DefaultUnit", 'FIL');
_defineProperty(FilecoinNumber, "zeroMap", (_defineProperty2 = {}, _defineProperty(_defineProperty2, CoinType.MAIN, {}), _defineProperty(_defineProperty2, CoinType.TEST, {}), _defineProperty2));
// Static FilecoinNumber(0, 'fil') instances for each CoinType and unit
_defineProperty(FilecoinNumber, "zeroMap", _defineProperty(_defineProperty({}, CoinType.MAIN, {}), CoinType.TEST, {}));
//# sourceMappingURL=FilecoinNumber.js.map

@@ -8,9 +8,5 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty";

_defineProperty(this, "currency", void 0);
_defineProperty(this, "apiKey", void 0);
_defineProperty(this, "apiURL", void 0);
_defineProperty(this, "rate", void 0);
if (!currency) throw new Error('No currency passed.');

@@ -22,14 +18,10 @@ this.currency = currency;

}
async cacheConversionRate() {
this.rate = new BigNumber(await this.convert(1, 'FIL', this.currency));
}
toFIL(amount) {
if (!amount) return this.toFIL('0');
if (!this.rate) {
throw new Error('Call cacheConversionRate() to get the conversion rate before calling .toFIL.');
}
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {

@@ -39,17 +31,12 @@ const filAmount = new BigNumber(amount).dividedBy(this.rate);

}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');
}
fromFIL(amount) {
if (!amount) return this.fromFIL('0');
if (!this.rate) throw new Error('Call cacheConversionRate() to get the conversion rate before calling .fromFIL.');
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {
return new BigNumber(amount).multipliedBy(this.rate);
}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber.');
}
async convert(amount, from, to) {

@@ -63,8 +50,6 @@ const get = bent('GET', 'json', {

}
getCachedConversionRate() {
return this.rate;
}
}
//# sourceMappingURL=Converter.js.map
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _FilecoinNumber;
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { BigNumber } from 'bignumber.js';
import BN from 'bn.js'; // not sure how we want to configure rounding for this
import BN from 'bn.js';
// not sure how we want to configure rounding for this
BigNumber.set({

@@ -16,3 +15,8 @@ ROUNDING_MODE: BigNumber.ROUND_HALF_DOWN

});
export let CoinType;
export let CoinType = /*#__PURE__*/function (CoinType) {
CoinType["MAIN"] = "f";
CoinType["TEST"] = "t";
return CoinType;
}({});
/**

@@ -26,7 +30,2 @@ * @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null`

(function (CoinType) {
CoinType["MAIN"] = "f";
CoinType["TEST"] = "t";
})(CoinType || (CoinType = {}));
function toBigNumberValue(value, denom) {

@@ -36,9 +35,6 @@ switch (denom) {

return value;
case 'picofil':
return new BigNumber(value).shiftedBy(-12);
case 'attofil':
return new BigNumber(value).shiftedBy(-18);
default:

@@ -48,2 +44,3 @@ throw new Error(`Unsupported denomination passed to FilecoinNumber constructor: ${denom}. Must be fil, picofil or attofil.`);

}
/**

@@ -56,17 +53,12 @@ * FilecoinNumber is a BigNumber specialization that

*/
export class FilecoinNumber extends BigNumber {
// Static FilecoinNumber(0, 'fil') instances for each CoinType and unit
// Constructor params
constructor(value, denom, coinType = CoinType.MAIN, unit = FilecoinNumber.DefaultUnit) {
super(toBigNumberValue(value, denom));
// Constructor params
_defineProperty(this, "coinType", void 0);
_defineProperty(this, "unit", void 0);
this.coinType = coinType;
this.unit = unit;
}
/**

@@ -76,12 +68,9 @@ * A static method that checks whether the

*/
static isFilecoinNumber(value) {
return BigNumber.isBigNumber(value) && 'toFil' in value && 'toAttoFil' in value && 'toPicoFil' in value;
}
/**
* Returns a static zero fil instance for the CoinType and unit
*/
static zero(coinType = CoinType.MAIN, unit = FilecoinNumber.DefaultUnit) {

@@ -91,23 +80,18 @@ if (this.zeroMap[coinType][unit] === undefined) this.zeroMap[coinType][unit] = new FilecoinNumber(0, 'fil', coinType, unit);

}
static min(...n) {
return new FilecoinNumber(super.min.apply(null, n), 'fil', n[0].coinType, n[0].unit);
}
static max(...n) {
return new FilecoinNumber(super.max.apply(null, n), 'fil', n[0].coinType, n[0].unit);
}
static minimum(...n) {
return this.min.apply(null, n);
}
static maximum(...n) {
return this.max.apply(null, n);
}
/**
* Returns the unit with a 't' prefix for testnets
*/
get displayUnit() {

@@ -117,15 +101,13 @@ const addT = this.coinType === CoinType.TEST && this.unit === FilecoinNumber.DefaultUnit;

}
/**
* Checks whether the unit is the default unit
*/
get isDefaultUnit() {
return this.unit === FilecoinNumber.DefaultUnit;
}
/**
* Returns the denominator specific unit (omits 't' if adding other prefix)
*/
getDenomUnit(denom) {

@@ -135,31 +117,27 @@ const unitDenom = denom.replace(/fil$/, '');

}
/**
* Expresses this FilecoinNumber as a FIL string
*/
toFil() {
return this.toString();
}
/**
* Expresses this FilecoinNumber as a PicoFIL string
*/
toPicoFil() {
return this.shiftedBy(12).toString();
}
/**
* Expresses this FilecoinNumber as an AttoFIL string
*/
toAttoFil() {
return this.shiftedBy(18).toFixed(0, BigNumber.ROUND_DOWN);
}
/**
* Expresses this FilecoinNumber as a CBOR Buffer
*/
toCBOR() {

@@ -171,7 +149,6 @@ if (this.isNaN() || this.isZero()) return Buffer.from([]);

}
/**
* Expresses this FilecoinNumber as a balance string
*/
formatBalance(options) {

@@ -184,6 +161,5 @@ const truncate = (options === null || options === void 0 ? void 0 : options.truncate) ?? true;

const prefix = options === null || options === void 0 ? void 0 : options.prefix;
const toFormat = (value, format) => padZeros ? value.toFormat(decimals, BigNumber.ROUND_DOWN, format) : value.toFormat(format);
const toFormat = (value, format) => padZeros ? value.toFormat(decimals, BigNumber.ROUND_DOWN, format) : value.toFormat(format); // Create format configuration
// Create format configuration
const format = {

@@ -195,12 +171,14 @@ decimalSeparator: '.',

prefix
}; // When not rounding, it doesn't make sense to truncate either.
};
// When not rounding, it doesn't make sense to truncate either.
// Return the original value when it's zero or when not rounding.
if (this.isZero() || !round) return toFormat(this, format);
if (this.isZero() || !round) return toFormat(this, format); // Round down by default to avoid showing higher balance
// Round down by default to avoid showing higher balance
const rounded = this.dp(decimals, BigNumber.ROUND_DOWN);
const rounded = this.dp(decimals, BigNumber.ROUND_DOWN); // Value is zero after rounding
// Value is zero after rounding
if (rounded.isZero()) {
const isNegative = this.isNegative();
if (decimals === 0) {

@@ -222,11 +200,11 @@ // We rounded to 0 decimals, so we show

}
} // Return rounded value between -1000 and 1000 or when not truncating
}
// Return rounded value between -1000 and 1000 or when not truncating
const isLt1K = rounded.isGreaterThan(-1000) && rounded.isLessThan(1000);
if (isLt1K || !truncate) return toFormat(rounded, format); // Truncate values below -1000 or above 1000
if (isLt1K || !truncate) return toFormat(rounded, format);
// Truncate values below -1000 or above 1000
let power = 0;
const units = ['K', 'M', 'B', 'T'];
for (const unit of units) {

@@ -236,3 +214,2 @@ const unitRaw = rounded.dividedBy(Math.pow(1000, ++power));

const isLt1K = unitVal.isGreaterThan(-1000) && unitVal.isLessThan(1000);
if (isLt1K || unit === 'T') {

@@ -244,20 +221,18 @@ const suffix = `${unit}${format.suffix}`;

}
} // Should never hit here
}
// Should never hit here
throw new Error('Failed to format balance');
}
/**
* Returns a copy of this FilecoinNumber
*/
clone() {
return new FilecoinNumber(this, 'fil', this.coinType, this.unit);
}
/**
* Returns an copy of this FilecoinNumber that is rounded up
*/
roundUp(decimalPlaces = 0) {

@@ -267,7 +242,6 @@ const bigNr = super.dp(decimalPlaces, BigNumber.ROUND_UP);

}
/**
* Returns an copy of this FilecoinNumber that is rounded down
*/
roundDown(decimalPlaces = 0) {

@@ -277,58 +251,52 @@ const bigNr = super.dp(decimalPlaces, BigNumber.ROUND_DOWN);

}
/**
* Returns an absolute value copy of this FilecoinNumber
*/
abs() {
return new FilecoinNumber(super.abs(), 'fil', this.coinType, this.unit);
}
/**
* Returns an absolute value copy of this FilecoinNumber
*/
absoluteValue() {
return this.abs();
}
/**
* Returns a negated copy of this FilecoinNumber (multiplied by -1)
*/
negated() {
return new FilecoinNumber(super.negated(), 'fil', this.coinType, this.unit);
}
/**
* Returns a copy of this FilecoinNumber divided by the supplied value n
*/
div(n) {
return new FilecoinNumber(super.div(n), 'fil', this.coinType, this.unit);
}
/**
* Returns a copy of this FilecoinNumber divided by the supplied value n
*/
dividedBy(n) {
return this.div(n);
}
/**
* Returns a copy of this FilecoinNumber multiplied by the supplied value n
*/
times(n) {
return new FilecoinNumber(super.times(n), 'fil', this.coinType, this.unit);
}
/**
* Returns a copy of this FilecoinNumber multiplied by the supplied value n
*/
multipliedBy(n) {
return this.times(n);
}
/**

@@ -338,7 +306,6 @@ * Returns a copy of this FilecoinNumber increased by the supplied value n

*/
plus(n) {
return new FilecoinNumber(super.plus(n), 'fil', this.coinType, this.unit);
}
/**

@@ -348,7 +315,6 @@ * Returns a copy of this FilecoinNumber decreased by the supplied value n

*/
minus(n) {
return new FilecoinNumber(super.minus(n), 'fil', this.coinType, this.unit);
}
/**

@@ -358,7 +324,6 @@ * Returns `true` if the value of this BigNumber is equal to the value of `n`, otherwise returns `false`.

*/
eq(n) {
return super.eq(n);
}
/**

@@ -368,7 +333,6 @@ * Returns `true` if the value of this BigNumber is greater than the value of `n`, otherwise returns `false`.

*/
gt(n) {
return super.gt(n);
}
/**

@@ -378,7 +342,6 @@ * Returns `true` if the value of this BigNumber is greater than or equal to the value of `n`, otherwise returns `false`.

*/
gte(n) {
return super.gte(n);
}
/**

@@ -388,7 +351,6 @@ * Returns `true` if the value of this BigNumber is less than the value of `n`, otherwise returns `false`.

*/
lt(n) {
return super.lt(n);
}
/**

@@ -398,7 +360,6 @@ * Returns `true` if the value of this BigNumber is less than or equal to the value of `n`, otherwise returns `false`.

*/
lte(n) {
return super.lte(n);
}
/**

@@ -408,7 +369,6 @@ * Returns `true` if the value of this BigNumber is equal to the value of `n`, otherwise returns `false`.

*/
isEqualTo(n) {
return super.isEqualTo(n);
}
/**

@@ -418,7 +378,6 @@ * Returns `true` if the value of this BigNumber is greater than the value of `n`, otherwise returns `false`.

*/
isGreaterThan(n) {
return super.isGreaterThan(n);
}
/**

@@ -428,7 +387,6 @@ * Returns `true` if the value of this BigNumber is greater than or equal to the value of `n`, otherwise returns `false`.

*/
isGreaterThanOrEqualTo(n) {
return super.isGreaterThanOrEqualTo(n);
}
/**

@@ -438,7 +396,6 @@ * Returns `true` if the value of this BigNumber is less than the value of `n`, otherwise returns `false`.

*/
isLessThan(n) {
return super.isLessThan(n);
}
/**

@@ -448,12 +405,9 @@ * Returns `true` if the value of this BigNumber is less than or equal to the value of `n`, otherwise returns `false`.

*/
isLessThanOrEqualTo(n) {
return super.isLessThanOrEqualTo(n);
}
}
_FilecoinNumber = FilecoinNumber;
_defineProperty(FilecoinNumber, "DefaultUnit", 'FIL');
// Static FilecoinNumber(0, 'fil') instances for each CoinType and unit
_defineProperty(FilecoinNumber, "zeroMap", {

@@ -460,0 +414,0 @@ [CoinType.MAIN]: {},

{
"name": "@glif/filecoin-number",
"version": "3.0.0",
"version": "3.0.2",
"description": "a javascript package to handle Filecoin numbers like Fil and AttoFil",

@@ -50,3 +50,3 @@ "main": "./dist/index.js",

},
"gitHead": "a8ead170d63e9ce07eb4b933f31a4709f4b640a3"
"gitHead": "1a1ba285434fd3d37592f1955a9225a53287ecc7"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc