chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
56 lines • 2.47 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Currency = void 0;
const CurrencyAmount_1 = require("./CurrencyAmount");
const MarketsProvider_1 = require("../../../MarketsProvider");
const decimal_js_1 = __importDefault(require("decimal.js"));
class Currency {
currencyInfo;
currencyProviders;
client;
constructor(currencyInfo, client, currencyProviders) {
this.currencyInfo = currencyInfo;
this.currencyProviders = currencyProviders;
this.client = client;
}
async amountFiat(amountStr, fiatCurrency) {
const markets = await MarketsProvider_1.MarketsProvider.getMarketData(this.client);
let amountFiat;
try {
amountFiat = new decimal_js_1.default(amountStr);
}
catch {
throw new Error(`Invalid amount string: ${amountStr}`);
}
// Calculate total in usd
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);
const totalUsd = amountFiat.mul(fiatRateUsd);
//Calculate total in crypto
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);
if (cryptoRateUsd.eq(0))
return new CurrencyAmount_1.CurrencyAmount(this.currencyInfo, new decimal_js_1.default(0), this.client);
const totalCrypto = totalUsd.div(cryptoRateUsd);
return new CurrencyAmount_1.CurrencyAmount(this.currencyInfo, totalCrypto, this.client);
}
async getUsdRate() {
const markets = await MarketsProvider_1.MarketsProvider.getMarketData(this.client);
return new decimal_js_1.default(markets.crypto.find(t => t.rateUsd).rateUsd);
}
async getPublicKey() {
return (await this.currencyProviders.getPublicKeyProvider(this.currencyInfo))();
}
async getPrivateKey() {
return (await this.currencyProviders.getPrivateKeyProvider(this.currencyInfo))();
}
}
exports.Currency = Currency;
//# sourceMappingURL=Currency.js.map