Socket
Socket
Sign inDemoInstall

@glif/filecoin-number

Package Overview
Dependencies
Maintainers
1
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 1.0.0-lerna.4 to 1.1.0-beta.0

dist/Converter.d.ts

167

dist/Converter.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Converter = void 0;
var _bent = _interopRequireDefault(require("bent"));
var _bignumber = require("bignumber.js");
var _FilecoinNumber = require("./FilecoinNumber");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

@@ -7,22 +20,62 @@

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const bent = require('bent');
var Converter = /*#__PURE__*/function () {
function Converter(currency, options) {
_classCallCheck(this, Converter);
const BigNumber = require('bignumber.js');
_defineProperty(this, "currency", void 0);
const FilecoinNumber = require('./FilecoinNumber');
_defineProperty(this, "apiKey", void 0);
class Converter {
constructor(currency, {
apiKey,
apiURL
}) {
var _this = this;
_defineProperty(this, "apiURL", void 0);
_defineProperty(this, "cacheConversionRate", /*#__PURE__*/_asyncToGenerator(function* () {
_this.rate = new BigNumber(yield _this.convert(1, 'FIL', _this.currency));
}));
_defineProperty(this, "rate", void 0);
_defineProperty(this, "toFIL", amount => {
if (!currency) throw new Error('No currency passed.');
this.currency = currency;
this.apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || '';
this.apiURL = (options === null || options === void 0 ? void 0 : options.apiURL) || 'https://pro-api.coinmarketcap.com/';
this.rate = null;
}
_createClass(Converter, [{
key: "cacheConversionRate",
value: function () {
var _cacheConversionRate = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.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();
}
}
}, _callee, this);
}));
function cacheConversionRate() {
return _cacheConversionRate.apply(this, arguments);
}
return cacheConversionRate;
}()
}, {
key: "toFIL",
value: function toFIL(amount) {
if (!amount) return this.toFIL('0');

@@ -34,48 +87,74 @@

if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {
const filAmount = new BigNumber(amount).dividedBy(this.rate);
return new FilecoinNumber(filAmount, 'fil');
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof _bignumber.BigNumber) {
var filAmount = new _bignumber.BigNumber(amount).dividedBy(this.rate);
return new _FilecoinNumber.FilecoinNumber(filAmount, 'fil');
}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');
});
_defineProperty(this, "fromFIL", amount => {
}
}, {
key: "fromFIL",
value: function 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);
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.');
});
}
}, {
key: "convert",
value: function () {
var _convert = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(amount, from, to) {
var get, res;
return regeneratorRuntime.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));
_defineProperty(this, "convert", /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(function* (amount, from, to) {
const get = bent('GET', 'json', {
'X-CMC_PRO_API_KEY': _this.apiKey
});
const res = yield get(`${_this.apiURL}/v1/tools/price-conversion?symbol=${from}&amount=${amount}&convert=${to}`);
if (!res.data || !res.data.quote || !res.data.quote[to]) throw new Error('No conversion price found.');
return res.data.quote[to].price;
});
case 3:
res = _context2.sent;
return function (_x, _x2, _x3) {
return _ref2.apply(this, arguments);
};
}());
if (!(!res.data || !res.data.quote || !res.data.quote[to])) {
_context2.next = 6;
break;
}
_defineProperty(this, "getCachedConversionRate", () => this.rate);
throw new Error('No conversion price found.');
if (!currency) throw new Error('No currency passed.');
this.currency = currency;
this.apiKey = apiKey || '';
this.apiURL = apiURL || 'https://pro-api.coinmarketcap.com/';
this.rate = null;
}
case 6:
return _context2.abrupt("return", res.data.quote[to].price);
}
case 7:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
module.exports = Converter;
function convert(_x, _x2, _x3) {
return _convert.apply(this, arguments);
}
return convert;
}()
}, {
key: "getCachedConversionRate",
value: function getCachedConversionRate() {
return this.rate;
}
}]);
return Converter;
}();
exports.Converter = Converter;
//# sourceMappingURL=Converter.js.map
"use strict";
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FilecoinNumber = void 0;
const BigNumber = require('bignumber.js'); // not sure how we want to configure rounding for this
var _bignumber = require("bignumber.js");
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
BigNumber.set({
ROUNDING_MODE: BigNumber.ROUND_HALF_DOWN
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
// not sure how we want to configure rounding for this
_bignumber.BigNumber.set({
ROUNDING_MODE: _bignumber.BigNumber.ROUND_HALF_DOWN
});
BigNumber.config({
_bignumber.BigNumber.config({
EXPONENTIAL_AT: 1e9
}); // stores filecoin numbers in denominations of Fil, not AttoFil
});
class FilecoinNumber extends BigNumber {
constructor(amount, denom) {
if (!denom) throw new Error('No Filecoin denomination passed in constructor.');
const formattedDenom = denom.toLowerCase();
if (formattedDenom !== 'fil' && formattedDenom !== 'picofil' && formattedDenom !== 'attofil') throw new Error('Unsupported denomination passed in constructor. Must pass picofil or attofil.');
function asBigNumber(amount, denom) {
if (!denom) {
throw new Error('No Filecoin denomination passed in constructor.');
}
if (formattedDenom === 'picofil') {
super(new BigNumber(amount).shiftedBy(-12));
var formattedDenom = denom.toLowerCase();
_defineProperty(this, "toFil", () => this.toString());
if (formattedDenom !== 'fil' && formattedDenom !== 'picofil' && formattedDenom !== 'attofil') {
throw new Error('Unsupported denomination passed in constructor. Must pass picofil or attofil.');
}
_defineProperty(this, "toPicoFil", () => this.shiftedBy(12).toString());
if (formattedDenom === 'picofil') {
return new _bignumber.BigNumber(amount).shiftedBy(-12);
} else if (formattedDenom === 'attofil') {
return new _bignumber.BigNumber(amount).shiftedBy(-18);
} else {
return amount;
}
} // stores filecoin numbers in denominations of Fil, not AttoFil
_defineProperty(this, "toAttoFil", () => this.shiftedBy(18).toFixed(0, 1));
} else if (formattedDenom === 'attofil') {
super(new BigNumber(amount).shiftedBy(-18));
_defineProperty(this, "toFil", () => this.toString());
var FilecoinNumber = /*#__PURE__*/function (_BigNumber) {
_inherits(FilecoinNumber, _BigNumber);
_defineProperty(this, "toPicoFil", () => this.shiftedBy(12).toString());
var _super = _createSuper(FilecoinNumber);
_defineProperty(this, "toAttoFil", () => this.shiftedBy(18).toFixed(0, 1));
} else {
super(amount);
function FilecoinNumber(amount, denom) {
_classCallCheck(this, FilecoinNumber);
_defineProperty(this, "toFil", () => this.toString());
return _super.call(this, asBigNumber(amount, denom));
}
_defineProperty(this, "toPicoFil", () => this.shiftedBy(12).toString());
_defineProperty(this, "toAttoFil", () => this.shiftedBy(18).toFixed(0, 1));
_createClass(FilecoinNumber, [{
key: "toFil",
value: function toFil() {
return this.toString();
}
}
}, {
key: "toPicoFil",
value: function toPicoFil() {
return this.shiftedBy(12).toString();
}
}, {
key: "toAttoFil",
value: function toAttoFil() {
return this.shiftedBy(18).toFixed(0, 1);
}
}]);
}
return FilecoinNumber;
}(_bignumber.BigNumber);
module.exports = FilecoinNumber;
exports.FilecoinNumber = FilecoinNumber;
//# sourceMappingURL=FilecoinNumber.js.map
"use strict";
const BigNumber = require('bignumber.js');
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
BigNumber: true
};
Object.defineProperty(exports, "BigNumber", {
enumerable: true,
get: function get() {
return _bignumber.BigNumber;
}
});
const Converter = require('./Converter');
var _bignumber = require("bignumber.js");
const FilecoinNumber = require('./FilecoinNumber');
var _FilecoinNumber = require("./FilecoinNumber");
module.exports = {
BigNumber,
Converter,
FilecoinNumber
};
Object.keys(_FilecoinNumber).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _FilecoinNumber[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _FilecoinNumber[key];
}
});
});
var _Converter = require("./Converter");
Object.keys(_Converter).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _Converter[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _Converter[key];
}
});
});
//# sourceMappingURL=index.js.map

