@helium/currency
Version:
Utilities for handling different currency types on the Helium blockchain
153 lines • 7.33 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable prefer-destructuring */
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const CurrencyType_1 = __importDefault(require("./CurrencyType"));
const currency_types_1 = require("./currency_types");
const Errors_1 = require("./Errors");
const FORMAT = {
decimalSeparator: '.',
groupSeparator: ',',
groupSize: 3,
secondaryGroupSize: 0,
fractionGroupSeparator: ' ',
fractionGroupSize: 0,
};
bignumber_js_1.default.config({
EXPONENTIAL_AT: [-10, 20],
FORMAT,
});
const DC_TO_USD_MULTIPLIER = 0.00001;
class Balance {
constructor(integerBalance, type) {
this.type = type;
this.integerBalance = integerBalance || 0;
this.bigInteger = new bignumber_js_1.default(this.integerBalance);
this.bigBalance = this.bigInteger.times(type.coefficient);
this.floatBalance = this.bigBalance.toNumber();
}
static fromFloat(float, currencyType) {
const bigFloat = new bignumber_js_1.default(float);
const integerBalance = bigFloat.dividedBy(currencyType.coefficient).toNumber();
return new Balance(integerBalance, currencyType);
}
static fromIntAndTicker(integerBalance, ticker) {
const currencyType = CurrencyType_1.default.fromTicker(ticker);
return new Balance(integerBalance, currencyType);
}
static fromFloatAndTicker(float, ticker) {
const currencyType = CurrencyType_1.default.fromTicker(ticker);
return this.fromFloat(float, currencyType);
}
toString(maxDecimalPlaces, options) {
var _a, _b, _c;
const decimalSeparator = (options === null || options === void 0 ? void 0 : options.decimalSeparator) || '.';
const groupSeparator = (options === null || options === void 0 ? void 0 : options.groupSeparator) || ',';
const showTicker = (options === null || options === void 0 ? void 0 : options.showTicker) === undefined ? true : options.showTicker;
const format = { decimalSeparator, groupSeparator, groupSize: 3 };
const roundingMode = (options === null || options === void 0 ? void 0 : options.roundingMode) || bignumber_js_1.default.ROUND_DOWN;
const decimalPlacesToDisplay = maxDecimalPlaces !== null && maxDecimalPlaces !== void 0 ? maxDecimalPlaces : (_a = this.type.format) === null || _a === void 0 ? void 0 : _a.decimalPlaces;
const keepTrailingZeroes = (_b = options === null || options === void 0 ? void 0 : options.showTrailingZeroes) !== null && _b !== void 0 ? _b : (_c = this.type.format) === null || _c === void 0 ? void 0 : _c.showTrailingZeroes;
let numberString = '';
if (decimalPlacesToDisplay !== undefined && decimalPlacesToDisplay !== null) {
let decimalPlaces = decimalPlacesToDisplay;
if (!keepTrailingZeroes) {
decimalPlaces = Math.min(decimalPlacesToDisplay, this.bigBalance
.decimalPlaces(decimalPlacesToDisplay, roundingMode)
.decimalPlaces() || 0);
}
numberString = this.bigBalance.toFormat(decimalPlaces, roundingMode, format);
}
else {
numberString = this.bigBalance.toFormat(format);
}
// if the rounded amount is 0, then show the full amount
if (numberString === '0') {
numberString = this.bigBalance.toFormat({ decimalSeparator, groupSeparator });
}
return showTicker ? [numberString, this.type.ticker].join(' ') : numberString;
}
plus(balance) {
if (this.type.ticker !== balance.type.ticker)
throw Errors_1.MixedCurrencyTypeError;
return new Balance(this.bigInteger.plus(balance.bigInteger).toNumber(), this.type);
}
minus(balance) {
if (this.type.ticker !== balance.type.ticker)
throw Errors_1.MixedCurrencyTypeError;
return new Balance(this.bigInteger.minus(balance.bigInteger).toNumber(), this.type);
}
times(n) {
return new Balance(this.bigInteger.times(n).toNumber(), this.type);
}
dividedBy(n) {
return new Balance(this.bigInteger.dividedBy(n).toNumber(), this.type);
}
toNetworkTokens(oraclePrice) {
if (this.type instanceof currency_types_1.NetworkTokens)
return this;
if (this.type instanceof currency_types_1.MobileTokens ||
this.type instanceof currency_types_1.IotTokens ||
this.type instanceof currency_types_1.SolTokens) {
throw Errors_1.UnsupportedCurrencyConversionError;
}
if (!oraclePrice)
throw Errors_1.OraclePriceRequiredError;
return new Balance(this.toUsd()
.bigBalance.dividedBy(oraclePrice.bigBalance)
.dividedBy(CurrencyType_1.default.networkToken.coefficient)
.toNumber(), CurrencyType_1.default.networkToken);
}
toTestNetworkTokens(oraclePrice) {
if (this.type instanceof currency_types_1.TestNetworkTokens)
return this;
if (this.type instanceof currency_types_1.MobileTokens ||
this.type instanceof currency_types_1.IotTokens ||
this.type instanceof currency_types_1.SolTokens) {
throw Errors_1.UnsupportedCurrencyConversionError;
}
if (!oraclePrice)
throw Errors_1.OraclePriceRequiredError;
return new Balance(this.toUsd()
.bigBalance.dividedBy(oraclePrice.bigBalance)
.dividedBy(CurrencyType_1.default.testNetworkToken.coefficient)
.toNumber(), CurrencyType_1.default.testNetworkToken);
}
toUsd(oraclePrice) {
if (this.type instanceof currency_types_1.USDollars)
return this;
if (this.type instanceof currency_types_1.DataCredits) {
return new Balance(this.bigBalance
.times(DC_TO_USD_MULTIPLIER)
.dividedBy(CurrencyType_1.default.usd.coefficient)
.toNumber(), CurrencyType_1.default.usd);
}
if (this.type instanceof currency_types_1.NetworkTokens) {
if (!oraclePrice)
throw Errors_1.OraclePriceRequiredError;
return new Balance(this.bigBalance
.times(oraclePrice.bigBalance)
.dividedBy(CurrencyType_1.default.usd.coefficient)
.toNumber(), CurrencyType_1.default.usd);
}
throw Errors_1.UnsupportedCurrencyConversionError;
}
toDataCredits(oraclePrice) {
if (this.type instanceof currency_types_1.DataCredits)
return this;
if (this.type instanceof currency_types_1.USDollars) {
return new Balance(this.bigBalance.dividedBy(DC_TO_USD_MULTIPLIER).toNumber(), CurrencyType_1.default.dataCredit);
}
if (this.type instanceof currency_types_1.NetworkTokens) {
if (!oraclePrice)
throw Errors_1.OraclePriceRequiredError;
return this.toUsd(oraclePrice).toDataCredits();
}
throw Errors_1.UnsupportedCurrencyConversionError;
}
}
exports.default = Balance;
//# sourceMappingURL=Balance.js.map