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