@riocrypto/common-server 1.0.786 → 1.0.788
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/clients/currency-api-client.d.ts +1 -0
- package/build/clients/currency-api-client.js +25 -0
- package/build/helpers/calculate-volatility-markup.d.ts +1 -0
- package/build/helpers/calculate-volatility-markup.js +18 -0
- package/build/helpers/calculate-volatility.d.ts +7 -0
- package/build/helpers/calculate-volatility.js +38 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/package.json +1 -1
|
@@ -3,6 +3,7 @@ declare class CurrencyAPIClient {
|
|
|
3
3
|
private apiKey;
|
|
4
4
|
constructor(apiKey: string);
|
|
5
5
|
convert(from: Fiat, to: Fiat): Promise<number>;
|
|
6
|
+
getHistoricalExchangeRates(from: Fiat, to: Fiat, start: string, end: string): Promise<any>;
|
|
6
7
|
}
|
|
7
8
|
export declare const buildCurrencyAPIClient: () => Promise<CurrencyAPIClient>;
|
|
8
9
|
export {};
|
|
@@ -69,6 +69,31 @@ class CurrencyAPIClient {
|
|
|
69
69
|
}
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
|
+
getHistoricalExchangeRates(from, to, start, end) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
try {
|
|
75
|
+
let exchangeRates;
|
|
76
|
+
const cachedResponse = cache.get(`v3/range?apikey=${this.apiKey}&datetime_start=${start}&datetime_end=${end}&accuracy=minute¤cies=${to}&base_currency=${from}`);
|
|
77
|
+
if (cachedResponse) {
|
|
78
|
+
exchangeRates = cachedResponse.data;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const currencyAPIResponse = yield axios_1.default.get(`https://api.currencyapi.com/v3/range?apikey=${this.apiKey}&datetime_start=${start}&datetime_end=${end}&accuracy=minute¤cies=${to}&base_currency=${from}`);
|
|
82
|
+
exchangeRates = currencyAPIResponse.data.data;
|
|
83
|
+
cache.set(`v3/range?apikey=${this.apiKey}&datetime_start=${start}&datetime_end=${end}&accuracy=minute¤cies=${to}&base_currency=${from}`, currencyAPIResponse.data);
|
|
84
|
+
}
|
|
85
|
+
return exchangeRates;
|
|
86
|
+
}
|
|
87
|
+
catch (e) {
|
|
88
|
+
const cachedResponse = cache.get(`v3/range?apikey=${this.apiKey}&datetime_start=${start}&datetime_end=${end}&accuracy=minute¤cies=${to}&base_currency=${from}`);
|
|
89
|
+
if (!cachedResponse) {
|
|
90
|
+
throw new Error(`Unable to get hsitorical exchange rates from currencyAPI for ${from} to ${to}`);
|
|
91
|
+
}
|
|
92
|
+
const exchangeRates = cachedResponse.data;
|
|
93
|
+
return exchangeRates;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
72
97
|
}
|
|
73
98
|
const buildCurrencyAPIClient = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
74
99
|
const CURRENCY_API_KEY = yield secret_manager_client_1.secretManagerClient.getSecretValue("CURRENCY_API_KEY");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const calculateExchangeRateWithVolatilityMarkup: (conversionRateFromUSD: number, volatility: number, riskFactor: number, priceDirection: "increase" | "decrease" | "stable") => number;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateExchangeRateWithVolatilityMarkup = void 0;
|
|
4
|
+
const calculateExchangeRateWithVolatilityMarkup = (conversionRateFromUSD, volatility, riskFactor, priceDirection) => {
|
|
5
|
+
const maximumPriceChange = volatility * riskFactor;
|
|
6
|
+
let priceWithMarkup;
|
|
7
|
+
if (priceDirection === "increase") {
|
|
8
|
+
priceWithMarkup = conversionRateFromUSD + maximumPriceChange;
|
|
9
|
+
}
|
|
10
|
+
else if (priceDirection === "decrease") {
|
|
11
|
+
priceWithMarkup = conversionRateFromUSD - maximumPriceChange;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
priceWithMarkup = conversionRateFromUSD;
|
|
15
|
+
}
|
|
16
|
+
return priceWithMarkup;
|
|
17
|
+
};
|
|
18
|
+
exports.calculateExchangeRateWithVolatilityMarkup = calculateExchangeRateWithVolatilityMarkup;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.calculateVolatility = void 0;
|
|
13
|
+
const currency_api_client_1 = require("../clients/currency-api-client");
|
|
14
|
+
const calculateVolatility = (from, to, start, end) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
const currencyAPIClient = yield (0, currency_api_client_1.buildCurrencyAPIClient)();
|
|
16
|
+
const data = yield currencyAPIClient.getHistoricalExchangeRates(from, to, start.toISOString(), end.toISOString());
|
|
17
|
+
const values = data.map((point) => point.currencies.MXN.value);
|
|
18
|
+
const average = values.reduce((sum, value) => sum + value, 0) / values.length;
|
|
19
|
+
const squaredDeviations = values.map((value) => (value - average) ** 2);
|
|
20
|
+
const variance = squaredDeviations.reduce((sum, deviation) => sum + deviation, 0) /
|
|
21
|
+
values.length;
|
|
22
|
+
const volatility = Math.sqrt(variance);
|
|
23
|
+
const currentPrice = values[values.length - 1];
|
|
24
|
+
const previousPrice = values[values.length - 2];
|
|
25
|
+
const priceChange = currentPrice - previousPrice;
|
|
26
|
+
let priceDirection;
|
|
27
|
+
if (Math.abs(priceChange) > volatility) {
|
|
28
|
+
priceDirection = priceChange > 0 ? "increase" : "decrease";
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
priceDirection = "stable";
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
volatility,
|
|
35
|
+
priceDirection,
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
exports.calculateVolatility = calculateVolatility;
|
package/build/index.d.ts
CHANGED
|
@@ -46,3 +46,5 @@ export * from "./helpers/get-platform-and-partner-fees";
|
|
|
46
46
|
export * from "./helpers/get-processor-fees";
|
|
47
47
|
export * from "./helpers/get-network-fees";
|
|
48
48
|
export * from "./helpers/calculate-aggregate-volume";
|
|
49
|
+
export * from "./helpers/calculate-volatility";
|
|
50
|
+
export * from "./helpers/calculate-volatility-markup";
|
package/build/index.js
CHANGED
|
@@ -62,3 +62,5 @@ __exportStar(require("./helpers/get-platform-and-partner-fees"), exports);
|
|
|
62
62
|
__exportStar(require("./helpers/get-processor-fees"), exports);
|
|
63
63
|
__exportStar(require("./helpers/get-network-fees"), exports);
|
|
64
64
|
__exportStar(require("./helpers/calculate-aggregate-volume"), exports);
|
|
65
|
+
__exportStar(require("./helpers/calculate-volatility"), exports);
|
|
66
|
+
__exportStar(require("./helpers/calculate-volatility-markup"), exports);
|