@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,97 @@
|
|
|
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.getAllowance = void 0;
|
|
40
|
+
var utils_1 = require("./../utils");
|
|
41
|
+
var Chains_1 = require("./../config/Chains");
|
|
42
|
+
var config_1 = require("./../config");
|
|
43
|
+
function getAllowance(account, tokenAddress, decimals, chainName, approveContract, myWallet) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
45
|
+
var contract, approve, e_1, approve, e_2;
|
|
46
|
+
return __generator(this, function (_a) {
|
|
47
|
+
switch (_a.label) {
|
|
48
|
+
case 0:
|
|
49
|
+
contract = null;
|
|
50
|
+
if (!(chainName === 'ont' || chainName === 'solana' || chainName === 'osmosis' || chainName === 'sifchain')) return [3 /*break*/, 1];
|
|
51
|
+
return [2 /*return*/, '-1'];
|
|
52
|
+
case 1:
|
|
53
|
+
if (!(chainName === 'terra')) return [3 /*break*/, 2];
|
|
54
|
+
return [2 /*return*/, '1000000000000000'];
|
|
55
|
+
case 2:
|
|
56
|
+
if (!(chainName === 'tron')) return [3 /*break*/, 7];
|
|
57
|
+
if (tokenAddress.toLowerCase() === "t9yd14nj9j7xab4dbgeix9h8unkkhxuwwb") {
|
|
58
|
+
return [2 /*return*/, '-1'];
|
|
59
|
+
}
|
|
60
|
+
_a.label = 3;
|
|
61
|
+
case 3:
|
|
62
|
+
_a.trys.push([3, 6, , 7]);
|
|
63
|
+
return [4 /*yield*/, myWallet.contract().at(tokenAddress)];
|
|
64
|
+
case 4:
|
|
65
|
+
contract = _a.sent();
|
|
66
|
+
return [4 /*yield*/, contract.methods
|
|
67
|
+
.allowance(account, approveContract)
|
|
68
|
+
.call()];
|
|
69
|
+
case 5:
|
|
70
|
+
approve = _a.sent();
|
|
71
|
+
approve = (approve && approve.approve) || approve || 0;
|
|
72
|
+
return [2 /*return*/, utils_1.utils.getFixed(utils_1.utils.decimals2Amount(approve || 0, decimals), 6)];
|
|
73
|
+
case 6:
|
|
74
|
+
e_1 = _a.sent();
|
|
75
|
+
console.log("triggerConfirmedConstantContract", e_1);
|
|
76
|
+
return [2 /*return*/, '-1'];
|
|
77
|
+
case 7:
|
|
78
|
+
if (!Chains_1.chains.isNativeToken(chainName, tokenAddress)) return [3 /*break*/, 8];
|
|
79
|
+
return [2 /*return*/, '-1'];
|
|
80
|
+
case 8:
|
|
81
|
+
_a.trys.push([8, 10, , 11]);
|
|
82
|
+
contract = new myWallet.eth.Contract(config_1.ERC20_abi, tokenAddress);
|
|
83
|
+
return [4 /*yield*/, contract.methods
|
|
84
|
+
.allowance(account, approveContract)
|
|
85
|
+
.call()];
|
|
86
|
+
case 9:
|
|
87
|
+
approve = _a.sent();
|
|
88
|
+
return [2 /*return*/, utils_1.utils.getFixed(utils_1.utils.decimals2Amount(approve || 0, decimals), 6)];
|
|
89
|
+
case 10:
|
|
90
|
+
e_2 = _a.sent();
|
|
91
|
+
return [2 /*return*/, '-1'];
|
|
92
|
+
case 11: return [2 /*return*/];
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
exports.getAllowance = getAllowance;
|
|
@@ -0,0 +1,221 @@
|
|
|
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.getBalance = void 0;
|
|
43
|
+
var axios = require('axios');
|
|
44
|
+
var utils_1 = require("./../utils");
|
|
45
|
+
var terra_js_1 = require("@terra-money/terra.js");
|
|
46
|
+
var web3_1 = __importDefault(require("web3"));
|
|
47
|
+
var config_1 = require("./../config");
|
|
48
|
+
var web3_js_1 = require("@solana/web3.js");
|
|
49
|
+
var Chains_1 = require("./../config/Chains");
|
|
50
|
+
function getBalance(account, tokenAddressOrSymbol, decimals, chainName, myWallet) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
52
|
+
var balanceObj, balance, contract, sum_1, _a, value, terra, result, _symbol, token, amount, res, e_1, balanceDecimals, e_2, _b, result, e_3;
|
|
53
|
+
return __generator(this, function (_c) {
|
|
54
|
+
switch (_c.label) {
|
|
55
|
+
case 0:
|
|
56
|
+
balanceObj = {
|
|
57
|
+
short: 0,
|
|
58
|
+
long: 0,
|
|
59
|
+
decimals: decimals,
|
|
60
|
+
tokenAddress: tokenAddressOrSymbol
|
|
61
|
+
};
|
|
62
|
+
balance = 0;
|
|
63
|
+
contract = null;
|
|
64
|
+
if (!(chainName === 'ont')) return [3 /*break*/, 2];
|
|
65
|
+
return [4 /*yield*/, axios.get("https://ethapi.openocean.finance/v1/ont/token-balance?account=".concat(account, "&token=").concat(tokenAddressOrSymbol))];
|
|
66
|
+
case 1:
|
|
67
|
+
balance = _c.sent();
|
|
68
|
+
if (balance.data && balance.data[tokenAddressOrSymbol]) {
|
|
69
|
+
balanceObj.short = balance.data[tokenAddressOrSymbol].balance;
|
|
70
|
+
balanceObj.long = balance.data[tokenAddressOrSymbol].balance * (Math.pow(10, decimals));
|
|
71
|
+
}
|
|
72
|
+
return [3 /*break*/, 31];
|
|
73
|
+
case 2:
|
|
74
|
+
if (!(chainName === 'solana')) return [3 /*break*/, 7];
|
|
75
|
+
if (!(tokenAddressOrSymbol === "So11111111111111111111111111111111111111112")) return [3 /*break*/, 4];
|
|
76
|
+
return [4 /*yield*/, myWallet.connection.getBalance(new web3_js_1.PublicKey(account))];
|
|
77
|
+
case 3:
|
|
78
|
+
balance = _c.sent();
|
|
79
|
+
return [3 /*break*/, 6];
|
|
80
|
+
case 4: return [4 /*yield*/, myWallet.connection.getParsedTokenAccountsByOwner(new web3_js_1.PublicKey(account), {
|
|
81
|
+
mint: new web3_js_1.PublicKey(tokenAddressOrSymbol),
|
|
82
|
+
})];
|
|
83
|
+
case 5:
|
|
84
|
+
balance = _c.sent();
|
|
85
|
+
sum_1 = 0;
|
|
86
|
+
_a = (balance || {}).value, value = _a === void 0 ? [] : _a;
|
|
87
|
+
value.forEach(function (item) {
|
|
88
|
+
var account = (item || {}).account;
|
|
89
|
+
var data = (account || {}).data;
|
|
90
|
+
var parsed = (data || {}).parsed;
|
|
91
|
+
var info = (parsed || {}).info;
|
|
92
|
+
var tokenAmount = (info || {}).tokenAmount;
|
|
93
|
+
var _a = (tokenAmount || {}).amount, amount = _a === void 0 ? 0 : _a;
|
|
94
|
+
sum_1 += +amount;
|
|
95
|
+
});
|
|
96
|
+
balance = sum_1;
|
|
97
|
+
_c.label = 6;
|
|
98
|
+
case 6:
|
|
99
|
+
balanceObj.long = balance;
|
|
100
|
+
balanceObj.short = +utils_1.utils.toFixed(utils_1.utils.decimals2Amount(balance || 0, decimals), 6);
|
|
101
|
+
return [3 /*break*/, 31];
|
|
102
|
+
case 7:
|
|
103
|
+
if (!(chainName === 'terra')) return [3 /*break*/, 13];
|
|
104
|
+
terra = new terra_js_1.LCDClient({
|
|
105
|
+
URL: "https://lcd.terra.dev",
|
|
106
|
+
chainID: "columbus-5",
|
|
107
|
+
});
|
|
108
|
+
return [4 /*yield*/, terra.bank.balance(account)];
|
|
109
|
+
case 8:
|
|
110
|
+
result = _c.sent();
|
|
111
|
+
_symbol = {
|
|
112
|
+
'luna': 'uluna',
|
|
113
|
+
'ust': 'uusd',
|
|
114
|
+
}[tokenAddressOrSymbol.toLowerCase()] || tokenAddressOrSymbol.toLowerCase();
|
|
115
|
+
token = result[0]._coins[_symbol];
|
|
116
|
+
amount = 0;
|
|
117
|
+
if (!token) return [3 /*break*/, 9];
|
|
118
|
+
amount = token.amount.toNumber();
|
|
119
|
+
return [3 /*break*/, 12];
|
|
120
|
+
case 9:
|
|
121
|
+
_c.trys.push([9, 11, , 12]);
|
|
122
|
+
return [4 /*yield*/, axios.get("https://fcd.terra.dev/wasm/contracts/".concat(tokenAddressOrSymbol, "/store"), {
|
|
123
|
+
params: {
|
|
124
|
+
query_msg: {
|
|
125
|
+
balance: {
|
|
126
|
+
address: account,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
cache: false,
|
|
131
|
+
})];
|
|
132
|
+
case 10:
|
|
133
|
+
res = _c.sent();
|
|
134
|
+
amount = +(res.result.balance || 0);
|
|
135
|
+
return [3 /*break*/, 12];
|
|
136
|
+
case 11:
|
|
137
|
+
e_1 = _c.sent();
|
|
138
|
+
console.log('terra balanceOf', e_1);
|
|
139
|
+
return [3 /*break*/, 12];
|
|
140
|
+
case 12:
|
|
141
|
+
balanceObj.long = amount;
|
|
142
|
+
balanceObj.short = +utils_1.utils.toFixed(utils_1.utils.decimals2Amount(amount || 0, decimals), 6);
|
|
143
|
+
return [3 /*break*/, 31];
|
|
144
|
+
case 13:
|
|
145
|
+
if (!(chainName === 'tron')) return [3 /*break*/, 20];
|
|
146
|
+
if (!(tokenAddressOrSymbol.toLowerCase() === "t9yd14nj9j7xab4dbgeix9h8unkkhxuwwb")) return [3 /*break*/, 15];
|
|
147
|
+
return [4 /*yield*/, myWallet.sdk.trx.getBalance(account)];
|
|
148
|
+
case 14:
|
|
149
|
+
balance = _c.sent();
|
|
150
|
+
balanceObj.long = myWallet.sdk.fromSun(balance) * (Math.pow(10, decimals));
|
|
151
|
+
balanceObj.short = myWallet.sdk.fromSun(balance);
|
|
152
|
+
_c.label = 15;
|
|
153
|
+
case 15:
|
|
154
|
+
_c.trys.push([15, 18, , 19]);
|
|
155
|
+
return [4 /*yield*/, myWallet.sdk.contract().at(tokenAddressOrSymbol)];
|
|
156
|
+
case 16:
|
|
157
|
+
contract = _c.sent();
|
|
158
|
+
return [4 /*yield*/, contract.balanceOf(account).call()];
|
|
159
|
+
case 17:
|
|
160
|
+
balance = _c.sent();
|
|
161
|
+
balanceDecimals = web3_1.default.utils.hexToNumberString(balance || 0);
|
|
162
|
+
if (balanceDecimals) {
|
|
163
|
+
balanceObj.long = Number(balanceDecimals);
|
|
164
|
+
balanceObj.short = +utils_1.utils.toFixed(utils_1.utils.decimals2Amount(balanceDecimals, decimals), 6);
|
|
165
|
+
}
|
|
166
|
+
return [3 /*break*/, 19];
|
|
167
|
+
case 18:
|
|
168
|
+
e_2 = _c.sent();
|
|
169
|
+
console.log("triggerConfirmedConstantContract", e_2);
|
|
170
|
+
return [3 /*break*/, 19];
|
|
171
|
+
case 19: return [3 /*break*/, 31];
|
|
172
|
+
case 20:
|
|
173
|
+
if (!(chainName === 'osmosis' || chainName === 'sifchain')) return [3 /*break*/, 22];
|
|
174
|
+
return [4 /*yield*/, myWallet.lcdClient.bank.balances(account)];
|
|
175
|
+
case 21:
|
|
176
|
+
_b = (_c.sent()).result, result = _b === void 0 ? [] : _b;
|
|
177
|
+
result.forEach(function (item) {
|
|
178
|
+
var amount = item.amount, denom = item.denom;
|
|
179
|
+
if (denom === tokenAddressOrSymbol) {
|
|
180
|
+
balance = amount;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
balanceObj.long = balance;
|
|
184
|
+
balanceObj.short = +utils_1.utils.toFixed(utils_1.utils.decimals2Amount(balance, decimals), 6);
|
|
185
|
+
return [3 /*break*/, 31];
|
|
186
|
+
case 22:
|
|
187
|
+
if (!Chains_1.chains.isNativeToken(chainName, tokenAddressOrSymbol)) return [3 /*break*/, 27];
|
|
188
|
+
console.log('myWallet', myWallet);
|
|
189
|
+
if (!(myWallet.key === "GnosisSafeWallet")) return [3 /*break*/, 24];
|
|
190
|
+
return [4 /*yield*/, myWallet.sdk.eth.getBalance([account, "latest"])];
|
|
191
|
+
case 23:
|
|
192
|
+
balance = _c.sent();
|
|
193
|
+
return [3 /*break*/, 26];
|
|
194
|
+
case 24: return [4 /*yield*/, myWallet.sdk.eth.getBalance(account)];
|
|
195
|
+
case 25:
|
|
196
|
+
balance = _c.sent();
|
|
197
|
+
_c.label = 26;
|
|
198
|
+
case 26: return [3 /*break*/, 30];
|
|
199
|
+
case 27:
|
|
200
|
+
_c.trys.push([27, 29, , 30]);
|
|
201
|
+
contract = new myWallet.sdk.eth.Contract(config_1.ERC20_abi, tokenAddressOrSymbol);
|
|
202
|
+
return [4 /*yield*/, contract.methods.balanceOf(account).call()];
|
|
203
|
+
case 28:
|
|
204
|
+
// 查看某个账号的代币余额
|
|
205
|
+
balance = _c.sent();
|
|
206
|
+
return [3 /*break*/, 30];
|
|
207
|
+
case 29:
|
|
208
|
+
e_3 = _c.sent();
|
|
209
|
+
return [3 /*break*/, 30];
|
|
210
|
+
case 30:
|
|
211
|
+
if (balance) {
|
|
212
|
+
balanceObj.long = balance;
|
|
213
|
+
balanceObj.short = +utils_1.utils.toFixed(utils_1.utils.decimals2Amount(balance, decimals), 6);
|
|
214
|
+
}
|
|
215
|
+
_c.label = 31;
|
|
216
|
+
case 31: return [2 /*return*/, balanceObj];
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
exports.getBalance = getBalance;
|
|
@@ -0,0 +1,267 @@
|
|
|
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.swapSdk = exports.SwapSdk = void 0;
|
|
43
|
+
var RequestVo_1 = require("./RequestVo");
|
|
44
|
+
var ajx_1 = require("../utils/ajx");
|
|
45
|
+
var utils_1 = require("../utils");
|
|
46
|
+
var ConnectWallet_1 = require("./ConnectWallet");
|
|
47
|
+
var Approve_1 = require("./Approve");
|
|
48
|
+
var config_1 = require("../config");
|
|
49
|
+
var Swap_1 = require("./Swap");
|
|
50
|
+
var getBalance_1 = require("./getBalance");
|
|
51
|
+
var getAllowance_1 = require("./getAllowance");
|
|
52
|
+
var web3_1 = __importDefault(require("web3"));
|
|
53
|
+
var Chains_1 = require("../config/Chains");
|
|
54
|
+
var SwapSdk = /** @class */ (function () {
|
|
55
|
+
function SwapSdk() {
|
|
56
|
+
this.i = 0;
|
|
57
|
+
}
|
|
58
|
+
SwapSdk.prototype.setApi = function (api) {
|
|
59
|
+
this.api = api;
|
|
60
|
+
};
|
|
61
|
+
SwapSdk.prototype.swapQuote = function (reqSwapVo) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0:
|
|
66
|
+
if (!this.wallet)
|
|
67
|
+
throw new Error('No linked wallet');
|
|
68
|
+
if (this.wallet && this.chain.key != reqSwapVo.chain) {
|
|
69
|
+
throw new Error('Need to switch chains');
|
|
70
|
+
}
|
|
71
|
+
return [4 /*yield*/, this.api.swapQuote(reqSwapVo)];
|
|
72
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
SwapSdk.prototype.swap = function (swapData) {
|
|
78
|
+
var swap = new Swap_1.Swap(swapData, this.wallet, this.chain);
|
|
79
|
+
setTimeout(function () {
|
|
80
|
+
swap.send();
|
|
81
|
+
}, 200);
|
|
82
|
+
return swap;
|
|
83
|
+
};
|
|
84
|
+
// public swapLocal(swapData: any) {
|
|
85
|
+
// let swap = new Swap(swapData, this.wallet, this.chain, true)
|
|
86
|
+
// return swap.send()
|
|
87
|
+
// }
|
|
88
|
+
SwapSdk.prototype.fastSwap = function (swapData) {
|
|
89
|
+
var _this = this;
|
|
90
|
+
return new Promise(function (resolve, reject) {
|
|
91
|
+
_this.swap(swapData)
|
|
92
|
+
.on('transactionHash', resolve)
|
|
93
|
+
.on('error', reject);
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
// public fastSwapLocal(swapData: any) {
|
|
97
|
+
// return new Promise((resolve, reject) => {
|
|
98
|
+
// this.swapLocal(swapData)
|
|
99
|
+
// .on('transactionHash', resolve)
|
|
100
|
+
// .on('error', reject);
|
|
101
|
+
// })
|
|
102
|
+
// }
|
|
103
|
+
SwapSdk.prototype.getGas = function (swapData) {
|
|
104
|
+
var swap = new Swap_1.Swap(swapData, this.wallet, this.chain);
|
|
105
|
+
return swap.getGas();
|
|
106
|
+
};
|
|
107
|
+
SwapSdk.prototype.getGasLocal = function (swapData) {
|
|
108
|
+
var swap = new Swap_1.Swap(swapData, this.wallet, this.chain, true);
|
|
109
|
+
return swap.getGas();
|
|
110
|
+
};
|
|
111
|
+
SwapSdk.prototype.approve = function (reqApproveVo) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
+
var errors, contract, approve;
|
|
114
|
+
var _this = this;
|
|
115
|
+
return __generator(this, function (_a) {
|
|
116
|
+
switch (_a.label) {
|
|
117
|
+
case 0:
|
|
118
|
+
if (!this.wallet)
|
|
119
|
+
throw new Error('No linked wallet');
|
|
120
|
+
if (this.wallet && this.chain.key != reqApproveVo.chain)
|
|
121
|
+
throw new Error('Network error');
|
|
122
|
+
return [4 /*yield*/, (0, ajx_1.validateReq)(reqApproveVo, RequestVo_1.ReqApproveVo)];
|
|
123
|
+
case 1:
|
|
124
|
+
errors = _a.sent();
|
|
125
|
+
if (errors)
|
|
126
|
+
throw new Error(errors);
|
|
127
|
+
if (!reqApproveVo.tokenAbi)
|
|
128
|
+
reqApproveVo.tokenAbi = config_1.ERC20_abi;
|
|
129
|
+
contract = null;
|
|
130
|
+
if (this.wallet.sdk && this.wallet.sdk.eth && this.wallet.sdk.eth.Contract) {
|
|
131
|
+
contract = new this.wallet.sdk.eth.Contract(reqApproveVo.tokenAbi, reqApproveVo.tokenAddress);
|
|
132
|
+
if (!contract || !contract.methods || !contract.methods.approve)
|
|
133
|
+
throw new Error('Contract error');
|
|
134
|
+
}
|
|
135
|
+
approve = new Approve_1.Approve(contract, this.wallet);
|
|
136
|
+
setTimeout(function () {
|
|
137
|
+
approve.send(reqApproveVo, _this.wallet.address);
|
|
138
|
+
}, 200);
|
|
139
|
+
return [2 /*return*/, approve];
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
SwapSdk.prototype.connectWallet = function (reqConnectWalletVo) {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
146
|
+
var errors, data, params;
|
|
147
|
+
return __generator(this, function (_a) {
|
|
148
|
+
switch (_a.label) {
|
|
149
|
+
case 0: return [4 /*yield*/, (0, ajx_1.validateReq)(reqConnectWalletVo, RequestVo_1.ReqConnectWalletVo)];
|
|
150
|
+
case 1:
|
|
151
|
+
errors = _a.sent();
|
|
152
|
+
if (!errors) return [3 /*break*/, 2];
|
|
153
|
+
throw new Error(errors);
|
|
154
|
+
case 2: return [4 /*yield*/, ConnectWallet_1.ConnectWallet.link(reqConnectWalletVo)];
|
|
155
|
+
case 3:
|
|
156
|
+
data = _a.sent();
|
|
157
|
+
if (data) {
|
|
158
|
+
if (reqConnectWalletVo.localRpcUrl) {
|
|
159
|
+
this.localProvider = new web3_1.default(new web3_1.default.providers.HttpProvider(reqConnectWalletVo.localRpcUrl));
|
|
160
|
+
this.localRpcUrl = reqConnectWalletVo.localRpcUrl;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.localProvider = null;
|
|
164
|
+
this.localRpcUrl = '';
|
|
165
|
+
if (data.chain.compiler == 'EVM') {
|
|
166
|
+
params = Chains_1.chains.ethereumChainParams[reqConnectWalletVo.chain];
|
|
167
|
+
if (params) {
|
|
168
|
+
this.localRpcUrl = params[0].rpcUrls[0];
|
|
169
|
+
}
|
|
170
|
+
else if (data.chain.chainId == '1') {
|
|
171
|
+
this.localRpcUrl = 'https://mainnet.infura.io/v3/';
|
|
172
|
+
}
|
|
173
|
+
else if (data.chain.chainId == '4') {
|
|
174
|
+
this.localRpcUrl = 'https://rinkeby.infura.io/v3/';
|
|
175
|
+
}
|
|
176
|
+
if (this.localRpcUrl) {
|
|
177
|
+
this.localProvider = data.wallet.sdk;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
this.wallet = data.wallet;
|
|
182
|
+
this.chain = data.chain;
|
|
183
|
+
}
|
|
184
|
+
return [2 /*return*/, this];
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
SwapSdk.prototype.getWallet = function () {
|
|
190
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
191
|
+
return __generator(this, function (_a) {
|
|
192
|
+
switch (_a.label) {
|
|
193
|
+
case 0:
|
|
194
|
+
if (this.wallet)
|
|
195
|
+
return [2 /*return*/, this.wallet];
|
|
196
|
+
return [4 /*yield*/, utils_1.utils.sleep(1000)];
|
|
197
|
+
case 1:
|
|
198
|
+
_a.sent();
|
|
199
|
+
return [2 /*return*/, this.getWallet()];
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
SwapSdk.prototype.getChain = function () {
|
|
205
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
206
|
+
return __generator(this, function (_a) {
|
|
207
|
+
switch (_a.label) {
|
|
208
|
+
case 0:
|
|
209
|
+
if (this.chain)
|
|
210
|
+
return [2 /*return*/, this.chain];
|
|
211
|
+
return [4 /*yield*/, utils_1.utils.sleep(1000)];
|
|
212
|
+
case 1:
|
|
213
|
+
_a.sent();
|
|
214
|
+
return [2 /*return*/, this.getChain()];
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
SwapSdk.prototype.getBalance = function (reqBalanceVo) {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
221
|
+
var errors;
|
|
222
|
+
return __generator(this, function (_a) {
|
|
223
|
+
switch (_a.label) {
|
|
224
|
+
case 0: return [4 /*yield*/, (0, ajx_1.validateReq)(reqBalanceVo, RequestVo_1.ReqBalanceVo)];
|
|
225
|
+
case 1:
|
|
226
|
+
errors = _a.sent();
|
|
227
|
+
if (errors)
|
|
228
|
+
throw new Error(errors);
|
|
229
|
+
if (!reqBalanceVo.account)
|
|
230
|
+
reqBalanceVo.account = this.wallet.address;
|
|
231
|
+
if (!this.localProvider) return [3 /*break*/, 3];
|
|
232
|
+
return [4 /*yield*/, (0, getBalance_1.getBalance)(reqBalanceVo.account, reqBalanceVo.tokenAddressOrSymbol, reqBalanceVo.decimals, reqBalanceVo.chain, {
|
|
233
|
+
sdk: this.localProvider,
|
|
234
|
+
key: this.wallet.key
|
|
235
|
+
})];
|
|
236
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
237
|
+
case 3: return [4 /*yield*/, (0, getBalance_1.getBalance)(reqBalanceVo.account, reqBalanceVo.tokenAddressOrSymbol, reqBalanceVo.decimals, reqBalanceVo.chain, this.wallet)];
|
|
238
|
+
case 4: return [2 /*return*/, _a.sent()];
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
SwapSdk.prototype.getAllowance = function (reqAllowanceVo) {
|
|
244
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
245
|
+
var errors;
|
|
246
|
+
return __generator(this, function (_a) {
|
|
247
|
+
switch (_a.label) {
|
|
248
|
+
case 0: return [4 /*yield*/, (0, ajx_1.validateReq)(reqAllowanceVo, RequestVo_1.ReqAllowanceVo)];
|
|
249
|
+
case 1:
|
|
250
|
+
errors = _a.sent();
|
|
251
|
+
if (errors)
|
|
252
|
+
throw new Error(errors);
|
|
253
|
+
if (!reqAllowanceVo.account)
|
|
254
|
+
reqAllowanceVo.account = this.wallet.sdk.address;
|
|
255
|
+
if (!this.localProvider) return [3 /*break*/, 3];
|
|
256
|
+
return [4 /*yield*/, (0, getAllowance_1.getAllowance)(reqAllowanceVo.account, reqAllowanceVo.tokenAddress, reqAllowanceVo.decimals, reqAllowanceVo.chain, reqAllowanceVo.approveContract, this.localProvider)];
|
|
257
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
258
|
+
case 3: return [4 /*yield*/, (0, getAllowance_1.getAllowance)(reqAllowanceVo.account, reqAllowanceVo.tokenAddress, reqAllowanceVo.decimals, reqAllowanceVo.chain, reqAllowanceVo.approveContract, this.wallet.sdk)];
|
|
259
|
+
case 4: return [2 /*return*/, _a.sent()];
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
return SwapSdk;
|
|
265
|
+
}());
|
|
266
|
+
exports.SwapSdk = SwapSdk;
|
|
267
|
+
exports.swapSdk = new SwapSdk();
|