@riocrypto/common-server 1.0.791 → 1.0.793

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.
@@ -13,7 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.buildCurrencyAPIClient = void 0;
16
- const common_1 = require("@riocrypto/common");
17
16
  const axios_1 = __importDefault(require("axios"));
18
17
  const secret_manager_client_1 = require("./secret-manager-client");
19
18
  const node_cache_1 = __importDefault(require("node-cache"));
@@ -24,8 +23,8 @@ class CurrencyAPIClient {
24
23
  }
25
24
  convert(from, to) {
26
25
  return __awaiter(this, void 0, void 0, function* () {
26
+ let exchangeRate = 0;
27
27
  try {
28
- let exchangeRate;
29
28
  const cachedResponse = cache.get(`v3/latest?apikey=${this.apiKey}&base_currency=${from}&currencies=${to}`);
30
29
  if (cachedResponse) {
31
30
  exchangeRate = cachedResponse.data[to].value;
@@ -35,18 +34,6 @@ class CurrencyAPIClient {
35
34
  exchangeRate = currencyAPIResponse.data.data[to].value;
36
35
  cache.set(`v3/latest?apikey=${this.apiKey}&base_currency=${from}&currencies=${to}`, currencyAPIResponse.data);
37
36
  }
38
- let exchangeRateMultiplier = 0.999;
39
- if (from === common_1.Fiat.PEN || to === common_1.Fiat.PEN) {
40
- const now = new Date();
41
- const peruTimezoneOffset = -5; // Peru is in the UTC-5 time zone
42
- const peruTime = new Date(now.getTime() + peruTimezoneOffset * 60 * 60 * 1000);
43
- const isPastSixPmInPeru = peruTime.getHours() >= 18;
44
- if (isPastSixPmInPeru) {
45
- exchangeRateMultiplier = 0.997;
46
- }
47
- }
48
- const rioExchangeRate = to === from ? exchangeRate : exchangeRate * exchangeRateMultiplier;
49
- return rioExchangeRate;
50
37
  }
51
38
  catch (e) {
52
39
  const cachedResponse = cache.get(`v3/latest?apikey=${this.apiKey}&base_currency=${from}&currencies=${to}`);
@@ -54,19 +41,11 @@ class CurrencyAPIClient {
54
41
  throw new Error(`Unable to get exchange rate from currencyAPI for ${from} to ${to}`);
55
42
  }
56
43
  const exchangeRate = cachedResponse.data[to].value;
57
- let exchangeRateMultiplier = 0.999;
58
- if (from === common_1.Fiat.PEN || to === common_1.Fiat.PEN) {
59
- const now = new Date();
60
- const peruTimezoneOffset = -5; // Peru is in the UTC-5 time zone
61
- const peruTime = new Date(now.getTime() + peruTimezoneOffset * 60 * 60 * 1000);
62
- const isPastSixPmInPeru = peruTime.getHours() >= 18;
63
- if (isPastSixPmInPeru) {
64
- exchangeRateMultiplier = 0.997;
65
- }
66
- }
67
- const rioExchangeRate = to === from ? exchangeRate : exchangeRate * exchangeRateMultiplier;
68
- return rioExchangeRate;
69
44
  }
45
+ if (!exchangeRate) {
46
+ throw new Error("Unable to get exchange rate");
47
+ }
48
+ return exchangeRate;
70
49
  });
71
50
  }
72
51
  getHistoricalExchangeRates(from, to, start, end) {
@@ -0,0 +1,6 @@
1
+ import { Fiat } from "@riocrypto/common";
2
+ export declare const getExchangeRates: (from: Fiat, to: Fiat) => Promise<{
3
+ conversionRate: number;
4
+ conversionRateWithVolatilityMarkup: number;
5
+ conversionRateWithAllMarkups: number;
6
+ }>;
@@ -0,0 +1,73 @@
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.getExchangeRates = void 0;
13
+ const common_1 = require("@riocrypto/common");
14
+ const currency_api_client_1 = require("../clients/currency-api-client");
15
+ const QUOTE_DURATION_MINUTES = 10;
16
+ const getExchangeRates = (from, to) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const currencyAPIClient = yield (0, currency_api_client_1.buildCurrencyAPIClient)();
18
+ const { startDate, endDate } = getDateRange();
19
+ const data = yield currencyAPIClient.getHistoricalExchangeRates(from, to, startDate.toISOString(), endDate.toISOString());
20
+ const values = data.map((point) => point.currencies.MXN.value);
21
+ const average = values.reduce((sum, value) => sum + value, 0) / values.length;
22
+ const squaredDeviations = values.map((value) => (value - average) ** 2);
23
+ const variance = squaredDeviations.reduce((sum, deviation) => sum + deviation, 0) /
24
+ values.length;
25
+ const volatility = Math.sqrt(variance);
26
+ const currentPrice = values[values.length - 1];
27
+ const previousPrice = values[values.length - 2];
28
+ const priceChange = currentPrice - previousPrice;
29
+ let priceDirection;
30
+ if (Math.abs(priceChange) > volatility) {
31
+ priceDirection = priceChange > 0 ? "increase" : "decrease";
32
+ }
33
+ else {
34
+ priceDirection = "stable";
35
+ }
36
+ const maximumPriceChange = volatility * QUOTE_DURATION_MINUTES;
37
+ const conversionRate = yield currencyAPIClient.convert(from, to);
38
+ let conversionRateWithVolatilityMarkup;
39
+ if (priceDirection === "increase") {
40
+ conversionRateWithVolatilityMarkup = conversionRate + maximumPriceChange;
41
+ }
42
+ else if (priceDirection === "decrease") {
43
+ conversionRateWithVolatilityMarkup = conversionRate - maximumPriceChange;
44
+ }
45
+ else {
46
+ conversionRateWithVolatilityMarkup = conversionRate;
47
+ }
48
+ let exchangeRateMultiplier = 0.999;
49
+ if (from === common_1.Fiat.PEN || to === common_1.Fiat.PEN) {
50
+ const now = new Date();
51
+ const peruTimezoneOffset = -5; // Peru is in the UTC-5 time zone
52
+ const peruTime = new Date(now.getTime() + peruTimezoneOffset * 60 * 60 * 1000);
53
+ const isPastSixPmInPeru = peruTime.getHours() >= 18;
54
+ if (isPastSixPmInPeru) {
55
+ exchangeRateMultiplier = 0.997;
56
+ }
57
+ }
58
+ const conversionRateWithAllMarkups = to === from
59
+ ? conversionRateWithVolatilityMarkup
60
+ : conversionRateWithVolatilityMarkup * exchangeRateMultiplier;
61
+ return {
62
+ conversionRate,
63
+ conversionRateWithVolatilityMarkup,
64
+ conversionRateWithAllMarkups,
65
+ };
66
+ });
67
+ exports.getExchangeRates = getExchangeRates;
68
+ function getDateRange() {
69
+ const endDate = new Date(); // Current time
70
+ // Subtract 10 minutes from the end_date
71
+ const startDate = new Date(endDate.getTime() - QUOTE_DURATION_MINUTES * 60 * 1000);
72
+ return { startDate, endDate };
73
+ }
package/build/index.d.ts CHANGED
@@ -46,5 +46,4 @@ 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";
49
+ export * from "./helpers/getExchangeRates";
package/build/index.js CHANGED
@@ -62,5 +62,4 @@ __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);
65
+ __exportStar(require("./helpers/getExchangeRates"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.791",
3
+ "version": "1.0.793",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -1 +0,0 @@
1
- export declare const calculateExchangeRateWithVolatilityMarkup: (conversionRateFromUSD: number, volatility: number, minutes: number, priceDirection: "increase" | "decrease" | "stable") => number;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateExchangeRateWithVolatilityMarkup = void 0;
4
- const calculateExchangeRateWithVolatilityMarkup = (conversionRateFromUSD, volatility, minutes, priceDirection) => {
5
- const maximumPriceChange = volatility * minutes;
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;
@@ -1,7 +0,0 @@
1
- import { Fiat } from "@riocrypto/common";
2
- interface VolatilityResult {
3
- volatility: number;
4
- priceDirection: string;
5
- }
6
- export declare const calculateVolatility: (from: Fiat, to: Fiat, start: Date, end: Date) => Promise<VolatilityResult>;
7
- export {};
@@ -1,38 +0,0 @@
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;