@lifi/sdk 1.0.0
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/CHANGELOG.md +354 -0
- package/LICENSE +1 -0
- package/README.md +16 -0
- package/dist/Lifi.d.ts +186 -0
- package/dist/Lifi.js +461 -0
- package/dist/allowance/index.d.ts +22 -0
- package/dist/allowance/index.js +160 -0
- package/dist/allowance/utils.d.ts +14 -0
- package/dist/allowance/utils.js +137 -0
- package/dist/balances/index.d.ts +11 -0
- package/dist/balances/index.js +104 -0
- package/dist/balances/utils.d.ts +5 -0
- package/dist/balances/utils.js +255 -0
- package/dist/connectors.d.ts +6 -0
- package/dist/connectors.js +150 -0
- package/dist/execution/StatusManager.d.ts +63 -0
- package/dist/execution/StatusManager.js +150 -0
- package/dist/execution/StepExecutor.d.ts +15 -0
- package/dist/execution/StepExecutor.js +139 -0
- package/dist/execution/allowance.execute.d.ts +4 -0
- package/dist/execution/allowance.execute.js +156 -0
- package/dist/execution/balanceCheck.execute.d.ts +3 -0
- package/dist/execution/balanceCheck.execute.js +86 -0
- package/dist/execution/bridges/bridge.execute.d.ts +7 -0
- package/dist/execution/bridges/bridge.execute.js +218 -0
- package/dist/execution/exchanges/swap.execute.d.ts +7 -0
- package/dist/execution/exchanges/swap.execute.js +222 -0
- package/dist/execution/index.d.ts +1 -0
- package/dist/execution/index.js +17 -0
- package/dist/execution/switchChain.d.ts +16 -0
- package/dist/execution/switchChain.js +102 -0
- package/dist/execution/utils.d.ts +5 -0
- package/dist/execution/utils.js +175 -0
- package/dist/helpers.d.ts +18 -0
- package/dist/helpers.js +85 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +27 -0
- package/dist/services/ApiService.d.ts +14 -0
- package/dist/services/ApiService.js +350 -0
- package/dist/services/ChainsService.d.ts +11 -0
- package/dist/services/ChainsService.js +108 -0
- package/dist/services/ConfigService.d.ts +23 -0
- package/dist/services/ConfigService.js +133 -0
- package/dist/typeguards.d.ts +4 -0
- package/dist/typeguards.js +55 -0
- package/dist/types/ERC20.d.ts +22 -0
- package/dist/types/ERC20.js +53 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +22 -0
- package/dist/types/internal.types.d.ts +75 -0
- package/dist/types/internal.types.js +2 -0
- package/dist/utils/errors.d.ts +73 -0
- package/dist/utils/errors.js +147 -0
- package/dist/utils/getProvider.d.ts +3 -0
- package/dist/utils/getProvider.js +11 -0
- package/dist/utils/multicall.d.ts +10 -0
- package/dist/utils/multicall.js +111 -0
- package/dist/utils/multicallAbi.json +313 -0
- package/dist/utils/parseError.d.ts +37 -0
- package/dist/utils/parseError.js +184 -0
- package/dist/utils/utils.d.ts +26 -0
- package/dist/utils/utils.js +188 -0
- package/package.json +90 -0
|
@@ -0,0 +1,137 @@
|
|
|
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.groupByChain = exports.getAllowanceViaMulticall = exports.setApproval = exports.getApproved = void 0;
|
|
43
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
44
|
+
var ethers_1 = require("ethers");
|
|
45
|
+
var ChainsService_1 = __importDefault(require("../services/ChainsService"));
|
|
46
|
+
var types_1 = require("../types");
|
|
47
|
+
var errors_1 = require("../utils/errors");
|
|
48
|
+
var multicall_1 = require("../utils/multicall");
|
|
49
|
+
var getApproved = function (signer, tokenAddress, contractAddress) { return __awaiter(void 0, void 0, void 0, function () {
|
|
50
|
+
var signerAddress, erc20, approved, e_1;
|
|
51
|
+
return __generator(this, function (_a) {
|
|
52
|
+
switch (_a.label) {
|
|
53
|
+
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
54
|
+
case 1:
|
|
55
|
+
signerAddress = _a.sent();
|
|
56
|
+
erc20 = new ethers_1.Contract(tokenAddress, types_1.ERC20_ABI, signer);
|
|
57
|
+
_a.label = 2;
|
|
58
|
+
case 2:
|
|
59
|
+
_a.trys.push([2, 4, , 5]);
|
|
60
|
+
return [4 /*yield*/, erc20.allowance(signerAddress, contractAddress)];
|
|
61
|
+
case 3:
|
|
62
|
+
approved = _a.sent();
|
|
63
|
+
return [2 /*return*/, new bignumber_js_1.default(approved.toString())];
|
|
64
|
+
case 4:
|
|
65
|
+
e_1 = _a.sent();
|
|
66
|
+
return [2 /*return*/, new bignumber_js_1.default(0)];
|
|
67
|
+
case 5: return [2 /*return*/];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}); };
|
|
71
|
+
exports.getApproved = getApproved;
|
|
72
|
+
var setApproval = function (signer, tokenAddress, contractAddress, amount) {
|
|
73
|
+
var erc20 = new ethers_1.Contract(tokenAddress, types_1.ERC20_ABI, signer);
|
|
74
|
+
return erc20.approve(contractAddress, amount);
|
|
75
|
+
};
|
|
76
|
+
exports.setApproval = setApproval;
|
|
77
|
+
var getAllowanceViaMulticall = function (signer, chainId, tokenData) { return __awaiter(void 0, void 0, void 0, function () {
|
|
78
|
+
var chainsService, chain, ownerAddress, calls, result, parsedResult;
|
|
79
|
+
return __generator(this, function (_a) {
|
|
80
|
+
switch (_a.label) {
|
|
81
|
+
case 0:
|
|
82
|
+
chainsService = ChainsService_1.default.getInstance();
|
|
83
|
+
return [4 /*yield*/, chainsService.getChainById(chainId)];
|
|
84
|
+
case 1:
|
|
85
|
+
chain = _a.sent();
|
|
86
|
+
if (!chain.multicallAddress) {
|
|
87
|
+
throw new errors_1.ServerError("No multicall address configured for chainId ".concat(chainId, "."));
|
|
88
|
+
}
|
|
89
|
+
return [4 /*yield*/, signer.getAddress()];
|
|
90
|
+
case 2:
|
|
91
|
+
ownerAddress = _a.sent();
|
|
92
|
+
calls = [];
|
|
93
|
+
tokenData.map(function (_a) {
|
|
94
|
+
var token = _a.token, approvalAddress = _a.approvalAddress;
|
|
95
|
+
calls.push({
|
|
96
|
+
address: token.address,
|
|
97
|
+
name: 'allowance',
|
|
98
|
+
params: [ownerAddress, approvalAddress],
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
return [4 /*yield*/, (0, multicall_1.fetchDataUsingMulticall)(calls, types_1.ERC20_ABI, chainId, chain.multicallAddress)];
|
|
102
|
+
case 3:
|
|
103
|
+
result = _a.sent();
|
|
104
|
+
if (!result.length) {
|
|
105
|
+
throw new errors_1.ServerError("Couldn't load allowance from chainId ".concat(chainId, " using multicall."));
|
|
106
|
+
}
|
|
107
|
+
parsedResult = result.map(function (_a) {
|
|
108
|
+
var _b;
|
|
109
|
+
var data = _a.data;
|
|
110
|
+
return ({
|
|
111
|
+
approvalAmount: (_b = data) !== null && _b !== void 0 ? _b : new bignumber_js_1.default(0),
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
return [2 /*return*/, tokenData.map(function (_a, i) {
|
|
115
|
+
var token = _a.token, approvalAddress = _a.approvalAddress;
|
|
116
|
+
return ({
|
|
117
|
+
token: token,
|
|
118
|
+
approvalAddress: approvalAddress,
|
|
119
|
+
approvedAmount: parsedResult[i].approvalAmount,
|
|
120
|
+
});
|
|
121
|
+
})];
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}); };
|
|
125
|
+
exports.getAllowanceViaMulticall = getAllowanceViaMulticall;
|
|
126
|
+
var groupByChain = function (tokenDataList) {
|
|
127
|
+
// group by chain
|
|
128
|
+
var tokenDataByChain = {};
|
|
129
|
+
tokenDataList.forEach(function (tokenData) {
|
|
130
|
+
if (!tokenDataByChain[tokenData.token.chainId]) {
|
|
131
|
+
tokenDataByChain[tokenData.token.chainId] = [];
|
|
132
|
+
}
|
|
133
|
+
tokenDataByChain[tokenData.token.chainId].push(tokenData);
|
|
134
|
+
});
|
|
135
|
+
return tokenDataByChain;
|
|
136
|
+
};
|
|
137
|
+
exports.groupByChain = groupByChain;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Token, TokenAmount } from '@lifi/types';
|
|
2
|
+
declare const _default: {
|
|
3
|
+
getTokenBalance: (walletAddress: string, token: Token) => Promise<TokenAmount | null>;
|
|
4
|
+
getTokenBalances: (walletAddress: string, tokens: Token[]) => Promise<TokenAmount[]>;
|
|
5
|
+
getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
|
|
6
|
+
[chainId: number]: Token[];
|
|
7
|
+
}) => Promise<{
|
|
8
|
+
[chainId: number]: TokenAmount[];
|
|
9
|
+
}>;
|
|
10
|
+
};
|
|
11
|
+
export default _default;
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
var utils_1 = __importDefault(require("./utils"));
|
|
43
|
+
var getTokenBalance = function (walletAddress, token) { return __awaiter(void 0, void 0, void 0, function () {
|
|
44
|
+
var tokenAmounts;
|
|
45
|
+
return __generator(this, function (_a) {
|
|
46
|
+
switch (_a.label) {
|
|
47
|
+
case 0: return [4 /*yield*/, getTokenBalances(walletAddress, [token])];
|
|
48
|
+
case 1:
|
|
49
|
+
tokenAmounts = _a.sent();
|
|
50
|
+
return [2 /*return*/, tokenAmounts.length ? tokenAmounts[0] : null];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}); };
|
|
54
|
+
var getTokenBalances = function (walletAddress, tokens) { return __awaiter(void 0, void 0, void 0, function () {
|
|
55
|
+
var tokensByChain, tokenAmountsByChain;
|
|
56
|
+
return __generator(this, function (_a) {
|
|
57
|
+
switch (_a.label) {
|
|
58
|
+
case 0:
|
|
59
|
+
tokensByChain = {};
|
|
60
|
+
tokens.forEach(function (token) {
|
|
61
|
+
if (!tokensByChain[token.chainId]) {
|
|
62
|
+
tokensByChain[token.chainId] = [];
|
|
63
|
+
}
|
|
64
|
+
tokensByChain[token.chainId].push(token);
|
|
65
|
+
});
|
|
66
|
+
return [4 /*yield*/, getTokenBalancesForChains(walletAddress, tokensByChain)];
|
|
67
|
+
case 1:
|
|
68
|
+
tokenAmountsByChain = _a.sent();
|
|
69
|
+
return [2 /*return*/, Object.values(tokenAmountsByChain).flat()];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}); };
|
|
73
|
+
var getTokenBalancesForChains = function (walletAddress, tokensByChain) { return __awaiter(void 0, void 0, void 0, function () {
|
|
74
|
+
var tokenAmountsByChain, promises;
|
|
75
|
+
return __generator(this, function (_a) {
|
|
76
|
+
switch (_a.label) {
|
|
77
|
+
case 0:
|
|
78
|
+
tokenAmountsByChain = {};
|
|
79
|
+
promises = Object.keys(tokensByChain).map(function (chainIdStr) { return __awaiter(void 0, void 0, void 0, function () {
|
|
80
|
+
var chainId, tokenAmounts;
|
|
81
|
+
return __generator(this, function (_a) {
|
|
82
|
+
switch (_a.label) {
|
|
83
|
+
case 0:
|
|
84
|
+
chainId = parseInt(chainIdStr);
|
|
85
|
+
return [4 /*yield*/, utils_1.default.getBalances(walletAddress, tokensByChain[chainId])];
|
|
86
|
+
case 1:
|
|
87
|
+
tokenAmounts = _a.sent();
|
|
88
|
+
tokenAmountsByChain[chainId] = tokenAmounts;
|
|
89
|
+
return [2 /*return*/];
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}); });
|
|
93
|
+
return [4 /*yield*/, Promise.allSettled(promises)];
|
|
94
|
+
case 1:
|
|
95
|
+
_a.sent();
|
|
96
|
+
return [2 /*return*/, tokenAmountsByChain];
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}); };
|
|
100
|
+
exports.default = {
|
|
101
|
+
getTokenBalance: getTokenBalance,
|
|
102
|
+
getTokenBalances: getTokenBalances,
|
|
103
|
+
getTokenBalancesForChains: getTokenBalancesForChains,
|
|
104
|
+
};
|
|
@@ -0,0 +1,255 @@
|
|
|
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 (_) 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
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
54
|
+
var ethers_1 = require("ethers");
|
|
55
|
+
var connectors_1 = require("../connectors");
|
|
56
|
+
var multicall_1 = require("../utils/multicall");
|
|
57
|
+
var utils_1 = require("../utils/utils");
|
|
58
|
+
var balanceAbi = [
|
|
59
|
+
{
|
|
60
|
+
constant: true,
|
|
61
|
+
inputs: [{ name: 'who', type: 'address' }],
|
|
62
|
+
name: 'balanceOf',
|
|
63
|
+
outputs: [{ name: '', type: 'uint256' }],
|
|
64
|
+
payable: false,
|
|
65
|
+
stateMutability: 'view',
|
|
66
|
+
type: 'function',
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
constant: true,
|
|
70
|
+
inputs: [{ name: 'addr', type: 'address' }],
|
|
71
|
+
name: 'getEthBalance',
|
|
72
|
+
outputs: [{ name: 'balance', type: 'uint256' }],
|
|
73
|
+
payable: false,
|
|
74
|
+
stateMutability: 'view',
|
|
75
|
+
type: 'function',
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
var getBalances = function (walletAddress, tokens) { return __awaiter(void 0, void 0, void 0, function () {
|
|
79
|
+
var chainId;
|
|
80
|
+
return __generator(this, function (_a) {
|
|
81
|
+
switch (_a.label) {
|
|
82
|
+
case 0:
|
|
83
|
+
if (tokens.length === 0) {
|
|
84
|
+
return [2 /*return*/, []];
|
|
85
|
+
}
|
|
86
|
+
chainId = tokens[0].chainId;
|
|
87
|
+
tokens.forEach(function (token) {
|
|
88
|
+
if (token.chainId !== chainId) {
|
|
89
|
+
// eslint-disable-next-line no-console
|
|
90
|
+
console.warn("Requested tokens have to be on same chain.");
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return [4 /*yield*/, (0, connectors_1.getMulticallAddress)(chainId)];
|
|
95
|
+
case 1:
|
|
96
|
+
if ((_a.sent()) && tokens.length > 1) {
|
|
97
|
+
return [2 /*return*/, getBalancesFromProviderUsingMulticall(walletAddress, tokens)];
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
return [2 /*return*/, getBalancesFromProvider(walletAddress, tokens)];
|
|
101
|
+
}
|
|
102
|
+
return [2 /*return*/];
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}); };
|
|
106
|
+
var getBalancesFromProviderUsingMulticall = function (walletAddress, tokens) { return __awaiter(void 0, void 0, void 0, function () {
|
|
107
|
+
var chainId, multicallAddress;
|
|
108
|
+
return __generator(this, function (_a) {
|
|
109
|
+
switch (_a.label) {
|
|
110
|
+
case 0:
|
|
111
|
+
chainId = tokens[0].chainId;
|
|
112
|
+
return [4 /*yield*/, (0, connectors_1.getMulticallAddress)(chainId)];
|
|
113
|
+
case 1:
|
|
114
|
+
multicallAddress = _a.sent();
|
|
115
|
+
if (!multicallAddress) {
|
|
116
|
+
throw new Error('No multicallAddress found for the given chain.');
|
|
117
|
+
}
|
|
118
|
+
return [2 /*return*/, executeMulticall(walletAddress, tokens, multicallAddress, chainId)];
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}); };
|
|
122
|
+
var executeMulticall = function (walletAddress, tokens, multicallAddress, chainId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
123
|
+
var calls, res;
|
|
124
|
+
return __generator(this, function (_a) {
|
|
125
|
+
switch (_a.label) {
|
|
126
|
+
case 0:
|
|
127
|
+
calls = [];
|
|
128
|
+
tokens.map(function (token) {
|
|
129
|
+
if ((0, utils_1.isZeroAddress)(token.address)) {
|
|
130
|
+
calls.push({
|
|
131
|
+
address: multicallAddress,
|
|
132
|
+
name: 'getEthBalance',
|
|
133
|
+
params: [walletAddress],
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
calls.push({
|
|
138
|
+
address: token.address,
|
|
139
|
+
name: 'balanceOf',
|
|
140
|
+
params: [walletAddress],
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return [4 /*yield*/, fetchViaMulticall(calls, balanceAbi, chainId, multicallAddress)];
|
|
145
|
+
case 1:
|
|
146
|
+
res = _a.sent();
|
|
147
|
+
if (!res.length) {
|
|
148
|
+
return [2 /*return*/, []];
|
|
149
|
+
}
|
|
150
|
+
return [2 /*return*/, tokens.map(function (token, i) {
|
|
151
|
+
var amount = new bignumber_js_1.default(res[i].amount.toString() || '0')
|
|
152
|
+
.shiftedBy(-token.decimals)
|
|
153
|
+
.toFixed();
|
|
154
|
+
return __assign(__assign({}, token), { amount: amount || '0', blockNumber: res[i].blockNumber });
|
|
155
|
+
})];
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}); };
|
|
159
|
+
var fetchViaMulticall = function (calls, abi, chainId, multicallAddress) { return __awaiter(void 0, void 0, void 0, function () {
|
|
160
|
+
var result;
|
|
161
|
+
return __generator(this, function (_a) {
|
|
162
|
+
switch (_a.label) {
|
|
163
|
+
case 0: return [4 /*yield*/, (0, multicall_1.fetchDataUsingMulticall)(calls, abi, chainId, multicallAddress)];
|
|
164
|
+
case 1:
|
|
165
|
+
result = _a.sent();
|
|
166
|
+
return [2 /*return*/, result.map(function (_a) {
|
|
167
|
+
var data = _a.data, blockNumber = _a.blockNumber;
|
|
168
|
+
return ({
|
|
169
|
+
amount: data ? data : new bignumber_js_1.default(0),
|
|
170
|
+
blockNumber: blockNumber,
|
|
171
|
+
});
|
|
172
|
+
})];
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}); };
|
|
176
|
+
var getBalancesFromProvider = function (walletAddress, tokens) { return __awaiter(void 0, void 0, void 0, function () {
|
|
177
|
+
var chainId, rpc, tokenAmountPromises;
|
|
178
|
+
return __generator(this, function (_a) {
|
|
179
|
+
switch (_a.label) {
|
|
180
|
+
case 0:
|
|
181
|
+
chainId = tokens[0].chainId;
|
|
182
|
+
return [4 /*yield*/, (0, connectors_1.getRpcProvider)(chainId)];
|
|
183
|
+
case 1:
|
|
184
|
+
rpc = _a.sent();
|
|
185
|
+
tokenAmountPromises = tokens.map(function (token) { return __awaiter(void 0, void 0, void 0, function () {
|
|
186
|
+
var amount, blockNumber, balance, e_1;
|
|
187
|
+
return __generator(this, function (_a) {
|
|
188
|
+
switch (_a.label) {
|
|
189
|
+
case 0:
|
|
190
|
+
amount = '0';
|
|
191
|
+
_a.label = 1;
|
|
192
|
+
case 1:
|
|
193
|
+
_a.trys.push([1, 3, , 4]);
|
|
194
|
+
return [4 /*yield*/, getBalanceFromProvider(walletAddress, token.address, chainId, rpc)];
|
|
195
|
+
case 2:
|
|
196
|
+
balance = _a.sent();
|
|
197
|
+
amount = new bignumber_js_1.default(balance.amount.toString())
|
|
198
|
+
.shiftedBy(-token.decimals)
|
|
199
|
+
.toString();
|
|
200
|
+
blockNumber = balance.blockNumber;
|
|
201
|
+
return [3 /*break*/, 4];
|
|
202
|
+
case 3:
|
|
203
|
+
e_1 = _a.sent();
|
|
204
|
+
// eslint-disable-next-line no-console
|
|
205
|
+
console.warn(e_1);
|
|
206
|
+
return [3 /*break*/, 4];
|
|
207
|
+
case 4: return [2 /*return*/, __assign(__assign({}, token), { amount: amount, blockNumber: blockNumber })];
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}); });
|
|
211
|
+
return [2 /*return*/, Promise.all(tokenAmountPromises)];
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}); };
|
|
215
|
+
var getBalanceFromProvider = function (walletAddress, assetId, chainId, provider) { return __awaiter(void 0, void 0, void 0, function () {
|
|
216
|
+
var blockNumber, balance, contract;
|
|
217
|
+
return __generator(this, function (_a) {
|
|
218
|
+
switch (_a.label) {
|
|
219
|
+
case 0: return [4 /*yield*/, getCurrentBlockNumber(chainId)];
|
|
220
|
+
case 1:
|
|
221
|
+
blockNumber = _a.sent();
|
|
222
|
+
if (!(0, utils_1.isZeroAddress)(assetId)) return [3 /*break*/, 3];
|
|
223
|
+
return [4 /*yield*/, provider.getBalance(walletAddress, blockNumber)];
|
|
224
|
+
case 2:
|
|
225
|
+
balance = _a.sent();
|
|
226
|
+
return [3 /*break*/, 5];
|
|
227
|
+
case 3:
|
|
228
|
+
contract = new ethers_1.ethers.Contract(assetId, ['function balanceOf(address owner) view returns (uint256)'], provider);
|
|
229
|
+
return [4 /*yield*/, contract.balanceOf(walletAddress, {
|
|
230
|
+
blockTag: blockNumber,
|
|
231
|
+
})];
|
|
232
|
+
case 4:
|
|
233
|
+
balance = _a.sent();
|
|
234
|
+
_a.label = 5;
|
|
235
|
+
case 5: return [2 /*return*/, {
|
|
236
|
+
amount: balance,
|
|
237
|
+
blockNumber: blockNumber,
|
|
238
|
+
}];
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}); };
|
|
242
|
+
var getCurrentBlockNumber = function (chainId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
243
|
+
var rpc;
|
|
244
|
+
return __generator(this, function (_a) {
|
|
245
|
+
switch (_a.label) {
|
|
246
|
+
case 0: return [4 /*yield*/, (0, connectors_1.getRpcProvider)(chainId)];
|
|
247
|
+
case 1:
|
|
248
|
+
rpc = _a.sent();
|
|
249
|
+
return [2 /*return*/, rpc.getBlockNumber()];
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}); };
|
|
253
|
+
exports.default = {
|
|
254
|
+
getBalances: getBalances,
|
|
255
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ChainId } from './types';
|
|
2
|
+
import { FallbackProvider } from '@ethersproject/providers';
|
|
3
|
+
export declare const getRpcUrl: (chainId: ChainId, archive?: boolean) => Promise<string>;
|
|
4
|
+
export declare const getRpcUrls: (chainId: ChainId, archive?: boolean) => Promise<string[]>;
|
|
5
|
+
export declare const getRpcProvider: (chainId: number, archive?: boolean) => Promise<FallbackProvider>;
|
|
6
|
+
export declare const getMulticallAddress: (chainId: ChainId) => Promise<string | undefined>;
|
|
@@ -0,0 +1,150 @@
|
|
|
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
|
+
var _a;
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.getMulticallAddress = exports.getRpcProvider = exports.getRpcUrls = exports.getRpcUrl = void 0;
|
|
44
|
+
var ethers_1 = require("ethers");
|
|
45
|
+
var _1 = require(".");
|
|
46
|
+
var types_1 = require("./types");
|
|
47
|
+
var ConfigService_1 = __importDefault(require("./services/ConfigService"));
|
|
48
|
+
// cached providers
|
|
49
|
+
var chainProviders = {};
|
|
50
|
+
// Archive RPC Provider
|
|
51
|
+
var archiveRpcs = (_a = {},
|
|
52
|
+
_a[types_1.ChainId.ETH] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/eth/mainnet/archive',
|
|
53
|
+
_a[types_1.ChainId.BSC] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/bsc/mainnet/archive',
|
|
54
|
+
_a[types_1.ChainId.POL] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/polygon/mainnet/archive',
|
|
55
|
+
_a[types_1.ChainId.FTM] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/fantom/mainnet',
|
|
56
|
+
_a);
|
|
57
|
+
// RPC Urls
|
|
58
|
+
var getRpcUrl = function (chainId, archive) {
|
|
59
|
+
if (archive === void 0) { archive = false; }
|
|
60
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
61
|
+
var rpcUrls;
|
|
62
|
+
return __generator(this, function (_a) {
|
|
63
|
+
switch (_a.label) {
|
|
64
|
+
case 0: return [4 /*yield*/, (0, exports.getRpcUrls)(chainId, archive)];
|
|
65
|
+
case 1:
|
|
66
|
+
rpcUrls = _a.sent();
|
|
67
|
+
return [2 /*return*/, rpcUrls[0]];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
exports.getRpcUrl = getRpcUrl;
|
|
73
|
+
var getRpcUrls = function (chainId, archive) {
|
|
74
|
+
if (archive === void 0) { archive = false; }
|
|
75
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
76
|
+
var configService, config;
|
|
77
|
+
return __generator(this, function (_a) {
|
|
78
|
+
switch (_a.label) {
|
|
79
|
+
case 0:
|
|
80
|
+
if (archive && archiveRpcs[chainId]) {
|
|
81
|
+
return [2 /*return*/, [archiveRpcs[chainId]]];
|
|
82
|
+
}
|
|
83
|
+
configService = ConfigService_1.default.getInstance();
|
|
84
|
+
return [4 /*yield*/, configService.getConfigAsync()];
|
|
85
|
+
case 1:
|
|
86
|
+
config = _a.sent();
|
|
87
|
+
return [2 /*return*/, config.rpcs[chainId]];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
exports.getRpcUrls = getRpcUrls;
|
|
93
|
+
var getRandomProvider = function (providerList) {
|
|
94
|
+
var index = (0, _1.getRandomNumber)(0, providerList.length - 1);
|
|
95
|
+
return providerList[index];
|
|
96
|
+
};
|
|
97
|
+
// Provider
|
|
98
|
+
var getRpcProvider = function (chainId, archive) {
|
|
99
|
+
if (archive === void 0) { archive = false; }
|
|
100
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
101
|
+
var _a, _b, _c, _d, urls;
|
|
102
|
+
return __generator(this, function (_e) {
|
|
103
|
+
switch (_e.label) {
|
|
104
|
+
case 0:
|
|
105
|
+
if (!(archive && archiveRpcs[chainId])) return [3 /*break*/, 2];
|
|
106
|
+
_b = (_a = ethers_1.providers.FallbackProvider).bind;
|
|
107
|
+
_d = (_c = ethers_1.providers.StaticJsonRpcProvider).bind;
|
|
108
|
+
return [4 /*yield*/, (0, exports.getRpcUrl)(chainId, archive)];
|
|
109
|
+
case 1:
|
|
110
|
+
// return archive PRC, but don't cache it
|
|
111
|
+
return [2 /*return*/, new (_b.apply(_a, [void 0, [
|
|
112
|
+
new (_d.apply(_c, [void 0, _e.sent(), chainId]))()
|
|
113
|
+
]]))()];
|
|
114
|
+
case 2:
|
|
115
|
+
if (!!chainProviders[chainId]) return [3 /*break*/, 4];
|
|
116
|
+
chainProviders[chainId] = [];
|
|
117
|
+
return [4 /*yield*/, (0, exports.getRpcUrls)(chainId, archive)];
|
|
118
|
+
case 3:
|
|
119
|
+
urls = _e.sent();
|
|
120
|
+
urls.forEach(function (url) {
|
|
121
|
+
chainProviders[chainId].push(new ethers_1.providers.FallbackProvider([
|
|
122
|
+
new ethers_1.providers.StaticJsonRpcProvider(url, chainId),
|
|
123
|
+
]));
|
|
124
|
+
});
|
|
125
|
+
_e.label = 4;
|
|
126
|
+
case 4:
|
|
127
|
+
if (!chainProviders[chainId].length) {
|
|
128
|
+
throw new _1.ServerError("Unable to configure provider for chain ".concat(chainId));
|
|
129
|
+
}
|
|
130
|
+
return [2 /*return*/, getRandomProvider(chainProviders[chainId])];
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
exports.getRpcProvider = getRpcProvider;
|
|
136
|
+
// Multicall
|
|
137
|
+
var getMulticallAddress = function (chainId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
138
|
+
var configService, config;
|
|
139
|
+
return __generator(this, function (_a) {
|
|
140
|
+
switch (_a.label) {
|
|
141
|
+
case 0:
|
|
142
|
+
configService = ConfigService_1.default.getInstance();
|
|
143
|
+
return [4 /*yield*/, configService.getConfigAsync()];
|
|
144
|
+
case 1:
|
|
145
|
+
config = _a.sent();
|
|
146
|
+
return [2 /*return*/, config.multicallAddresses[chainId]];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}); };
|
|
150
|
+
exports.getMulticallAddress = getMulticallAddress;
|