@riocrypto/common-server 1.0.790 → 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) {
@@ -1,6 +1,6 @@
1
1
  import { Country, Fiat, Processor, User } from "@riocrypto/common";
2
2
  import { Mongoose } from "mongoose";
3
- export declare const getPlatformAndPartnerFees: (mongoose: Mongoose, amountFiat: number, fiat: Fiat, processor: Processor, country: Country, partnerId?: string, brokerId?: string, user?: User, markup?: number, discount?: number) => Promise<{
3
+ export declare const getPlatformAndPartnerFees: (mongoose: Mongoose, conversionRateToUSD: number, amountFiat: number, fiat: Fiat, processor: Processor, country: Country, partnerId?: string, brokerId?: string, user?: User, markup?: number, discount?: number) => Promise<{
4
4
  platformFeeFiat: number;
5
5
  platformFeeFiatTax: number;
6
6
  partnerFeeFiat: number;
@@ -12,19 +12,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getPlatformAndPartnerFees = void 0;
13
13
  const rio_settings_1 = require("../models/rio-settings");
14
14
  const common_1 = require("@riocrypto/common");
15
- const kapstar_client_1 = require("../clients/kapstar-client");
16
- const currency_api_client_1 = require("../clients/currency-api-client");
17
15
  const partner_1 = require("../models/partner");
18
16
  const user_1 = require("../models/user");
19
17
  const calculate_aggregate_volume_1 = require("./calculate-aggregate-volume");
20
- const getPlatformAndPartnerFees = (mongoose, amountFiat, fiat, processor, country, partnerId, brokerId, user, markup, discount) => __awaiter(void 0, void 0, void 0, function* () {
18
+ const getPlatformAndPartnerFees = (mongoose, conversionRateToUSD, amountFiat, fiat, processor, country, partnerId, brokerId, user, markup, discount) => __awaiter(void 0, void 0, void 0, function* () {
21
19
  var _a, _b;
22
20
  let platformFeeFiat;
23
21
  let platformFeeFiatTax = 0;
24
22
  let partnerFeeFiat = 0;
25
23
  let feesComped = false;
26
24
  let amountUSD;
27
- let conversionRateToUSD = 0;
28
25
  const RioSettings = (0, rio_settings_1.buildRioSettings)(mongoose);
29
26
  const rioSettings = yield RioSettings.findOne();
30
27
  if (!rioSettings) {
@@ -33,17 +30,6 @@ const getPlatformAndPartnerFees = (mongoose, amountFiat, fiat, processor, countr
33
30
  if (discount && markup) {
34
31
  throw new Error("Cannot specify markup and discount");
35
32
  }
36
- if (processor === common_1.Processor.Kapstar) {
37
- const kapstarClient = yield (0, kapstar_client_1.buildKapstarClient)();
38
- conversionRateToUSD = yield kapstarClient.getBRLExchangeRateUSD();
39
- }
40
- else if (fiat !== common_1.Fiat.USD) {
41
- const currencyAPIClient = yield (0, currency_api_client_1.buildCurrencyAPIClient)();
42
- conversionRateToUSD = yield currencyAPIClient.convert(fiat, common_1.Fiat.USD);
43
- }
44
- else if (fiat === common_1.Fiat.USD) {
45
- conversionRateToUSD = 1;
46
- }
47
33
  amountUSD = amountFiat * conversionRateToUSD;
48
34
  if (!amountUSD) {
49
35
  throw new Error("Could not convert amount to USD");
@@ -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.790",
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;