@riocrypto/common-server 1.0.1298 → 1.0.1301
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/fireblocks-client.js +26 -13
- package/build/helpers/get-exchange-rates.d.ts +6 -0
- package/build/helpers/get-exchange-rates.js +83 -0
- package/build/helpers/get-network-fees.d.ts +5 -0
- package/build/helpers/get-network-fees.js +37 -0
- package/build/helpers/get-processor-fees.d.ts +2 -0
- package/build/helpers/get-processor-fees.js +49 -0
- package/build/index.d.ts +3 -0
- package/build/index.js +3 -0
- package/package.json +1 -1
|
@@ -8,11 +8,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.buildFireblocksClient = void 0;
|
|
13
16
|
const fireblocks_sdk_1 = require("fireblocks-sdk");
|
|
14
17
|
const common_1 = require("@riocrypto/common");
|
|
15
18
|
const secret_manager_client_1 = require("./secret-manager-client");
|
|
19
|
+
const node_cache_1 = __importDefault(require("node-cache"));
|
|
20
|
+
const cache = new node_cache_1.default({ stdTTL: 60 });
|
|
16
21
|
class FireblocksClient {
|
|
17
22
|
constructor(privateKey, apiKey) {
|
|
18
23
|
this.fireblocks = new fireblocks_sdk_1.FireblocksSDK(privateKey, apiKey, "https://api.fireblocks.io");
|
|
@@ -90,23 +95,31 @@ class FireblocksClient {
|
|
|
90
95
|
}
|
|
91
96
|
getTotalVaultUSDBalance(vaultId) {
|
|
92
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
-
const vaultAccount = yield this.fireblocks.getVaultAccountById(vaultId);
|
|
94
98
|
let balance = 0;
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
99
|
+
const cachedResponse = cache.get(`this.fireblocks.getVaultAccountById(${vaultId})`);
|
|
100
|
+
if (cachedResponse) {
|
|
101
|
+
balance = cachedResponse;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const vaultAccount = yield this.fireblocks.getVaultAccountById(vaultId);
|
|
105
|
+
if (vaultAccount.assets) {
|
|
106
|
+
for (const asset of vaultAccount.assets) {
|
|
107
|
+
if ([
|
|
108
|
+
common_1.Crypto.USDC,
|
|
109
|
+
common_1.Crypto.USDCPolygon,
|
|
110
|
+
common_1.Crypto.USDCPolygonBridged,
|
|
111
|
+
common_1.Crypto.USDCSolana,
|
|
112
|
+
common_1.Crypto.USDTEthereum,
|
|
113
|
+
common_1.Crypto.USDTPolygon,
|
|
114
|
+
common_1.Crypto.USDTTron,
|
|
115
|
+
].includes(asset.id)) {
|
|
116
|
+
balance += Number(asset.available || 0);
|
|
117
|
+
}
|
|
107
118
|
}
|
|
108
119
|
}
|
|
120
|
+
cache.set(`this.fireblocks.getVaultAccountById(${vaultId})`, balance);
|
|
109
121
|
}
|
|
122
|
+
console.log("Balance is ", balance);
|
|
110
123
|
return balance;
|
|
111
124
|
});
|
|
112
125
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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 kapstar_client_1 = require("../clients/kapstar-client");
|
|
16
|
+
const QUOTE_DURATION_MINUTES = 10;
|
|
17
|
+
const getExchangeRates = (from, to, noMarkups) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const currencyAPIClient = yield (0, currency_api_client_1.buildCurrencyAPIClient)();
|
|
19
|
+
const kapstarClient = yield (0, kapstar_client_1.buildKapstarClient)();
|
|
20
|
+
const conversionRate = to === common_1.Fiat.BRL
|
|
21
|
+
? yield kapstarClient.getBRLExchangeRateUSD()
|
|
22
|
+
: yield currencyAPIClient.convert(from, to);
|
|
23
|
+
if (noMarkups) {
|
|
24
|
+
return {
|
|
25
|
+
conversionRate,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
let conversionRateWithMarkups = conversionRate;
|
|
29
|
+
// Time based markups
|
|
30
|
+
if (from === common_1.Fiat.PEN || to === common_1.Fiat.PEN) {
|
|
31
|
+
const now = new Date();
|
|
32
|
+
const peruTime = new Date(now.toLocaleString("en-US", { timeZone: "America/Lima" }));
|
|
33
|
+
const hours = peruTime.getHours();
|
|
34
|
+
const minutes = peruTime.getMinutes();
|
|
35
|
+
// Check if it's between 2:10 PM and 12:00 AM
|
|
36
|
+
if ((hours === 14 && minutes >= 10) || hours > 14 || hours === 0) {
|
|
37
|
+
conversionRateWithMarkups = conversionRate * 0.997;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if ((from === common_1.Fiat.MXN && to === common_1.Fiat.USD) ||
|
|
41
|
+
(from === common_1.Fiat.USD && to === common_1.Fiat.MXN)) {
|
|
42
|
+
const now = new Date();
|
|
43
|
+
const mexicoCityTime = new Date(now.toLocaleString("en-US", { timeZone: "America/Mexico_City" }));
|
|
44
|
+
const hours = mexicoCityTime.getHours();
|
|
45
|
+
const minutes = mexicoCityTime.getMinutes();
|
|
46
|
+
if ((hours === 15 && minutes >= 45) ||
|
|
47
|
+
(hours > 15 && hours < 24) ||
|
|
48
|
+
(hours === 0 && minutes === 0)) {
|
|
49
|
+
conversionRateWithMarkups = conversionRate * 0.997;
|
|
50
|
+
}
|
|
51
|
+
else if (hours >= 0 && hours < 7) {
|
|
52
|
+
conversionRateWithMarkups = conversionRate * 0.996;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
console.log("conversion rate with markups", conversionRateWithMarkups);
|
|
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 / 2);
|
|
67
|
+
safestPrice = safestPrice - maximumPriceChange;
|
|
68
|
+
// Bank based markups
|
|
69
|
+
let exchangeRateMultiplier = from === common_1.Fiat.PEN ? 0.9987 : 0.9997;
|
|
70
|
+
safestPrice = safestPrice * exchangeRateMultiplier;
|
|
71
|
+
return {
|
|
72
|
+
conversionRate,
|
|
73
|
+
conversionRateWithMarkups,
|
|
74
|
+
safestPrice,
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
exports.getExchangeRates = getExchangeRates;
|
|
78
|
+
function getDateRange() {
|
|
79
|
+
const endDate = new Date(); // Current time
|
|
80
|
+
// Subtract 10 minutes from the end_date
|
|
81
|
+
const startDate = new Date(endDate.getTime() - 10 * 60 * 1000);
|
|
82
|
+
return { startDate, endDate };
|
|
83
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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.getNetworkFees = void 0;
|
|
13
|
+
const common_1 = require("@riocrypto/common");
|
|
14
|
+
const ccxt_client_1 = require("../clients/ccxt-client");
|
|
15
|
+
const fireblocks_client_1 = require("../clients/fireblocks-client");
|
|
16
|
+
const getNetworkFees = (crypto, conversionRateToUSD) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
const address = crypto === common_1.Crypto.USDCSolana
|
|
18
|
+
? "HN7cABqLq46Es1jh92dQQisAq662SmxELLLsHHe4YWrH"
|
|
19
|
+
: "0x66ECBd38905CC32249ddd6f4f0B4A3DdDDCbd608";
|
|
20
|
+
const fireblocksClient = yield (0, fireblocks_client_1.buildFireblocksClient)();
|
|
21
|
+
// Hacky way bc ccxt doesn't support TRON, so quote in MATIC
|
|
22
|
+
const cryptoToWithdraw = crypto === common_1.Crypto.USDTTron ? common_1.Crypto.USDCPolygon : crypto;
|
|
23
|
+
const withdrawFee = yield fireblocksClient.estimateWithdrawFee(cryptoToWithdraw, address, 100);
|
|
24
|
+
if (!withdrawFee) {
|
|
25
|
+
console.log("No withdraw fee");
|
|
26
|
+
throw new common_1.GenericInternalError();
|
|
27
|
+
}
|
|
28
|
+
// Hacky way bc ccxt doesn't support TRON, so quote in MATIC
|
|
29
|
+
const gasSymbol = crypto === common_1.Crypto.USDTTron
|
|
30
|
+
? "MATIC"
|
|
31
|
+
: new common_1.CryptoAsset(crypto).getGasSymbol();
|
|
32
|
+
const withdrawFeeUSDResponse = yield ccxt_client_1.ccxtWrapper.getSellQuote(gasSymbol, common_1.Fiat.USD, undefined, Number(withdrawFee));
|
|
33
|
+
const withdrawFeeInBaseCrypto = withdrawFeeUSDResponse.amountFiat;
|
|
34
|
+
const withdrawFeeInBaseFiat = Number(withdrawFeeUSDResponse.amountFiat) * (1 / conversionRateToUSD);
|
|
35
|
+
return { withdrawFeeInBaseFiat, withdrawFeeInBaseCrypto };
|
|
36
|
+
});
|
|
37
|
+
exports.getNetworkFees = getNetworkFees;
|
|
@@ -0,0 +1,49 @@
|
|
|
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.getProcessorFees = void 0;
|
|
13
|
+
const common_1 = require("@riocrypto/common");
|
|
14
|
+
const getProcessorFees = (processor, amountFiat, conversionRateFromUSD, side, fiat, USBankTransferMethod) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
if (processor === common_1.Processor.Bridge) {
|
|
16
|
+
const wireFee = USBankTransferMethod === "wire" ? 20 : 0;
|
|
17
|
+
const x = (amountFiat * (1 / conversionRateFromUSD) * 0.0015 + wireFee) *
|
|
18
|
+
conversionRateFromUSD;
|
|
19
|
+
return x;
|
|
20
|
+
}
|
|
21
|
+
else if (processor === common_1.Processor.Kapstar) {
|
|
22
|
+
return amountFiat * 0.006;
|
|
23
|
+
}
|
|
24
|
+
else if (processor === common_1.Processor.Interbank) {
|
|
25
|
+
if (side === common_1.Side.Buy) {
|
|
26
|
+
return amountFiat * 0.005;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
let baseFee = amountFiat * 0.005;
|
|
30
|
+
if (fiat === common_1.Fiat.PEN) {
|
|
31
|
+
baseFee += 4.47;
|
|
32
|
+
}
|
|
33
|
+
else if (fiat === common_1.Fiat.USD) {
|
|
34
|
+
baseFee += 1.16 * conversionRateFromUSD;
|
|
35
|
+
}
|
|
36
|
+
return baseFee;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else if (processor === common_1.Processor.SWIFT) {
|
|
40
|
+
return 25 * conversionRateFromUSD;
|
|
41
|
+
}
|
|
42
|
+
else if (processor === common_1.Processor.SPID) {
|
|
43
|
+
return 4 * conversionRateFromUSD;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
exports.getProcessorFees = getProcessorFees;
|
package/build/index.d.ts
CHANGED
|
@@ -60,7 +60,10 @@ export * from "./clients/cosigner-client";
|
|
|
60
60
|
export * from "./clients/bitso-client";
|
|
61
61
|
export * from "./clients/circle-client";
|
|
62
62
|
export * from "./helpers/get-user-helper";
|
|
63
|
+
export * from "./helpers/get-processor-fees";
|
|
64
|
+
export * from "./helpers/get-network-fees";
|
|
63
65
|
export * from "./helpers/calculate-aggregate-volume-and-revenue";
|
|
64
66
|
export * from "./helpers/calculate-weekly-volume-and-revenue";
|
|
67
|
+
export * from "./helpers/get-exchange-rates";
|
|
65
68
|
export * from "./helpers/get-user";
|
|
66
69
|
export * from "./helpers/unescape";
|
package/build/index.js
CHANGED
|
@@ -76,7 +76,10 @@ __exportStar(require("./clients/cosigner-client"), exports);
|
|
|
76
76
|
__exportStar(require("./clients/bitso-client"), exports);
|
|
77
77
|
__exportStar(require("./clients/circle-client"), exports);
|
|
78
78
|
__exportStar(require("./helpers/get-user-helper"), exports);
|
|
79
|
+
__exportStar(require("./helpers/get-processor-fees"), exports);
|
|
80
|
+
__exportStar(require("./helpers/get-network-fees"), exports);
|
|
79
81
|
__exportStar(require("./helpers/calculate-aggregate-volume-and-revenue"), exports);
|
|
80
82
|
__exportStar(require("./helpers/calculate-weekly-volume-and-revenue"), exports);
|
|
83
|
+
__exportStar(require("./helpers/get-exchange-rates"), exports);
|
|
81
84
|
__exportStar(require("./helpers/get-user"), exports);
|
|
82
85
|
__exportStar(require("./helpers/unescape"), exports);
|