@openocean.finance/openocean-sdk 1.2.54 → 1.2.56
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/Approve.js +57 -48
- package/lib/swapSdk/index.js +37 -5
- package/package.json +1 -1
package/lib/swapSdk/Approve.js
CHANGED
|
@@ -57,7 +57,7 @@ var Approve = /** @class */ (function () {
|
|
|
57
57
|
}
|
|
58
58
|
Approve.prototype.send = function (reqApproveVo, address) {
|
|
59
59
|
return __awaiter(this, void 0, void 0, function () {
|
|
60
|
-
var web3, data, safeTxHash, e_1, hash, e_2, _a, _b, gasAmount, error_1, json;
|
|
60
|
+
var _gasPrice, web3, data, safeTxHash, e_1, hash, e_2, _a, _b, gasAmount, error_1, json;
|
|
61
61
|
var _this = this;
|
|
62
62
|
return __generator(this, function (_c) {
|
|
63
63
|
switch (_c.label) {
|
|
@@ -66,13 +66,22 @@ var Approve = /** @class */ (function () {
|
|
|
66
66
|
this.key = 0;
|
|
67
67
|
this.approveContract = reqApproveVo.approveContract;
|
|
68
68
|
this.tokenAddress = reqApproveVo.tokenAddress;
|
|
69
|
-
if (String(reqApproveVo.gasPrice).indexOf('.') > -1 || Number(reqApproveVo.gasPrice) < Math.pow(10, 6)) {
|
|
69
|
+
if (reqApproveVo.gasPrice && (String(reqApproveVo.gasPrice).indexOf('.') > -1 || Number(reqApproveVo.gasPrice) < Math.pow(10, 6))) {
|
|
70
70
|
reqApproveVo.gasPrice = new bignumber_js_1.default(reqApproveVo.gasPrice + '').times(Math.pow(10, 9)).toFixed(0);
|
|
71
71
|
}
|
|
72
|
+
return [4 /*yield*/, this.wallet.sdk.eth.getGasPrice()];
|
|
73
|
+
case 1:
|
|
74
|
+
_gasPrice = _c.sent();
|
|
75
|
+
if (_gasPrice && reqApproveVo.gasPrice) {
|
|
76
|
+
reqApproveVo.gasPrice = (0, bignumber_js_1.default)(_gasPrice).comparedTo(reqApproveVo.gasPrice) > 0 ? reqApproveVo.gasPrice : _gasPrice;
|
|
77
|
+
}
|
|
78
|
+
else if (_gasPrice && !reqApproveVo.gasPrice) {
|
|
79
|
+
reqApproveVo.gasPrice = _gasPrice;
|
|
80
|
+
}
|
|
72
81
|
if (reqApproveVo.decimals) {
|
|
73
82
|
reqApproveVo.amount = (0, bignumber_js_1.default)(reqApproveVo.amount).times(Math.pow(10, reqApproveVo.decimals)).toFixed(0);
|
|
74
83
|
}
|
|
75
|
-
if (!(this.wallet.key === "GnosisSafeWallet")) return [3 /*break*/,
|
|
84
|
+
if (!(this.wallet.key === "GnosisSafeWallet")) return [3 /*break*/, 6];
|
|
76
85
|
web3 = new web3_1.default();
|
|
77
86
|
data = web3.eth.abi.encodeFunctionCall({
|
|
78
87
|
inputs: [
|
|
@@ -89,9 +98,9 @@ var Approve = /** @class */ (function () {
|
|
|
89
98
|
type: "function",
|
|
90
99
|
}, [this.approveContract, reqApproveVo.amount]);
|
|
91
100
|
console.log("data", this.approveContract, reqApproveVo.amount, data);
|
|
92
|
-
_c.label =
|
|
93
|
-
case
|
|
94
|
-
_c.trys.push([
|
|
101
|
+
_c.label = 2;
|
|
102
|
+
case 2:
|
|
103
|
+
_c.trys.push([2, 4, , 5]);
|
|
95
104
|
return [4 /*yield*/, this.wallet.sdk.txs.send({
|
|
96
105
|
txs: [
|
|
97
106
|
{
|
|
@@ -101,31 +110,31 @@ var Approve = /** @class */ (function () {
|
|
|
101
110
|
},
|
|
102
111
|
],
|
|
103
112
|
})];
|
|
104
|
-
case
|
|
113
|
+
case 3:
|
|
105
114
|
safeTxHash = (_c.sent()).safeTxHash;
|
|
106
115
|
console.log("safeTxHash", safeTxHash);
|
|
107
116
|
setTimeout(function () {
|
|
108
117
|
console.log("successCallback");
|
|
109
118
|
_this.successCallback(1);
|
|
110
119
|
}, 3000);
|
|
111
|
-
return [3 /*break*/,
|
|
112
|
-
case
|
|
120
|
+
return [3 /*break*/, 5];
|
|
121
|
+
case 4:
|
|
113
122
|
e_1 = _c.sent();
|
|
114
123
|
setTimeout(function () {
|
|
115
124
|
_this.errorCallback(e_1);
|
|
116
125
|
}, 500);
|
|
117
|
-
return [3 /*break*/,
|
|
118
|
-
case
|
|
119
|
-
case 5:
|
|
120
|
-
if (!(this.wallet.key === "LedgerWallet")) return [3 /*break*/, 6];
|
|
121
|
-
this.signEthTransactionByLedger(reqApproveVo, address);
|
|
122
|
-
return [3 /*break*/, 23];
|
|
126
|
+
return [3 /*break*/, 5];
|
|
127
|
+
case 5: return [3 /*break*/, 24];
|
|
123
128
|
case 6:
|
|
124
|
-
if (!(this.wallet.key === "
|
|
125
|
-
this.
|
|
126
|
-
|
|
129
|
+
if (!(this.wallet.key === "LedgerWallet")) return [3 /*break*/, 7];
|
|
130
|
+
this.signEthTransactionByLedger(reqApproveVo, address);
|
|
131
|
+
return [3 /*break*/, 24];
|
|
127
132
|
case 7:
|
|
128
|
-
|
|
133
|
+
if (!(this.wallet.key === "TronLink")) return [3 /*break*/, 15];
|
|
134
|
+
this.approveContract = "TRtQA3D9BFRLFw3rajKBziAof2eAWkAL7B";
|
|
135
|
+
_c.label = 8;
|
|
136
|
+
case 8:
|
|
137
|
+
_c.trys.push([8, 13, , 14]);
|
|
129
138
|
hash = "";
|
|
130
139
|
this.amount = reqApproveVo.amount + "";
|
|
131
140
|
if (!([
|
|
@@ -133,71 +142,71 @@ var Approve = /** @class */ (function () {
|
|
|
133
142
|
"TCFLL5dx5ZJdKnWuesXxi1VPwjLVmWZZy9",
|
|
134
143
|
"TKfjV9RNKJJCqPvBtK8L7Knykh7DNWvnYt",
|
|
135
144
|
"TNUC9Qb1rRpS5CbWLmNMxXBjyFoydXjWFR"
|
|
136
|
-
].indexOf(this.tokenAddress) >= 0)) return [3 /*break*/,
|
|
145
|
+
].indexOf(this.tokenAddress) >= 0)) return [3 /*break*/, 10];
|
|
137
146
|
return [4 /*yield*/, this.contract.methods
|
|
138
147
|
.approve(this.approveContract)
|
|
139
148
|
.send({ feeLimit: 100000000 })];
|
|
140
|
-
case 8:
|
|
141
|
-
hash = _c.sent();
|
|
142
|
-
return [3 /*break*/, 11];
|
|
143
149
|
case 9:
|
|
150
|
+
hash = _c.sent();
|
|
151
|
+
return [3 /*break*/, 12];
|
|
152
|
+
case 10:
|
|
144
153
|
console.log('send approve', this.amount);
|
|
145
154
|
return [4 /*yield*/, this.contract.methods
|
|
146
155
|
.approve(this.approveContract, this.amount)
|
|
147
156
|
.send({ feeLimit: 100000000 })];
|
|
148
|
-
case 10:
|
|
149
|
-
hash = _c.sent();
|
|
150
|
-
_c.label = 11;
|
|
151
157
|
case 11:
|
|
158
|
+
hash = _c.sent();
|
|
159
|
+
_c.label = 12;
|
|
160
|
+
case 12:
|
|
152
161
|
console.log('hash', hash);
|
|
153
162
|
this.successCallback(1);
|
|
154
|
-
return [3 /*break*/,
|
|
155
|
-
case
|
|
163
|
+
return [3 /*break*/, 14];
|
|
164
|
+
case 13:
|
|
156
165
|
e_2 = _c.sent();
|
|
157
166
|
this.errorCallback(e_2);
|
|
158
|
-
return [3 /*break*/,
|
|
159
|
-
case
|
|
160
|
-
case
|
|
161
|
-
if (!!reqApproveVo.amount) return [3 /*break*/,
|
|
167
|
+
return [3 /*break*/, 14];
|
|
168
|
+
case 14: return [3 /*break*/, 24];
|
|
169
|
+
case 15:
|
|
170
|
+
if (!!reqApproveVo.amount) return [3 /*break*/, 17];
|
|
162
171
|
_a = reqApproveVo;
|
|
163
172
|
return [4 /*yield*/, this.contract.methods.totalSupply().call()];
|
|
164
|
-
case 15:
|
|
165
|
-
_a.amount = _c.sent();
|
|
166
|
-
_c.label = 16;
|
|
167
173
|
case 16:
|
|
168
|
-
|
|
174
|
+
_a.amount = _c.sent();
|
|
175
|
+
_c.label = 17;
|
|
176
|
+
case 17:
|
|
177
|
+
if (!(this.tokenAddress === "0xfa17b330bcc4e7f3e2456996d89a5a54ab044831")) return [3 /*break*/, 19];
|
|
169
178
|
_b = reqApproveVo;
|
|
170
179
|
return [4 /*yield*/, this.contract.methods
|
|
171
180
|
.balanceOf(this.account)
|
|
172
181
|
.call()];
|
|
173
|
-
case
|
|
182
|
+
case 18:
|
|
174
183
|
_b.amount = _c.sent();
|
|
175
184
|
if (Number(reqApproveVo.amount) == 0) {
|
|
176
185
|
this.errorCallback("Insufficient Balance.");
|
|
177
186
|
return [2 /*return*/];
|
|
178
187
|
}
|
|
179
|
-
_c.label = 18;
|
|
180
|
-
case 18:
|
|
181
|
-
this.amount = reqApproveVo.amount + "";
|
|
182
|
-
gasAmount = "80000";
|
|
183
188
|
_c.label = 19;
|
|
184
189
|
case 19:
|
|
185
|
-
|
|
190
|
+
this.amount = reqApproveVo.amount + "";
|
|
191
|
+
gasAmount = "80000";
|
|
192
|
+
_c.label = 20;
|
|
193
|
+
case 20:
|
|
194
|
+
_c.trys.push([20, 22, , 23]);
|
|
186
195
|
return [4 /*yield*/, this.contract.methods
|
|
187
196
|
.approve(this.approveContract, this.amount)
|
|
188
197
|
.estimateGas({
|
|
189
198
|
from: this.account,
|
|
190
199
|
})];
|
|
191
|
-
case 20:
|
|
192
|
-
gasAmount = _c.sent();
|
|
193
|
-
return [3 /*break*/, 22];
|
|
194
200
|
case 21:
|
|
201
|
+
gasAmount = _c.sent();
|
|
202
|
+
return [3 /*break*/, 23];
|
|
203
|
+
case 22:
|
|
195
204
|
error_1 = _c.sent();
|
|
196
205
|
setTimeout(function () {
|
|
197
206
|
_this.errorCallback(error_1);
|
|
198
207
|
}, 500);
|
|
199
208
|
return [2 /*return*/];
|
|
200
|
-
case
|
|
209
|
+
case 23:
|
|
201
210
|
json = {
|
|
202
211
|
from: address,
|
|
203
212
|
};
|
|
@@ -220,8 +229,8 @@ var Approve = /** @class */ (function () {
|
|
|
220
229
|
_this.receiptCallback(receipt);
|
|
221
230
|
_this.getSuccess(receipt);
|
|
222
231
|
});
|
|
223
|
-
_c.label =
|
|
224
|
-
case
|
|
232
|
+
_c.label = 24;
|
|
233
|
+
case 24: return [2 /*return*/];
|
|
225
234
|
}
|
|
226
235
|
});
|
|
227
236
|
});
|
package/lib/swapSdk/index.js
CHANGED
|
@@ -35,6 +35,9 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
35
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
38
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
42
|
exports.swapSdk = exports.SwapSdk = void 0;
|
|
40
43
|
var RequestVo_1 = require("./RequestVo");
|
|
@@ -45,6 +48,7 @@ var config_1 = require("../config");
|
|
|
45
48
|
var Swap_1 = require("./Swap");
|
|
46
49
|
var getBalance_1 = require("./getBalance");
|
|
47
50
|
var getAllowance_1 = require("./getAllowance");
|
|
51
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
48
52
|
var wallet_1 = require("@openocean.finance/wallet");
|
|
49
53
|
var SwapSdk = /** @class */ (function () {
|
|
50
54
|
function SwapSdk() {
|
|
@@ -78,11 +82,39 @@ var SwapSdk = /** @class */ (function () {
|
|
|
78
82
|
};
|
|
79
83
|
SwapSdk.prototype.fastSwap = function (swapData) {
|
|
80
84
|
var _this = this;
|
|
81
|
-
return new Promise(function (resolve, reject) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
.
|
|
85
|
-
|
|
85
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
86
|
+
var chainId, _gasPrice;
|
|
87
|
+
return __generator(this, function (_a) {
|
|
88
|
+
switch (_a.label) {
|
|
89
|
+
case 0:
|
|
90
|
+
if (!(this.chain.compiler == 'EVM')) return [3 /*break*/, 2];
|
|
91
|
+
return [4 /*yield*/, this.wallet.sdk.eth.getChainId()];
|
|
92
|
+
case 1:
|
|
93
|
+
chainId = _a.sent();
|
|
94
|
+
console.log('chainId', chainId, swapData.chainId);
|
|
95
|
+
if (chainId != swapData.chainId || !chainId) {
|
|
96
|
+
reject({
|
|
97
|
+
message: "Please be aware: Your wallet's network is different from OpenOcean's. Switch networks before sending transaction."
|
|
98
|
+
});
|
|
99
|
+
return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
_a.label = 2;
|
|
102
|
+
case 2: return [4 /*yield*/, this.wallet.sdk.eth.getGasPrice()];
|
|
103
|
+
case 3:
|
|
104
|
+
_gasPrice = _a.sent();
|
|
105
|
+
if (_gasPrice && swapData.gasPrice) {
|
|
106
|
+
swapData.gasPrice = (0, bignumber_js_1.default)(_gasPrice).comparedTo(swapData.gasPrice) > 0 ? swapData.gasPrice : _gasPrice;
|
|
107
|
+
}
|
|
108
|
+
else if (_gasPrice && !swapData.gasPrice) {
|
|
109
|
+
swapData.gasPrice = _gasPrice;
|
|
110
|
+
}
|
|
111
|
+
this.swap(swapData)
|
|
112
|
+
.on('transactionHash', resolve)
|
|
113
|
+
.on('error', reject);
|
|
114
|
+
return [2 /*return*/];
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}); });
|
|
86
118
|
};
|
|
87
119
|
SwapSdk.prototype.getGas = function (swapData) {
|
|
88
120
|
var swap = new Swap_1.Swap(swapData, this.wallet, this.chain);
|