@openocean.finance/openocean-sdk 0.1.58 → 0.1.59

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,739 @@
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 (_) 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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Swap = exports.ReqSwapVo = void 0;
40
+ var aggregator_1 = require("../asset/abi/aggregator");
41
+ var RequestVo_1 = require("../api/vo/RequestVo");
42
+ Object.defineProperty(exports, "ReqSwapVo", { enumerable: true, get: function () { return RequestVo_1.ReqSwapVo; } });
43
+ var Chains_1 = require("../config/Chains");
44
+ var web3_js_1 = require("@solana/web3.js");
45
+ var terra_js_1 = require("@terra-money/terra.js");
46
+ var ontology_ts_sdk_1 = require("ontology-ts-sdk");
47
+ var ontology_dapi_1 = require("@ont-dev/ontology-dapi");
48
+ var NotoMobile_1 = require("./NotoMobile");
49
+ var axios = require('axios');
50
+ var bs58 = require("bs58");
51
+ ontology_dapi_1.client.registerClient({});
52
+ var Swap = /** @class */ (function () {
53
+ function Swap(res, wallet, chain, isNew) {
54
+ this.getDataCallback = function () { };
55
+ this.errorCallback = function () { };
56
+ this.transactionHashCallback = function () { };
57
+ this.receiptCallback = function () { };
58
+ this.successCallback = function () { };
59
+ this.res = res;
60
+ this.wallet = wallet;
61
+ this.chain = chain;
62
+ }
63
+ Swap.prototype.send = function () {
64
+ var _this = this;
65
+ setTimeout(function () {
66
+ switch (_this.chain.compiler) {
67
+ case 'EVM':
68
+ if (_this.isNew) {
69
+ _this.sendEthTransactionNew();
70
+ }
71
+ else {
72
+ _this.sendEthTransaction();
73
+ }
74
+ break;
75
+ case 'SOL':
76
+ _this.sendSolanaTransaction();
77
+ break;
78
+ case 'TRON':
79
+ _this.sendTronTransaction();
80
+ break;
81
+ case 'TERRA':
82
+ _this.sendTerraTransaction();
83
+ break;
84
+ case 'ONT':
85
+ _this.sendONTTransaction();
86
+ break;
87
+ }
88
+ }, 200);
89
+ return this;
90
+ };
91
+ Swap.prototype.getGas = function () {
92
+ var _this = this;
93
+ return new Promise(function (r, j) {
94
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
95
+ var _a, _b, _c;
96
+ return __generator(this, function (_d) {
97
+ switch (_d.label) {
98
+ case 0:
99
+ _a = this.chain.compiler;
100
+ switch (_a) {
101
+ case 'EVM': return [3 /*break*/, 1];
102
+ }
103
+ return [3 /*break*/, 6];
104
+ case 1:
105
+ if (!this.isNew) return [3 /*break*/, 3];
106
+ _b = r;
107
+ return [4 /*yield*/, this.getGasNew()];
108
+ case 2:
109
+ _b.apply(void 0, [_d.sent()]);
110
+ return [3 /*break*/, 5];
111
+ case 3:
112
+ _c = r;
113
+ return [4 /*yield*/, this.getGasOld()];
114
+ case 4:
115
+ _c.apply(void 0, [_d.sent()]);
116
+ _d.label = 5;
117
+ case 5: return [3 /*break*/, 6];
118
+ case 6: return [2 /*return*/];
119
+ }
120
+ });
121
+ }); }, 200);
122
+ });
123
+ };
124
+ Swap.prototype.sendONTTransaction = function () {
125
+ return __awaiter(this, void 0, void 0, function () {
126
+ var _a, approve, swap, transaction, inAmount, inToken, instance_1, account;
127
+ return __generator(this, function (_b) {
128
+ switch (_b.label) {
129
+ case 0:
130
+ _a = this.res, approve = _a.approve, swap = _a.swap, transaction = _a.transaction, inAmount = _a.inAmount, inToken = _a.inToken;
131
+ if (!(this.wallet.key === "OntoMobile")) return [3 /*break*/, 2];
132
+ instance_1 = new NotoMobile_1.NotoMobile(approve ? approve : swap);
133
+ return [4 /*yield*/, new Promise(function (r, q) {
134
+ instance_1.$on('close', function (result, action, account) {
135
+ if (action === 'login' && result === 'success') {
136
+ r(account);
137
+ }
138
+ else {
139
+ q(action);
140
+ }
141
+ });
142
+ })];
143
+ case 1:
144
+ account = _b.sent();
145
+ this.transactionHashCallback(account);
146
+ return [3 /*break*/, 3];
147
+ case 2:
148
+ if (approve) {
149
+ this.approveOnt(transaction, inAmount, inToken);
150
+ }
151
+ else {
152
+ this.sendOntTransactionSdk(transaction);
153
+ }
154
+ _b.label = 3;
155
+ case 3: return [2 /*return*/];
156
+ }
157
+ });
158
+ });
159
+ };
160
+ Swap.prototype.sendSolanaTransaction = function () {
161
+ return __awaiter(this, void 0, void 0, function () {
162
+ var res, _a, setupTransaction, swapTransaction, cleanupTransaction, list, recentBlock_1, transactions, i, _i, transactions_1, transaction_1, txid, transaction, signed, signature, result, bytes, _b, msg, data, bytes, e_1;
163
+ return __generator(this, function (_c) {
164
+ switch (_c.label) {
165
+ case 0:
166
+ res = this.res;
167
+ _c.label = 1;
168
+ case 1:
169
+ _c.trys.push([1, 19, , 20]);
170
+ if (!(res.dex == "jupiter")) return [3 /*break*/, 11];
171
+ _a = JSON.parse(res.transaction), setupTransaction = _a.setupTransaction, swapTransaction = _a.swapTransaction, cleanupTransaction = _a.cleanupTransaction;
172
+ list = [setupTransaction, swapTransaction, cleanupTransaction].filter(Boolean);
173
+ return [4 /*yield*/, this.wallet.connection.getLatestBlockhash()];
174
+ case 2:
175
+ recentBlock_1 = _c.sent();
176
+ transactions = list.map(function (tx) {
177
+ var transaction = web3_js_1.Transaction.from(Buffer.from(tx, "base64"));
178
+ transaction.recentBlockhash = recentBlock_1.blockhash;
179
+ return transaction;
180
+ });
181
+ return [4 /*yield*/, this.wallet.sdk.signAllTransactions(transactions)];
182
+ case 3: return [4 /*yield*/, _c.sent()];
183
+ case 4:
184
+ _c.sent();
185
+ i = 0;
186
+ _i = 0, transactions_1 = transactions;
187
+ _c.label = 5;
188
+ case 5:
189
+ if (!(_i < transactions_1.length)) return [3 /*break*/, 10];
190
+ transaction_1 = transactions_1[_i];
191
+ i++;
192
+ return [4 /*yield*/, this.wallet.connection.sendRawTransaction(transaction_1.serialize({ requireAllSignatures: false }))];
193
+ case 6:
194
+ txid = _c.sent();
195
+ if (!(i < list.length)) return [3 /*break*/, 8];
196
+ return [4 /*yield*/, this.wallet.connection.confirmTransaction(txid)];
197
+ case 7:
198
+ _c.sent();
199
+ return [3 /*break*/, 9];
200
+ case 8:
201
+ this.transactionHashCallback(txid);
202
+ _c.label = 9;
203
+ case 9:
204
+ _i++;
205
+ return [3 /*break*/, 5];
206
+ case 10: return [2 /*return*/];
207
+ case 11:
208
+ transaction = web3_js_1.Transaction.from(Buffer.from(res.transaction, res.dex == "jupiter" ? "base64" : "hex"));
209
+ signed = null;
210
+ signature = null;
211
+ if (!this.wallet.sdk.isCoin98) return [3 /*break*/, 13];
212
+ return [4 /*yield*/, this.wallet.sdk.request({
213
+ method: 'sol_sign',
214
+ params: [transaction]
215
+ })];
216
+ case 12:
217
+ result = _c.sent();
218
+ console.log("Got signature, submitting transaction");
219
+ bytes = bs58.decode(result.signature);
220
+ transaction.signatures[0].signature = bytes;
221
+ transaction.feePayer = this.wallet.customPublicKey;
222
+ signed = transaction;
223
+ return [3 /*break*/, 17];
224
+ case 13:
225
+ if (!this.wallet.sdk.isSlopeWallet) return [3 /*break*/, 15];
226
+ return [4 /*yield*/, this.wallet.sdk.signTransaction(bs58.encode(transaction.serializeMessage()))];
227
+ case 14:
228
+ _b = _c.sent(), msg = _b.msg, data = _b.data;
229
+ if (msg !== 'ok')
230
+ return [2 /*return*/];
231
+ bytes = bs58.decode(data.signature);
232
+ transaction.signatures[0].signature = bytes;
233
+ transaction.feePayer = this.wallet.customPublicKey;
234
+ signed = transaction;
235
+ return [3 /*break*/, 17];
236
+ case 15: return [4 /*yield*/, this.wallet.sdk.signTransaction(transaction)];
237
+ case 16:
238
+ signed = _c.sent();
239
+ _c.label = 17;
240
+ case 17: return [4 /*yield*/, this.wallet.connection.sendRawTransaction(signed.serialize({ requireAllSignatures: false }))];
241
+ case 18:
242
+ signature = _c.sent();
243
+ // this.receiptCallback(signature)
244
+ this.transactionHashCallback(signature);
245
+ return [3 /*break*/, 20];
246
+ case 19:
247
+ e_1 = _c.sent();
248
+ this.errorCallback(e_1.message);
249
+ return [3 /*break*/, 20];
250
+ case 20: return [2 /*return*/];
251
+ }
252
+ });
253
+ });
254
+ };
255
+ Swap.prototype.getGasOld = function () {
256
+ return __awaiter(this, void 0, void 0, function () {
257
+ var _a, inToken, inAmount, data, to, gas;
258
+ return __generator(this, function (_b) {
259
+ switch (_b.label) {
260
+ case 0:
261
+ _a = this.res, inToken = _a.inToken, inAmount = _a.inAmount, data = _a.data, to = _a.to;
262
+ return [4 /*yield*/, this.wallet.sdk.eth.estimateGas({
263
+ from: this.wallet.address,
264
+ to: to,
265
+ data: data,
266
+ value: Chains_1.chains.isNativeToken(this.chain.key, inToken.address) ? inAmount : 0
267
+ })];
268
+ case 1:
269
+ gas = _b.sent();
270
+ return [2 /*return*/, Math.ceil(gas * 1.15)];
271
+ }
272
+ });
273
+ });
274
+ };
275
+ Swap.prototype.getGasNew = function () {
276
+ return __awaiter(this, void 0, void 0, function () {
277
+ var _a, inToken, inAmount, outAmount, outToken, data, to, myWallet, contract, invitee, path, amounts, swapAddr, swapExtraData, gas;
278
+ return __generator(this, function (_b) {
279
+ switch (_b.label) {
280
+ case 0:
281
+ _a = this.res, inToken = _a.inToken, inAmount = _a.inAmount, outAmount = _a.outAmount, outToken = _a.outToken, data = _a.data, to = _a.to;
282
+ myWallet = this.wallet.sdk;
283
+ contract = new myWallet.eth.Contract(aggregator_1.aggregator, Chains_1.chains.getProxyContract(this.chain.key));
284
+ invitee = this.wallet.address;
285
+ path = [inToken.address, outToken.address];
286
+ amounts = [inAmount, outAmount];
287
+ swapAddr = to;
288
+ swapExtraData = data;
289
+ return [4 /*yield*/, contract.methods
290
+ .swap(invitee, path, amounts, swapAddr, swapExtraData)
291
+ .estimateGas({
292
+ from: this.wallet.address,
293
+ value: Chains_1.chains.isNativeToken(this.chain.key, inToken.address) ? inAmount : 0
294
+ })];
295
+ case 1:
296
+ gas = _b.sent();
297
+ return [2 /*return*/, Math.ceil(gas * 1.5)];
298
+ }
299
+ });
300
+ });
301
+ };
302
+ Swap.prototype.sendEthTransactionNew = function () {
303
+ return __awaiter(this, void 0, void 0, function () {
304
+ var _a, inToken, inAmount, outAmount, outToken, data, to, ethGasPrice, gasPrice, myWallet, contract, invitee, path, amounts, swapAddr, swapExtraData, swapParams, maxFeePerGas, maxPriorityFeePerGas;
305
+ var _b;
306
+ var _this = this;
307
+ return __generator(this, function (_c) {
308
+ switch (_c.label) {
309
+ case 0:
310
+ _a = this.res, inToken = _a.inToken, inAmount = _a.inAmount, outAmount = _a.outAmount, outToken = _a.outToken, data = _a.data, to = _a.to, ethGasPrice = _a.ethGasPrice, gasPrice = _a.gasPrice;
311
+ myWallet = this.wallet.sdk;
312
+ contract = new myWallet.eth.Contract(aggregator_1.aggregator, Chains_1.chains.getProxyContract(this.chain.key));
313
+ invitee = this.wallet.address;
314
+ path = [inToken.address, outToken.address];
315
+ amounts = [inAmount, outAmount];
316
+ swapAddr = to;
317
+ swapExtraData = data;
318
+ _b = {
319
+ from: this.wallet.address
320
+ };
321
+ return [4 /*yield*/, this.getGasNew()];
322
+ case 1:
323
+ swapParams = (_b.gas = _c.sent(),
324
+ _b.to = to,
325
+ _b.data = data,
326
+ _b);
327
+ if (ethGasPrice) {
328
+ maxFeePerGas = ethGasPrice.maxFeePerGas, maxPriorityFeePerGas = ethGasPrice.maxPriorityFeePerGas;
329
+ if (maxFeePerGas && maxPriorityFeePerGas) {
330
+ swapParams.maxFeePerGas = +maxFeePerGas;
331
+ swapParams.maxPriorityFeePerGas = +maxPriorityFeePerGas;
332
+ }
333
+ }
334
+ else {
335
+ swapParams.gasPrice = +gasPrice;
336
+ }
337
+ if (Chains_1.chains.isNativeToken(this.chain.key, inToken.address)) {
338
+ swapParams.value = inAmount;
339
+ }
340
+ contract.methods
341
+ .swap(invitee, path, amounts, swapAddr, swapExtraData)
342
+ .send(swapParams)
343
+ .on('error', function (error) {
344
+ _this.errorCallback(error);
345
+ })
346
+ .on('transactionHash', function (transactionHash) {
347
+ _this.transactionHashCallback(transactionHash);
348
+ })
349
+ .on('receipt', function (receipt) {
350
+ _this.receiptCallback(receipt);
351
+ // this.getSuccess()
352
+ });
353
+ return [2 /*return*/];
354
+ }
355
+ });
356
+ });
357
+ };
358
+ Swap.prototype.sendEthTransaction = function () {
359
+ return __awaiter(this, void 0, void 0, function () {
360
+ var _a, inToken, inAmount, data, to, ethGasPrice, gasPrice, swapParams, safeTxHash, e_2, gas, e_3, maxFeePerGas, maxPriorityFeePerGas;
361
+ var _this = this;
362
+ return __generator(this, function (_b) {
363
+ switch (_b.label) {
364
+ case 0:
365
+ _a = this.res, inToken = _a.inToken, inAmount = _a.inAmount, data = _a.data, to = _a.to, ethGasPrice = _a.ethGasPrice, gasPrice = _a.gasPrice;
366
+ swapParams = {
367
+ from: this.wallet.address,
368
+ to: to,
369
+ data: data
370
+ };
371
+ if (Chains_1.chains.isNativeToken(this.chain.key, inToken.address)) {
372
+ swapParams.value = inAmount;
373
+ }
374
+ if (!(this.wallet.key === "GnosisSafeWallet")) return [3 /*break*/, 5];
375
+ _b.label = 1;
376
+ case 1:
377
+ _b.trys.push([1, 3, , 4]);
378
+ return [4 /*yield*/, this.wallet.sdk.txs.send({
379
+ txs: [
380
+ {
381
+ to: swapParams.to,
382
+ value: swapParams.value || '0',
383
+ data: swapParams.data,
384
+ },
385
+ ],
386
+ })];
387
+ case 2:
388
+ safeTxHash = (_b.sent()).safeTxHash;
389
+ this.getGnosisSafeTransaction(safeTxHash);
390
+ return [3 /*break*/, 4];
391
+ case 3:
392
+ e_2 = _b.sent();
393
+ this.errorCallback((e_2 && e_2.message) || e_2);
394
+ return [3 /*break*/, 4];
395
+ case 4: return [2 /*return*/];
396
+ case 5:
397
+ _b.trys.push([5, 7, , 8]);
398
+ return [4 /*yield*/, this.getGasOld()];
399
+ case 6:
400
+ gas = _b.sent();
401
+ swapParams.gas = gas;
402
+ return [3 /*break*/, 8];
403
+ case 7:
404
+ e_3 = _b.sent();
405
+ if (e_3 && e_3.message.indexOf('JSON-RPC error.') != -1) {
406
+ e_3 = JSON.parse(e_3.message.split('JSON-RPC error.')[1]);
407
+ }
408
+ this.errorCallback((e_3 && e_3.message) || e_3);
409
+ return [3 /*break*/, 8];
410
+ case 8:
411
+ if (ethGasPrice) {
412
+ maxFeePerGas = ethGasPrice.maxFeePerGas, maxPriorityFeePerGas = ethGasPrice.maxPriorityFeePerGas;
413
+ if (maxFeePerGas && maxPriorityFeePerGas) {
414
+ swapParams.maxFeePerGas = +maxFeePerGas;
415
+ swapParams.maxPriorityFeePerGas = +maxPriorityFeePerGas;
416
+ }
417
+ }
418
+ else if (gasPrice) {
419
+ swapParams.gasPrice = +gasPrice;
420
+ }
421
+ this.wallet.sdk.eth.sendTransaction(swapParams)
422
+ .on('error', function (error) {
423
+ _this.errorCallback(error);
424
+ })
425
+ .on('transactionHash', function (transactionHash) {
426
+ _this.transactionHashCallback(transactionHash);
427
+ })
428
+ .on('receipt', function (receipt) {
429
+ _this.receiptCallback(receipt);
430
+ // this.getSuccess()
431
+ });
432
+ return [2 /*return*/];
433
+ }
434
+ });
435
+ });
436
+ };
437
+ Swap.prototype.getGnosisSafeTransaction = function (safeTxHash) {
438
+ return __awaiter(this, void 0, void 0, function () {
439
+ var txHash;
440
+ var _this = this;
441
+ return __generator(this, function (_a) {
442
+ switch (_a.label) {
443
+ case 0: return [4 /*yield*/, this.wallet.sdk.txs.getBySafeTxHash(safeTxHash)];
444
+ case 1:
445
+ txHash = (_a.sent()).txHash;
446
+ console.log('safeTxHash', safeTxHash, txHash);
447
+ if (txHash) {
448
+ this.transactionHashCallback(txHash);
449
+ }
450
+ else {
451
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
452
+ return __generator(this, function (_a) {
453
+ switch (_a.label) {
454
+ case 0: return [4 /*yield*/, this.getGnosisSafeTransaction(safeTxHash)];
455
+ case 1:
456
+ _a.sent();
457
+ return [2 /*return*/];
458
+ }
459
+ });
460
+ }); }, 2000);
461
+ }
462
+ return [2 /*return*/];
463
+ }
464
+ });
465
+ });
466
+ };
467
+ Swap.prototype.sendTronTransaction = function () {
468
+ return __awaiter(this, void 0, void 0, function () {
469
+ var _a, inToken, outToken, inAmount, outAmount, minOutAmount, addresses, calldata, offsets, gasLimitsAndValues, data, _contract, swapParams;
470
+ var _this = this;
471
+ return __generator(this, function (_b) {
472
+ switch (_b.label) {
473
+ case 0:
474
+ _a = this.res, inToken = _a.inToken, outToken = _a.outToken, inAmount = _a.inAmount, outAmount = _a.outAmount, minOutAmount = _a.minOutAmount, addresses = _a.addresses, calldata = _a.calldata, offsets = _a.offsets, gasLimitsAndValues = _a.gasLimitsAndValues;
475
+ return [4 /*yield*/, axios.get("https://ethapi.openocean.finance/v1/tron/exchange")];
476
+ case 1:
477
+ data = (_b.sent()).data;
478
+ return [4 /*yield*/, this.wallet.sdk.contract(data.abi, data.contract)];
479
+ case 2:
480
+ _contract = _b.sent();
481
+ swapParams = {
482
+ feeLimit: 300000000,
483
+ };
484
+ if (inToken.toLowerCase() === "t9yd14nj9j7xab4dbgeix9h8unkkhxuwwb") {
485
+ swapParams.callValue = inAmount;
486
+ }
487
+ try {
488
+ _contract.methods
489
+ .swap(inToken, outToken, inAmount, minOutAmount, outAmount, // guaranteedAmount
490
+ "0x0000000000000000000000000000000000000000", // referrer
491
+ addresses, calldata, offsets, gasLimitsAndValues)
492
+ .send(swapParams, function (result, txHash) {
493
+ console.log("state.multicall.methods.swap", result);
494
+ if (result) {
495
+ _this.errorCallback(result.message || result);
496
+ }
497
+ else {
498
+ _this.transactionHashCallback(txHash);
499
+ }
500
+ });
501
+ }
502
+ catch (e) {
503
+ this.errorCallback(e || e.message);
504
+ }
505
+ return [2 /*return*/];
506
+ }
507
+ });
508
+ });
509
+ };
510
+ Swap.prototype.sendTerraTransaction = function () {
511
+ return __awaiter(this, void 0, void 0, function () {
512
+ var data, address, gasPrices, msg, _a, fee, accountInfo, e_4;
513
+ var _this = this;
514
+ return __generator(this, function (_b) {
515
+ switch (_b.label) {
516
+ case 0:
517
+ _b.trys.push([0, 6, , 7]);
518
+ return [4 /*yield*/, axios.get("https://ethapi.openocean.finance/v1/terra/exchange")];
519
+ case 1:
520
+ data = (_b.sent()).data;
521
+ address = this.wallet.address;
522
+ return [4 /*yield*/, axios.get("https://ethapi.openocean.finance/v1/terra/gas-price", { cache: true })];
523
+ case 2:
524
+ gasPrices = _b.sent();
525
+ return [4 /*yield*/, this.getTerraMsgExecuteContract(this.res, data, address, gasPrices.data)];
526
+ case 3:
527
+ msg = _b.sent();
528
+ return [4 /*yield*/, this.getTerraFee(address, msg, gasPrices.data)];
529
+ case 4:
530
+ _a = _b.sent(), fee = _a.fee, accountInfo = _a.accountInfo;
531
+ return [4 /*yield*/, this.wallet.sdk.post({
532
+ msgs: [msg],
533
+ gasAdjustment: 1.5,
534
+ waitForConfirmation: true,
535
+ fee: fee,
536
+ account_number: accountInfo.account_number,
537
+ sequence: accountInfo.sequence,
538
+ purgeQueue: true,
539
+ })];
540
+ case 5:
541
+ _b.sent();
542
+ this.wallet.sdk.on("onPost", function (data) {
543
+ var _a = data || {}, result = _a.result, success = _a.success;
544
+ if (success) {
545
+ var txhash = (result || {}).txhash;
546
+ _this.transactionHashCallback(txhash);
547
+ }
548
+ else {
549
+ _this.errorCallback('Transaction failed');
550
+ }
551
+ });
552
+ return [3 /*break*/, 7];
553
+ case 6:
554
+ e_4 = _b.sent();
555
+ this.errorCallback(e_4.message || e_4);
556
+ return [3 /*break*/, 7];
557
+ case 7: return [2 /*return*/];
558
+ }
559
+ });
560
+ });
561
+ };
562
+ Swap.prototype.approveOnt = function (transaction, _amount, inToken) {
563
+ return __awaiter(this, void 0, void 0, function () {
564
+ var scriptHash, operation, gasLimit, args, params, result, e_5;
565
+ return __generator(this, function (_a) {
566
+ switch (_a.label) {
567
+ case 0:
568
+ _a.trys.push([0, 2, , 3]);
569
+ scriptHash = transaction.scriptHash, operation = transaction.operation, gasLimit = transaction.gasLimit, args = transaction.args;
570
+ params = {
571
+ contract: inToken,
572
+ operation: "approve",
573
+ args: [
574
+ {
575
+ type: "Address",
576
+ value: this.wallet.address,
577
+ },
578
+ {
579
+ type: "ByteArray",
580
+ value: ontology_ts_sdk_1.utils.reverseHex(scriptHash),
581
+ },
582
+ {
583
+ type: "ByteArray",
584
+ value: ontology_ts_sdk_1.utils.bigIntToBytes(_amount + ""),
585
+ },
586
+ ],
587
+ gasPrice: 2500,
588
+ gasLimit: 40000,
589
+ };
590
+ return [4 /*yield*/, ontology_dapi_1.client.api.smartContract.invoke(params)];
591
+ case 1:
592
+ result = _a.sent();
593
+ console.log("approveOnt params, result", params, result);
594
+ this.sendOntTransactionSdk(transaction);
595
+ return [3 /*break*/, 3];
596
+ case 2:
597
+ e_5 = _a.sent();
598
+ // tslint:disable-next-line:no-console
599
+ console.log("onScCall error:", e_5);
600
+ this.errorCallback((e_5 && e_5.message) || e_5);
601
+ return [3 /*break*/, 3];
602
+ case 3: return [2 /*return*/];
603
+ }
604
+ });
605
+ });
606
+ };
607
+ Swap.prototype.sendOntTransactionSdk = function (transaction) {
608
+ return __awaiter(this, void 0, void 0, function () {
609
+ var scriptHash, operation, gasLimit, args, params, result, e_6;
610
+ return __generator(this, function (_a) {
611
+ switch (_a.label) {
612
+ case 0:
613
+ _a.trys.push([0, 2, , 3]);
614
+ scriptHash = transaction.scriptHash, operation = transaction.operation, gasLimit = transaction.gasLimit, args = transaction.args;
615
+ params = {
616
+ scriptHash: scriptHash,
617
+ operation: operation,
618
+ args: args.map(function (item) {
619
+ var type = item.type;
620
+ if (["Long", "Integer"].indexOf(type) >= 0) {
621
+ item.value = Number(item.value);
622
+ }
623
+ return item;
624
+ }),
625
+ gasPrice: 2500,
626
+ gasLimit: 60000,
627
+ requireIdentity: false,
628
+ };
629
+ return [4 /*yield*/, ontology_dapi_1.client.api.smartContract.invoke(params)];
630
+ case 1:
631
+ result = _a.sent();
632
+ if (result && (result.transaction)) {
633
+ this.transactionHashCallback(result.transaction);
634
+ }
635
+ else {
636
+ this.errorCallback('Progress transactions submitted.');
637
+ }
638
+ return [3 /*break*/, 3];
639
+ case 2:
640
+ e_6 = _a.sent();
641
+ this.errorCallback((e_6 && e_6.message) || e_6);
642
+ return [3 /*break*/, 3];
643
+ case 3: return [2 /*return*/];
644
+ }
645
+ });
646
+ });
647
+ };
648
+ Swap.prototype.getTerraFee = function (address, msg, gasPrices) {
649
+ return __awaiter(this, void 0, void 0, function () {
650
+ var terra, accountInfo, fee, e_7;
651
+ return __generator(this, function (_a) {
652
+ switch (_a.label) {
653
+ case 0:
654
+ _a.trys.push([0, 3, , 4]);
655
+ terra = new terra_js_1.LCDClient({
656
+ chainID: "columbus-5",
657
+ URL: "https://lcd.terra.dev",
658
+ gasPrices: gasPrices,
659
+ gasAdjustment: 1.75,
660
+ });
661
+ return [4 /*yield*/, terra.auth.accountInfo(address)];
662
+ case 1:
663
+ accountInfo = _a.sent();
664
+ return [4 /*yield*/, terra.tx.estimateFee([
665
+ {
666
+ sequenceNumber: accountInfo.sequence,
667
+ publicKey: accountInfo.public_key,
668
+ },
669
+ ], {
670
+ msgs: [msg],
671
+ feeDenoms: ["uusd"],
672
+ })];
673
+ case 2:
674
+ fee = _a.sent();
675
+ return [2 /*return*/, {
676
+ fee: fee,
677
+ accountInfo: accountInfo,
678
+ }];
679
+ case 3:
680
+ e_7 = _a.sent();
681
+ return [2 /*return*/, null];
682
+ case 4: return [2 /*return*/];
683
+ }
684
+ });
685
+ });
686
+ };
687
+ Swap.prototype.getTerraMsgExecuteContract = function (res, res2, sender, gasPrices) {
688
+ try {
689
+ var inToken = res.inToken, inAmount = res.inAmount, data = res.data;
690
+ var dataObj = data.msgs.map(function (item) {
691
+ return JSON.parse(item);
692
+ });
693
+ var execute_swap_operations = dataObj[0].execute_msg.execute_swap_operations;
694
+ var contract = res2.contract;
695
+ var address = inToken.address;
696
+ var msg = null;
697
+ if (gasPrices[address]) {
698
+ var coins = {};
699
+ coins[address] = +inAmount;
700
+ msg = new terra_js_1.MsgExecuteContract(sender, contract, {
701
+ execute_swap_operations: execute_swap_operations,
702
+ }, coins);
703
+ }
704
+ else {
705
+ msg = new terra_js_1.MsgExecuteContract(sender, address, {
706
+ send: {
707
+ contract: contract,
708
+ amount: inAmount,
709
+ msg: btoa(JSON.stringify({ execute_swap_operations: execute_swap_operations })),
710
+ },
711
+ }, []);
712
+ }
713
+ return msg;
714
+ }
715
+ catch (e) {
716
+ return null;
717
+ }
718
+ };
719
+ Swap.prototype.on = function (events, callback) {
720
+ if (events === 'error') {
721
+ this.errorCallback = callback;
722
+ }
723
+ else if (events === 'transactionHash') {
724
+ this.transactionHashCallback = callback;
725
+ }
726
+ else if (events === 'receipt') {
727
+ this.receiptCallback = callback;
728
+ }
729
+ else if (events === 'success') {
730
+ this.successCallback = callback;
731
+ }
732
+ else if (events === 'getDataSuccess') {
733
+ this.getDataCallback = callback;
734
+ }
735
+ return this;
736
+ };
737
+ return Swap;
738
+ }());
739
+ exports.Swap = Swap;