UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

69 lines 3.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CurrencyAmount = void 0; const decimal_js_1 = __importDefault(require("decimal.js")); const MarketsProvider_1 = require("../../../MarketsProvider"); class CurrencyAmount { currencyInfo; client; baseAmount; get baseSymbol() { return this.currencyInfo.symbol; } get minimalUnitAmount() { return this.baseAmount.mul((new decimal_js_1.default(10)).pow(this.currencyInfo.decimals)); } get minimalUnitSymbol() { return this.currencyInfo.minimalUnitSymbol; } get str() { if (this.baseAmount.decimalPlaces() > 5) return `${this.baseAmount.toFixed(5)}... ${this.baseSymbol}`; return `${this.baseAmount.toString()} ${this.baseSymbol}`; } decimals; constructor(currencyInfo, baseAmount, client) { this.currencyInfo = currencyInfo; this.baseAmount = baseAmount; this.decimals = currencyInfo.decimals; this.client = client; } plus(currencyAmount) { if (currencyAmount.currencyInfo.id != currencyAmount.currencyInfo.id) throw new Error('Invalid amount'); return new CurrencyAmount(this.currencyInfo, this.baseAmount.plus(currencyAmount.baseAmount), this.client); } minus(currencyAmount) { if (currencyAmount.currencyInfo.id != currencyAmount.currencyInfo.id) throw new Error('Invalid amount'); return new CurrencyAmount(this.currencyInfo, this.baseAmount.minus(currencyAmount.baseAmount), this.client); } div(currencyAmount) { if (currencyAmount.currencyInfo.id != currencyAmount.currencyInfo.id) throw new Error('Invalid amount'); return new CurrencyAmount(this.currencyInfo, this.baseAmount.div(currencyAmount.baseAmount), this.client); } mul(currencyAmount) { if (currencyAmount.currencyInfo.id != currencyAmount.currencyInfo.id) throw new Error('Invalid amount'); return new CurrencyAmount(this.currencyInfo, this.baseAmount.mul(currencyAmount.baseAmount), this.client); } async toFiat(fiatCurrency) { const markets = await MarketsProvider_1.MarketsProvider.getMarketData(this.client); const cryptoData = markets.crypto.find(t => t.id === this.currencyInfo.nativeTokenId); if (!cryptoData) throw new Error('Crypto rate not found'); const cryptoRateUsd = new decimal_js_1.default(cryptoData.rateUsd); const totalUsd = cryptoRateUsd.mul(this.baseAmount); const fiatData = markets.fiat.find(t => t.symbol === fiatCurrency); if (!fiatData) throw new Error(`Fiat currency ${fiatCurrency} not found`); const fiatRateUsd = new decimal_js_1.default(fiatData.rateUsd); return totalUsd.div(fiatRateUsd); } } exports.CurrencyAmount = CurrencyAmount; //# sourceMappingURL=CurrencyAmount.js.map