@defillama/sdk 5.0.10 → 5.0.12

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.
Files changed (40) hide show
  1. package/build/Balances.d.ts +10 -0
  2. package/build/Balances.js +51 -107
  3. package/build/ChainApi.d.ts +1 -0
  4. package/build/ChainApi.js +152 -253
  5. package/build/abi/abi2.js +47 -144
  6. package/build/abi/convertResults.js +9 -29
  7. package/build/abi/index.js +220 -326
  8. package/build/abi/multicall.js +115 -217
  9. package/build/abi/multicall3.js +59 -123
  10. package/build/abi/rpcCall.js +43 -106
  11. package/build/abi/tron.js +153 -283
  12. package/build/api.js +3 -4
  13. package/build/computeTVL/blocks.d.ts +1 -1
  14. package/build/computeTVL/blocks.js +2 -1
  15. package/build/computeTVL/humanizeNumber.js +6 -7
  16. package/build/computeTVL/prices.js +55 -145
  17. package/build/erc20/index.js +34 -122
  18. package/build/eth/index.js +37 -107
  19. package/build/general.js +22 -23
  20. package/build/generalUtil.js +68 -127
  21. package/build/index.js +2 -2
  22. package/build/providers.json +1 -1
  23. package/build/util/blocks.d.ts +5 -2
  24. package/build/util/blocks.js +234 -373
  25. package/build/util/blockscout.js +13 -58
  26. package/build/util/cache.js +112 -274
  27. package/build/util/computeTVL.js +72 -150
  28. package/build/util/cosmos.js +21 -75
  29. package/build/util/debugLog.js +5 -13
  30. package/build/util/env.d.ts +3 -2
  31. package/build/util/env.js +30 -35
  32. package/build/util/index.d.ts +5 -3
  33. package/build/util/index.js +105 -134
  34. package/build/util/internal-cache.js +7 -8
  35. package/build/util/logs.d.ts +1 -2
  36. package/build/util/logs.js +148 -236
  37. package/build/util/promisePool.js +11 -62
  38. package/build/util/r2.js +36 -95
  39. package/build/util/updateProviderList.js +23 -83
  40. package/package.json +2 -3
@@ -1,185 +1,156 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
50
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
4
  };
52
5
  Object.defineProperty(exports, "__esModule", { value: true });
53
6
  exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.getLogs = exports.sliceIntoChunks = exports.runInPromisePool = exports.convertToBigInt = exports.lookupBlock = exports.getTimestamp = exports.getLatestBlock = void 0;
54
- var general_1 = require("../general");
55
- var ethers_1 = require("ethers");
56
- var generalUtil_1 = require("../generalUtil");
57
- var debugLog_1 = require("./debugLog");
58
- var promisePool_1 = __importDefault(require("./promisePool"));
7
+ const general_1 = require("../general");
8
+ const ethers_1 = require("ethers");
9
+ const generalUtil_1 = require("../generalUtil");
10
+ const debugLog_1 = require("./debugLog");
11
+ const promisePool_1 = __importDefault(require("./promisePool"));
59
12
  var blocks_1 = require("./blocks");
60
13
  Object.defineProperty(exports, "getLatestBlock", { enumerable: true, get: function () { return blocks_1.getLatestBlock; } });
61
14
  Object.defineProperty(exports, "getTimestamp", { enumerable: true, get: function () { return blocks_1.getTimestamp; } });
62
15
  Object.defineProperty(exports, "lookupBlock", { enumerable: true, get: function () { return blocks_1.lookupBlock; } });
63
16
  var generalUtil_2 = require("../generalUtil");
64
17
  Object.defineProperty(exports, "convertToBigInt", { enumerable: true, get: function () { return generalUtil_2.convertToBigInt; } });
18
+ const p_limit_1 = __importDefault(require("p-limit"));
19
+ const env_1 = require("./env");
65
20
  exports.runInPromisePool = promisePool_1.default;
