@riocrypto/common-server 1.0.1031 → 1.0.1032

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.
@@ -1,7 +1,6 @@
1
1
  import { Fiat } from "@riocrypto/common";
2
- export declare const getExchangeRates: (from: Fiat, to: Fiat, volatilitySafeguard: boolean) => Promise<{
2
+ export declare const getExchangeRates: (from: Fiat, to: Fiat, noMarkups: boolean) => Promise<{
3
3
  conversionRate: number;
4
- conversionRateWithVolatilityMarkup: number;
5
- conversionRateWithRioMarkup: number;
6
- conversionRateWithAllMarkups: number;
4
+ conversionRateWithMarkups?: number | undefined;
5
+ safestPrice?: number | undefined;
7
6
  }>;
@@ -14,35 +14,19 @@ const common_1 = require("@riocrypto/common");
14
14
  const currency_api_client_1 = require("../clients/currency-api-client");
15
15
  const kapstar_client_1 = require("../clients/kapstar-client");
16
16
  const QUOTE_DURATION_MINUTES = 10;
17
- const getExchangeRates = (from, to, volatilitySafeguard) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const getExchangeRates = (from, to, noMarkups) => __awaiter(void 0, void 0, void 0, function* () {
18
18
  const currencyAPIClient = yield (0, currency_api_client_1.buildCurrencyAPIClient)();
19
19
  const kapstarClient = yield (0, kapstar_client_1.buildKapstarClient)();
20
20
  const conversionRate = to === common_1.Fiat.BRL
21
21
  ? yield kapstarClient.getBRLExchangeRateUSD()
22
22
  : yield currencyAPIClient.convert(from, to);
23
- if (!volatilitySafeguard) {
23
+ if (noMarkups) {
24
24
  return {
25
25
  conversionRate,
26
- conversionRateWithVolatilityMarkup: conversionRate,
27
- conversionRateWithRioMarkup: conversionRate,
28
- conversionRateWithAllMarkups: conversionRate,
29
26
  };
30
27
  }
31
- const { startDate, endDate } = getDateRange();
32
- const data = yield currencyAPIClient.getHistoricalExchangeRates(from, to, startDate.toISOString(), endDate.toISOString());
33
- const values = data.map((point) => point.currencies[to].value);
34
- const average = values.reduce((sum, value) => sum + value, 0) / values.length;
35
- const squaredDeviations = values.map((value) => (value - average) ** 2);
36
- const variance = squaredDeviations.reduce((sum, deviation) => sum + deviation, 0) /
37
- values.length;
38
- const volatility = Math.sqrt(variance);
39
- const maximumPriceChange = volatility * QUOTE_DURATION_MINUTES;
40
- let conversionRateWithVolatilityMarkup;
41
- conversionRateWithVolatilityMarkup = conversionRate - maximumPriceChange;
42
- let exchangeRateMultiplier = 0.9997;
43
- if (from === common_1.Fiat.PEN) {
44
- exchangeRateMultiplier = 0.9987;
45
- }
28
+ let conversionRateWithMarkups = conversionRate;
29
+ // Time based markups
46
30
  if (from === common_1.Fiat.PEN || to === common_1.Fiat.PEN) {
47
31
  const now = new Date();
48
32
  const peruTime = new Date(now.toLocaleString("en-US", { timeZone: "America/Lima" }));
@@ -50,12 +34,9 @@ const getExchangeRates = (from, to, volatilitySafeguard) => __awaiter(void 0, vo
50
34
  const minutes = peruTime.getMinutes();
51
35
  // Check if it's between 2:10 PM and 12:00 AM
52
36
  if ((hours === 14 && minutes >= 10) || hours > 14 || hours === 0) {
53
- exchangeRateMultiplier = 0.997;
37
+ conversionRateWithMarkups = conversionRate * 0.997;
54
38
  }
55
39
  }
56
- if (from === common_1.Fiat.USD) {
57
- exchangeRateMultiplier = 0.999;
58
- }
59
40
  if ((from === common_1.Fiat.MXN && to === common_1.Fiat.USD) ||
60
41
  (from === common_1.Fiat.USD && to === common_1.Fiat.MXN)) {
61
42
  const now = new Date();
@@ -66,21 +47,31 @@ const getExchangeRates = (from, to, volatilitySafeguard) => __awaiter(void 0, vo
66
47
  if ((hours === 14 && minutes >= 30) ||
67
48
  (hours > 14 && hours < 18) ||
68
49
  (hours === 18 && minutes === 0)) {
69
- exchangeRateMultiplier = 0.997;
50
+ conversionRateWithMarkups = conversionRate * 0.997;
70
51
  }
71
52
  else if (hours >= 0 && hours < 8) {
72
- exchangeRateMultiplier = 0.996;
53
+ conversionRateWithMarkups = conversionRate * 0.996;
73
54
  }
74
55
  }
75
- const conversionRateWithRioMarkup = to === from ? conversionRate : conversionRate * exchangeRateMultiplier;
76
- const conversionRateWithAllMarkups = to === from
77
- ? conversionRateWithVolatilityMarkup
78
- : conversionRateWithVolatilityMarkup * exchangeRateMultiplier;
56
+ let safestPrice = conversionRateWithMarkups;
57
+ // Volatility based markups
58
+ const { startDate, endDate } = getDateRange();
59
+ const data = yield currencyAPIClient.getHistoricalExchangeRates(from, to, startDate.toISOString(), endDate.toISOString());
60
+ const values = data.map((point) => point.currencies[to].value);
61
+ const average = values.reduce((sum, value) => sum + value, 0) / values.length;
62
+ const squaredDeviations = values.map((value) => (value - average) ** 2);
63
+ const variance = squaredDeviations.reduce((sum, deviation) => sum + deviation, 0) /
64
+ values.length;
65
+ const volatility = Math.sqrt(variance);
66
+ const maximumPriceChange = volatility * QUOTE_DURATION_MINUTES;
67
+ safestPrice = safestPrice - maximumPriceChange;
68
+ // Bank based markups
69
+ let exchangeRateMultiplier = from === common_1.Fiat.PEN ? 0.9987 : 0.9997;
70
+ safestPrice = safestPrice * exchangeRateMultiplier;
79
71
  return {
80
72
  conversionRate,
81
- conversionRateWithVolatilityMarkup,
82
- conversionRateWithRioMarkup,
83
- conversionRateWithAllMarkups,
73
+ conversionRateWithMarkups,
74
+ safestPrice,
84
75
  };
85
76
  });
86
77
  exports.getExchangeRates = getExchangeRates;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.1031",
3
+ "version": "1.0.1032",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "@aws-sdk/client-s3": "^3.297.0",
25
25
  "@everapi/currencyapi-js": "^1.0.6",
26
26
  "@google-cloud/storage": "^6.9.5",
27
- "@riocrypto/common": "^1.0.887",
27
+ "@riocrypto/common": "^1.0.889",
28
28
  "@types/express": "^4.17.13",
29
29
  "axios": "^0.27.2",
30
30
  "ccxt": "^3.0.30",