@riocrypto/common 1.0.432 → 1.0.435
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/bitstamp-client.d.ts +9 -0
- package/build/clients/bitstamp-client.js +44 -0
- package/build/clients/ccxt-wrapper.d.ts +38 -0
- package/build/clients/ccxt-wrapper.js +117 -0
- package/build/clients/fixer-client.d.ts +8 -0
- package/build/clients/fixer-client.js +34 -0
- package/build/index.d.ts +3 -0
- package/build/index.js +3 -0
- package/build/middlewares/authorize.js +24 -18
- package/package.json +2 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Crypto } from "../types/crypto";
|
|
2
|
+
declare class BitstampClient {
|
|
3
|
+
private apiKey;
|
|
4
|
+
private secret;
|
|
5
|
+
constructor(apiKey: string, secret: string);
|
|
6
|
+
getWithdrawFee(crypto: Crypto): number | null;
|
|
7
|
+
}
|
|
8
|
+
export declare const bitstampClient: BitstampClient;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bitstampClient = void 0;
|
|
4
|
+
const crypto_1 = require("../types/crypto");
|
|
5
|
+
class BitstampClient {
|
|
6
|
+
constructor(apiKey, secret) {
|
|
7
|
+
this.apiKey = apiKey;
|
|
8
|
+
this.secret = secret;
|
|
9
|
+
}
|
|
10
|
+
getWithdrawFee(crypto) {
|
|
11
|
+
if (crypto === crypto_1.Crypto.BTC) {
|
|
12
|
+
return 0.0005;
|
|
13
|
+
}
|
|
14
|
+
else if (crypto === crypto_1.Crypto.ETH) {
|
|
15
|
+
return 0.006;
|
|
16
|
+
}
|
|
17
|
+
else if (crypto === crypto_1.Crypto.USDC) {
|
|
18
|
+
return 20;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// private getHeaders(data: string, url: string, path: string, method: string) {
|
|
26
|
+
// const nonce = uuidv4();
|
|
27
|
+
// const timestamp = Date.now();
|
|
28
|
+
// let message = `${
|
|
29
|
+
// this.apiKey
|
|
30
|
+
// }${method}${url}${path}${""}${""}${nonce}${timestamp}${"v2"}${data}`;
|
|
31
|
+
// const headers = {
|
|
32
|
+
// "X-Auth": "BITSTAMP " + this.apiKey,
|
|
33
|
+
// "X-Auth-Signature": crypto
|
|
34
|
+
// .createHmac("sha256", this.secret)
|
|
35
|
+
// .update(message)
|
|
36
|
+
// .digest("hex"),
|
|
37
|
+
// "X-Auth-Nonce": nonce,
|
|
38
|
+
// "X-Auth-Timestamp": timestamp,
|
|
39
|
+
// "X-Auth-Version": "v2",
|
|
40
|
+
// };
|
|
41
|
+
// return headers;
|
|
42
|
+
// }
|
|
43
|
+
// }
|
|
44
|
+
exports.bitstampClient = new BitstampClient(process.env.BITSTAMP_API_KEY, process.env.BITSTAMP_SECRET);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CCXTLiquidityProvider } from "../types/ccxt-liquidity-provider";
|
|
2
|
+
import { Fiat } from "../types/fiat";
|
|
3
|
+
import { Side } from "../types/side";
|
|
4
|
+
declare class CCXTWrapper {
|
|
5
|
+
constructor();
|
|
6
|
+
createBuyOrder(amount: number, crypto: Crypto, fiat: Fiat, ccxtLiquidityProvider: CCXTLiquidityProvider): Promise<{
|
|
7
|
+
ccxtId: any;
|
|
8
|
+
price: any;
|
|
9
|
+
amountCrypto: any;
|
|
10
|
+
amountFiat: number;
|
|
11
|
+
crypto: Crypto;
|
|
12
|
+
fiat: Fiat;
|
|
13
|
+
side: Side;
|
|
14
|
+
}>;
|
|
15
|
+
createSellOrder(amount: number, crypto: Crypto, fiat: Fiat, ccxtLiquidityProvider: CCXTLiquidityProvider): Promise<{
|
|
16
|
+
ccxtId: any;
|
|
17
|
+
price: any;
|
|
18
|
+
amountCrypto: number;
|
|
19
|
+
amountFiat: number;
|
|
20
|
+
crypto: Crypto;
|
|
21
|
+
fiat: Fiat;
|
|
22
|
+
side: Side;
|
|
23
|
+
}>;
|
|
24
|
+
transfer(side: Side, amount: number, crypto: Crypto, cryptoAddress: string, ccxtLiquidityProvider: CCXTLiquidityProvider): Promise<void>;
|
|
25
|
+
getBuyQuote(amount: number, crypto: Crypto, fiat: Fiat): Promise<{
|
|
26
|
+
price: number;
|
|
27
|
+
amountCrypto: number;
|
|
28
|
+
amountFiat: number;
|
|
29
|
+
crypto: Crypto;
|
|
30
|
+
fiat: Fiat;
|
|
31
|
+
side: Side;
|
|
32
|
+
}>;
|
|
33
|
+
private getOptimalExchange;
|
|
34
|
+
private round;
|
|
35
|
+
private getExchange;
|
|
36
|
+
}
|
|
37
|
+
export declare const ccxtWrapper: CCXTWrapper;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ccxtWrapper = void 0;
|
|
16
|
+
const ccxt_1 = __importDefault(require("ccxt"));
|
|
17
|
+
const ccxt_liquidity_provider_1 = require("../types/ccxt-liquidity-provider");
|
|
18
|
+
const side_1 = require("../types/side");
|
|
19
|
+
class CCXTWrapper {
|
|
20
|
+
constructor() { }
|
|
21
|
+
createBuyOrder(amount, crypto, fiat, ccxtLiquidityProvider) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const exchange = yield this.getExchange(ccxtLiquidityProvider);
|
|
24
|
+
const order = yield exchange.createOrder(`${crypto}/${fiat}`, "market", "buy", amount);
|
|
25
|
+
return {
|
|
26
|
+
ccxtId: order.id,
|
|
27
|
+
price: order.price,
|
|
28
|
+
amountCrypto: order.amount,
|
|
29
|
+
amountFiat: amount,
|
|
30
|
+
crypto,
|
|
31
|
+
fiat,
|
|
32
|
+
side: side_1.Side.Buy,
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
createSellOrder(amount, crypto, fiat, ccxtLiquidityProvider) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
const exchange = yield this.getExchange(ccxtLiquidityProvider);
|
|
40
|
+
const order = yield exchange.createOrder(`${crypto}/${fiat}`, "market", "sell", amount);
|
|
41
|
+
return {
|
|
42
|
+
ccxtId: order.id,
|
|
43
|
+
price: order.price,
|
|
44
|
+
amountCrypto: amount,
|
|
45
|
+
amountFiat: order.amount * order.price,
|
|
46
|
+
crypto,
|
|
47
|
+
fiat,
|
|
48
|
+
side: side_1.Side.Sell,
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
transfer(side, amount, crypto, cryptoAddress, ccxtLiquidityProvider) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
const exchange = yield this.getExchange(ccxtLiquidityProvider);
|
|
55
|
+
const params = ccxtLiquidityProvider === ccxt_liquidity_provider_1.CCXTLiquidityProvider.Kraken
|
|
56
|
+
? {
|
|
57
|
+
key: "Metamask",
|
|
58
|
+
}
|
|
59
|
+
: undefined;
|
|
60
|
+
yield exchange.withdraw(crypto, this.round(amount, 8), cryptoAddress, undefined, params);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
getBuyQuote(amount, crypto, fiat) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
const exchange = yield this.getOptimalExchange(crypto);
|
|
66
|
+
if (!exchange)
|
|
67
|
+
throw new Error("Unable to get optimal exchange");
|
|
68
|
+
let ticker = yield exchange.fetchTicker(`${crypto}/${fiat}`);
|
|
69
|
+
if (!ticker.vwap)
|
|
70
|
+
throw new Error("Unable to get ticker vwap");
|
|
71
|
+
return {
|
|
72
|
+
price: ticker.vwap,
|
|
73
|
+
amountCrypto: amount / ticker.vwap,
|
|
74
|
+
amountFiat: amount,
|
|
75
|
+
crypto,
|
|
76
|
+
fiat,
|
|
77
|
+
side: side_1.Side.Buy,
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
getOptimalExchange(crypto) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
for (let exchangeId of ccxt_1.default.exchanges) {
|
|
84
|
+
if (exchangeId === "bitstamp") {
|
|
85
|
+
//@ts-ignore
|
|
86
|
+
const exchangeClass = ccxt_1.default[exchangeId], exchange = new exchangeClass({
|
|
87
|
+
apiKey: process.env[`${exchangeId.toLocaleUpperCase()}_API_KEY`],
|
|
88
|
+
secret: process.env[`${exchangeId.toLocaleUpperCase()}_SECRET`],
|
|
89
|
+
});
|
|
90
|
+
return exchange;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
round(value, precision) {
|
|
97
|
+
if (Number.isInteger(precision)) {
|
|
98
|
+
var shift = Math.pow(10, precision);
|
|
99
|
+
// Limited preventing decimal issue
|
|
100
|
+
return Math.round(value * shift + 0.00000000000001) / shift;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
return Math.round(value);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
getExchange(ccxtLiquidityProvider) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
const exchangeClass = ccxt_1.default[ccxtLiquidityProvider], exchange = new exchangeClass({
|
|
110
|
+
apiKey: process.env[`${ccxtLiquidityProvider.toLocaleUpperCase()}_API_KEY`],
|
|
111
|
+
secret: process.env[`${ccxtLiquidityProvider.toLocaleUpperCase()}_SECRET`],
|
|
112
|
+
});
|
|
113
|
+
return exchange;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.ccxtWrapper = new CCXTWrapper();
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.fixerClient = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
class FixerClient {
|
|
18
|
+
constructor(apiKey) {
|
|
19
|
+
this.apiKey = apiKey;
|
|
20
|
+
}
|
|
21
|
+
convert(from, to) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const fixerResponse = yield axios_1.default.get(`https://api.apilayer.com/fixer/latest?base=${from}&symbols=${to}`, {
|
|
24
|
+
headers: {
|
|
25
|
+
apikey: this.apiKey,
|
|
26
|
+
"Accept-Encoding": "gzip,deflate,compress",
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
const exchangeRate = fixerResponse.data.rates[to];
|
|
30
|
+
return exchangeRate;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.fixerClient = new FixerClient(process.env.FIXER_API_KEY);
|
package/build/index.d.ts
CHANGED
|
@@ -155,3 +155,6 @@ export * from "./models/bank-payout";
|
|
|
155
155
|
export * from "./models/order";
|
|
156
156
|
export * from "./models/partner";
|
|
157
157
|
export * from "./models/user";
|
|
158
|
+
export * from "./clients/bitstamp-client";
|
|
159
|
+
export * from "./clients/ccxt-wrapper";
|
|
160
|
+
export * from "./clients/fixer-client";
|
package/build/index.js
CHANGED
|
@@ -172,3 +172,6 @@ __exportStar(require("./models/bank-payout"), exports);
|
|
|
172
172
|
__exportStar(require("./models/order"), exports);
|
|
173
173
|
__exportStar(require("./models/partner"), exports);
|
|
174
174
|
__exportStar(require("./models/user"), exports);
|
|
175
|
+
__exportStar(require("./clients/bitstamp-client"), exports);
|
|
176
|
+
__exportStar(require("./clients/ccxt-wrapper"), exports);
|
|
177
|
+
__exportStar(require("./clients/fixer-client"), exports);
|
|
@@ -27,7 +27,7 @@ const missing_security_question_error_1 = require("../errors/missing-security-qu
|
|
|
27
27
|
const admin_only_error_1 = require("../errors/admin-only-error");
|
|
28
28
|
const not_broker_error_1 = require("../errors/not-broker-error");
|
|
29
29
|
const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
-
var _a, _b, _c, _d, _e, _f, _g, _h
|
|
30
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
31
31
|
let missingKYC = false;
|
|
32
32
|
if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Auth) ||
|
|
33
33
|
authorizationTypes.includes(authorization_type_1.AuthorizationType.UserNoKYC) ||
|
|
@@ -126,22 +126,25 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
|
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Broker)) {
|
|
129
|
-
let
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
129
|
+
let brokerBusinessUser;
|
|
130
|
+
try {
|
|
131
|
+
const BusinessUser = yield (0, business_user_1.buildBusinessUser)(mongoose);
|
|
132
|
+
brokerBusinessUser = yield BusinessUser.findOne({
|
|
133
|
+
authId: req.currentAuth.id,
|
|
134
|
+
attributes: { $e: user_attribute_1.UserAttribute.broker },
|
|
135
|
+
});
|
|
136
|
+
if (!brokerBusinessUser) {
|
|
137
|
+
throw new Error();
|
|
138
|
+
}
|
|
139
|
+
if ((brokerBusinessUser === null || brokerBusinessUser === void 0 ? void 0 : brokerBusinessUser.kycStatus) !== kyc_status_1.KYCStatus.businessApproved) {
|
|
140
|
+
missingKYC = true;
|
|
141
|
+
throw new Error();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
brokerBusinessUser = null;
|
|
143
146
|
}
|
|
144
|
-
const user =
|
|
147
|
+
const user = brokerBusinessUser;
|
|
145
148
|
if (user) {
|
|
146
149
|
req.user = user;
|
|
147
150
|
}
|
|
@@ -155,14 +158,17 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
|
|
|
155
158
|
}
|
|
156
159
|
if ((authorizationTypes.includes(authorization_type_1.AuthorizationType.User) ||
|
|
157
160
|
authorizationTypes.includes(authorization_type_1.AuthorizationType.UserNoKYC)) &&
|
|
158
|
-
((
|
|
161
|
+
((_g = req.cookies) === null || _g === void 0 ? void 0 : _g.jwt)) {
|
|
159
162
|
throw new missing_user_error_1.MissingUserError();
|
|
160
163
|
}
|
|
161
164
|
if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Admin) &&
|
|
162
165
|
authorizationTypes.length === 1 &&
|
|
163
|
-
((
|
|
166
|
+
((_h = req.cookies) === null || _h === void 0 ? void 0 : _h.jwt)) {
|
|
164
167
|
throw new admin_only_error_1.AdminOnlyError();
|
|
165
168
|
}
|
|
169
|
+
if (!req.user && authorizationTypes.includes(authorization_type_1.AuthorizationType.Broker)) {
|
|
170
|
+
throw new not_broker_error_1.NotBrokerError();
|
|
171
|
+
}
|
|
166
172
|
throw new not_authorized_error_1.NotAuthorizedError();
|
|
167
173
|
}
|
|
168
174
|
next();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@riocrypto/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.435",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"@aws-sdk/client-s3": "^3.297.0",
|
|
24
24
|
"@types/express": "^4.17.13",
|
|
25
25
|
"axios": "^0.27.2",
|
|
26
|
+
"ccxt": "^3.0.30",
|
|
26
27
|
"express": "^4.18.1",
|
|
27
28
|
"express-validator": "^6.14.2",
|
|
28
29
|
"jsonwebtoken": "^8.5.1",
|