@defillama/sdk 5.0.10 → 5.0.11

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.
@@ -1,61 +1,14 @@
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; } });
@@ -63,123 +16,110 @@ Object.defineProperty(exports, "lookupBlock", { enumerable: true, get: function
63
16
  var generalUtil_2 = require("../generalUtil");
64
17
  Object.defineProperty(exports, "convertToBigInt", { enumerable: true, get: function () { return generalUtil_2.convertToBigInt; } });
65
18
  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);
19
+ function sliceIntoChunks(arr, chunkSize = 100) {
20
+ const res = [];
21
+ for (let i = 0; i < arr.length; i += chunkSize) {
22
+ const chunk = arr.slice(i, i + chunkSize);
71
23
  res.push(chunk);
72
24
  }
73
25
  return res;
74
26
  }
75
27
  exports.sliceIntoChunks = sliceIntoChunks;
76
28
  // SMALL INCOMPATIBILITY: On the old API we don't return ids but we should
77
- function getLogs(params) {
29
+ async function getLogs(params) {
78
30
  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
- }];
31
+ if (params.toBlock === undefined || params.fromBlock === undefined) {
32
+ throw new Error("toBlock and fromBlock need to be defined in all calls to getLogs");
33
+ }
34
+ const filter = {
35
+ address: params.target,
36
+ topics: (_a = params.topics) !== null && _a !== void 0 ? _a : [ethers_1.ethers.id(params.topic)],
37
+ fromBlock: params.fromBlock,
38
+ 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
39
+ };
40
+ let logs = [];
41
+ let blockSpread = params.toBlock - params.fromBlock;
42
+ let currentBlock = params.fromBlock;
43
+ const provider = (0, general_1.getProvider)(params.chain, true);
44
+ while (currentBlock < params.toBlock) {
45
+ const nextBlock = Math.min(params.toBlock, currentBlock + blockSpread);
46
+ let logParams = {
47
+ ...filter,
48
+ fromBlock: currentBlock,
49
+ toBlock: nextBlock
50
+ };
51
+ try {
52
+ const partLogs = await provider.getLogs(logParams);
53
+ logs = logs.concat(partLogs);
54
+ currentBlock = nextBlock;
55
+ }
56
+ catch (e) {
57
+ (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 })}`);
58
+ if (blockSpread >= 1500) {
59
+ // We got too many results
60
+ // 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
61
+ blockSpread = Math.floor(blockSpread / 2);
143
62
  }
144
- });
63
+ else {
64
+ const error = (0, generalUtil_1.formError)(e, { chain: params.chain, target: params.target, provider });
65
+ error.message = `[chain: ${params.chain}] ${error === null || error === void 0 ? void 0 : error.message} params: ${JSON.stringify(logParams)}`;
66
+ throw e;
67
+ }
68
+ }
69
+ }
70
+ if (params.keys.length > 0) {
71
+ if (params.keys[0] !== "topics") {
72
+ throw new Error("Unsupported");
73
+ }
74
+ return {
75
+ output: logs.map((log) => log.topics)
76
+ };
77
+ }
78
+ // ethers v5 logs had 'logIndex' but not ethers v6, so adding field to keep it compatible
79
+ logs.forEach((log) => {
80
+ log.logIndex = log.index;
81
+ delete log.provider;
145
82
  });
83
+ return {
84
+ output: logs
85
+ };
146
86
  }
147
87
  exports.getLogs = getLogs;
148
88
  function normalizeAddress(address) {
149
89
  // sol amd tezos case sensitive so no normalising
150
- var prefix = address.substring(0, address.indexOf(":"));
90
+ const prefix = address.substring(0, address.indexOf(":"));
151
91
  if (["solana", "tezos"].includes(prefix))
152
92
  return address;
153
93
  return address.toLowerCase();
154
94
  }
155
95
  exports.normalizeAddress = normalizeAddress;
156
96
  function normalizePrefixes(address) {
157
- var prefix = address.substring(0, address.indexOf(":"));
97
+ const prefix = address.substring(0, address.indexOf(":"));
158
98
  if (["solana", "tezos"].includes(prefix))
159
99
  return address;
160
100
  return address.startsWith("0x")
161
- ? "ethereum:".concat(address.toLowerCase())
101
+ ? `ethereum:${address.toLowerCase()}`
162
102
  : !address.includes(":")
163
- ? "coingecko:".concat(address.toLowerCase())
103
+ ? `coingecko:${address.toLowerCase()}`
164
104
  : address.toLowerCase();
165
105
  }
166
106
  exports.normalizePrefixes = normalizePrefixes;
167
- var ethereumAddress = "ethereum:0x0000000000000000000000000000000000000000";
168
- var weth = "ethereum:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
107
+ const ethereumAddress = "ethereum:0x0000000000000000000000000000000000000000";
108
+ const weth = "ethereum:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
169
109
  function normalizeBalances(balances) {
170
110
  var _a;
171
- Object.keys(balances).map(function (key) {
111
+ Object.keys(balances).map((key) => {
172
112
  if (+balances[key] === 0) {
173
113
  delete balances[key];
174
114
  return;
175
115
  }
176
- var normalisedKey = normalizePrefixes(key);
116
+ const normalisedKey = normalizePrefixes(key);
177
117
  if (normalisedKey == key)
178
118
  return;
179
119
  (0, generalUtil_1.sumSingleBalance)(balances, normalisedKey, balances[key]);
180
120
  delete balances[key];
181
121
  });
182
- var eth = balances[ethereumAddress];
122
+ const eth = balances[ethereumAddress];
183
123
  if (eth !== undefined) {
184
124
  balances[weth] = (BigInt((_a = balances[weth]) !== null && _a !== void 0 ? _a : 0) + BigInt(eth)).toString();
185
125
  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
  };