chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
61 lines • 2.68 kB
JavaScript
import { toDecimal } from '../../InternalUtils/NumberLike';
export class CurrencyAmount {
currencyInfo;
markets;
baseAmount;
get baseSymbol() {
return this.currencyInfo.symbol;
}
get minimalUnitAmount() {
return this.baseAmount.mul(toDecimal(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, markets) {
this.currencyInfo = currencyInfo;
this.baseAmount = baseAmount;
this.decimals = currencyInfo.decimals;
this.markets = markets;
}
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.markets);
}
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.markets);
}
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.markets);
}
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.markets);
}
async toFiat(fiatCurrency, useCache = true) {
const markets = await this.markets.get(useCache);
const cryptoData = markets.crypto.find((t) => t.id === this.currencyInfo.nativeTokenId);
if (!cryptoData)
throw new Error('Crypto rate not found');
const cryptoRateUsd = toDecimal(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 = toDecimal(fiatData.rateUsd);
return totalUsd.div(fiatRateUsd);
}
}
//# sourceMappingURL=CurrencyAmount.js.map