@@ -7,116 +7,139 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var bent = require('bent');
import bent from 'bent';
import { BigNumber } from 'bignumber.js';
import { FilecoinNumber } from './FilecoinNumber';
export var Converter = /*#__PURE__*/function () {
function Converter(currency, options) {
_classCallCheck(this, Converter);
var BigNumber = require('bignumber.js');
_defineProperty(this, "currency", void 0);
var FilecoinNumber = require('./FilecoinNumber');
_defineProperty(this, "apiKey", void 0);
var Converter = function Converter(currency, _ref) {
var _this = this;
_defineProperty(this, "apiURL", void 0);
var apiKey = _ref.apiKey,
apiURL = _ref.apiURL;
_defineProperty(this, "rate", void 0);
_classCallCheck(this, Converter);
if (!currency) throw new Error('No currency passed.');
this.currency = currency;
this.apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || '';
this.apiURL = (options === null || options === void 0 ? void 0 : options.apiURL) || 'https://pro-api.coinmarketcap.com/';
this.rate = null;
}
_defineProperty(this, "cacheConversionRate", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
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);
_createClass(Converter, [{
key: "cacheConversionRate",
value: function () {
var _cacheConversionRate = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
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 3:
_context.t1 = _context.sent;
this.rate = new _context.t0(_context.t1);
case 5:
case "end":
return _context.stop();
}
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function cacheConversionRate() {
return _cacheConversionRate.apply(this, arguments);
}
}, _callee);
})));
_defineProperty(this, "toFIL", function (amount) {
if (!amount) return _this.toFIL('0');
return cacheConversionRate;
}()
}, {
key: "toFIL",
value: function 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 (!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) {
var filAmount = new BigNumber(amount).dividedBy(_this.rate);
return new FilecoinNumber(filAmount, 'fil');
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {
var filAmount = new BigNumber(amount).dividedBy(this.rate);
return new FilecoinNumber(filAmount, 'fil');
}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');
}
}, {
key: "fromFIL",
value: function fromFIL(amount) {
if (!amount) return this.fromFIL('0');
if (!this.rate) throw new Error('Call cacheConversionRate() to get the conversion rate before calling .fromFIL.');
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');
});
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {
return new BigNumber(amount).multipliedBy(this.rate);
}
_defineProperty(this, "fromFIL", function (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.');
}
}, {
key: "convert",
value: function () {
var _convert = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(amount, from, to) {
var get, res;
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));
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber.');
});
case 3:
res = _context2.sent;
_defineProperty(this, "convert", /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(amount, from, to) {
var get, res;
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));
if (!(!res.data || !res.data.quote || !res.data.quote[to])) {
_context2.next = 6;
break;
}
case 3:
res = _context2.sent;
throw new Error('No conversion price found.');
if (!(!res.data || !res.data.quote || !res.data.quote[to])) {
_context2.next = 6;
break;
}
case 6:
return _context2.abrupt("return", res.data.quote[to].price);
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();
case 7:
case "end":
return _context2.stop();
}
}
}
}, _callee2);
}));
}, _callee2, this);
}));
return function (_x, _x2, _x3) {
return _ref3.apply(this, arguments);
};
}());
function convert(_x, _x2, _x3) {
return _convert.apply(this, arguments);
}
_defineProperty(this, "getCachedConversionRate", function () {
return _this.rate;
});
return convert;
}()
}, {
key: "getCachedConversionRate",
value: function getCachedConversionRate() {
return this.rate;
}
}]);
if (!currency) throw new Error('No currency passed.');
this.currency = currency;
this.apiKey = apiKey || '';
this.apiURL = apiURL || 'https://pro-api.coinmarketcap.com/';
this.rate = null;
};
module.exports = Converter;
return Converter;
}();
//# sourceMappingURL=Converter.js.map