66
- function sliceIntoChunks(arr, chunkSize) {
67
- if (chunkSize === void 0) { chunkSize = 100; }
68
- var res = [];
69
- for (var i = 0; i < arr.length; i += chunkSize) {
70
- var chunk = arr.slice(i, i + chunkSize);
21
+ function sliceIntoChunks(arr, chunkSize = 100) {
22
+ const res = [];
23
+ for (let i = 0; i < arr.length; i += chunkSize) {
24
+ const chunk = arr.slice(i, i + chunkSize);
71
25
  res.push(chunk);
72
26
  }
73
27
  return res;
74
28
  }
75
29
  exports.sliceIntoChunks = sliceIntoChunks;
30
+ const chainLogLimiters = {};
31
+ function getChainLimiter(chain, providerUrl) {
32
+ if (!chainLogLimiters[chain])
33
+ chainLogLimiters[chain] = createLimiter(chain);
34
+ return chainLogLimiters[chain];
35
+ function createLimiter(chain) {
36
+ return (0, p_limit_1.default)((0, env_1.getParallelGetLogsLimit)(chain, providerUrl));
37
+ }
38
+ }
39
+ async function getLogs(options) {
40
+ const getLimiter = () => getChainLimiter(options.chain, (0, generalUtil_1.getProviderUrl)(options.provider));
41
+ // if provider is not passed, we try with archival node first (if set) and then with non-archival node
42
+ let provider;
43
+ if (!options.provider) {
44
+ options.provider = (0, general_1.getProvider)(options.chain, true);
45
+ provider = options.provider;
46
+ try {
47
+ const res = await getLimiter()(() => _getLogs(options));
48
+ return res;
49
+ }
50
+ catch (e) {
51
+ options.provider = (0, general_1.getProvider)(options.chain, false);
52
+ // if (provider === options.provider) throw e // the provider for archival and non-archival request is the same
53
+ (0, debugLog_1.debugLog)(`Error fetching logs for chain ${options.chain} ${(0, generalUtil_1.formError)(e, { chain: options.chain, target: options.target, })}, retrying...`);
54
+ return getLimiter()(() => _getLogs(options));
55
+ }
56
+ }
57
+ return getLimiter()(() => _getLogs(options));
58
+ }
59
+ exports.getLogs = getLogs;
76
60
  // SMALL INCOMPATIBILITY: On the old API we don't return ids but we should
77
- function getLogs(params) {
78
- var _a;
79
- return __awaiter(this, void 0, void 0, function () {
80
- var filter, logs, blockSpread, currentBlock, provider, nextBlock, logParams, partLogs, e_1, error;
81
- return __generator(this, function (_b) {
82
- switch (_b.label) {
83
- case 0:
84
- if (params.toBlock === undefined || params.fromBlock === undefined) {
85
- throw new Error("toBlock and fromBlock need to be defined in all calls to getLogs");
86
- }
87
- filter = {
88
- address: params.target,
89
- topics: (_a = params.topics) !== null && _a !== void 0 ? _a : [ethers_1.ethers.id(params.topic)],
90
- fromBlock: params.fromBlock,
91
- toBlock: params.toBlock // We don't replicate Defipulse's bug because the results end up being the same anyway and hopefully they'll eventually fix it
92
- };
93
- logs = [];
94
- blockSpread = params.toBlock - params.fromBlock;
95
- currentBlock = params.fromBlock;
96
- provider = (0, general_1.getProvider)(params.chain, true);
97
- _b.label = 1;
98
- case 1:
99
- if (!(currentBlock < params.toBlock)) return [3 /*break*/, 6];
100
- nextBlock = Math.min(params.toBlock, currentBlock + blockSpread);
101
- logParams = __assign(__assign({}, filter), { fromBlock: currentBlock, toBlock: nextBlock });
102
- _b.label = 2;
103
- case 2:
104
- _b.trys.push([2, 4, , 5]);
105
- return [4 /*yield*/, provider.getLogs(logParams)];
106
- case 3:
107
- partLogs = _b.sent();
108
- logs = logs.concat(partLogs);
109
- currentBlock = nextBlock;
110
- return [3 /*break*/, 5];
111
- case 4:
112
- e_1 = _b.sent();
113
- (0, debugLog_1.debugLog)("Error fetching logs for chain ".concat(params.chain, " blockSpread: ").concat(blockSpread, ". ").concat((0, generalUtil_1.formError)(e_1, { chain: params.chain, target: params.target, provider: provider })));
114
- if (blockSpread >= 1500) {
115
- // We got too many results
116
- // We could chop it up into 1500 block spreads as that is guaranteed to always return but then we'll have to make a lot of queries (easily >1000), so instead we'll keep dividing the block spread by two until we make it
117
- blockSpread = Math.floor(blockSpread / 2);
118
- }
119
- else {
120
- error = (0, generalUtil_1.formError)(e_1, { chain: params.chain, target: params.target, provider: provider });
121
- error.message = "[chain: ".concat(params.chain, "] ").concat(error === null || error === void 0 ? void 0 : error.message, " params: ").concat(JSON.stringify(logParams));
122
- throw e_1;
123
- }
124
- return [3 /*break*/, 5];
125
- case 5: return [3 /*break*/, 1];
126
- case 6:
127
- if (params.keys.length > 0) {
128
- if (params.keys[0] !== "topics") {
129
- throw new Error("Unsupported");
130
- }
131
- return [2 /*return*/, {
132
- output: logs.map(function (log) { return log.topics; })
133
- }];
134
- }
135
- // ethers v5 logs had 'logIndex' but not ethers v6, so adding field to keep it compatible
136
- logs.forEach(function (log) {
137
- log.logIndex = log.index;
138
- delete log.provider;
139
- });
140
- return [2 /*return*/, {
141
- output: logs
142
- }];
61
+ async function _getLogs(params) {
62
+ var _a, _b;
63
+ if (params.toBlock === undefined || params.fromBlock === undefined) {
64
+ throw new Error("toBlock and fromBlock need to be defined in all calls to getLogs");
65
+ }
66
+ const filter = {
67
+ address: params.target,
68
+ topics: (_a = params.topics) !== null && _a !== void 0 ? _a : [ethers_1.ethers.id(params.topic)],
69
+ fromBlock: params.fromBlock,
70
+ toBlock: params.toBlock // We don't replicate Defipulse's bug because the results end up being the same anyway and hopefully they'll eventually fix it
71
+ };
72
+ let logs = [];
73
+ let blockSpread = params.toBlock - params.fromBlock;
74
+ let currentBlock = params.fromBlock;
75
+ const provider = (_b = params.provider) !== null && _b !== void 0 ? _b : (0, general_1.getProvider)(params.chain, true);
76
+ while (currentBlock < params.toBlock) {
77
+ const nextBlock = Math.min(params.toBlock, currentBlock + blockSpread);
78
+ let logParams = {
79
+ ...filter,
80
+ fromBlock: currentBlock,
81
+ toBlock: nextBlock
82
+ };
83
+ try {
84
+ const partLogs = await provider.getLogs(logParams);
85
+ logs = logs.concat(partLogs);
86
+ currentBlock = nextBlock;
87
+ }
88
+ catch (e) {
89
+ (0, debugLog_1.debugLog)(`Error fetching logs for chain ${params.chain} blockSpread: ${blockSpread}. ${(0, generalUtil_1.formError)(e, { chain: params.chain, target: params.target, provider })}`);
90
+ if (blockSpread >= 1500) {
91
+ // We got too many results
92
+ // We could chop it up into 1500 block spreads as that is guaranteed to always return but then we'll have to make a lot of queries (easily >1000), so instead we'll keep dividing the block spread by two until we make it
93
+ blockSpread = Math.floor(blockSpread / 2);
94
+ }
95
+ else {
96
+ const error = (0, generalUtil_1.formError)(e, { chain: params.chain, target: params.target, provider });
97
+ error.message = `[chain: ${params.chain}] ${error === null || error === void 0 ? void 0 : error.message} params: ${JSON.stringify(logParams)}`;
98
+ throw e;
143
99
  }
144
- });
100
+ }
101
+ }
102
+ if (params.keys.length > 0) {
103
+ if (params.keys[0] !== "topics") {
104
+ throw new Error("Unsupported");
105
+ }
106
+ return {
107
+ output: logs.map((log) => log.topics)
108
+ };
109
+ }
110
+ // ethers v5 logs had 'logIndex' but not ethers v6, so adding field to keep it compatible
111
+ logs.forEach((log) => {
112
+ log.logIndex = log.index;
113
+ delete log.provider;
145
114
  });
115
+ return {
116
+ output: logs
117
+ };
146
118
  }
147
- exports.getLogs = getLogs;
148
119
  function normalizeAddress(address) {
149
120
  // sol amd tezos case sensitive so no normalising
150
- var prefix = address.substring(0, address.indexOf(":"));
121
+ const prefix = address.substring(0, address.indexOf(":"));
151
122
  if (["solana", "tezos"].includes(prefix))
152
123
  return address;
153
124
  return address.toLowerCase();
154
125
  }
155
126
  exports.normalizeAddress = normalizeAddress;
156
127
  function normalizePrefixes(address) {
157
- var prefix = address.substring(0, address.indexOf(":"));
128
+ const prefix = address.substring(0, address.indexOf(":"));
158
129
  if (["solana", "tezos"].includes(prefix))
159
130
  return address;
160
131
  return address.startsWith("0x")
161
- ? "ethereum:".concat(address.toLowerCase())
132
+ ? `ethereum:${address.toLowerCase()}`
162
133
  : !address.includes(":")
163
- ? "coingecko:".concat(address.toLowerCase())
134
+ ? `coingecko:${address.toLowerCase()}`
164
135
  : address.toLowerCase();
165
136
  }
166
137
  exports.normalizePrefixes = normalizePrefixes;
167
- var ethereumAddress = "ethereum:0x0000000000000000000000000000000000000000";
168
- var weth = "ethereum:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
138
+ const ethereumAddress = "ethereum:0x0000000000000000000000000000000000000000";
139
+ const weth = "ethereum:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
169
140
  function normalizeBalances(balances) {
170
141
  var _a;
171
- Object.keys(balances).map(function (key) {
142
+ Object.keys(balances).map((key) => {
172
143
  if (+balances[key] === 0) {
173
144
  delete balances[key];
174
145
  return;
175
146
  }
176
- var normalisedKey = normalizePrefixes(key);
147
+ const normalisedKey = normalizePrefixes(key);
177
148
  if (normalisedKey == key)
178
149
  return;
179
150
  (0, generalUtil_1.sumSingleBalance)(balances, normalisedKey, balances[key]);
180
151
  delete balances[key];
181
152
  });
182
- var eth = balances[ethereumAddress];
153
+ const eth = balances[ethereumAddress];
183
154
  if (eth !== undefined) {
184
155
  balances[weth] = (BigInt((_a = balances[weth]) !== null && _a !== void 0 ? _a : 0) + BigInt(eth)).toString();
185
156
  delete balances[ethereumAddress];
@@ -24,14 +24,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.retriveCache = exports.startCache = exports.saveCache = exports.setCache = exports.getCache = void 0;
27
- var fs = __importStar(require("fs"));
28
- var env_1 = require("./env");
27
+ const fs = __importStar(require("fs"));
28
+ const env_1 = require("./env");
29
29
  // const cacheFile = __dirname + '/../../../sc-cache1.json'
30
- var cacheFile = env_1.ENV_CONSTANTS.INTERNAL_SDK_CACHE_FILE;
31
- var cache = {};
30
+ const cacheFile = env_1.ENV_CONSTANTS.INTERNAL_SDK_CACHE_FILE;
31
+ let cache = {};
32
32
  // if (cacheFile) cache = require(cacheFile) // disabled for now
33
33
  function getCache(options) {
34
- var address = options.address, abi = options.abi, _a = options.chain, chain = _a === void 0 ? "ethereum" : _a;
34
+ let { address, abi, chain = "ethereum" } = options;
35
35
  if (!address)
36
36
  return;
37
37
  address = address.slice(2).toLowerCase();
@@ -43,7 +43,7 @@ function getCache(options) {
43
43
  }
44
44
  exports.getCache = getCache;
45
45
  function setCache(options) {
46
- var address = options.address, abi = options.abi, _a = options.chain, chain = _a === void 0 ? "ethereum" : _a, value = options.value;
46
+ let { address, abi, chain = "ethereum", value } = options;
47
47
  if (!address)
48
48
  return;
49
49
  address = address.slice(2).toLowerCase();
@@ -58,8 +58,7 @@ function saveCache() {
58
58
  fs.writeFileSync(cacheFile || './cache.json', JSON.stringify(cache));
59
59
  }
60
60
  exports.saveCache = saveCache;
61
- function startCache(_cache) {
62
- if (_cache === void 0) { _cache = {}; }
61
+ function startCache(_cache = {}) {
63
62
  cache = _cache;
64
63
  }
65
64
  exports.startCache = startCache;
@@ -23,8 +23,7 @@ export type GetLogsOptions = {
23
23
  export declare function getLogs(options: GetLogsOptions): Promise<EventLog[] | EventLog[][] | any[]>;
24
24
  export type logCache = {
25
25
  logs: EventLog[];
26
- metadata?: {
27
- version: string;
26
+ metadata: {
28
27
  fromBlock: number;
29
28
  toBlock: number;
30
29
  };