@pioneer-platform/changelly-client 8.3.1
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/lib/client.d.ts +19 -0
- package/lib/client.js +101 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +221 -0
- package/package.json +33 -0
- package/tsconfig.json +13 -0
package/lib/client.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type CallbackFunction = (err: Error | null, response?: any) => void;
|
|
2
|
+
declare class Changelly {
|
|
3
|
+
private apiKey;
|
|
4
|
+
private apiSecret;
|
|
5
|
+
private client;
|
|
6
|
+
private _socket;
|
|
7
|
+
constructor(apiKey: string, apiSecret: string);
|
|
8
|
+
private _id;
|
|
9
|
+
private _sign;
|
|
10
|
+
private _request;
|
|
11
|
+
getCurrencies(callback: CallbackFunction): void;
|
|
12
|
+
createTransaction(from: string, to: string, address: string, amount: number, extraId: string | null, callback: CallbackFunction): void;
|
|
13
|
+
getMinAmount(from: string, to: string, callback: CallbackFunction): void;
|
|
14
|
+
getExchangeAmount(from: string, to: string, amount: number, callback: CallbackFunction): void;
|
|
15
|
+
getTransactions(limit: number, offset: number, currency: string, address: string, extraId: string, callback: CallbackFunction): void;
|
|
16
|
+
getStatus(id: string, callback: CallbackFunction): void;
|
|
17
|
+
on(channel: string, callback: (...args: any[]) => void): void;
|
|
18
|
+
}
|
|
19
|
+
export = Changelly;
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var io = __importStar(require("socket.io-client"));
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
var jayson = __importStar(require("jayson"));
|
|
28
|
+
var crypto = __importStar(require("crypto"));
|
|
29
|
+
var Changelly = /** @class */ (function () {
|
|
30
|
+
function Changelly(apiKey, apiSecret) {
|
|
31
|
+
var _this = this;
|
|
32
|
+
this.apiKey = apiKey;
|
|
33
|
+
this.apiSecret = apiSecret;
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
this.client = jayson.client.https('https://api.changelly.com');
|
|
36
|
+
this._socket = io.connect('https://api.changelly.com', {
|
|
37
|
+
reconnection: true,
|
|
38
|
+
reconnectionDelay: 1000,
|
|
39
|
+
reconnectionDelayMax: 5000,
|
|
40
|
+
reconnectionAttempts: Infinity,
|
|
41
|
+
});
|
|
42
|
+
this._socket.on('connect', function () {
|
|
43
|
+
var message = { "Login": {} };
|
|
44
|
+
_this._socket.emit('subscribe', {
|
|
45
|
+
apiKey: _this.apiKey,
|
|
46
|
+
sign: _this._sign(message),
|
|
47
|
+
message: message
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
Changelly.prototype._id = function () {
|
|
52
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
53
|
+
var r = Math.random() * 16 | 0;
|
|
54
|
+
var v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
55
|
+
return v.toString(16);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
Changelly.prototype._sign = function (message) {
|
|
59
|
+
return crypto
|
|
60
|
+
.createHmac('sha512', this.apiSecret)
|
|
61
|
+
.update(JSON.stringify(message))
|
|
62
|
+
.digest('hex');
|
|
63
|
+
};
|
|
64
|
+
Changelly.prototype._request = function (method, options, callback) {
|
|
65
|
+
var id = this._id();
|
|
66
|
+
var message = jayson.utils.request(method, options, id);
|
|
67
|
+
this.client.options.headers = {
|
|
68
|
+
'api-key': this.apiKey,
|
|
69
|
+
'sign': this._sign(message)
|
|
70
|
+
};
|
|
71
|
+
this.client.request(method, options, id, callback);
|
|
72
|
+
};
|
|
73
|
+
Changelly.prototype.getCurrencies = function (callback) {
|
|
74
|
+
this._request('getCurrencies', {}, callback);
|
|
75
|
+
};
|
|
76
|
+
Changelly.prototype.createTransaction = function (from, to, address, amount, extraId, callback) {
|
|
77
|
+
var params = { from: from, to: to, address: address, amount: amount, extraId: extraId };
|
|
78
|
+
this._request('createTransaction', params, callback);
|
|
79
|
+
};
|
|
80
|
+
Changelly.prototype.getMinAmount = function (from, to, callback) {
|
|
81
|
+
var params = { from: from, to: to };
|
|
82
|
+
this._request('getMinAmount', params, callback);
|
|
83
|
+
};
|
|
84
|
+
Changelly.prototype.getExchangeAmount = function (from, to, amount, callback) {
|
|
85
|
+
var params = { from: from, to: to, amount: amount };
|
|
86
|
+
this._request('getExchangeAmount', params, callback);
|
|
87
|
+
};
|
|
88
|
+
Changelly.prototype.getTransactions = function (limit, offset, currency, address, extraId, callback) {
|
|
89
|
+
var params = { limit: limit, offset: offset, currency: currency, address: address, extraId: extraId };
|
|
90
|
+
this._request('getTransactions', params, callback);
|
|
91
|
+
};
|
|
92
|
+
Changelly.prototype.getStatus = function (id, callback) {
|
|
93
|
+
var params = { id: id };
|
|
94
|
+
this._request('getStatus', params, callback);
|
|
95
|
+
};
|
|
96
|
+
Changelly.prototype.on = function (channel, callback) {
|
|
97
|
+
this._socket.on(channel, callback);
|
|
98
|
+
};
|
|
99
|
+
return Changelly;
|
|
100
|
+
}());
|
|
101
|
+
module.exports = Changelly;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare let Changelly: any;
|
|
2
|
+
declare const TAG = " | blocknative | ";
|
|
3
|
+
declare const CHANGELLY_API_KEY: string | undefined;
|
|
4
|
+
declare const CHANGELLY_API_SECRET: string | undefined;
|
|
5
|
+
declare let changelly: any;
|
|
6
|
+
declare let shortListSymbolToCaip: any;
|
|
7
|
+
declare let networkSupport: any[];
|
|
8
|
+
declare function get_currencies(): Promise<any>;
|
|
9
|
+
declare function create_transaction(from: string, to: string, address: string, amount: number, extraId?: string): Promise<any>;
|
|
10
|
+
declare function get_min_amount(from: string, to: string): Promise<any>;
|
|
11
|
+
declare function get_exchange_amount(from: string, to: string, amount: number): Promise<any>;
|
|
12
|
+
declare function get_transactions(limit: number, offset: number, currency?: string, address?: string, extraId?: string): Promise<any>;
|
|
13
|
+
declare function get_status(id: string): Promise<any>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var Changelly = require('@bithighlander/changelly');
|
|
39
|
+
var TAG = " | blocknative | ";
|
|
40
|
+
var CHANGELLY_API_KEY = process.env['CHANGELLY_API_KEY'];
|
|
41
|
+
var CHANGELLY_API_SECRET = process.env['CHANGELLY_API_SECRET'];
|
|
42
|
+
if (!CHANGELLY_API_KEY)
|
|
43
|
+
throw new Error('CHANGELLY_API_KEY not set');
|
|
44
|
+
if (!CHANGELLY_API_SECRET)
|
|
45
|
+
throw new Error('CHANGELLY_API_SECRET not set');
|
|
46
|
+
var changelly;
|
|
47
|
+
var shortListSymbolToCaip = require("@pioneer-platform/pioneer-caip").shortListSymbolToCaip;
|
|
48
|
+
var networkSupport = [
|
|
49
|
+
shortListSymbolToCaip["XRP"],
|
|
50
|
+
shortListSymbolToCaip["DASH"],
|
|
51
|
+
shortListSymbolToCaip["ZEC"],
|
|
52
|
+
// shortListSymbolToCaip["BSV"], //TODO
|
|
53
|
+
// shortListSymbolToCaip["ADA"], //TODO
|
|
54
|
+
// shortListSymbolToCaip["EOS"], //TODO
|
|
55
|
+
shortListSymbolToCaip["GAIA"],
|
|
56
|
+
shortListSymbolToCaip["BNB"],
|
|
57
|
+
shortListSymbolToCaip["DOGE"],
|
|
58
|
+
shortListSymbolToCaip["BTC"],
|
|
59
|
+
shortListSymbolToCaip["ETH"],
|
|
60
|
+
shortListSymbolToCaip["LTC"],
|
|
61
|
+
shortListSymbolToCaip["THOR"],
|
|
62
|
+
shortListSymbolToCaip["BCH"],
|
|
63
|
+
shortListSymbolToCaip["GNO"],
|
|
64
|
+
shortListSymbolToCaip["MATIC"],
|
|
65
|
+
shortListSymbolToCaip["AVAX"],
|
|
66
|
+
];
|
|
67
|
+
module.exports = {
|
|
68
|
+
init: function (settings) {
|
|
69
|
+
changelly = new Changelly(CHANGELLY_API_KEY, CHANGELLY_API_SECRET);
|
|
70
|
+
},
|
|
71
|
+
networkSupport: function () {
|
|
72
|
+
return networkSupport;
|
|
73
|
+
},
|
|
74
|
+
getCurrenciesAsync: function () {
|
|
75
|
+
return get_currencies();
|
|
76
|
+
},
|
|
77
|
+
getQuote: function (from, to, address, amount, extraId) {
|
|
78
|
+
return create_transaction(from, to, address, amount, extraId);
|
|
79
|
+
},
|
|
80
|
+
// createTransactionAsync: function(from: string, to: string, address: string, amount: number, extraId?: string): Promise<any> {
|
|
81
|
+
// return create_transaction(from, to, address, amount, extraId);
|
|
82
|
+
// },
|
|
83
|
+
getMinAmountAsync: function (from, to) {
|
|
84
|
+
return get_min_amount(from, to);
|
|
85
|
+
},
|
|
86
|
+
getExchangeAmountAsync: function (from, to, amount) {
|
|
87
|
+
return get_exchange_amount(from, to, amount);
|
|
88
|
+
},
|
|
89
|
+
getTransactionsAsync: function (limit, offset, currency, address, extraId) {
|
|
90
|
+
return get_transactions(limit, offset, currency, address, extraId);
|
|
91
|
+
},
|
|
92
|
+
getStatusAsync: function (id) {
|
|
93
|
+
return get_status(id);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
function get_currencies() {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
98
|
+
return __generator(this, function (_a) {
|
|
99
|
+
try {
|
|
100
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
101
|
+
changelly.getCurrencies(function (err, data) {
|
|
102
|
+
if (err)
|
|
103
|
+
reject(err);
|
|
104
|
+
else
|
|
105
|
+
resolve(data);
|
|
106
|
+
});
|
|
107
|
+
})];
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
console.error(TAG, "get_currencies error:", e);
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
return [2 /*return*/];
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function create_transaction(from, to, address, amount, extraId) {
|
|
118
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
119
|
+
return __generator(this, function (_a) {
|
|
120
|
+
try {
|
|
121
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
122
|
+
changelly.createTransaction(from, to, address, amount, extraId, function (err, data) {
|
|
123
|
+
if (err)
|
|
124
|
+
reject(err);
|
|
125
|
+
else
|
|
126
|
+
resolve(data);
|
|
127
|
+
});
|
|
128
|
+
})];
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
console.error(TAG, "create_transaction error:", e);
|
|
132
|
+
throw e;
|
|
133
|
+
}
|
|
134
|
+
return [2 /*return*/];
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function get_min_amount(from, to) {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
140
|
+
return __generator(this, function (_a) {
|
|
141
|
+
try {
|
|
142
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
143
|
+
changelly.getMinAmount(from, to, function (err, data) {
|
|
144
|
+
if (err)
|
|
145
|
+
reject(err);
|
|
146
|
+
else
|
|
147
|
+
resolve(data);
|
|
148
|
+
});
|
|
149
|
+
})];
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
console.error(TAG, "get_min_amount error:", e);
|
|
153
|
+
throw e;
|
|
154
|
+
}
|
|
155
|
+
return [2 /*return*/];
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function get_exchange_amount(from, to, amount) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
+
return __generator(this, function (_a) {
|
|
162
|
+
try {
|
|
163
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
164
|
+
changelly.getExchangeAmount(from, to, amount, function (err, data) {
|
|
165
|
+
if (err)
|
|
166
|
+
reject(err);
|
|
167
|
+
else
|
|
168
|
+
resolve(data);
|
|
169
|
+
});
|
|
170
|
+
})];
|
|
171
|
+
}
|
|
172
|
+
catch (e) {
|
|
173
|
+
console.error(TAG, "get_exchange_amount error:", e);
|
|
174
|
+
throw e;
|
|
175
|
+
}
|
|
176
|
+
return [2 /*return*/];
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
function get_transactions(limit, offset, currency, address, extraId) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
182
|
+
return __generator(this, function (_a) {
|
|
183
|
+
try {
|
|
184
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
185
|
+
changelly.getTransactions(limit, offset, currency, address, extraId, function (err, data) {
|
|
186
|
+
if (err)
|
|
187
|
+
reject(err);
|
|
188
|
+
else
|
|
189
|
+
resolve(data);
|
|
190
|
+
});
|
|
191
|
+
})];
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
console.error(TAG, "get_transactions error:", e);
|
|
195
|
+
throw e;
|
|
196
|
+
}
|
|
197
|
+
return [2 /*return*/];
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
function get_status(id) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
203
|
+
return __generator(this, function (_a) {
|
|
204
|
+
try {
|
|
205
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
206
|
+
changelly.getStatus(id, function (err, data) {
|
|
207
|
+
if (err)
|
|
208
|
+
reject(err);
|
|
209
|
+
else
|
|
210
|
+
resolve(data);
|
|
211
|
+
});
|
|
212
|
+
})];
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
console.error(TAG, "get_status error:", e);
|
|
216
|
+
throw e;
|
|
217
|
+
}
|
|
218
|
+
return [2 /*return*/];
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pioneer-platform/changelly-client",
|
|
3
|
+
"version": "8.3.1",
|
|
4
|
+
"main": "./lib/index.js",
|
|
5
|
+
"types": "./lib/index.d.ts",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@bithighlander/changelly": "^1.0.0",
|
|
8
|
+
"@pioneer-platform/loggerdog": "^8.3.1",
|
|
9
|
+
"axios": "^1.3.4",
|
|
10
|
+
"dotenv": "^8.2.0",
|
|
11
|
+
"jayson": "^4.1.0",
|
|
12
|
+
"micro-ftch": "^0.3.1"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"npm": "npm i",
|
|
16
|
+
"test": "npm run build && node __tests__/test-module.js",
|
|
17
|
+
"build": "tsc -p .",
|
|
18
|
+
"prepublish": "npm run build",
|
|
19
|
+
"refresh": "rm -rf ./node_modules ./package-lock.json && npm install"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/jest": "^25.2.3",
|
|
23
|
+
"@types/node": "^13.13.21",
|
|
24
|
+
"@types/source-map-support": "^0.5.3",
|
|
25
|
+
"jest": "^26.4.2",
|
|
26
|
+
"onchange": "^7.0.2",
|
|
27
|
+
"serve": "^11.3.2",
|
|
28
|
+
"source-map-support": "^0.5.19",
|
|
29
|
+
"ts-jest": "^29.0.5",
|
|
30
|
+
"typescript": "^5.0.2"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "aeae28273014ab69b42f22abec159c6693a56c40"
|
|
33
|
+
}
|
package/tsconfig.json
ADDED