@openocean.finance/openocean-sdk 1.2.2 → 1.2.3

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.
Files changed (59) hide show
  1. package/lib/api/index.js +139 -0
  2. package/lib/api/vo/RequestVo.js +411 -0
  3. package/lib/asset/abi/ERC20_abi.js +119 -0
  4. package/lib/asset/abi/aggregator.js +391 -0
  5. package/lib/config/index.js +33 -0
  6. package/lib/index.d.ts +7 -1
  7. package/lib/index.js +50 -2
  8. package/lib/swapSdk/Approve.js +342 -0
  9. package/lib/swapSdk/NotoMobile.js +141 -0
  10. package/lib/swapSdk/RequestVo.js +94 -0
  11. package/lib/swapSdk/Swap.js +1123 -0
  12. package/lib/swapSdk/getAllowance.js +97 -0
  13. package/lib/swapSdk/getBalance.js +280 -0
  14. package/lib/swapSdk/index.js +257 -0
  15. package/lib/swapSdk/qrcode.d.ts +6 -0
  16. package/lib/swapSdk/qrcode.js +969 -0
  17. package/lib/utils/ajx.js +150 -0
  18. package/lib/utils/index.js +369 -0
  19. package/lib/utils/web3.js +9 -0
  20. package/lib/v1/abis/ERC20.d.ts +16 -0
  21. package/lib/v1/abis/ERC20.js +22 -0
  22. package/lib/v1/abis/IUniswapV2Pair.d.ts +83 -0
  23. package/lib/v1/abis/IUniswapV2Pair.js +1434 -0
  24. package/lib/v1/constants.d.ts +58 -0
  25. package/lib/v1/constants.js +71 -0
  26. package/lib/v1/entities/currency.d.ts +27 -0
  27. package/lib/v1/entities/currency.js +39 -0
  28. package/lib/v1/entities/fractions/currencyAmount.d.ts +19 -0
  29. package/lib/v1/entities/fractions/currencyAmount.js +83 -0
  30. package/lib/v1/entities/fractions/fraction.d.ts +19 -0
  31. package/lib/v1/entities/fractions/fraction.js +109 -0
  32. package/lib/v1/entities/fractions/index.d.ts +5 -0
  33. package/lib/v1/entities/fractions/index.js +21 -0
  34. package/lib/v1/entities/fractions/percent.d.ts +6 -0
  35. package/lib/v1/entities/fractions/percent.js +37 -0
  36. package/lib/v1/entities/fractions/price.d.ts +18 -0
  37. package/lib/v1/entities/fractions/price.js +90 -0
  38. package/lib/v1/entities/fractions/tokenAmount.d.ts +9 -0
  39. package/lib/v1/entities/fractions/tokenAmount.js +43 -0
  40. package/lib/v1/entities/index.d.ts +6 -0
  41. package/lib/v1/entities/index.js +22 -0
  42. package/lib/v1/entities/pair.d.ts +41 -0
  43. package/lib/v1/entities/pair.js +210 -0
  44. package/lib/v1/entities/route.d.ts +14 -0
  45. package/lib/v1/entities/route.js +43 -0
  46. package/lib/v1/entities/token.d.ts +27 -0
  47. package/lib/v1/entities/token.js +87 -0
  48. package/lib/v1/entities/trade.d.ts +106 -0
  49. package/lib/v1/entities/trade.js +336 -0
  50. package/lib/v1/errors.d.ts +16 -0
  51. package/lib/v1/errors.js +56 -0
  52. package/lib/v1/fetcher.d.ts +28 -0
  53. package/lib/v1/fetcher.js +140 -0
  54. package/lib/v1/router.d.ts +56 -0
  55. package/lib/v1/router.js +97 -0
  56. package/lib/v1/utils.d.ts +7 -0
  57. package/lib/v1/utils.js +87 -0
  58. package/package.json +4 -1
  59. package/lib/index.js.LICENSE.txt +0 -279
