@defillama/sdk 5.0.173 → 5.0.177
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/build/Balances.d.ts +2 -2
- package/build/ChainApi.d.ts +26 -14
- package/build/ChainApi.js +58 -28
- package/build/abi/abi2.js +23 -15
- package/build/abi/cachedABIs.d.ts +1 -1
- package/build/abi/convertResults.d.ts +4 -1
- package/build/abi/convertResults.js +20 -15
- package/build/abi/index.js +16 -39
- package/build/abi/multicall.js +6 -31
- package/build/abi/multicall3.js +36 -37
- package/build/abi/rpcCall.js +1 -2
- package/build/abi/tron.d.ts +5 -6
- package/build/abi/tron.js +79 -198
- package/build/api.js +17 -7
- package/build/api2.js +17 -7
- package/build/computeTVL/humanizeNumber.js +1 -2
- package/build/erc20/index.js +5 -6
- package/build/eth/index.d.ts +1 -0
- package/build/eth/index.js +39 -16
- package/build/general.js +2 -2
- package/build/generalUtil.d.ts +2 -24
- package/build/generalUtil.js +39 -246
- package/build/index.js +17 -7
- package/build/providers.json +1 -1
- package/build/util/LlamaProvider.js +8 -3
- package/build/util/blocks.js +17 -20
- package/build/util/cache.d.ts +11 -2
- package/build/util/cache.js +53 -9
- package/build/util/common.d.ts +33 -0
- package/build/util/common.js +352 -0
- package/build/util/computeTVL.js +6 -11
- package/build/util/cosmos.js +6 -9
- package/build/util/debugLog.js +3 -3
- package/build/util/elastic.d.ts +19 -0
- package/build/util/elastic.js +54 -6
- package/build/util/env.d.ts +7 -1
- package/build/util/env.js +28 -15
- package/build/util/graph.js +4 -8
- package/build/util/index.d.ts +1 -1
- package/build/util/index.js +17 -15
- package/build/util/indexer.js +29 -19
- package/build/util/internal-cache.js +22 -13
- package/build/util/logs.js +7 -7
- package/build/util/promisePool.d.ts +4 -1
- package/build/util/promisePool.js +22 -7
- package/build/util/r2.d.ts +0 -1
- package/build/util/r2.js +4 -9
- package/build/util/removeRedundantRPCs.js +2 -2
- package/build/util/transactions.js +1 -2
- package/build/util/updateProviderList.js +6 -6
- package/package.json +3 -1
- package/build/computeTVL/prices.d.ts +0 -9
- package/build/computeTVL/prices.js +0 -73
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// keep no dependency here else it will create circular dependencies
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.convertToBigInt = convertToBigInt;
|
|
38
|
+
exports.sumMultiBalanceOf = sumMultiBalanceOf;
|
|
39
|
+
exports.sumSingleBalance = sumSingleBalance;
|
|
40
|
+
exports.mergeBalances = mergeBalances;
|
|
41
|
+
exports.removeTokenBalance = removeTokenBalance;
|
|
42
|
+
exports.sumChainTvls = sumChainTvls;
|
|
43
|
+
exports.getUniqueAddresses = getUniqueAddresses;
|
|
44
|
+
exports.getProviderUrl = getProviderUrl;
|
|
45
|
+
exports.formErrorString = formErrorString;
|
|
46
|
+
exports.shortenString = shortenString;
|
|
47
|
+
exports.formError = formError;
|
|
48
|
+
exports.fetchJson = fetchJson;
|
|
49
|
+
exports.postJson = postJson;
|
|
50
|
+
exports.tronToEvmAddress = tronToEvmAddress;
|
|
51
|
+
exports.evmToTronAddress = evmToTronAddress;
|
|
52
|
+
exports.sleep = sleep;
|
|
53
|
+
exports.sleepRandom = sleepRandom;
|
|
54
|
+
exports.fixTronCallParams = fixTronCallParams;
|
|
55
|
+
const axios_1 = __importStar(require("axios"));
|
|
56
|
+
const ethers = __importStar(require("ethers"));
|
|
57
|
+
const TronAddressFormatter = __importStar(require("tron-format-address"));
|
|
58
|
+
const env_1 = require("./env");
|
|
59
|
+
function convertToBigInt(value) {
|
|
60
|
+
const balance = value;
|
|
61
|
+
if (!value)
|
|
62
|
+
return BigInt(0);
|
|
63
|
+
if (typeof value === 'bigint')
|
|
64
|
+
return value;
|
|
65
|
+
if (typeof value === 'number')
|
|
66
|
+
value = value.toString();
|
|
67
|
+
if (typeof value === 'string') {
|
|
68
|
+
if (!value.startsWith('0x') && value.includes('e')) { // Convert scientific notation to standard notation
|
|
69
|
+
let [base, e] = value.split('e');
|
|
70
|
+
const [lead, decimal] = base.split('.');
|
|
71
|
+
let final;
|
|
72
|
+
let exponent = +e;
|
|
73
|
+
if (exponent >= 0) {
|
|
74
|
+
final = lead + (decimal || '') + '0'.repeat(exponent - (decimal || '').length);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// NOTE: unforutenately BigInt doesn't support negative exponents, so this ends up being 0
|
|
78
|
+
exponent = exponent * -1;
|
|
79
|
+
final = BigInt(lead) / BigInt('1' + Array(exponent).fill(0).join(''));
|
|
80
|
+
}
|
|
81
|
+
return BigInt(final);
|
|
82
|
+
}
|
|
83
|
+
return BigInt(value);
|
|
84
|
+
}
|
|
85
|
+
if (typeof value === 'string')
|
|
86
|
+
return BigInt(value);
|
|
87
|
+
throw new Error(`Invalid balance: ${balance}`);
|
|
88
|
+
}
|
|
89
|
+
function sumMultiBalanceOf(balances, results, allCallsMustBeSuccessful = true, transformAddress = (addr) => addr) {
|
|
90
|
+
results.output.map((result) => {
|
|
91
|
+
var _a;
|
|
92
|
+
if (result.success) {
|
|
93
|
+
const address = transformAddress(result.input.target);
|
|
94
|
+
const balance = result.output;
|
|
95
|
+
if (BigInt(balance) <= 0) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
balances[address] = (BigInt((_a = balances[address]) !== null && _a !== void 0 ? _a : 0) + BigInt(balance)).toString();
|
|
99
|
+
}
|
|
100
|
+
else if (allCallsMustBeSuccessful) {
|
|
101
|
+
console.error(result);
|
|
102
|
+
throw new Error(`balanceOf multicall failed`);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function sumSingleBalance(balances, token, balance, chain) {
|
|
107
|
+
if (typeof balance === 'bigint')
|
|
108
|
+
balance = balance.toString();
|
|
109
|
+
isValidNumber(balance);
|
|
110
|
+
if (+balance === 0)
|
|
111
|
+
return;
|
|
112
|
+
if (chain)
|
|
113
|
+
token = `${chain}:${token}`;
|
|
114
|
+
if (typeof balance === 'object') {
|
|
115
|
+
if (typeof balance.toString === 'function')
|
|
116
|
+
balance = balance.toString();
|
|
117
|
+
else
|
|
118
|
+
throw new Error('Invalid balance value:' + balance);
|
|
119
|
+
}
|
|
120
|
+
if (typeof balance === 'number' || (balances[token] && typeof balances[token] === 'number')) {
|
|
121
|
+
const prevBalance = +(balances.hasOwnProperty(token) ? balances[token] : 0);
|
|
122
|
+
if (typeof prevBalance !== 'number' || isNaN(prevBalance))
|
|
123
|
+
throw new Error(`Trying to merge token balance and coingecko amount for ${token} current balance: ${balance} previous balance: ${balances[token]}`);
|
|
124
|
+
const value = prevBalance + +balance;
|
|
125
|
+
isValidNumber(value);
|
|
126
|
+
balances[token] = value;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
const prevBalance = convertToBigInt(balances[token]);
|
|
130
|
+
const value = (prevBalance + convertToBigInt(balance));
|
|
131
|
+
isValidNumber(Number(value));
|
|
132
|
+
balances[token] = Number(value).toString();
|
|
133
|
+
}
|
|
134
|
+
function isValidNumber(value) {
|
|
135
|
+
if (typeof value === 'bigint')
|
|
136
|
+
return;
|
|
137
|
+
if ([null, undefined].includes(value) || isNaN(+value))
|
|
138
|
+
throw new Error(`Invalid balance: ${balance}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function mergeBalances(balances, balancesToMerge) {
|
|
142
|
+
if (balances === balancesToMerge)
|
|
143
|
+
return;
|
|
144
|
+
Object.entries(balancesToMerge).forEach((balance) => {
|
|
145
|
+
sumSingleBalance(balances, balance[0], balance[1]);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function removeTokenBalance(balances, token, isCaseSensitive = false) {
|
|
149
|
+
const re = new RegExp(token, isCaseSensitive ? undefined : 'i');
|
|
150
|
+
Object.keys(balances).forEach(key => {
|
|
151
|
+
if (re.test(key))
|
|
152
|
+
delete balances[key];
|
|
153
|
+
});
|
|
154
|
+
return balances;
|
|
155
|
+
}
|
|
156
|
+
function sumChainTvls(chainTvls) {
|
|
157
|
+
return async (timestamp, ethBlock, chainBlocks, params) => {
|
|
158
|
+
const api = params.api;
|
|
159
|
+
await Promise.all(chainTvls.map(async (chainTvl) => {
|
|
160
|
+
const chainBalances = await chainTvl(timestamp, ethBlock, chainBlocks, params);
|
|
161
|
+
api.addBalances(chainBalances);
|
|
162
|
+
}));
|
|
163
|
+
return api.getBalances();
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function getUniqueAddresses(addresses, chain) {
|
|
167
|
+
if (!addresses.length)
|
|
168
|
+
return [];
|
|
169
|
+
const isTronAddress = chain === 'tron' && addresses[0].includes('T');
|
|
170
|
+
const toLowerCase = !isTronAddress;
|
|
171
|
+
const set = {};
|
|
172
|
+
addresses.forEach(i => set[toLowerCase ? i.toLowerCase() : i] = true);
|
|
173
|
+
return Object.keys(set);
|
|
174
|
+
}
|
|
175
|
+
function getProviderUrl(provider) {
|
|
176
|
+
if (provider === null || provider === void 0 ? void 0 : provider.isCustomLlamaProvider)
|
|
177
|
+
return provider.rpcs.map((i) => i.url).join(', ');
|
|
178
|
+
if (provider instanceof ethers.FallbackProvider)
|
|
179
|
+
provider = provider.providerConfigs[0].provider;
|
|
180
|
+
if (provider instanceof ethers.JsonRpcProvider)
|
|
181
|
+
return provider._getConnection().url;
|
|
182
|
+
return '';
|
|
183
|
+
}
|
|
184
|
+
function formErrorString(e, errorParams = {}) {
|
|
185
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4;
|
|
186
|
+
if (e instanceof axios_1.AxiosError) {
|
|
187
|
+
let message = `[host: ${shortenString((_b = (_a = e === null || e === void 0 ? void 0 : e.config) === null || _a === void 0 ? void 0 : _a.url) !== null && _b !== void 0 ? _b : '', 120)}] `;
|
|
188
|
+
// if (e?.config?.method === 'post' && e?.config?.data)
|
|
189
|
+
// message += shortenString(`method: POST data: ${e?.config?.data} `)
|
|
190
|
+
if ((_c = e === null || e === void 0 ? void 0 : e.response) === null || _c === void 0 ? void 0 : _c.data) {
|
|
191
|
+
if (e.response.status)
|
|
192
|
+
message += `[${e.response.status}] `;
|
|
193
|
+
let dataString = shortenString(JSON.stringify(e.response.data));
|
|
194
|
+
if (typeof e.response.data === 'object') {
|
|
195
|
+
if ((_d = e.response.data.error) === null || _d === void 0 ? void 0 : _d.message)
|
|
196
|
+
dataString = shortenString('' + e.response.data.error.message);
|
|
197
|
+
else if (e.response.data.message)
|
|
198
|
+
dataString = shortenString('' + e.response.data.message);
|
|
199
|
+
}
|
|
200
|
+
if (dataString.startsWith('"<!DOCTYPE html>') || dataString.startsWith('"<html'))
|
|
201
|
+
dataString = 'HTML error page';
|
|
202
|
+
message += dataString;
|
|
203
|
+
return message;
|
|
204
|
+
}
|
|
205
|
+
if (e === null || e === void 0 ? void 0 : e.message)
|
|
206
|
+
return shortenString(message + e.message);
|
|
207
|
+
}
|
|
208
|
+
const isGraphError = typeof e === 'object' && ((_e = e === null || e === void 0 ? void 0 : e.stack) === null || _e === void 0 ? void 0 : _e.includes('graphql-request')) && ((_g = (_f = e.response) === null || _f === void 0 ? void 0 : _f.errors) === null || _g === void 0 ? void 0 : _g.length);
|
|
209
|
+
if (isGraphError)
|
|
210
|
+
return shortenString(`GraphQL request failed! ${e.response.errors.map((i) => i.message).join(', ')}`);
|
|
211
|
+
if (!e) {
|
|
212
|
+
if (errorParams.promisePoolErrors) {
|
|
213
|
+
const errors = errorParams.promisePoolErrors.map((i) => formErrorString(i));
|
|
214
|
+
return `Promise pool failed! \n ${errors.join('\n')}`;
|
|
215
|
+
}
|
|
216
|
+
if (errorParams.isMultiCallError) {
|
|
217
|
+
const targetStr = errorParams.target ? `[target: ${errorParams.target}]` : '';
|
|
218
|
+
let errorString = `Multicall failed! \n [chain: ${(_h = errorParams.chain) !== null && _h !== void 0 ? _h : "ethereum"}] [fail count: ${(_j = errorParams.failedQueries) === null || _j === void 0 ? void 0 : _j.length}] [abi: ${errorParams.abi}] ${targetStr} `;
|
|
219
|
+
(_k = errorParams.failedQueries) === null || _k === void 0 ? void 0 : _k.forEach((i) => {
|
|
220
|
+
i = i.input;
|
|
221
|
+
let target = !targetStr ? ` target: ${i.target}` : '';
|
|
222
|
+
let params = i.params && i.params.length ? `params: ${i.params.length === 1 ? i.params[0] : i.params.join(', ')}]` : '';
|
|
223
|
+
errorString += `\n ${target} ${params}`;
|
|
224
|
+
});
|
|
225
|
+
// truncate error string to 310 chars
|
|
226
|
+
return errorString.length > 310 ? errorString.slice(0, 310).concat('...') : errorString;
|
|
227
|
+
}
|
|
228
|
+
return '';
|
|
229
|
+
}
|
|
230
|
+
if (e.llamaRPCError) {
|
|
231
|
+
const errorString = `Llama RPC error! method: ${e.method} `;
|
|
232
|
+
const errors = e.errors.map((i) => { var _a; return `- host: ${i === null || i === void 0 ? void 0 : i.host} error: ${(_a = i === null || i === void 0 ? void 0 : i.error) === null || _a === void 0 ? void 0 : _a.message}`; });
|
|
233
|
+
return errorString + '\n' + errors.join('\n');
|
|
234
|
+
}
|
|
235
|
+
let errorString = e.toString();
|
|
236
|
+
if (typeof e !== 'object')
|
|
237
|
+
return errorString;
|
|
238
|
+
if (((e.reason || e.method || e.shortMessage) && e.code) || errorParams.result === '0x') { // ethers.js error
|
|
239
|
+
const errorMessage = (_s = (_r = (_q = (_p = (_o = (_m = (_l = e.info) === null || _l === void 0 ? void 0 : _l.error) === null || _m === void 0 ? void 0 : _m.message) !== null && _o !== void 0 ? _o : e.message) !== null && _p !== void 0 ? _p : e.shortMessage) !== null && _q !== void 0 ? _q : e.errorName) !== null && _r !== void 0 ? _r : e.reason) !== null && _s !== void 0 ? _s : '';
|
|
240
|
+
let method = (_w = (_t = e.method) !== null && _t !== void 0 ? _t : (_v = (_u = e.info) === null || _u === void 0 ? void 0 : _u.request) === null || _v === void 0 ? void 0 : _v.method) !== null && _w !== void 0 ? _w : errorParams.abi;
|
|
241
|
+
if (e.body) {
|
|
242
|
+
try {
|
|
243
|
+
e.body = JSON.parse(e.body);
|
|
244
|
+
}
|
|
245
|
+
catch (e) { }
|
|
246
|
+
if ((_y = (_x = e.body) === null || _x === void 0 ? void 0 : _x.error) === null || _y === void 0 ? void 0 : _y.message)
|
|
247
|
+
return e.body.error.message;
|
|
248
|
+
}
|
|
249
|
+
if (!method && e.requestBody) {
|
|
250
|
+
try {
|
|
251
|
+
e.requestBody = JSON.parse(e.requestBody);
|
|
252
|
+
}
|
|
253
|
+
catch (e) { }
|
|
254
|
+
method = (_z = e.requestBody) === null || _z === void 0 ? void 0 : _z.method;
|
|
255
|
+
}
|
|
256
|
+
else if (!e.provider && errorParams.provider)
|
|
257
|
+
e.provider = errorParams.provider;
|
|
258
|
+
let providerUrl = getProviderUrl((_0 = e.provider) !== null && _0 !== void 0 ? _0 : errorParams.provider);
|
|
259
|
+
if (e.serverError)
|
|
260
|
+
return `host: ${providerUrl} ${e.serverError.toString()}`;
|
|
261
|
+
if (e.results) {
|
|
262
|
+
let errors = e.results.filter((i) => i.error).map((i) => i.error);
|
|
263
|
+
if (errors.length) {
|
|
264
|
+
const eStrings = errors.map((i) => formError(i));
|
|
265
|
+
return `Failed to call method: ${method} provider: ${providerUrl}
|
|
266
|
+
${eStrings.join('\n')}`;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
else if (e.code === 'CALL_EXCEPTION' || errorParams.isCallError) {
|
|
270
|
+
let extraInfo = 'target: ' + errorParams.target;
|
|
271
|
+
if (errorParams.params && Array.isArray(errorParams.params) && errorParams.params.length)
|
|
272
|
+
extraInfo += shortenString(' params: ' + errorParams.params.join(', '));
|
|
273
|
+
return `Failed to call: ${method} ${extraInfo} on chain: [${errorParams.chain}] rpc: ${providerUrl} call reverted, reason: ${errorMessage} ${(_1 = e.errorArgs) !== null && _1 !== void 0 ? _1 : ''}`;
|
|
274
|
+
}
|
|
275
|
+
else if (e.code === 'BAD_DATA') {
|
|
276
|
+
if (!method)
|
|
277
|
+
method = (_3 = (_2 = e === null || e === void 0 ? void 0 : e.info) === null || _2 === void 0 ? void 0 : _2.payload) === null || _3 === void 0 ? void 0 : _3.method;
|
|
278
|
+
let extraInfo = 'target: ' + errorParams.target;
|
|
279
|
+
return `Failed method: ${method} ${shortenString(extraInfo)} on chain: [${errorParams.chain}] rpc: ${providerUrl} reason: ${errorMessage} ${(_4 = e.errorArgs) !== null && _4 !== void 0 ? _4 : ''}`;
|
|
280
|
+
}
|
|
281
|
+
if (method === 'eth_blockNumber')
|
|
282
|
+
return `host: ${providerUrl} reason: ${e.reason} code: ${e.code}`;
|
|
283
|
+
if (method === 'getBlockNumber')
|
|
284
|
+
return `Failed to call ${method} ${providerUrl} reason: ${e.reason} code: ${e.code}`;
|
|
285
|
+
return `Failed to call ${method} on ${providerUrl} reason: ${e.reason} code: ${e.code}`;
|
|
286
|
+
}
|
|
287
|
+
return errorString;
|
|
288
|
+
}
|
|
289
|
+
function shortenString(str, length = env_1.defaultShortenStringLength) {
|
|
290
|
+
return str.length > length ? str.slice(0, length).concat('...') : str;
|
|
291
|
+
}
|
|
292
|
+
function formError(e, errorParams = {}) {
|
|
293
|
+
var _a;
|
|
294
|
+
const errorParamsSet = Object.keys(errorParams).length;
|
|
295
|
+
if ((e === null || e === void 0 ? void 0 : e._isCustomError) || (!errorParamsSet && typeof e === 'object' && !Object.keys(e).length))
|
|
296
|
+
return e; // already formatted or vannila error
|
|
297
|
+
const error = new Error(formErrorString(e, errorParams));
|
|
298
|
+
try {
|
|
299
|
+
if (errorParams.isMultiCallError)
|
|
300
|
+
error._underlyingErrors = (_a = errorParams === null || errorParams === void 0 ? void 0 : errorParams.failedQueries) === null || _a === void 0 ? void 0 : _a.map((i) => shortenString(i.error.toString()));
|
|
301
|
+
else
|
|
302
|
+
error._underlyingError = shortenString(e.toString());
|
|
303
|
+
}
|
|
304
|
+
catch (e) { }
|
|
305
|
+
error._isCustomError = true;
|
|
306
|
+
error.message = shortenString(error.message, 2999);
|
|
307
|
+
error.stack = '';
|
|
308
|
+
return error;
|
|
309
|
+
}
|
|
310
|
+
async function fetchJson(url, params = {}) {
|
|
311
|
+
const res = await (0, axios_1.default)(url, params).catch(e => { throw formError(e); });
|
|
312
|
+
return res.data;
|
|
313
|
+
}
|
|
314
|
+
async function postJson(url, data = {}, params = {}) {
|
|
315
|
+
const res = await axios_1.default.post(url, data, params).catch(e => { throw formError(e); });
|
|
316
|
+
return res.data;
|
|
317
|
+
}
|
|
318
|
+
function tronToEvmAddress(address) {
|
|
319
|
+
if (!address.startsWith('0x'))
|
|
320
|
+
return TronAddressFormatter.toHex(address);
|
|
321
|
+
return address;
|
|
322
|
+
}
|
|
323
|
+
function evmToTronAddress(address) {
|
|
324
|
+
if (address.startsWith('0x'))
|
|
325
|
+
return TronAddressFormatter.fromHex(address);
|
|
326
|
+
return address;
|
|
327
|
+
}
|
|
328
|
+
function sleep(ms) {
|
|
329
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
330
|
+
}
|
|
331
|
+
async function sleepRandom(max, min = 0) {
|
|
332
|
+
const ms = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
333
|
+
return sleep(ms);
|
|
334
|
+
}
|
|
335
|
+
// convert all the tron addresses in the params to evm addresses (from Txxx to 0x...)
|
|
336
|
+
function fixTronCallParams(params) {
|
|
337
|
+
const wrappedHexify = (v) => {
|
|
338
|
+
if (typeof v === 'string' && v.startsWith('T'))
|
|
339
|
+
return tronToEvmAddress(v);
|
|
340
|
+
return v;
|
|
341
|
+
};
|
|
342
|
+
if (typeof params === 'object') {
|
|
343
|
+
if (params.target)
|
|
344
|
+
params.target = tronToEvmAddress(params.target);
|
|
345
|
+
if (Array.isArray(params.params))
|
|
346
|
+
params.params = params.params.map(wrappedHexify);
|
|
347
|
+
if (typeof params.params === 'string')
|
|
348
|
+
params.params = wrappedHexify(params.params);
|
|
349
|
+
}
|
|
350
|
+
if (Array.isArray(params.calls))
|
|
351
|
+
params.calls.forEach(fixTronCallParams);
|
|
352
|
+
}
|
package/build/util/computeTVL.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.default = computeTVL;
|
|
4
|
+
exports.getUnique = getUnique;
|
|
7
5
|
const _1 = require(".");
|
|
8
|
-
const
|
|
9
|
-
const axios_1 = __importDefault(require("axios"));
|
|
6
|
+
const common_1 = require("./common");
|
|
10
7
|
const env_1 = require("./env");
|
|
11
8
|
const currentPriceCache = {};
|
|
12
9
|
const priceCaches = {};
|
|
@@ -38,13 +35,12 @@ async function computeTVL(balances, timestamp, { debug = false } = {}) {
|
|
|
38
35
|
usdTvl += value;
|
|
39
36
|
if (symbol === 'ETH')
|
|
40
37
|
symbol = 'WETH';
|
|
41
|
-
(0,
|
|
38
|
+
(0, common_1.sumSingleBalance)(usdTokenBalances, symbol, value);
|
|
42
39
|
if (debug && !isNaN(+value))
|
|
43
40
|
debugData.tokenData.push({ symbol, value, token, price, confidence, balance, decimals, });
|
|
44
41
|
});
|
|
45
42
|
return { usdTvl, usdTokenBalances, debugData, };
|
|
46
43
|
}
|
|
47
|
-
exports.default = computeTVL;
|
|
48
44
|
function tokenToKey(token) {
|
|
49
45
|
if (token.startsWith("0x"))
|
|
50
46
|
token = `ethereum:${token}`;
|
|
@@ -84,12 +80,12 @@ async function updatePriceCache(keys, timestamp) {
|
|
|
84
80
|
}
|
|
85
81
|
async function getPrices(keys) {
|
|
86
82
|
if (!timestamp) {
|
|
87
|
-
const { coins } = await
|
|
83
|
+
const { coins } = await (0, common_1.fetchJson)(`https://coins.llama.fi/prices/current/${keys.join(',')}`);
|
|
88
84
|
return coins;
|
|
89
85
|
}
|
|
90
86
|
// fetch post with timestamp in body
|
|
91
87
|
const coinsApiKey = env_1.ENV_CONSTANTS['COINS_API_KEY'];
|
|
92
|
-
const { coins } = await
|
|
88
|
+
const { coins } = await (0, common_1.postJson)("https://coins.llama.fi/prices?source=internal&apikey=" + coinsApiKey, { coins: keys, timestamp });
|
|
93
89
|
return coins;
|
|
94
90
|
}
|
|
95
91
|
}
|
|
@@ -105,7 +101,6 @@ function getUnique(addresses) {
|
|
|
105
101
|
addresses.forEach(i => set[i] = true);
|
|
106
102
|
return Object.keys(set);
|
|
107
103
|
}
|
|
108
|
-
exports.getUnique = getUnique;
|
|
109
104
|
function isNearlyNow(timestamp) {
|
|
110
105
|
if (!timestamp)
|
|
111
106
|
return true;
|
package/build/util/cosmos.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
|
|
3
|
+
exports.isCosmosChain = void 0;
|
|
4
|
+
exports.getCosmosBlock = getCosmosBlock;
|
|
5
|
+
exports.getCosmosProvider = getCosmosProvider;
|
|
8
6
|
const env_1 = require("./env");
|
|
7
|
+
const common_1 = require("./common");
|
|
9
8
|
// where to find chain info
|
|
10
9
|
// https://proxy.atomscan.com/chains.json
|
|
11
10
|
// https://cosmos-chain.directory/chains/cosmoshub
|
|
@@ -49,8 +48,8 @@ async function getCosmosBlock(block = 'latest', chain = 'cosmos') {
|
|
|
49
48
|
var _a;
|
|
50
49
|
const endPoint = (0, env_1.getEnvRPC)(chain) || endPoints[chain] || 'https://rest.cosmos.directory/' + chain;
|
|
51
50
|
try {
|
|
52
|
-
const
|
|
53
|
-
const { block: { header: { height, time } } } =
|
|
51
|
+
const data = await (0, common_1.postJson)(`${endPoint}/blocks/${block}`);
|
|
52
|
+
const { block: { header: { height, time } } } = data;
|
|
54
53
|
return {
|
|
55
54
|
number: Number(height),
|
|
56
55
|
timestamp: Math.floor(Date.parse(time) / 1000),
|
|
@@ -62,10 +61,8 @@ async function getCosmosBlock(block = 'latest', chain = 'cosmos') {
|
|
|
62
61
|
throw new Error(message);
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
|
-
exports.getCosmosBlock = getCosmosBlock;
|
|
66
64
|
function getCosmosProvider(chain) {
|
|
67
65
|
return {
|
|
68
66
|
getBlock: (block = 'latest') => getCosmosBlock(block, chain)
|
|
69
67
|
};
|
|
70
68
|
}
|
|
71
|
-
exports.getCosmosProvider = getCosmosProvider;
|
package/build/util/debugLog.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.DEBUG_LEVEL2 = exports.DEBUG_ENABLED = void 0;
|
|
4
|
+
exports.debugLog = debugLog;
|
|
5
|
+
exports.debugTable = debugTable;
|
|
4
6
|
const env_1 = require("./env");
|
|
5
7
|
exports.DEBUG_ENABLED = env_1.ENV_CONSTANTS.DEBUG_ENABLED === "true";
|
|
6
8
|
exports.DEBUG_LEVEL2 = env_1.ENV_CONSTANTS.DEBUG_LEVEL2 === "true";
|
|
@@ -9,10 +11,8 @@ function debugLog(...args) {
|
|
|
9
11
|
return;
|
|
10
12
|
console.log(...args);
|
|
11
13
|
}
|
|
12
|
-
exports.debugLog = debugLog;
|
|
13
14
|
function debugTable(...args) {
|
|
14
15
|
if (!exports.DEBUG_ENABLED && !exports.DEBUG_LEVEL2)
|
|
15
16
|
return;
|
|
16
17
|
console.table(...args);
|
|
17
18
|
}
|
|
18
|
-
exports.debugTable = debugTable;
|
package/build/util/elastic.d.ts
CHANGED
|
@@ -21,4 +21,23 @@ export declare function addErrorLog(body: {
|
|
|
21
21
|
error: object;
|
|
22
22
|
metadata: Metadata;
|
|
23
23
|
}): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Performs a search operation using the Elasticsearch client
|
|
26
|
+
*
|
|
27
|
+
* This is a wrapper around the Elasticsearch client's search method.
|
|
28
|
+
* See the Elasticsearch documentation for details:
|
|
29
|
+
* https://www.elastic.co/guide/en/elasticsearch/reference/8.13/search-search.html
|
|
30
|
+
*
|
|
31
|
+
* @returns The search results as returned by Elasticsearch
|
|
32
|
+
* @throws Error if Elasticsearch client is not configured
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_search | Elasticsearch Node.js client search API}
|
|
35
|
+
*/
|
|
36
|
+
export declare function search(...options: Parameters<Client['search']>): Promise<ReturnType<Client['search']>>;
|
|
37
|
+
export declare function getAllLogs({ index, scrollTime, size, query }: {
|
|
38
|
+
index: string;
|
|
39
|
+
scrollTime?: string;
|
|
40
|
+
size?: number;
|
|
41
|
+
query?: object;
|
|
42
|
+
}): Promise<any[]>;
|
|
24
43
|
export {};
|
package/build/util/elastic.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getClient = getClient;
|
|
4
|
+
exports.writeLog = writeLog;
|
|
5
|
+
exports.addDebugLog = addDebugLog;
|
|
6
|
+
exports.addRuntimeLog = addRuntimeLog;
|
|
7
|
+
exports.addErrorLog = addErrorLog;
|
|
8
|
+
exports.search = search;
|
|
9
|
+
exports.getAllLogs = getAllLogs;
|
|
4
10
|
const elasticsearch_1 = require("@elastic/elasticsearch");
|
|
5
11
|
const env_1 = require("./env");
|
|
6
12
|
const debugLog_1 = require("./debugLog");
|
|
@@ -31,7 +37,6 @@ function getClient() {
|
|
|
31
37
|
});
|
|
32
38
|
return _client;
|
|
33
39
|
}
|
|
34
|
-
exports.getClient = getClient;
|
|
35
40
|
async function writeLog(index, log) {
|
|
36
41
|
const client = getClient();
|
|
37
42
|
if (!client)
|
|
@@ -45,7 +50,6 @@ async function writeLog(index, log) {
|
|
|
45
50
|
(0, debugLog_1.debugLog)((error === null || error === void 0 ? void 0 : error.message) || 'Error writing log to Elasticsearch');
|
|
46
51
|
}
|
|
47
52
|
}
|
|
48
|
-
exports.writeLog = writeLog;
|
|
49
53
|
function addYearAndMonth(index) {
|
|
50
54
|
const date = new Date();
|
|
51
55
|
return `${index}-${date.getUTCFullYear()}-${date.getUTCMonth()}`;
|
|
@@ -53,12 +57,56 @@ function addYearAndMonth(index) {
|
|
|
53
57
|
async function addDebugLog(body) {
|
|
54
58
|
await writeLog('debug-logs', body);
|
|
55
59
|
}
|
|
56
|
-
exports.addDebugLog = addDebugLog;
|
|
57
60
|
async function addRuntimeLog(body) {
|
|
58
61
|
await writeLog('debug-runtime-logs', body);
|
|
59
62
|
}
|
|
60
|
-
exports.addRuntimeLog = addRuntimeLog;
|
|
61
63
|
async function addErrorLog(body) {
|
|
62
64
|
await writeLog('error-logs', body);
|
|
63
65
|
}
|
|
64
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Performs a search operation using the Elasticsearch client
|
|
68
|
+
*
|
|
69
|
+
* This is a wrapper around the Elasticsearch client's search method.
|
|
70
|
+
* See the Elasticsearch documentation for details:
|
|
71
|
+
* https://www.elastic.co/guide/en/elasticsearch/reference/8.13/search-search.html
|
|
72
|
+
*
|
|
73
|
+
* @returns The search results as returned by Elasticsearch
|
|
74
|
+
* @throws Error if Elasticsearch client is not configured
|
|
75
|
+
*
|
|
76
|
+
* @see {@link https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_search | Elasticsearch Node.js client search API}
|
|
77
|
+
*/
|
|
78
|
+
async function search(...options) {
|
|
79
|
+
const client = getClient();
|
|
80
|
+
if (!client)
|
|
81
|
+
throw new Error('Elasticsearch client not configured');
|
|
82
|
+
return client.search(...options);
|
|
83
|
+
}
|
|
84
|
+
async function getAllLogs({ index, scrollTime = '1m', size = 100000, query = {
|
|
85
|
+
match_all: {},
|
|
86
|
+
} }) {
|
|
87
|
+
const client = getClient();
|
|
88
|
+
if (!client)
|
|
89
|
+
throw new Error('Elasticsearch client not configured');
|
|
90
|
+
const allLogs = [];
|
|
91
|
+
let response = await client.search({
|
|
92
|
+
index: index,
|
|
93
|
+
scroll: scrollTime,
|
|
94
|
+
body: { query, size, },
|
|
95
|
+
});
|
|
96
|
+
while (response.hits.hits.length) {
|
|
97
|
+
allLogs.push(...response.hits.hits.map((i) => {
|
|
98
|
+
const source = i._source;
|
|
99
|
+
// reduce final file size by removing fields we don't need
|
|
100
|
+
// delete source.chain
|
|
101
|
+
// delete source.address
|
|
102
|
+
// delete source.adapter
|
|
103
|
+
return source;
|
|
104
|
+
}));
|
|
105
|
+
(0, debugLog_1.debugLog)(`Fetched ${allLogs.length} records, ${response.hits.hits.length} in batch`);
|
|
106
|
+
response = await client.scroll({
|
|
107
|
+
scroll_id: response._scroll_id,
|
|
108
|
+
scroll: scrollTime,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return allLogs;
|
|
112
|
+
}
|
package/build/util/env.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
export declare const logGetBlockStats: boolean;
|
|
2
|
+
export declare const logGetLogsErrors: boolean;
|
|
3
|
+
export declare const logGetLogsDebug: boolean;
|
|
4
|
+
export declare const logGetLogsIndexer: boolean;
|
|
5
|
+
export declare const defaultShortenStringLength: number;
|
|
1
6
|
export declare const ENV_CONSTANTS: {
|
|
2
|
-
[
|
|
7
|
+
[key: string]: string | undefined;
|
|
3
8
|
};
|
|
4
9
|
export declare function getChainId(chain: string, chainId: number): number;
|
|
5
10
|
export declare function getMaxParallelRequests(chain: string): number;
|
|
@@ -13,3 +18,4 @@ export declare function getDefaultChunkSize(chain: string): number;
|
|
|
13
18
|
export declare function getBatchMaxCount(chain: string): number;
|
|
14
19
|
export declare function getArchivalRPCs(chain: string): string[];
|
|
15
20
|
export declare function getChainRPCs(chain: string, defaultList?: string[]): string | undefined;
|
|
21
|
+
export declare function getWhitelistedRPCs(chain: string): string[];
|