@openocean.finance/openocean-sdk 1.4.7 → 1.4.9
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/swapSdk/Swap.js
CHANGED
|
@@ -55,6 +55,7 @@ var ontology_dapi_1 = require("@ont-dev/ontology-dapi");
|
|
|
55
55
|
var NotoMobile_1 = require("./NotoMobile");
|
|
56
56
|
var web3_1 = __importDefault(require("web3"));
|
|
57
57
|
var config_1 = require("./../config");
|
|
58
|
+
var solanaTransactionSender_1 = require("./solanaTransactionSender");
|
|
58
59
|
var axios_1 = __importDefault(require("axios"));
|
|
59
60
|
// import bs58 from "bs58";
|
|
60
61
|
var bs58 = require("bs58");
|
|
@@ -190,15 +191,16 @@ var Swap = /** @class */ (function () {
|
|
|
190
191
|
});
|
|
191
192
|
};
|
|
192
193
|
Swap.prototype.sendSolanaTransaction = function () {
|
|
194
|
+
var _a, _b;
|
|
193
195
|
return __awaiter(this, void 0, void 0, function () {
|
|
194
|
-
var res, transaction, signed, result, bytes,
|
|
195
|
-
return __generator(this, function (
|
|
196
|
-
switch (
|
|
196
|
+
var res, transaction, signed, result, bytes, _c, msg, data, bytes, serializedTransaction, transactionResponse, txId, e_1;
|
|
197
|
+
return __generator(this, function (_d) {
|
|
198
|
+
switch (_d.label) {
|
|
197
199
|
case 0:
|
|
198
200
|
res = this.res;
|
|
199
|
-
|
|
201
|
+
_d.label = 1;
|
|
200
202
|
case 1:
|
|
201
|
-
|
|
203
|
+
_d.trys.push([1, 12, , 13]);
|
|
202
204
|
transaction = '';
|
|
203
205
|
if (res.dexId == 6 || res.dexId == 7) {
|
|
204
206
|
transaction = web3_js_1.VersionedTransaction.deserialize(Buffer.from(res.transaction, 'hex'));
|
|
@@ -213,7 +215,7 @@ var Swap = /** @class */ (function () {
|
|
|
213
215
|
params: [transaction],
|
|
214
216
|
})];
|
|
215
217
|
case 2:
|
|
216
|
-
result =
|
|
218
|
+
result = _d.sent();
|
|
217
219
|
console.log("Got signature, submitting transaction");
|
|
218
220
|
bytes = bs58.decode(result.signature);
|
|
219
221
|
transaction.signatures[0].signature = bytes;
|
|
@@ -224,7 +226,7 @@ var Swap = /** @class */ (function () {
|
|
|
224
226
|
if (!this.wallet.sdk.isSlopeWallet) return [3 /*break*/, 5];
|
|
225
227
|
return [4 /*yield*/, this.wallet.sdk.signTransaction(bs58.encode(transaction.serializeMessage ? transaction.serializeMessage() : transaction.message.serialize()))];
|
|
226
228
|
case 4:
|
|
227
|
-
|
|
229
|
+
_c = _d.sent(), msg = _c.msg, data = _c.data;
|
|
228
230
|
if (msg !== "ok")
|
|
229
231
|
return [2 /*return*/];
|
|
230
232
|
bytes = bs58.decode(data.signature);
|
|
@@ -234,29 +236,52 @@ var Swap = /** @class */ (function () {
|
|
|
234
236
|
return [3 /*break*/, 7];
|
|
235
237
|
case 5: return [4 /*yield*/, this.wallet.sdk.signTransaction(transaction)];
|
|
236
238
|
case 6:
|
|
237
|
-
signed =
|
|
238
|
-
|
|
239
|
+
signed = _d.sent();
|
|
240
|
+
_d.label = 7;
|
|
239
241
|
case 7:
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
242
|
+
serializedTransaction = signed.serialize({
|
|
243
|
+
verifySignatures: false,
|
|
244
|
+
requireAllSignatures: false
|
|
245
|
+
});
|
|
246
|
+
if (!(res.dexId == 6)) return [3 /*break*/, 9];
|
|
247
|
+
return [4 /*yield*/, (0, solanaTransactionSender_1.transactionSenderAndConfirmationWaiter)({
|
|
248
|
+
connection: this.wallet.connection,
|
|
249
|
+
// connection,
|
|
250
|
+
serializedTransaction: serializedTransaction,
|
|
251
|
+
blockhashWithExpiryBlockHeight: {
|
|
252
|
+
blockhash: transaction.message.recentBlockhash,
|
|
253
|
+
lastValidBlockHeight: res.lastValidBlockHeight,
|
|
254
|
+
},
|
|
249
255
|
})];
|
|
250
256
|
case 8:
|
|
251
|
-
|
|
257
|
+
transactionResponse = _d.sent();
|
|
258
|
+
if (transactionResponse && transactionResponse.response) {
|
|
259
|
+
if ((_a = transactionResponse.response.meta) === null || _a === void 0 ? void 0 : _a.err) {
|
|
260
|
+
this.errorCallback((_b = transactionResponse.response.meta) === null || _b === void 0 ? void 0 : _b.err);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
this.transactionHashCallback(transactionResponse.txId);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
this.errorCallback('Transaction not confirmed.');
|
|
268
|
+
}
|
|
269
|
+
return [3 /*break*/, 11];
|
|
270
|
+
case 9: return [4 /*yield*/, this.wallet.connection.sendRawTransaction(serializedTransaction, {
|
|
271
|
+
skipPreflight: true,
|
|
272
|
+
preflightCommitment: "confirmed",
|
|
273
|
+
})];
|
|
274
|
+
case 10:
|
|
275
|
+
txId = _d.sent();
|
|
252
276
|
// this.receiptCallback(signature)
|
|
253
277
|
this.transactionHashCallback(txId);
|
|
254
|
-
|
|
255
|
-
case
|
|
256
|
-
|
|
278
|
+
_d.label = 11;
|
|
279
|
+
case 11: return [3 /*break*/, 13];
|
|
280
|
+
case 12:
|
|
281
|
+
e_1 = _d.sent();
|
|
257
282
|
this.errorCallback(e_1.message);
|
|
258
|
-
return [3 /*break*/,
|
|
259
|
-
case
|
|
283
|
+
return [3 /*break*/, 13];
|
|
284
|
+
case 13: return [2 /*return*/];
|
|
260
285
|
}
|
|
261
286
|
});
|
|
262
287
|
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { BlockhashWithExpiryBlockHeight, Connection } from "@solana/web3.js";
|
|
3
|
+
type TransactionSenderAndConfirmationWaiterArgs = {
|
|
4
|
+
connection: Connection;
|
|
5
|
+
serializedTransaction: Buffer;
|
|
6
|
+
blockhashWithExpiryBlockHeight: BlockhashWithExpiryBlockHeight;
|
|
7
|
+
};
|
|
8
|
+
export declare function transactionSenderAndConfirmationWaiter({ connection, serializedTransaction, blockhashWithExpiryBlockHeight, }: TransactionSenderAndConfirmationWaiterArgs): Promise<any>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
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;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
exports.transactionSenderAndConfirmationWaiter = void 0;
|
|
54
|
+
var web3_js_1 = require("@solana/web3.js");
|
|
55
|
+
var promise_retry_1 = __importDefault(require("promise-retry"));
|
|
56
|
+
var wait = function (time) {
|
|
57
|
+
return new Promise(function (resolve) { return setTimeout(resolve, time); });
|
|
58
|
+
};
|
|
59
|
+
function transactionSenderAndConfirmationWaiter(_a) {
|
|
60
|
+
var connection = _a.connection, serializedTransaction = _a.serializedTransaction, blockhashWithExpiryBlockHeight = _a.blockhashWithExpiryBlockHeight;
|
|
61
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
62
|
+
var txid, controller, abortSignal, abortableResender, lastValidBlockHeight, e_1, response;
|
|
63
|
+
var _this = this;
|
|
64
|
+
return __generator(this, function (_b) {
|
|
65
|
+
switch (_b.label) {
|
|
66
|
+
case 0: return [4 /*yield*/, connection.sendRawTransaction(serializedTransaction, {
|
|
67
|
+
skipPreflight: true,
|
|
68
|
+
preflightCommitment: "confirmed",
|
|
69
|
+
})];
|
|
70
|
+
case 1:
|
|
71
|
+
txid = _b.sent();
|
|
72
|
+
console.log('txid', txid);
|
|
73
|
+
controller = new AbortController();
|
|
74
|
+
abortSignal = controller.signal;
|
|
75
|
+
abortableResender = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
76
|
+
var e_2;
|
|
77
|
+
return __generator(this, function (_a) {
|
|
78
|
+
switch (_a.label) {
|
|
79
|
+
case 0:
|
|
80
|
+
if (!true) return [3 /*break*/, 6];
|
|
81
|
+
return [4 /*yield*/, wait(2000)];
|
|
82
|
+
case 1:
|
|
83
|
+
_a.sent();
|
|
84
|
+
if (abortSignal.aborted)
|
|
85
|
+
return [2 /*return*/];
|
|
86
|
+
_a.label = 2;
|
|
87
|
+
case 2:
|
|
88
|
+
_a.trys.push([2, 4, , 5]);
|
|
89
|
+
return [4 /*yield*/, connection.sendRawTransaction(serializedTransaction, {
|
|
90
|
+
skipPreflight: true,
|
|
91
|
+
preflightCommitment: "confirmed",
|
|
92
|
+
})];
|
|
93
|
+
case 3:
|
|
94
|
+
_a.sent();
|
|
95
|
+
console.log('Resender-------------');
|
|
96
|
+
return [3 /*break*/, 5];
|
|
97
|
+
case 4:
|
|
98
|
+
e_2 = _a.sent();
|
|
99
|
+
console.warn("Failed to resend transaction: ".concat(e_2));
|
|
100
|
+
return [3 /*break*/, 5];
|
|
101
|
+
case 5: return [3 /*break*/, 0];
|
|
102
|
+
case 6: return [2 /*return*/];
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}); };
|
|
106
|
+
_b.label = 2;
|
|
107
|
+
case 2:
|
|
108
|
+
_b.trys.push([2, 4, 5, 6]);
|
|
109
|
+
abortableResender();
|
|
110
|
+
lastValidBlockHeight = blockhashWithExpiryBlockHeight.lastValidBlockHeight - 150;
|
|
111
|
+
// this would throw TransactionExpiredBlockheightExceededError
|
|
112
|
+
return [4 /*yield*/, Promise.race([
|
|
113
|
+
connection.confirmTransaction(__assign(__assign({}, blockhashWithExpiryBlockHeight), { lastValidBlockHeight: lastValidBlockHeight, signature: txid, abortSignal: abortSignal }), "confirmed"),
|
|
114
|
+
new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
|
|
115
|
+
var tx;
|
|
116
|
+
var _a;
|
|
117
|
+
return __generator(this, function (_b) {
|
|
118
|
+
switch (_b.label) {
|
|
119
|
+
case 0:
|
|
120
|
+
if (!!abortSignal.aborted) return [3 /*break*/, 3];
|
|
121
|
+
return [4 /*yield*/, wait(2000)];
|
|
122
|
+
case 1:
|
|
123
|
+
_b.sent();
|
|
124
|
+
return [4 /*yield*/, connection.getSignatureStatus(txid, {
|
|
125
|
+
searchTransactionHistory: false,
|
|
126
|
+
})];
|
|
127
|
+
case 2:
|
|
128
|
+
tx = _b.sent();
|
|
129
|
+
if (((_a = tx === null || tx === void 0 ? void 0 : tx.value) === null || _a === void 0 ? void 0 : _a.confirmationStatus) === "confirmed") {
|
|
130
|
+
resolve(tx);
|
|
131
|
+
}
|
|
132
|
+
return [3 /*break*/, 0];
|
|
133
|
+
case 3: return [2 /*return*/];
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}); }),
|
|
137
|
+
])];
|
|
138
|
+
case 3:
|
|
139
|
+
// this would throw TransactionExpiredBlockheightExceededError
|
|
140
|
+
_b.sent();
|
|
141
|
+
return [3 /*break*/, 6];
|
|
142
|
+
case 4:
|
|
143
|
+
e_1 = _b.sent();
|
|
144
|
+
if (e_1 instanceof web3_js_1.TransactionExpiredBlockheightExceededError) {
|
|
145
|
+
// we consume this error and getTransaction would return null
|
|
146
|
+
return [2 /*return*/, null];
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
// invalid state from web3.js
|
|
150
|
+
throw e_1;
|
|
151
|
+
}
|
|
152
|
+
return [3 /*break*/, 6];
|
|
153
|
+
case 5:
|
|
154
|
+
controller.abort();
|
|
155
|
+
return [7 /*endfinally*/];
|
|
156
|
+
case 6:
|
|
157
|
+
response = (0, promise_retry_1.default)(function (retry) { return __awaiter(_this, void 0, void 0, function () {
|
|
158
|
+
var response;
|
|
159
|
+
return __generator(this, function (_a) {
|
|
160
|
+
switch (_a.label) {
|
|
161
|
+
case 0: return [4 /*yield*/, connection.getTransaction(txid, {
|
|
162
|
+
commitment: "confirmed",
|
|
163
|
+
maxSupportedTransactionVersion: 0,
|
|
164
|
+
})];
|
|
165
|
+
case 1:
|
|
166
|
+
response = _a.sent();
|
|
167
|
+
if (!response) {
|
|
168
|
+
retry(response);
|
|
169
|
+
}
|
|
170
|
+
return [2 /*return*/, response];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}); }, {
|
|
174
|
+
retries: 5,
|
|
175
|
+
minTimeout: 1e3,
|
|
176
|
+
});
|
|
177
|
+
return [2 /*return*/, { response: response, txid: txid }];
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
exports.transactionSenderAndConfirmationWaiter = transactionSenderAndConfirmationWaiter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openocean.finance/openocean-sdk",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.9",
|
|
4
4
|
"description": "Openocean sdk",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/big.js": "^4.0.5",
|
|
28
28
|
"@types/jest": "^27.4.1",
|
|
29
|
+
"@types/promise-retry": "^1.1.6",
|
|
29
30
|
"assert": "^2.0.0",
|
|
30
31
|
"babel-jest": "^27.5.1",
|
|
31
32
|
"clean-webpack-plugin": "^4.0.0-alpha.0",
|
|
@@ -48,7 +49,7 @@
|
|
|
48
49
|
"dependencies": {
|
|
49
50
|
"@ethersproject/bignumber": "^5.1.1",
|
|
50
51
|
"@looksrare/sdk": "^0.12.1",
|
|
51
|
-
"@openocean.finance/wallet": "^1.7.
|
|
52
|
+
"@openocean.finance/wallet": "^1.7.9",
|
|
52
53
|
"@uniswap/v2-core": "^1.0.1",
|
|
53
54
|
"@walletconnect/web3-provider": "^1.7.8",
|
|
54
55
|
"aptos": "^1.3.17",
|
|
@@ -69,6 +70,7 @@
|
|
|
69
70
|
"node-polyfill-webpack-plugin": "^1.1.4",
|
|
70
71
|
"ontology-ts-sdk": "^1.1.16",
|
|
71
72
|
"path-browserify": "^1.0.1",
|
|
73
|
+
"promise-retry": "^2.0.1",
|
|
72
74
|
"prompts": "^2.4.1",
|
|
73
75
|
"reflect-metadata": "^0.1.13",
|
|
74
76
|
"tiny-invariant": "^1.3.1",
|