@@ -0,0 +1,1123 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.Swap = exports.ReqSwapVo = void 0;
43
+ var aggregator_1 = require("../asset/abi/aggregator");
44
+ var RequestVo_1 = require("../api/vo/RequestVo");
45
+ Object.defineProperty(exports, "ReqSwapVo", { enumerable: true, get: function () { return RequestVo_1.ReqSwapVo; } });
46
+ var wallet_1 = require("@openocean.finance/wallet");
47
+ var web3_js_1 = require("@solana/web3.js");
48
+ var terra_js_1 = require("@terra-money/terra.js");
49
+ var ontology_ts_sdk_1 = require("ontology-ts-sdk");
50
+ var aptos_1 = require("aptos");
51
+ var ethers_1 = require("ethers");
52
+ var bn_js_1 = __importDefault(require("bn.js"));
53
+ var borsh_1 = require("borsh");
54
+ var ontology_dapi_1 = require("@ont-dev/ontology-dapi");
55
+ var NotoMobile_1 = require("./NotoMobile");
56
+ var web3_1 = __importDefault(require("web3"));
57
+ var config_1 = require("./../config");
58
+ var axios = require("axios");
59
+ var bs58 = require("bs58");
60
+ ontology_dapi_1.client.registerClient({});
61
+ var providerEth = new ethers_1.ethers.providers.JsonRpcProvider({
62
+ url: "https://rpc.ankr.com/eth"
63
+ });
64
+ var Swap = /** @class */ (function () {
65
+ function Swap(res, wallet, chain, isNew) {
66
+ this.getDataCallback = function () { };
67
+ this.errorCallback = function () { };
68
+ this.transactionHashCallback = function () { };
69
+ this.receiptCallback = function () { };
70
+ this.successCallback = function () { };
71
+ this.res = res;
72
+ this.wallet = wallet;
73
+ this.chain = chain;
74
+ }
75
+ Swap.prototype.send = function () {
76
+ var _this = this;
77
+ setTimeout(function () {
78
+ switch (_this.chain.compiler) {
79
+ case "EVM":
80
+ if (_this.isNew) {
81
+ _this.sendEthTransactionNew();
82
+ }
83
+ else {
84
+ _this.sendEthTransaction();
85
+ }
86
+ break;
87
+ case "SOL":
88
+ _this.sendSolanaTransaction();
89
+ break;
90
+ case "TRON":
91
+ _this.sendTronTransaction();
92
+ break;
93
+ case "TERRA":
94
+ _this.sendTerraTransaction();
95
+ break;
96
+ case "ONT":
97
+ _this.sendONTTransaction();
98
+ break;
99
+ case "APTOS":
100
+ _this.sendAptosTransaction();
101
+ break;
102
+ case "NEAR":
103
+ _this.sendNearTransaction();
104
+ break;
105
+ case "COSMOS":
106
+ _this.sendCosmosTransaction();
107
+ break;
108
+ }
109
+ }, 200);
110
+ return this;
111
+ };
112
+ Swap.prototype.getGas = function () {
113
+ var _this = this;
114
+ return new Promise(function (r, j) {
115
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
116
+ var _a, _b, _c;
117
+ return __generator(this, function (_d) {
118
+ switch (_d.label) {
119
+ case 0:
120
+ _a = this.chain.compiler;
121
+ switch (_a) {
122
+ case "EVM": return [3 /*break*/, 1];
123
+ }
124
+ return [3 /*break*/, 6];
125
+ case 1:
126
+ if (!this.isNew) return [3 /*break*/, 3];
127
+ _b = r;
128
+ return [4 /*yield*/, this.getGasNew()];
129
+ case 2:
130
+ _b.apply(void 0, [_d.sent()]);
131
+ return [3 /*break*/, 5];
132
+ case 3:
133
+ _c = r;
134
+ return [4 /*yield*/, this.getGasOld()];
135
+ case 4:
136
+ _c.apply(void 0, [_d.sent()]);
137
+ _d.label = 5;
138
+ case 5: return [3 /*break*/, 6];
139
+ case 6: return [2 /*return*/];
140
+ }
141
+ });
142
+ }); }, 200);
143
+ });
144
+ };
145
+ Swap.prototype.sendONTTransaction = function () {
146
+ return __awaiter(this, void 0, void 0, function () {
147
+ var _a, approve, swap, transaction, inAmount, inToken, instance_1, account;
148
+ return __generator(this, function (_b) {
149
+ switch (_b.label) {
150
+ case 0:
151
+ _a = this.res, approve = _a.approve, swap = _a.swap, transaction = _a.transaction, inAmount = _a.inAmount, inToken = _a.inToken;
152
+ if (!(this.wallet.key === "OntoMobile")) return [3 /*break*/, 2];
153
+ instance_1 = new NotoMobile_1.NotoMobile(approve ? approve : swap);
154
+ return [4 /*yield*/, new Promise(function (r, q) {
155
+ instance_1.$on("close", function (result, action, account) {
156
+ if (action === "login" && result === "success") {
157
+ r(account);
158
+ }
159
+ else {
160
+ q(action);
161
+ }
162
+ });
163
+ })];
164
+ case 1:
165
+ account = _b.sent();
166
+ this.transactionHashCallback(account);
167
+ return [3 /*break*/, 3];
168
+ case 2:
169
+ if (approve) {
170
+ this.approveOnt(transaction, inAmount, inToken);
171
+ }
172
+ else {
173
+ this.sendOntTransactionSdk(transaction);
174
+ }
175
+ _b.label = 3;
176
+ case 3: return [2 /*return*/];
177
+ }
178
+ });
179
+ });
180
+ };
181
+ Swap.prototype.sendSolanaTransaction = function () {
182
+ return __awaiter(this, void 0, void 0, function () {
183
+ 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;
184
+ return __generator(this, function (_c) {
185
+ switch (_c.label) {
186
+ case 0:
187
+ res = this.res;
188
+ _c.label = 1;
189
+ case 1:
190
+ _c.trys.push([1, 19, , 20]);
191
+ if (!(res.dex == "jupiter")) return [3 /*break*/, 11];
192
+ _a = JSON.parse(res.transaction), setupTransaction = _a.setupTransaction, swapTransaction = _a.swapTransaction, cleanupTransaction = _a.cleanupTransaction;
193
+ list = [
194
+ setupTransaction,
195
+ swapTransaction,
196
+ cleanupTransaction,
197
+ ].filter(Boolean);
198
+ return [4 /*yield*/, this.wallet.connection.getLatestBlockhash()];
199
+ case 2:
200
+ recentBlock_1 = _c.sent();
201
+ transactions = list.map(function (tx) {
202
+ var transaction = web3_js_1.Transaction.from(Buffer.from(tx, "base64"));
203
+ transaction.recentBlockhash = recentBlock_1.blockhash;
204
+ return transaction;
205
+ });
206
+ return [4 /*yield*/, this.wallet.sdk.signAllTransactions(transactions)];
207
+ case 3: return [4 /*yield*/, _c.sent()];
208
+ case 4:
209
+ _c.sent();
210
+ i = 0;
211
+ _i = 0, transactions_1 = transactions;
212
+ _c.label = 5;
213
+ case 5:
214
+ if (!(_i < transactions_1.length)) return [3 /*break*/, 10];
215
+ transaction_1 = transactions_1[_i];
216
+ i++;
217
+ return [4 /*yield*/, this.wallet.connection.sendRawTransaction(transaction_1.serialize({ requireAllSignatures: false }))];
218
+ case 6:
219
+ txid = _c.sent();
220
+ if (!(i < list.length)) return [3 /*break*/, 8];
221
+ return [4 /*yield*/, this.wallet.connection.confirmTransaction(txid)];
222
+ case 7:
223
+ _c.sent();
224
+ return [3 /*break*/, 9];
225
+ case 8:
226
+ this.transactionHashCallback(txid);
227
+ _c.label = 9;
228
+ case 9:
229
+ _i++;
230
+ return [3 /*break*/, 5];
231
+ case 10: return [2 /*return*/];
232
+ case 11:
233
+ transaction = web3_js_1.Transaction.from(Buffer.from(res.transaction, res.dex == "jupiter" ? "base64" : "hex"));
234
+ signed = null;
235
+ signature = null;
236
+ if (!this.wallet.sdk.isCoin98) return [3 /*break*/, 13];
237
+ return [4 /*yield*/, this.wallet.sdk.request({
238
+ method: "sol_sign",
239
+ params: [transaction],
240
+ })];
241
+ case 12:
242
+ result = _c.sent();
243
+ console.log("Got signature, submitting transaction");
244
+ bytes = bs58.decode(result.signature);
245
+ transaction.signatures[0].signature = bytes;
246
+ transaction.feePayer = this.wallet.customPublicKey;
247
+ signed = transaction;
248
+ return [3 /*break*/, 17];
249
+ case 13:
250
+ if (!this.wallet.sdk.isSlopeWallet) return [3 /*break*/, 15];
251
+ return [4 /*yield*/, this.wallet.sdk.signTransaction(bs58.encode(transaction.serializeMessage()))];
252
+ case 14:
253
+ _b = _c.sent(), msg = _b.msg, data = _b.data;
254
+ if (msg !== "ok")
255
+ return [2 /*return*/];
256
+ bytes = bs58.decode(data.signature);
257
+ transaction.signatures[0].signature = bytes;
258
+ transaction.feePayer = this.wallet.customPublicKey;
259
+ signed = transaction;
260
+ return [3 /*break*/, 17];
261
+ case 15: return [4 /*yield*/, this.wallet.sdk.signTransaction(transaction)];
262
+ case 16:
263
+ signed = _c.sent();
264
+ _c.label = 17;
265
+ case 17: return [4 /*yield*/, this.wallet.connection.sendRawTransaction(signed.serialize({
266
+ verifySignatures: false,
267
+ requireAllSignatures: false
268
+ }), {
269
+ skipPreflight: true,
270
+ preflightCommitment: "confirmed",
271
+ })];
272
+ case 18:
273
+ signature = _c.sent();
274
+ // this.receiptCallback(signature)
275
+ this.transactionHashCallback(signature);
276
+ return [3 /*break*/, 20];
277
+ case 19:
278
+ e_1 = _c.sent();
279
+ this.errorCallback(e_1.message);
280
+ return [3 /*break*/, 20];
281
+ case 20: return [2 /*return*/];
282
+ }
283
+ });
284
+ });
285
+ };
286
+ Swap.prototype.sendAptosTransaction = function () {
287
+ return __awaiter(this, void 0, void 0, function () {
288
+ var data, transaction, hash, _a, result, hash, pendingTransaction, client_1, txn, hash, error_1;
289
+ return __generator(this, function (_b) {
290
+ switch (_b.label) {
291
+ case 0:
292
+ _b.trys.push([0, 9, , 10]);
293
+ data = this.res.data;
294
+ console.log("sendAptosTransaction", data, this.wallet.key);
295
+ if (!(this.wallet.key === "MartianWallet")) return [3 /*break*/, 3];
296
+ return [4 /*yield*/, this.wallet.sdk.generateTransaction(this.wallet.address, data)];
297
+ case 1:
298
+ transaction = _b.sent();
299
+ return [4 /*yield*/, this.wallet.sdk.signAndSubmitTransaction(transaction)];
300
+ case 2:
301
+ hash = _b.sent();
302
+ this.transactionHashCallback(hash);
303
+ return [3 /*break*/, 8];
304
+ case 3:
305
+ if (!(this.wallet.key === "PontemWallet")) return [3 /*break*/, 5];
306
+ return [4 /*yield*/, this.wallet.sdk.signAndSubmit(data)];
307
+ case 4:
308
+ _a = (_b.sent()).result, result = _a === void 0 ? {} : _a;
309
+ hash = result.hash;
310
+ console.log("sendAptosTransaction PontemWallet", hash);
311
+ this.transactionHashCallback(hash);
312
+ return [3 /*break*/, 8];
313
+ case 5: return [4 /*yield*/, this.wallet.sdk.signAndSubmitTransaction(data)];
314
+ case 6:
315
+ pendingTransaction = _b.sent();
316
+ client_1 = new aptos_1.AptosClient("https://fullnode.mainnet.aptoslabs.com/v1");
317
+ return [4 /*yield*/, client_1.waitForTransactionWithResult(pendingTransaction.hash)];
318
+ case 7:
319
+ txn = _b.sent();
320
+ console.log("sendAptosTransaction", txn);
321
+ hash = txn.hash;
322
+ this.transactionHashCallback(hash);
323
+ _b.label = 8;
324
+ case 8: return [3 /*break*/, 10];
325
+ case 9:
326
+ error_1 = _b.sent();
327
+ // see "Errors"
328
+ this.errorCallback(error_1);
329
+ return [3 /*break*/, 10];
330
+ case 10: return [2 /*return*/];
331
+ }
332
+ });
333
+ });
334
+ };
335
+ Swap.prototype._createTransaction = function (opt, wallet) {
336
+ return __awaiter(this, void 0, void 0, function () {
337
+ var accountId, localKey, accessKey, block, blockHash, publicKey, nonce;
338
+ return __generator(this, function (_a) {
339
+ switch (_a.label) {
340
+ case 0:
341
+ accountId = wallet.sdk.getAccountId();
342
+ return [4 /*yield*/, wallet.sdk._connectedAccount.connection.signer.getPublicKey(accountId, wallet.sdk._connectedAccount.connection.networkId)];
343
+ case 1:
344
+ localKey = _a.sent();
345
+ return [4 /*yield*/, wallet.sdk._connectedAccount.accessKeyForTransaction(opt.receiverId, opt.actions, localKey)];
346
+ case 2:
347
+ accessKey = _a.sent();
348
+ if (!accessKey) {
349
+ throw new Error("Cannot find matching key for transaction sent to ".concat(opt.receiverId));
350
+ }
351
+ return [4 /*yield*/, wallet.sdk._connectedAccount.connection.provider.block({ finality: 'final' })];
352
+ case 3:
353
+ block = _a.sent();
354
+ blockHash = (0, borsh_1.baseDecode)(block.header.hash);
355
+ publicKey = wallet.PublicKey.from(accessKey.public_key);
356
+ nonce = accessKey.access_key.nonce + opt.nonceOffset;
357
+ return [2 /*return*/, wallet.createTransaction(accountId, publicKey, opt.receiverId, nonce, opt.actions, blockHash)];
358
+ }
359
+ });
360
+ });
361
+ };
362
+ Swap.prototype.sendNearTransaction = function () {
363
+ return __awaiter(this, void 0, void 0, function () {
364
+ var transaction, wallet_2, transactions, currentTransactions, e_2, err, txResult, transaction_2, hash, error_2;
365
+ var _this = this;
366
+ return __generator(this, function (_a) {
367
+ switch (_a.label) {
368
+ case 0:
369
+ _a.trys.push([0, 9, , 10]);
370
+ transaction = this.res.transaction;
371
+ wallet_2 = this.wallet;
372
+ console.log("sendNearTransaction", transaction, wallet_2.key);
373
+ transactions = JSON.parse(Buffer.from(transaction, 'base64').toString());
374
+ if (!(wallet_2.key === "NearWallet" || wallet_2.key === "MyNearWallet")) return [3 /*break*/, 6];
375
+ _a.label = 1;
376
+ case 1:
377
+ _a.trys.push([1, 4, , 5]);
378
+ return [4 /*yield*/, Promise.all(transactions.map(function (t, i) {
379
+ return _this._createTransaction({
380
+ receiverId: t.receiverId,
381
+ nonceOffset: i + 1,
382
+ actions: t.functionCalls.map(function (fc) {
383
+ var data = wallet_2.functionCall(fc.methodName, fc.args, fc.gas ? new bn_js_1.default(fc.gas) : new bn_js_1.default('100000000000000'),
384
+ // fc.deposit ? new BN8(fc.deposit) : new BN8('0')
385
+ fc.deposit ? new bn_js_1.default(wallet_2.utils.format.parseNearAmount(fc.deposit) || '0') : new bn_js_1.default('0'));
386
+ return data;
387
+ })
388
+ }, wallet_2);
389
+ }))];
390
+ case 2:
391
+ currentTransactions = _a.sent();
392
+ return [4 /*yield*/, wallet_2.sdk.requestSignTransactions(currentTransactions)
393
+ // this.transactionHashCallback(hash);
394
+ ];
395
+ case 3:
396
+ _a.sent();
397
+ return [3 /*break*/, 5];
398
+ case 4:
399
+ e_2 = _a.sent();
400
+ err = e_2;
401
+ console.warn(e_2);
402
+ console.log("Error: " + e_2.message);
403
+ this.errorCallback(e_2.message);
404
+ return [3 /*break*/, 5];
405
+ case 5: return [3 /*break*/, 8];
406
+ case 6:
407
+ if (!(wallet_2.key === "MeteorWallet" || wallet_2.key === "SenderWallet")) return [3 /*break*/, 8];
408
+ return [4 /*yield*/, wallet_2.sdk.signAndSendTransactions({ transactions: transactions.map(function (t, i) {
409
+ return {
410
+ receiverId: t.receiverId,
411
+ nonceOffset: i + 1,
412
+ actions: t.functionCalls.map(function (fc) {
413
+ var actions = wallet_2.functionCall(fc.methodName, fc.args, fc.gas ? fc.gas : '100000000000000',
414
+ // fc.deposit ? new BN8(fc.deposit) : new BN8('0')
415
+ fc.deposit ? wallet_2.utils.format.parseNearAmount(fc.deposit) : '0');
416
+ console.log("FunctionCall", actions);
417
+ return {
418
+ type: "FunctionCall",
419
+ enum: actions.enum,
420
+ params: actions.functionCall
421
+ };
422
+ })
423
+ };
424
+ })
425
+ })];
426
+ case 7:
427
+ txResult = _a.sent();
428
+ console.log('signAndSendTransactions', txResult);
429
+ transaction_2 = { hash: "" };
430
+ if (txResult && txResult.length === 1) {
431
+ transaction_2 = txResult[txResult.length - 1].transaction || {};
432
+ }
433
+ else if (txResult && txResult.length > 1) {
434
+ transaction_2 = txResult.filter(function (item) {
435
+ var _a = (item && item.transaction || {}).actions, actions = _a === void 0 ? [] : _a;
436
+ var _actions = actions.filter(function (fc) {
437
+ var _a = fc.FunctionCall, FunctionCall = _a === void 0 ? {} : _a;
438
+ var method_name = FunctionCall.method_name;
439
+ return method_name === 'ft_transfer_call';
440
+ });
441
+ return _actions && _actions.length > 0;
442
+ });
443
+ if (transaction_2 && transaction_2.length) {
444
+ transaction_2 = transaction_2[0].transaction;
445
+ }
446
+ }
447
+ console.log('signAndSendTransactions', transaction_2);
448
+ hash = transaction_2.hash;
449
+ this.transactionHashCallback(hash);
450
+ _a.label = 8;
451
+ case 8: return [3 /*break*/, 10];
452
+ case 9:
453
+ error_2 = _a.sent();
454
+ // see "Errors"
455
+ this.errorCallback(error_2);
456
+ return [3 /*break*/, 10];
457
+ case 10: return [2 /*return*/];
458
+ }
459
+ });
460
+ });
461
+ };
462
+ Swap.prototype.sendCosmosTransaction = function () {
463
+ return __awaiter(this, void 0, void 0, function () {
464
+ var _a, transaction, cosmosReceiver, wallet, transactions, result, e_3, err, error_3;
465
+ return __generator(this, function (_b) {
466
+ switch (_b.label) {
467
+ case 0:
468
+ _b.trys.push([0, 5, , 6]);
469
+ _a = this.res, transaction = _a.transaction, cosmosReceiver = _a.cosmosReceiver;
470
+ wallet = this.wallet;
471
+ console.log("sendNearTransaction", transaction, wallet.key);
472
+ if (!(wallet.key === "KeplrWallet")) return [3 /*break*/, 4];
473
+ transactions = JSON.parse(Buffer.from(transaction, 'base64').toString());
474
+ if (cosmosReceiver && transactions.msgTransfer) {
475
+ transactions.msgTransfer.value.receiver = cosmosReceiver;
476
+ transactions.msg.push(transactions.msgTransfer);
477
+ }
478
+ console.log('transactions', transactions);
479
+ _b.label = 1;
480
+ case 1:
481
+ _b.trys.push([1, 3, , 4]);
482
+ return [4 /*yield*/, wallet.sdk.signAndBroadcast(transactions.account, transactions.msg, transactions.fee, transactions.memo)];
483
+ case 2:
484
+ result = _b.sent();
485
+ // assertIsDeliverTxSuccess(result);
486
+ // console.log('result', result)
487
+ if (result.code !== undefined && result.code !== 0) {
488
+ this.errorCallback(result.log || result.rawLog);
489
+ }
490
+ else {
491
+ this.transactionHashCallback(result.transactionHash);
492
+ }
493
+ return [3 /*break*/, 4];
494
+ case 3:
495
+ e_3 = _b.sent();
496
+ err = e_3;
497
+ console.warn(e_3);
498
+ console.log("Error: " + e_3.message);
499
+ this.errorCallback(e_3.message);
500
+ return [3 /*break*/, 4];
501
+ case 4: return [3 /*break*/, 6];
502
+ case 5:
503
+ error_3 = _b.sent();
504
+ // see "Errors"
505
+ this.errorCallback(error_3);
506
+ return [3 /*break*/, 6];
507
+ case 6: return [2 /*return*/];
508
+ }
509
+ });
510
+ });
511
+ };
512
+ Swap.prototype.getGasOld = function () {
513
+ return __awaiter(this, void 0, void 0, function () {
514
+ var _a, inToken, inAmount, data, to, gas;
515
+ return __generator(this, function (_b) {
516
+ switch (_b.label) {
517
+ case 0:
518
+ _a = this.res, inToken = _a.inToken, inAmount = _a.inAmount, data = _a.data, to = _a.to;
519
+ return [4 /*yield*/, this.wallet.sdk.eth.estimateGas({
520
+ from: this.wallet.address,
521
+ to: to,
522
+ data: data,
523
+ value: wallet_1.chainsObj.isNativeToken(this.chain.key, inToken.address)
524
+ ? inAmount
525
+ : 0,
526
+ })];
527
+ case 1:
528
+ gas = _b.sent();
529
+ return [2 /*return*/, Math.ceil(gas * 1.15)];
530
+ }
531
+ });
532
+ });
533
+ };
534
+ Swap.prototype.getGasNew = function () {
535
+ return __awaiter(this, void 0, void 0, function () {
536
+ var _a, inToken, inAmount, outAmount, outToken, data, to, myWallet, contract, invitee, path, amounts, swapAddr, swapExtraData, gas, e_4;
537
+ return __generator(this, function (_b) {
538
+ switch (_b.label) {
539
+ case 0:
540
+ _b.trys.push([0, 2, , 3]);
541
+ console.log('getGasNew');
542
+ _a = this.res, inToken = _a.inToken, inAmount = _a.inAmount, outAmount = _a.outAmount, outToken = _a.outToken, data = _a.data, to = _a.to;
543
+ myWallet = this.wallet.sdk;
544
+ contract = new myWallet.eth.Contract(aggregator_1.aggregator, (0, config_1.getProxyContract)(this.chain.key));
545
+ invitee = this.wallet.address;
546
+ path = [inToken.address, outToken.address];
547
+ amounts = [inAmount, outAmount];
548
+ swapAddr = to;
549
+ swapExtraData = data;
550
+ return [4 /*yield*/, contract.methods
551
+ .swap(invitee, path, amounts, swapAddr, swapExtraData)
552
+ .estimateGas({
553
+ from: this.wallet.address,
554
+ value: wallet_1.chainsObj.isNativeToken(this.chain.key, inToken.address)
555
+ ? inAmount
556
+ : 0,
557
+ })];
558
+ case 1:
559
+ gas = _b.sent();
560
+ return [2 /*return*/, Math.ceil(gas * 1.5)];
561
+ case 2:
562
+ e_4 = _b.sent();
563
+ console.log('getGasNew error', e_4);
564
+ return [3 /*break*/, 3];
565
+ case 3: return [2 /*return*/];
566
+ }
567
+ });
568
+ });
569
+ };
570
+ Swap.prototype.sendEthTransactionNew = function () {
571
+ return __awaiter(this, void 0, void 0, function () {
572
+ var _a, inToken, inAmount, outAmount, outToken, data, to, ethGasPrice, gasPrice, myWallet, contract, invitee, path, amounts, swapAddr, swapExtraData, swapParams, maxFeePerGas, maxPriorityFeePerGas;
573
+ var _b;
574
+ var _this = this;
575
+ return __generator(this, function (_c) {
576
+ switch (_c.label) {
577
+ case 0:
578
+ _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;
579
+ console.log('sendEthTransactionNew');
580
+ myWallet = this.wallet.sdk;
581
+ contract = new myWallet.eth.Contract(aggregator_1.aggregator, (0, config_1.getProxyContract)(this.chain.key));
582
+ invitee = this.wallet.address;
583
+ path = [inToken.address, outToken.address];
584
+ amounts = [inAmount, outAmount];
585
+ swapAddr = to;
586
+ swapExtraData = data;
587
+ _b = {
588
+ from: this.wallet.address
589
+ };
590
+ return [4 /*yield*/, this.getGasNew()];
591
+ case 1:
592
+ swapParams = (_b.gas = _c.sent(),
593
+ _b.to = to,
594
+ _b.data = data,
595
+ _b);
596
+ if (ethGasPrice) {
597
+ maxFeePerGas = ethGasPrice.maxFeePerGas, maxPriorityFeePerGas = ethGasPrice.maxPriorityFeePerGas;
598
+ if (maxFeePerGas && maxPriorityFeePerGas) {
599
+ swapParams.maxFeePerGas = +maxFeePerGas;
600
+ swapParams.maxPriorityFeePerGas = +maxPriorityFeePerGas;
601
+ }
602
+ }
603
+ else {
604
+ swapParams.gasPrice = +gasPrice;
605
+ }
606
+ if (wallet_1.chainsObj.isNativeToken(this.chain.key, inToken.address)) {
607
+ swapParams.value = inAmount;
608
+ }
609
+ contract.methods
610
+ .swap(invitee, path, amounts, swapAddr, swapExtraData)
611
+ .send(swapParams)
612
+ .on("error", function (error) {
613
+ _this.errorCallback(error);
614
+ })
615
+ .on("transactionHash", function (transactionHash) {
616
+ _this.transactionHashCallback(transactionHash);
617
+ })
618
+ .on("receipt", function (receipt) {
619
+ _this.receiptCallback(receipt);
620
+ // this.getSuccess()
621
+ });
622
+ return [2 /*return*/];
623
+ }
624
+ });
625
+ });
626
+ };
627
+ Swap.prototype.sendEthTransaction = function () {
628
+ return __awaiter(this, void 0, void 0, function () {
629
+ var _a, inToken, inAmount, data, to, ethGasPrice, gasPrice, estimatedGas, swapParams, maxFeePerGas, maxPriorityFeePerGas, safeTxHash, e_5, gas, e_6;
630
+ var _this = this;
631
+ return __generator(this, function (_b) {
632
+ switch (_b.label) {
633
+ case 0:
634
+ _a = this.res, inToken = _a.inToken, inAmount = _a.inAmount, data = _a.data, to = _a.to, ethGasPrice = _a.ethGasPrice, gasPrice = _a.gasPrice, estimatedGas = _a.estimatedGas;
635
+ console.log('sendEthTransaction');
636
+ swapParams = {
637
+ from: this.wallet.address,
638
+ to: to,
639
+ data: data,
640
+ };
641
+ if (ethGasPrice) {
642
+ maxFeePerGas = ethGasPrice.maxFeePerGas, maxPriorityFeePerGas = ethGasPrice.maxPriorityFeePerGas;
643
+ if (maxFeePerGas && maxPriorityFeePerGas) {
644
+ swapParams.maxFeePerGas = +maxFeePerGas;
645
+ swapParams.maxPriorityFeePerGas = +maxPriorityFeePerGas;
646
+ }
647
+ }
648
+ else if (gasPrice) {
649
+ swapParams.gasPrice = +gasPrice;
650
+ }
651
+ if (wallet_1.chainsObj.isNativeToken(this.chain.key, inToken.address)) {
652
+ swapParams.value = inAmount;
653
+ }
654
+ if (!(this.wallet.key === "GnosisSafeWallet")) return [3 /*break*/, 5];
655
+ _b.label = 1;
656
+ case 1:
657
+ _b.trys.push([1, 3, , 4]);
658
+ return [4 /*yield*/, this.wallet.sdk.txs.send({
659
+ txs: [
660
+ {
661
+ to: swapParams.to,
662
+ value: swapParams.value || "0",
663
+ data: swapParams.data,
664
+ },
665
+ ],
666
+ })];
667
+ case 2:
668
+ safeTxHash = (_b.sent()).safeTxHash;
669
+ this.getGnosisSafeTransaction(safeTxHash);
670
+ return [3 /*break*/, 4];
671
+ case 3:
672
+ e_5 = _b.sent();
673
+ this.errorCallback((e_5 && e_5.message) || e_5);
674
+ return [3 /*break*/, 4];
675
+ case 4: return [2 /*return*/];
676
+ case 5:
677
+ if (this.wallet.key === "LedgerWallet") {
678
+ swapParams.estimatedGas = estimatedGas;
679
+ this.signEthTransactionByLedger(swapParams);
680
+ return [2 /*return*/];
681
+ }
682
+ _b.label = 6;
683
+ case 6:
684
+ _b.trys.push([6, 8, , 9]);
685
+ return [4 /*yield*/, this.getGasOld()];
686
+ case 7:
687
+ gas = _b.sent();
688
+ swapParams.gas = gas;
689
+ return [3 /*break*/, 9];
690
+ case 8:
691
+ e_6 = _b.sent();
692
+ if (e_6 && e_6.message.indexOf("JSON-RPC error.") != -1) {
693
+ e_6 = JSON.parse(e_6.message.split("JSON-RPC error.")[1]);
694
+ }
695
+ this.errorCallback((e_6 && e_6.message) || e_6);
696
+ return [3 /*break*/, 9];
697
+ case 9:
698
+ this.wallet.sdk.eth
699
+ .sendTransaction(swapParams)
700
+ .on("error", function (error) {
701
+ _this.errorCallback(error);
702
+ })
703
+ .on("transactionHash", function (transactionHash) {
704
+ _this.transactionHashCallback(transactionHash);
705
+ })
706
+ .on("receipt", function (receipt) {
707
+ _this.receiptCallback(receipt);
708
+ // this.getSuccess()
709
+ });
710
+ return [2 /*return*/];
711
+ }
712
+ });
713
+ });
714
+ };
715
+ Swap.prototype.signEthTransactionByLedger = function (swapParams) {
716
+ return __awaiter(this, void 0, void 0, function () {
717
+ var account, myWallet, gasPrice, txParams, serializedTx, signature, signedTx, hash, e_7;
718
+ var _a;
719
+ return __generator(this, function (_b) {
720
+ switch (_b.label) {
721
+ case 0:
722
+ _b.trys.push([0, 5, , 6]);
723
+ console.log('signEthTransactionByLedger params', swapParams);
724
+ account = this.wallet.address;
725
+ myWallet = this.wallet.sdk;
726
+ return [4 /*yield*/, providerEth.getGasPrice()];
727
+ case 1:
728
+ gasPrice = _b.sent();
729
+ _a = {
730
+ to: swapParams.to,
731
+ gasPrice: gasPrice._hex,
732
+ gasLimit: web3_1.default.utils.numberToHex(swapParams.estimatedGas * 2)
733
+ };
734
+ return [4 /*yield*/, providerEth.getTransactionCount(account, "latest")];
735
+ case 2:
736
+ txParams = (_a.nonce = _b.sent(),
737
+ _a.chainId = 1,
738
+ _a.data = swapParams.data,
739
+ _a.value = web3_1.default.utils.numberToHex(swapParams.value || 0),
740
+ _a);
741
+ serializedTx = ethers_1.ethers.utils.serializeTransaction(txParams).slice(2);
742
+ console.log('signEthTransactionByLedger serializeTransaction', txParams, serializedTx);
743
+ return [4 /*yield*/, myWallet.signTransaction("44'/60'/0'/0/0", serializedTx)];
744
+ case 3:
745
+ signature = _b.sent();
746
+ console.log('signEthTransactionByLedger signature1', signature);
747
+ signature.r = "0x" + signature.r;
748
+ signature.s = "0x" + signature.s;
749
+ signature.v = parseInt("0x" + signature.v);
750
+ signature.from = account;
751
+ console.log('signEthTransactionByLedger signature2', signature);
752
+ signedTx = ethers_1.ethers.utils.serializeTransaction(txParams, signature);
753
+ console.log('signEthTransactionByLedger signedTx', signedTx);
754
+ return [4 /*yield*/, providerEth.sendTransaction(signedTx)];
755
+ case 4:
756
+ hash = (_b.sent()).hash;
757
+ console.log('signEthTransactionByLedger hash', hash);
758
+ this.transactionHashCallback(hash);
759
+ return [3 /*break*/, 6];
760
+ case 5:
761
+ e_7 = _b.sent();
762
+ this.errorCallback(e_7);
763
+ return [3 /*break*/, 6];
764
+ case 6: return [2 /*return*/];
765
+ }
766
+ });
767
+ });
768
+ };
769
+ Swap.prototype.getGnosisSafeTransaction = function (safeTxHash) {
770
+ return __awaiter(this, void 0, void 0, function () {
771
+ var txHash;
772
+ var _this = this;
773
+ return __generator(this, function (_a) {
774
+ switch (_a.label) {
775
+ case 0: return [4 /*yield*/, this.wallet.sdk.txs.getBySafeTxHash(safeTxHash)];
776
+ case 1:
777
+ txHash = (_a.sent()).txHash;
778
+ console.log("safeTxHash", safeTxHash, txHash);
779
+ if (txHash) {
780
+ this.transactionHashCallback(txHash);
781
+ }
782
+ else {
783
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
784
+ return __generator(this, function (_a) {
785
+ switch (_a.label) {
786
+ case 0: return [4 /*yield*/, this.getGnosisSafeTransaction(safeTxHash)];
787
+ case 1:
788
+ _a.sent();
789
+ return [2 /*return*/];
790
+ }
791
+ });
792
+ }); }, 2000);
793
+ }
794
+ return [2 /*return*/];
795
+ }
796
+ });
797
+ });
798
+ };
799
+ Swap.prototype.sendTronTransaction = function () {
800
+ return __awaiter(this, void 0, void 0, function () {
801
+ var _a, inToken, outToken, inAmount, outAmount, minOutAmount, addresses, calldata, offsets, gasLimitsAndValues, abi, contract, _contract, swapParams, e_8;
802
+ var _this = this;
803
+ return __generator(this, function (_b) {
804
+ switch (_b.label) {
805
+ case 0:
806
+ _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;
807
+ abi = [
808
+ {
809
+ "outputs": [
810
+ {
811
+ "name": "outAmount",
812
+ "type": "uint256"
813
+ }
814
+ ],
815
+ "inputs": [
816
+ {
817
+ "name": "inToken",
818
+ "type": "address"
819
+ },
820
+ {
821
+ "name": "outToken",
822
+ "type": "address"
823
+ },
824
+ {
825
+ "name": "inAmount",
826
+ "type": "uint256"
827
+ },
828
+ {
829
+ "name": "minOutAmount",
830
+ "type": "uint256"
831
+ },
832
+ {
833
+ "name": "referrer",
834
+ "type": "address"
835
+ },
836
+ {
837
+ "name": "addressesToCall",
838
+ "type": "address[]"
839
+ },
840
+ {
841
+ "name": "dataToCall",
842
+ "type": "bytes"
843
+ },
844
+ {
845
+ "name": "offsets",
846
+ "type": "uint256[]"
847
+ },
848
+ {
849
+ "name": "gasLimitsAndValues",
850
+ "type": "uint256[]"
851
+ }
852
+ ],
853
+ "name": "swap",
854
+ "stateMutability": "Payable",
855
+ "type": "Function"
856
+ }
857
+ ];
858
+ contract = "TAPJbS45zHWv6ogXFfnw3seiCSfb3RPa6V";
859
+ console.log("sendTronTransaction", this.res);
860
+ return [4 /*yield*/, this.wallet.sdk.contract(abi, contract)];
861
+ case 1:
862
+ _contract = _b.sent();
863
+ swapParams = {
864
+ feeLimit: 100000000,
865
+ };
866
+ if (inToken.toLowerCase() === "t9yd14nj9j7xab4dbgeix9h8unkkhxuwwb") {
867
+ swapParams.callValue = inAmount;
868
+ }
869
+ _b.label = 2;
870
+ case 2:
871
+ _b.trys.push([2, 4, , 5]);
872
+ return [4 /*yield*/, _contract.methods
873
+ .swap(inToken, outToken, inAmount, minOutAmount, '0x0000000000000000000000000000000000000000', addresses, calldata, offsets, gasLimitsAndValues).send(swapParams, function (result, txHash) {
874
+ console.log("state.multicall.methods.swap", result);
875
+ if (result) {
876
+ _this.errorCallback(result.message || result);
877
+ }
878
+ else {
879
+ _this.transactionHashCallback(txHash);
880
+ }
881
+ })];
882
+ case 3:
883
+ _b.sent();
884
+ return [3 /*break*/, 5];
885
+ case 4:
886
+ e_8 = _b.sent();
887
+ this.errorCallback(e_8 || e_8.message);
888
+ return [3 /*break*/, 5];
889
+ case 5: return [2 /*return*/];
890
+ }
891
+ });
892
+ });
893
+ };
894
+ Swap.prototype.sendTerraTransaction = function () {
895
+ return __awaiter(this, void 0, void 0, function () {
896
+ var data, address, gasPrices, msg, _a, fee, accountInfo, e_9;
897
+ var _this = this;
898
+ return __generator(this, function (_b) {
899
+ switch (_b.label) {
900
+ case 0:
901
+ _b.trys.push([0, 6, , 7]);
902
+ return [4 /*yield*/, axios.get("https://ethapi.openocean.finance/v1/terra/exchange")];
903
+ case 1:
904
+ data = (_b.sent()).data;
905
+ address = this.wallet.address;
906
+ return [4 /*yield*/, axios.get("https://ethapi.openocean.finance/v1/terra/gas-price", { cache: true })];
907
+ case 2:
908
+ gasPrices = _b.sent();
909
+ return [4 /*yield*/, this.getTerraMsgExecuteContract(this.res, data, address, gasPrices.data)];
910
+ case 3:
911
+ msg = _b.sent();
912
+ return [4 /*yield*/, this.getTerraFee(address, msg, gasPrices.data)];
913
+ case 4:
914
+ _a = _b.sent(), fee = _a.fee, accountInfo = _a.accountInfo;
915
+ return [4 /*yield*/, this.wallet.sdk.post({
916
+ msgs: [msg],
917
+ gasAdjustment: 1.5,
918
+ waitForConfirmation: true,
919
+ fee: fee,
920
+ account_number: accountInfo.account_number,
921
+ sequence: accountInfo.sequence,
922
+ purgeQueue: true,
923
+ })];
924
+ case 5:
925
+ _b.sent();
926
+ this.wallet.sdk.on("onPost", function (data) {
927
+ var _a = data || {}, result = _a.result, success = _a.success;
928
+ if (success) {
929
+ var txhash = (result || {}).txhash;
930
+ _this.transactionHashCallback(txhash);
931
+ }
932
+ else {
933
+ _this.errorCallback("Transaction failed");
934
+ }
935
+ });
936
+ return [3 /*break*/, 7];
937
+ case 6:
938
+ e_9 = _b.sent();
939
+ this.errorCallback(e_9.message || e_9);
940
+ return [3 /*break*/, 7];
941
+ case 7: return [2 /*return*/];
942
+ }
943
+ });
944
+ });
945
+ };
946
+ Swap.prototype.approveOnt = function (transaction, _amount, inToken) {
947
+ return __awaiter(this, void 0, void 0, function () {
948
+ var scriptHash, operation, gasLimit, args, params, result, e_10;
949
+ return __generator(this, function (_a) {
950
+ switch (_a.label) {
951
+ case 0:
952
+ _a.trys.push([0, 2, , 3]);
953
+ scriptHash = transaction.scriptHash, operation = transaction.operation, gasLimit = transaction.gasLimit, args = transaction.args;
954
+ params = {
955
+ contract: inToken,
956
+ operation: "approve",
957
+ args: [
958
+ {
959
+ type: "Address",
960
+ value: this.wallet.address,
961
+ },
962
+ {
963
+ type: "ByteArray",
964
+ value: ontology_ts_sdk_1.utils.reverseHex(scriptHash),
965
+ },
966
+ {
967
+ type: "ByteArray",
968
+ value: ontology_ts_sdk_1.utils.bigIntToBytes(_amount + ""),
969
+ },
970
+ ],
971
+ gasPrice: 2500,
972
+ gasLimit: 40000,
973
+ };
974
+ return [4 /*yield*/, ontology_dapi_1.client.api.smartContract.invoke(params)];
975
+ case 1:
976
+ result = _a.sent();
977
+ console.log("approveOnt params, result", params, result);
978
+ this.sendOntTransactionSdk(transaction);
979
+ return [3 /*break*/, 3];
980
+ case 2:
981
+ e_10 = _a.sent();
982
+ // tslint:disable-next-line:no-console
983
+ console.log("onScCall error:", e_10);
984
+ this.errorCallback((e_10 && e_10.message) || e_10);
985
+ return [3 /*break*/, 3];
986
+ case 3: return [2 /*return*/];
987
+ }
988
+ });
989
+ });
990
+ };
991
+ Swap.prototype.sendOntTransactionSdk = function (transaction) {
992
+ return __awaiter(this, void 0, void 0, function () {
993
+ var scriptHash, operation, gasLimit, args, params, result, e_11;
994
+ return __generator(this, function (_a) {
995
+ switch (_a.label) {
996
+ case 0:
997
+ _a.trys.push([0, 2, , 3]);
998
+ scriptHash = transaction.scriptHash, operation = transaction.operation, gasLimit = transaction.gasLimit, args = transaction.args;
999
+ params = {
1000
+ scriptHash: scriptHash,
1001
+ operation: operation,
1002
+ args: args.map(function (item) {
1003
+ var type = item.type;
1004
+ if (["Long", "Integer"].indexOf(type) >= 0) {
1005
+ item.value = Number(item.value);
1006
+ }
1007
+ return item;
1008
+ }),
1009
+ gasPrice: 2500,
1010
+ gasLimit: 60000,
1011
+ requireIdentity: false,
1012
+ };
1013
+ return [4 /*yield*/, ontology_dapi_1.client.api.smartContract.invoke(params)];
1014
+ case 1:
1015
+ result = _a.sent();
1016
+ if (result && result.transaction) {
1017
+ this.transactionHashCallback(result.transaction);
1018
+ }
1019
+ else {
1020
+ this.errorCallback("Progress transactions submitted.");
1021
+ }
1022
+ return [3 /*break*/, 3];
1023
+ case 2:
1024
+ e_11 = _a.sent();
1025
+ this.errorCallback((e_11 && e_11.message) || e_11);
1026
+ return [3 /*break*/, 3];
1027
+ case 3: return [2 /*return*/];
1028
+ }
1029
+ });
1030
+ });
1031
+ };
1032
+ Swap.prototype.getTerraFee = function (address, msg, gasPrices) {
1033
+ return __awaiter(this, void 0, void 0, function () {
1034
+ var terra, accountInfo, fee, e_12;
1035
+ return __generator(this, function (_a) {
1036
+ switch (_a.label) {
1037
+ case 0:
1038
+ _a.trys.push([0, 3, , 4]);
1039
+ terra = new terra_js_1.LCDClient({
1040
+ chainID: "columbus-5",
1041
+ URL: "https://lcd.terra.dev",
1042
+ gasPrices: gasPrices,
1043
+ gasAdjustment: 1.75,
1044
+ });
1045
+ return [4 /*yield*/, terra.auth.accountInfo(address)];
1046
+ case 1:
1047
+ accountInfo = _a.sent();
1048
+ return [4 /*yield*/, terra.tx.estimateFee([
1049
+ {
1050
+ sequenceNumber: accountInfo.sequence,
1051
+ publicKey: accountInfo.public_key,
1052
+ },
1053
+ ], {
1054
+ msgs: [msg],
1055
+ feeDenoms: ["uusd"],
1056
+ })];
1057
+ case 2:
1058
+ fee = _a.sent();
1059
+ return [2 /*return*/, {
1060
+ fee: fee,
1061
+ accountInfo: accountInfo,
1062
+ }];
1063
+ case 3:
1064
+ e_12 = _a.sent();
1065
+ return [2 /*return*/, null];
1066
+ case 4: return [2 /*return*/];
1067
+ }
1068
+ });
1069
+ });
1070
+ };
1071
+ Swap.prototype.getTerraMsgExecuteContract = function (res, res2, sender, gasPrices) {
1072
+ try {
1073
+ var inToken = res.inToken, inAmount = res.inAmount, data = res.data;
1074
+ var dataObj = data.msgs.map(function (item) {
1075
+ return JSON.parse(item);
1076
+ });
1077
+ var execute_swap_operations = dataObj[0].execute_msg.execute_swap_operations;
1078
+ var contract = res2.contract;
1079
+ var address = inToken.address;
1080
+ var msg = null;
1081
+ if (gasPrices[address]) {
1082
+ var coins = {};
1083
+ coins[address] = +inAmount;
1084
+ msg = new terra_js_1.MsgExecuteContract(sender, contract, {
1085
+ execute_swap_operations: execute_swap_operations,
1086
+ }, coins);
1087
+ }
1088
+ else {
1089
+ msg = new terra_js_1.MsgExecuteContract(sender, address, {
1090
+ send: {
1091
+ contract: contract,
1092
+ amount: inAmount,
1093
+ msg: btoa(JSON.stringify({ execute_swap_operations: execute_swap_operations })),
1094
+ },
1095
+ }, []);
1096
+ }
1097
+ return msg;
1098
+ }
1099
+ catch (e) {
1100
+ return null;
1101
+ }
1102
+ };
1103
+ Swap.prototype.on = function (events, callback) {
1104
+ if (events === "error") {
1105
+ this.errorCallback = callback;
1106
+ }
1107
+ else if (events === "transactionHash") {
1108
+ this.transactionHashCallback = callback;
1109
+ }
1110
+ else if (events === "receipt") {
1111
+ this.receiptCallback = callback;
1112
+ }
1113
+ else if (events === "success") {
1114
+ this.successCallback = callback;
1115
+ }
1116
+ else if (events === "getDataSuccess") {
1117
+ this.getDataCallback = callback;
1118
+ }
1119
+ return this;
1120
+ };
1121
+ return Swap;
1122
+ }());
1123
+ exports.Swap = Swap;