@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
75 lines • 3.24 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import { CONTEXT, Inject, Injectable, Scope } from 'graphql-modules';
import { getRateForCurrency, isCryptoCurrency } from '../helpers/exchange.helper.js';
import { CryptoExchangeProvider } from './crypto-exchange.provider.js';
import { FiatExchangeProvider } from './fiat-exchange.provider.js';
let ExchangeProvider = class ExchangeProvider {
context;
cryptoExchangeProvider;
fiatExchangeProvider;
localCurrency;
cryptoConversionFiatCurrency;
constructor(context, cryptoExchangeProvider, fiatExchangeProvider) {
this.context = context;
this.cryptoExchangeProvider = cryptoExchangeProvider;
this.fiatExchangeProvider = fiatExchangeProvider;
this.localCurrency = this.context.adminContext.defaultLocalCurrency;
this.cryptoConversionFiatCurrency =
this.context.adminContext.defaultCryptoConversionFiatCurrency;
}
async getExchangeRates(baseCurrency, quoteCurrency, date) {
let rate = 1;
// if base and quote are the same currency, return rate
if (baseCurrency === quoteCurrency) {
return rate;
}
// adjust rate and convert to FIAT if base or quote are crypto
const ifBaseIsCryptoAdjuster = async () => {
if (isCryptoCurrency(baseCurrency)) {
const { value } = await this.cryptoExchangeProvider.getCryptoExchangeRateLoader.load({
cryptoCurrency: baseCurrency,
date,
});
rate = rate * Number(value);
baseCurrency = this.cryptoConversionFiatCurrency;
}
};
const ifQuoteIsCryptoAdjuster = async () => {
if (isCryptoCurrency(quoteCurrency)) {
const { value } = await this.cryptoExchangeProvider.getCryptoExchangeRateLoader.load({
cryptoCurrency: quoteCurrency,
date,
});
rate = rate / Number(value);
quoteCurrency = this.cryptoConversionFiatCurrency;
}
};
const getFiatRatesPromise = this.fiatExchangeProvider.getExchangeRatesByDatesLoader.load(date);
const [rates] = await Promise.all([
getFiatRatesPromise,
ifBaseIsCryptoAdjuster(),
ifQuoteIsCryptoAdjuster(),
]);
// adjust rate and convert to local default currency if base or quote are not local
if (baseCurrency !== this.localCurrency) {
const baseRate = getRateForCurrency(baseCurrency, rates, this.localCurrency);
rate = rate * baseRate;
}
if (quoteCurrency !== this.localCurrency) {
const quoteRate = getRateForCurrency(quoteCurrency, rates, this.localCurrency);
rate = rate / quoteRate;
}
return rate;
}
};
ExchangeProvider = __decorate([
Injectable({
scope: Scope.Operation,
global: true,
}),
__param(0, Inject(CONTEXT)),
__metadata("design:paramtypes", [Object, CryptoExchangeProvider,
FiatExchangeProvider])
], ExchangeProvider);
export { ExchangeProvider };
//# sourceMappingURL=exchange.provider.js.map