@@ -5,2 +5,6 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

@@ -20,7 +24,4 @@

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import { BigNumber } from 'bignumber.js'; // not sure how we want to configure rounding for this
var BigNumber = require('bignumber.js'); // not sure how we want to configure rounding for this
BigNumber.set({

@@ -31,69 +32,55 @@ ROUNDING_MODE: BigNumber.ROUND_HALF_DOWN

EXPONENTIAL_AT: 1e9
}); // stores filecoin numbers in denominations of Fil, not AttoFil
});
var FilecoinNumber = /*#__PURE__*/function (_BigNumber) {
_inherits(FilecoinNumber, _BigNumber);
function asBigNumber(amount, denom) {
if (!denom) {
throw new Error('No Filecoin denomination passed in constructor.');
}
var _super = _createSuper(FilecoinNumber);
var formattedDenom = denom.toLowerCase();
function FilecoinNumber(amount, denom) {
var _this;
if (formattedDenom !== 'fil' && formattedDenom !== 'picofil' && formattedDenom !== 'attofil') {
throw new Error('Unsupported denomination passed in constructor. Must pass picofil or attofil.');
}
_classCallCheck(this, FilecoinNumber);
if (formattedDenom === 'picofil') {
return new BigNumber(amount).shiftedBy(-12);
} else if (formattedDenom === 'attofil') {
return new BigNumber(amount).shiftedBy(-18);
} else {
return amount;
}
} // stores filecoin numbers in denominations of Fil, not AttoFil
if (!denom) throw new Error('No Filecoin denomination passed in constructor.');
var formattedDenom = denom.toLowerCase();
if (formattedDenom !== 'fil' && formattedDenom !== 'picofil' && formattedDenom !== 'attofil') throw new Error('Unsupported denomination passed in constructor. Must pass picofil or attofil.');
if (formattedDenom === 'picofil') {
_this = _super.call(this, new BigNumber(amount).shiftedBy(-12));
export var FilecoinNumber = /*#__PURE__*/function (_BigNumber) {
_inherits(FilecoinNumber, _BigNumber);
_defineProperty(_assertThisInitialized(_this), "toFil", function () {
return _this.toString();
});
var _super = _createSuper(FilecoinNumber);
_defineProperty(_assertThisInitialized(_this), "toPicoFil", function () {
return _this.shiftedBy(12).toString();
});
function FilecoinNumber(amount, denom) {
_classCallCheck(this, FilecoinNumber);
_defineProperty(_assertThisInitialized(_this), "toAttoFil", function () {
return _this.shiftedBy(18).toFixed(0, 1);
});
} else if (formattedDenom === 'attofil') {
_this = _super.call(this, new BigNumber(amount).shiftedBy(-18));
return _super.call(this, asBigNumber(amount, denom));
}
_defineProperty(_assertThisInitialized(_this), "toFil", function () {
return _this.toString();
});
_defineProperty(_assertThisInitialized(_this), "toPicoFil", function () {
return _this.shiftedBy(12).toString();
});
_defineProperty(_assertThisInitialized(_this), "toAttoFil", function () {
return _this.shiftedBy(18).toFixed(0, 1);
});
} else {
_this = _super.call(this, amount);
_defineProperty(_assertThisInitialized(_this), "toFil", function () {
return _this.toString();
});
_defineProperty(_assertThisInitialized(_this), "toPicoFil", function () {
return _this.shiftedBy(12).toString();
});
_defineProperty(_assertThisInitialized(_this), "toAttoFil", function () {
return _this.shiftedBy(18).toFixed(0, 1);
});
_createClass(FilecoinNumber, [{
key: "toFil",
value: function toFil() {
return this.toString();
}
}, {
key: "toPicoFil",
value: function toPicoFil() {
return this.shiftedBy(12).toString();
}
}, {
key: "toAttoFil",
value: function toAttoFil() {
return this.shiftedBy(18).toFixed(0, 1);
}
}]);
return _possibleConstructorReturn(_this);
}
return FilecoinNumber;
}(BigNumber);
module.exports = FilecoinNumber;
//# sourceMappingURL=FilecoinNumber.js.map

@@ -1,12 +0,4 @@

var BigNumber = require('bignumber.js');
var Converter = require('./Converter');
var FilecoinNumber = require('./FilecoinNumber');
module.exports = {
BigNumber: BigNumber,
Converter: Converter,
FilecoinNumber: FilecoinNumber
};
export { BigNumber } from 'bignumber.js';
export * from './FilecoinNumber';
export * from './Converter';
//# sourceMappingURL=index.js.map

@@ -7,72 +7,74 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

const bent = require('bent');
import bent from 'bent';
import { BigNumber } from 'bignumber.js';
import { FilecoinNumber } from './FilecoinNumber';
export class Converter {
constructor(currency, options) {
_defineProperty(this, "currency", void 0);
const BigNumber = require('bignumber.js');
_defineProperty(this, "apiKey", void 0);
const FilecoinNumber = require('./FilecoinNumber');
_defineProperty(this, "apiURL", void 0);
class Converter {
constructor(currency, {
apiKey,
apiURL
}) {
_defineProperty(this, "rate", void 0);
if (!currency) throw new Error('No currency passed.');
this.currency = currency;
this.apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || '';
this.apiURL = (options === null || options === void 0 ? void 0 : options.apiURL) || 'https://pro-api.coinmarketcap.com/';
this.rate = null;
}
cacheConversionRate() {
var _this = this;
_defineProperty(this, "cacheConversionRate", /*#__PURE__*/_asyncToGenerator(function* () {
return _asyncToGenerator(function* () {
_this.rate = new BigNumber(yield _this.convert(1, 'FIL', _this.currency));
}));
})();
}
_defineProperty(this, "toFIL", amount => {
if (!amount) return this.toFIL('0');
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 (!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) {
const filAmount = new BigNumber(amount).dividedBy(this.rate);
return new FilecoinNumber(filAmount, 'fil');
}
if (typeof amount === 'string' || typeof amount === 'number' || amount instanceof BigNumber) {
const filAmount = new BigNumber(amount).dividedBy(this.rate);
return new FilecoinNumber(filAmount, 'fil');
}
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');
});
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber');
}
_defineProperty(this, "fromFIL", amount => {
if (!amount) return this.fromFIL('0');
if (!this.rate) throw new Error('Call cacheConversionRate() to get the conversion rate before calling .fromFIL.');
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);
}
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.');
});
throw new Error('Amount passed must be a Number, String, or an instanceof BigNumber.');
}
_defineProperty(this, "convert", /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(function* (amount, from, to) {
const get = bent('GET', 'json', {
'X-CMC_PRO_API_KEY': _this.apiKey
});
const res = yield get(`${_this.apiURL}/v1/tools/price-conversion?symbol=${from}&amount=${amount}&convert=${to}`);
if (!res.data || !res.data.quote || !res.data.quote[to]) throw new Error('No conversion price found.');
return res.data.quote[to].price;
convert(amount, from, to) {
var _this2 = this;
return _asyncToGenerator(function* () {
const get = bent('GET', 'json', {
'X-CMC_PRO_API_KEY': _this2.apiKey
});
const res = yield get(`${_this2.apiURL}/v1/tools/price-conversion?symbol=${from}&amount=${amount}&convert=${to}`);
if (!res.data || !res.data.quote || !res.data.quote[to]) throw new Error('No conversion price found.');
return res.data.quote[to].price;
})();
}
return function (_x, _x2, _x3) {
return _ref2.apply(this, arguments);
};
}());
_defineProperty(this, "getCachedConversionRate", () => this.rate);
if (!currency) throw new Error('No currency passed.');
this.currency = currency;
this.apiKey = apiKey || '';
this.apiURL = apiURL || 'https://pro-api.coinmarketcap.com/';
this.rate = null;
getCachedConversionRate() {
return this.rate;
}
}
module.exports = Converter;
//# sourceMappingURL=Converter.js.map

@@ -1,6 +0,3 @@

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import { BigNumber } from 'bignumber.js'; // not sure how we want to configure rounding for this
const BigNumber = require('bignumber.js'); // not sure how we want to configure rounding for this
BigNumber.set({

@@ -11,40 +8,43 @@ ROUNDING_MODE: BigNumber.ROUND_HALF_DOWN

EXPONENTIAL_AT: 1e9
}); // stores filecoin numbers in denominations of Fil, not AttoFil
});
class FilecoinNumber extends BigNumber {
constructor(amount, denom) {
if (!denom) throw new Error('No Filecoin denomination passed in constructor.');
const formattedDenom = denom.toLowerCase();
if (formattedDenom !== 'fil' && formattedDenom !== 'picofil' && formattedDenom !== 'attofil') throw new Error('Unsupported denomination passed in constructor. Must pass picofil or attofil.');
function asBigNumber(amount, denom) {
if (!denom) {
throw new Error('No Filecoin denomination passed in constructor.');
}
if (formattedDenom === 'picofil') {
super(new BigNumber(amount).shiftedBy(-12));
const formattedDenom = denom.toLowerCase();
_defineProperty(this, "toFil", () => this.toString());
if (formattedDenom !== 'fil' && formattedDenom !== 'picofil' && formattedDenom !== 'attofil') {
throw new Error('Unsupported denomination passed in constructor. Must pass picofil or attofil.');
}
_defineProperty(this, "toPicoFil", () => this.shiftedBy(12).toString());
if (formattedDenom === 'picofil') {
return new BigNumber(amount).shiftedBy(-12);
} else if (formattedDenom === 'attofil') {
return new BigNumber(amount).shiftedBy(-18);
} else {
return amount;
}
} // stores filecoin numbers in denominations of Fil, not AttoFil
_defineProperty(this, "toAttoFil", () => this.shiftedBy(18).toFixed(0, 1));
} else if (formattedDenom === 'attofil') {
super(new BigNumber(amount).shiftedBy(-18));
_defineProperty(this, "toFil", () => this.toString());
export class FilecoinNumber extends BigNumber {
constructor(amount, denom) {
super(asBigNumber(amount, denom));
}
_defineProperty(this, "toPicoFil", () => this.shiftedBy(12).toString());
toFil() {
return this.toString();
}
_defineProperty(this, "toAttoFil", () => this.shiftedBy(18).toFixed(0, 1));
} else {
super(amount);
toPicoFil() {
return this.shiftedBy(12).toString();
}
_defineProperty(this, "toFil", () => this.toString());
_defineProperty(this, "toPicoFil", () => this.shiftedBy(12).toString());
_defineProperty(this, "toAttoFil", () => this.shiftedBy(18).toFixed(0, 1));
}
toAttoFil() {
return this.shiftedBy(18).toFixed(0, 1);
}
}
module.exports = FilecoinNumber;
//# sourceMappingURL=FilecoinNumber.js.map

@@ -1,12 +0,4 @@

const BigNumber = require('bignumber.js');
const Converter = require('./Converter');
const FilecoinNumber = require('./FilecoinNumber');
module.exports = {
BigNumber,
Converter,
FilecoinNumber
};
export { BigNumber } from 'bignumber.js';
export * from './FilecoinNumber';
export * from './Converter';
//# sourceMappingURL=index.js.map
{
"name": "@glif/filecoin-number",
"version": "1.0.0-lerna.4",
"version": "1.1.0-beta.0",
"description": "a javascript package to handle Filecoin numbers like Fil and AttoFil",

@@ -8,10 +8,15 @@ "main": "./dist/index.js",

"module": "./module/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build-browser": "cross-env BABEL_ENV=browser babel ./src --out-dir ./lib --source-maps --copy-files",
"build-module": "cross-env BABEL_ENV=module babel ./src --out-dir ./module --source-maps --copy-files",
"build-node": "babel ./src --out-dir ./dist --source-maps --copy-files",
"build": "npm run build-node && npm run build-browser && npm run build-module",
"clean": "../../node_modules/.bin/rimraf dist/ lib/ module/",
"build-browser": "../../node_modules/.bin/cross-env BABEL_ENV=browser ../../node_modules/.bin/babel ./src --out-dir ./lib --extensions \".ts,.tsx\" --source-maps --ignore '**/__tests__'",
"build-module": "../../node_modules/.bin/cross-env BABEL_ENV=module ../../node_modules/.bin/babel ./src --out-dir ./module --extensions \".ts,.tsx\" --source-maps --ignore '**/__tests__'",
"build-node": "../../node_modules/.bin/babel ./src/ --out-dir ./dist/ --extensions \".ts,.tsx\" --source-maps --ignore '**/__tests__'",
"build-types": "../../node_modules/.bin/tsc --emitDeclarationOnly",
"build": "npm run clean && npm run build-types && npm run build-node && npm run build-browser && npm run build-module",
"prepublishOnly": "npm run build",
"test": "npm run build && jest",
"test:watch": "npm run build && jest --watch"
"test:watch": "npm run build && jest --watch",
"type-check": "../../node_modules/.bin/tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch"
},

@@ -22,19 +27,7 @@ "author": "Infinite Scroll <squad@infinitescroll.org> (https://infinitescroll.org)",

"@babel/polyfill": "7.7.0",
"bent": "7.1.0",
"bignumber.js": "9.0.0"
"bent": "7.3.12",
"bignumber.js": "9.0.1"
},
"devDependencies": {
"@babel/cli": "7.10.4",
"@babel/core": "7.10.4",
"@babel/plugin-proposal-class-properties": "7.10.4",
"@babel/plugin-proposal-object-rest-spread": "7.10.4",
"@babel/preset-env": "7.10.4",
"babel-eslint": "10.0.3",
"cross-env": "6.0.3",
"eslint": "6.1.0",
"eslint-config-airbnb": "18.0.1",
"eslint-config-prettier": "6.7.0",
"eslint-plugin-import": "2.19.1",
"jest": "26.1.0",
"prettier": "1.19.1"
"@types/bent": "^7.3.2"
},

@@ -50,3 +43,3 @@ "files": [

},
"gitHead": "8012888a626373e99226ae438a7f630d0eee47f9"
"gitHead": "140b071a501026e96cd4a75a01b8cd15bca8853b"
}

@@ -16,3 +16,3 @@ # Filecoin number

// it comes with 2 additional instance methods for showing the filecoin number in attofil or fil
// it comes with 2 additional instance methods for showing the filecoin number as a string in attofil or fil
const inPicoFil = filecoinNumber.toPicoFil()

@@ -19,0 +19,0 @@ const inAttoFil = filecoinNumber.toAttoFil()

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