@riocrypto/common 1.0.433 → 1.0.436

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.
@@ -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,8 @@
1
+ import { Fiat } from "../types/fiat";
2
+ declare class FixerClient {
3
+ private apiKey;
4
+ constructor(apiKey: string);
5
+ convert(from: Fiat, to: Fiat): Promise<number>;
6
+ }
7
+ export declare const fixerClient: FixerClient;
8
+ export {};
@@ -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,7 @@ 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 "./models/ccxt-order";
159
+ export * from "./clients/bitstamp-client";
160
+ export * from "./clients/ccxt-wrapper";
161
+ export * from "./clients/fixer-client";
package/build/index.js CHANGED
@@ -172,3 +172,7 @@ __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("./models/ccxt-order"), exports);
176
+ __exportStar(require("./clients/bitstamp-client"), exports);
177
+ __exportStar(require("./clients/ccxt-wrapper"), exports);
178
+ __exportStar(require("./clients/fixer-client"), exports);
@@ -0,0 +1,8 @@
1
+ import mongoose from "mongoose";
2
+ interface CCXTOrderDoc extends mongoose.Document {
3
+ orderId: string;
4
+ status: string;
5
+ ccxtId: string;
6
+ }
7
+ declare const buildCCXTOrder: (mongoose: typeof mongoose) => void;
8
+ export { buildCCXTOrder, CCXTOrderDoc };
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildCCXTOrder = void 0;
4
+ const buildCCXTOrder = (mongoose) => {
5
+ const CCXTOrderSchema = new mongoose.Schema({
6
+ orderId: {
7
+ type: String,
8
+ required: true,
9
+ },
10
+ ccxtId: {
11
+ type: String,
12
+ required: true,
13
+ },
14
+ status: {
15
+ type: String,
16
+ required: true,
17
+ },
18
+ }, {
19
+ toJSON: {
20
+ transform(doc, ret) {
21
+ ret.id = ret._id;
22
+ delete ret._id;
23
+ delete ret.__v;
24
+ },
25
+ },
26
+ });
27
+ CCXTOrderSchema.statics.build = (attrs) => {
28
+ return new CCXTOrder(attrs);
29
+ };
30
+ const CCXTOrder = mongoose.model("CCXTOrder", CCXTOrderSchema);
31
+ };
32
+ exports.buildCCXTOrder = buildCCXTOrder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common",
3
- "version": "1.0.433",
3
+ "version": "1.0.436",
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",