@defillama/sdk 4.0.76 → 4.0.78
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/ChainApi.d.ts +1 -0
- package/build/ChainApi.js +30 -0
- package/build/abi/abi2.js +4 -0
- package/build/computeTVL/blocks.d.ts +1 -19
- package/build/computeTVL/blocks.js +7 -187
- package/build/generalUtil.js +16 -6
- package/build/providers.json +1 -1
- package/build/util/blocks.d.ts +33 -0
- package/build/util/blocks.js +423 -0
- package/build/util/blockscout.d.ts +6 -0
- package/build/util/blockscout.js +71 -0
- package/build/util/cache.d.ts +5 -0
- package/build/util/cache.js +50 -2
- package/build/util/cosmos.d.ts +11 -0
- package/build/util/cosmos.js +124 -0
- package/build/util/index.d.ts +1 -13
- package/build/util/index.js +22 -281
- package/package.json +1 -1
|
@@ -0,0 +1,124 @@
|
|
|
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 (g && (g = 0, op[0] && (_ = 0)), _) 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.getCosmosProvider = exports.getCosmosBlock = exports.isCosmosChain = void 0;
|
|
43
|
+
var node_fetch_1 = __importDefault(require("node-fetch"));
|
|
44
|
+
// where to find chain info
|
|
45
|
+
// https://proxy.atomscan.com/chains.json
|
|
46
|
+
// https://cosmos-chain.directory/chains/cosmoshub
|
|
47
|
+
// https://cosmos-chain.directory/chains
|
|
48
|
+
var endPoints = {
|
|
49
|
+
crescent: "https://mainnet.crescent.network:1317",
|
|
50
|
+
osmosis: "https://osmosis-api.polkachu.com",
|
|
51
|
+
cosmos: "https://cosmoshub-lcd.stakely.io",
|
|
52
|
+
kujira: "https://kuji-api.kleomedes.network",
|
|
53
|
+
comdex: "https://rest.comdex.one",
|
|
54
|
+
terra: "https://terra-classic-lcd.publicnode.com",
|
|
55
|
+
terra2: "https://terra-lcd.publicnode.com",
|
|
56
|
+
umee: "https://umee-api.polkachu.com",
|
|
57
|
+
orai: "https://lcd.orai.io",
|
|
58
|
+
juno: "https://juno.api.m.stavr.tech",
|
|
59
|
+
cronos: "https://rest.mainnet.crypto.org",
|
|
60
|
+
chihuahua: "https://rest.cosmos.directory/chihuahua",
|
|
61
|
+
stargaze: "https://rest.stargaze-apis.com",
|
|
62
|
+
quicksilver: "https://rest.cosmos.directory/quicksilver",
|
|
63
|
+
persistence: "https://rest.cosmos.directory/persistence",
|
|
64
|
+
secret: "https://lcd.secret.express",
|
|
65
|
+
// chihuahua: "https://api.chihuahua.wtf",
|
|
66
|
+
injective: "https://sentry.lcd.injective.network:443",
|
|
67
|
+
migaloo: "https://migaloo-api.polkachu.com",
|
|
68
|
+
fxcore: "https://fx-rest.functionx.io",
|
|
69
|
+
xpla: "https://dimension-lcd.xpla.dev",
|
|
70
|
+
neutron: "https://rest-kralum.neutron-1.neutron.org",
|
|
71
|
+
quasar: "https://quasar-api.polkachu.com",
|
|
72
|
+
gravitybridge: "https://gravitychain.io:1317",
|
|
73
|
+
sei: "https://sei-api.polkachu.com",
|
|
74
|
+
aura: "https://lcd.aura.network",
|
|
75
|
+
archway: "https://api.mainnet.archway.io",
|
|
76
|
+
sifchain: "https://sifchain-api.polkachu.com",
|
|
77
|
+
nolus: "https://pirin-cl.nolus.network:1317",
|
|
78
|
+
bostrom: "https://lcd.bostrom.cybernode.ai"
|
|
79
|
+
};
|
|
80
|
+
var ibcChains = ['ibc', 'terra', 'terra2', 'crescent', 'osmosis', 'kujira', 'stargaze', 'juno', 'injective', 'cosmos', 'comdex', 'umee', 'orai', 'persistence', 'fxcore', 'neutron', 'quasar', 'chihuahua', 'sei', 'archway', 'migaloo', 'secret', 'aura', 'xpla', 'bostrom'];
|
|
81
|
+
var isCosmosChain = function (chain) { return ibcChains.includes(chain) || !!endPoints[chain]; };
|
|
82
|
+
exports.isCosmosChain = isCosmosChain;
|
|
83
|
+
function getCosmosBlock(block, chain) {
|
|
84
|
+
var _a;
|
|
85
|
+
if (block === void 0) { block = 'latest'; }
|
|
86
|
+
if (chain === void 0) { chain = 'cosmos'; }
|
|
87
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
88
|
+
var endPoint, data, _b, height, time, e_1, message;
|
|
89
|
+
return __generator(this, function (_c) {
|
|
90
|
+
switch (_c.label) {
|
|
91
|
+
case 0:
|
|
92
|
+
endPoint = process.env[chain.toUpperCase() + '_RPC'] || endPoints[chain] || 'https://rest.cosmos.directory/' + chain;
|
|
93
|
+
_c.label = 1;
|
|
94
|
+
case 1:
|
|
95
|
+
_c.trys.push([1, 4, , 5]);
|
|
96
|
+
return [4 /*yield*/, (0, node_fetch_1.default)("".concat(endPoint, "/blocks/").concat(block))];
|
|
97
|
+
case 2:
|
|
98
|
+
data = _c.sent();
|
|
99
|
+
return [4 /*yield*/, data.json()];
|
|
100
|
+
case 3:
|
|
101
|
+
_b = (_c.sent()).block.header, height = _b.height, time = _b.time;
|
|
102
|
+
return [2 /*return*/, {
|
|
103
|
+
number: Number(height),
|
|
104
|
+
timestamp: Math.floor(Date.parse(time) / 1000),
|
|
105
|
+
}];
|
|
106
|
+
case 4:
|
|
107
|
+
e_1 = _c.sent();
|
|
108
|
+
message = "Error fetchin cosmos block -\n chain: ".concat(chain, ", block: ").concat(block, ", endPoint: ").concat(endPoint, ", error: ").concat((_a = e_1 === null || e_1 === void 0 ? void 0 : e_1.message) !== null && _a !== void 0 ? _a : JSON.stringify(e_1));
|
|
109
|
+
throw new Error(message);
|
|
110
|
+
case 5: return [2 /*return*/];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
exports.getCosmosBlock = getCosmosBlock;
|
|
116
|
+
function getCosmosProvider(chain) {
|
|
117
|
+
return {
|
|
118
|
+
getBlock: function (block) {
|
|
119
|
+
if (block === void 0) { block = 'latest'; }
|
|
120
|
+
return getCosmosBlock(block, chain);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
exports.getCosmosProvider = getCosmosProvider;
|
package/build/util/index.d.ts
CHANGED
|
@@ -2,19 +2,9 @@ import { Chain } from "../general";
|
|
|
2
2
|
import type { Address } from "../types";
|
|
3
3
|
import type { Log } from "@ethersproject/abstract-provider";
|
|
4
4
|
import runInPromisePoolOrig from "./promisePool";
|
|
5
|
+
export { getLatestBlock, getTimestamp, lookupBlock, } from "./blocks";
|
|
5
6
|
export declare const runInPromisePool: typeof runInPromisePoolOrig;
|
|
6
7
|
export declare function sliceIntoChunks(arr: any[], chunkSize?: number): any[][];
|
|
7
|
-
interface TimestampBlock {
|
|
8
|
-
number: number;
|
|
9
|
-
timestamp: number;
|
|
10
|
-
}
|
|
11
|
-
export declare function getLatestBlock(chain: string): Promise<TimestampBlock>;
|
|
12
|
-
export declare function lookupBlock(timestamp: number, extraParams?: {
|
|
13
|
-
chain?: Chain | "terra" | "kava" | "algorand";
|
|
14
|
-
}): Promise<{
|
|
15
|
-
timestamp: number;
|
|
16
|
-
block: number;
|
|
17
|
-
}>;
|
|
18
8
|
export declare function getLogs(params: {
|
|
19
9
|
target: Address;
|
|
20
10
|
topic: string;
|
|
@@ -28,7 +18,6 @@ export declare function getLogs(params: {
|
|
|
28
18
|
} | {
|
|
29
19
|
output: Log[];
|
|
30
20
|
}>;
|
|
31
|
-
export declare function getTimestamp(height: number, chain: Chain): Promise<number>;
|
|
32
21
|
export declare function normalizeAddress(address: string): string;
|
|
33
22
|
export declare function normalizePrefixes(address: string): string;
|
|
34
23
|
export declare function normalizeBalances(balances: {
|
|
@@ -36,4 +25,3 @@ export declare function normalizeBalances(balances: {
|
|
|
36
25
|
}): {
|
|
37
26
|
[address: string]: string;
|
|
38
27
|
};
|
|
39
|
-
export {};
|
package/build/util/index.js
CHANGED
|
@@ -50,13 +50,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
50
50
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
51
|
};
|
|
52
52
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
-
exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.
|
|
53
|
+
exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.getLogs = exports.sliceIntoChunks = exports.runInPromisePool = exports.lookupBlock = exports.getTimestamp = exports.getLatestBlock = void 0;
|
|
54
54
|
var general_1 = require("../general");
|
|
55
|
-
var node_fetch_1 = __importDefault(require("node-fetch"));
|
|
56
55
|
var ethers_1 = require("ethers");
|
|
57
56
|
var generalUtil_1 = require("../generalUtil");
|
|
58
57
|
var debugLog_1 = require("./debugLog");
|
|
59
58
|
var promisePool_1 = __importDefault(require("./promisePool"));
|
|
59
|
+
var blocks_1 = require("./blocks");
|
|
60
|
+
Object.defineProperty(exports, "getLatestBlock", { enumerable: true, get: function () { return blocks_1.getLatestBlock; } });
|
|
61
|
+
Object.defineProperty(exports, "getTimestamp", { enumerable: true, get: function () { return blocks_1.getTimestamp; } });
|
|
62
|
+
Object.defineProperty(exports, "lookupBlock", { enumerable: true, get: function () { return blocks_1.lookupBlock; } });
|
|
60
63
|
exports.runInPromisePool = promisePool_1.default;
|
|
61
64
|
function sliceIntoChunks(arr, chunkSize) {
|
|
62
65
|
if (chunkSize === void 0) { chunkSize = 100; }
|
|
@@ -68,264 +71,13 @@ function sliceIntoChunks(arr, chunkSize) {
|
|
|
68
71
|
return res;
|
|
69
72
|
}
|
|
70
73
|
exports.sliceIntoChunks = sliceIntoChunks;
|
|
71
|
-
var terraBlockProvider = {
|
|
72
|
-
getBlock: function (height) { return __awaiter(void 0, void 0, void 0, function () {
|
|
73
|
-
return __generator(this, function (_a) {
|
|
74
|
-
return [2 /*return*/, (0, node_fetch_1.default)("https://lcd.terra.dev/blocks/".concat(height))
|
|
75
|
-
.then(function (res) { return res.json(); })
|
|
76
|
-
.then(function (block) { return ({
|
|
77
|
-
number: Number(block.block.header.height),
|
|
78
|
-
timestamp: Math.round(Date.parse(block.block.header.time) / 1000),
|
|
79
|
-
}); })];
|
|
80
|
-
});
|
|
81
|
-
}); }
|
|
82
|
-
};
|
|
83
|
-
var algorandBlockProvider = {
|
|
84
|
-
getBlock: function (height) { return __awaiter(void 0, void 0, void 0, function () {
|
|
85
|
-
return __generator(this, function (_a) {
|
|
86
|
-
if (height !== 'latest')
|
|
87
|
-
return [2 /*return*/, (0, node_fetch_1.default)("https://algoindexer.algoexplorerapi.io/v2/blocks/".concat(height))
|
|
88
|
-
.then(function (res) { return res.json(); })
|
|
89
|
-
.then(function (block) { return ({
|
|
90
|
-
number: block.round,
|
|
91
|
-
timestamp: block.timestamp
|
|
92
|
-
}); })];
|
|
93
|
-
return [2 /*return*/, (0, node_fetch_1.default)('https://algoindexer.algoexplorerapi.io/health')
|
|
94
|
-
.then(function (res) { return res.json(); })
|
|
95
|
-
.then(function (block) { return algorandBlockProvider.getBlock(block.round); })];
|
|
96
|
-
});
|
|
97
|
-
}); }
|
|
98
|
-
};
|
|
99
|
-
function getBlock(provider, height, chain) {
|
|
100
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
101
|
-
var block;
|
|
102
|
-
return __generator(this, function (_a) {
|
|
103
|
-
switch (_a.label) {
|
|
104
|
-
case 0: return [4 /*yield*/, provider.getBlock(height)];
|
|
105
|
-
case 1:
|
|
106
|
-
block = _a.sent();
|
|
107
|
-
if (block === null) {
|
|
108
|
-
throw new Error("Can't get block of chain ".concat(chain !== null && chain !== void 0 ? chain : "ethereum"));
|
|
109
|
-
}
|
|
110
|
-
return [2 /*return*/, block];
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
function getExtraProvider(chain) {
|
|
116
|
-
if (chain === "terra") {
|
|
117
|
-
return terraBlockProvider;
|
|
118
|
-
}
|
|
119
|
-
else if (chain === "algorand") {
|
|
120
|
-
return algorandBlockProvider;
|
|
121
|
-
}
|
|
122
|
-
return (0, general_1.getProvider)(chain);
|
|
123
|
-
}
|
|
124
|
-
function getLatestBlock(chain) {
|
|
125
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
126
|
-
var provider;
|
|
127
|
-
return __generator(this, function (_a) {
|
|
128
|
-
provider = getExtraProvider(chain);
|
|
129
|
-
return [2 /*return*/, getBlock(provider, "latest", chain)];
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
exports.getLatestBlock = getLatestBlock;
|
|
134
|
-
var intialBlocks = {
|
|
135
|
-
terra: 4724001,
|
|
136
|
-
crab: 4969901
|
|
137
|
-
};
|
|
138
|
-
var blockscoutEndpoints = {
|
|
139
|
-
celo: 'https://explorer.celo.org',
|
|
140
|
-
kava: 'https://explorer.kava.io',
|
|
141
|
-
onus: 'https://explorer.onuschain.io',
|
|
142
|
-
base: 'https://base.blockscout.com',
|
|
143
|
-
scroll: 'https://blockscout.scroll.io',
|
|
144
|
-
};
|
|
145
|
-
function getBlockscoutBlock(timestamp, chain) {
|
|
146
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
147
|
-
var api, res, data;
|
|
148
|
-
return __generator(this, function (_a) {
|
|
149
|
-
switch (_a.label) {
|
|
150
|
-
case 0:
|
|
151
|
-
api = "".concat(blockscoutEndpoints[chain], "/api?module=block&action=getblocknobytime×tamp=").concat(timestamp, "&closest=before");
|
|
152
|
-
return [4 /*yield*/, (0, node_fetch_1.default)(api)];
|
|
153
|
-
case 1:
|
|
154
|
-
res = _a.sent();
|
|
155
|
-
return [4 /*yield*/, res.json()];
|
|
156
|
-
case 2:
|
|
157
|
-
data = _a.sent();
|
|
158
|
-
if (data.status !== '1')
|
|
159
|
-
throw new Error(data.message);
|
|
160
|
-
return [2 /*return*/, {
|
|
161
|
-
timestamp: timestamp,
|
|
162
|
-
block: +data.result.blockNumber
|
|
163
|
-
}];
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
var blockTimeCache = {};
|
|
169
|
-
function lookupBlock(timestamp, extraParams) {
|
|
170
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
171
|
-
if (extraParams === void 0) { extraParams = {}; }
|
|
172
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
173
|
-
function updateBlock(blocks) {
|
|
174
|
-
if (blocks === void 0) { blocks = []; }
|
|
175
|
-
blocks.forEach(addBlockToCache);
|
|
176
|
-
var getPrecision = function (block) { return block.timestamp - timestamp > 0 ? block.timestamp - timestamp : timestamp - block.timestamp; };
|
|
177
|
-
blocks.push(highBlock, lowBlock);
|
|
178
|
-
blocks.sort(function (a, b) { return getPrecision(a) - getPrecision(b); });
|
|
179
|
-
block = blocks[0];
|
|
180
|
-
// find the closest upper and lower bound between 4 points
|
|
181
|
-
lowBlock = blocks.filter(function (i) { return i.timestamp < timestamp; }).reduce(function (lowestBlock, block) { return (timestamp - lowestBlock.timestamp) < (timestamp - block.timestamp) ? lowestBlock : block; });
|
|
182
|
-
highBlock = blocks.filter(function (i) { return i.timestamp > timestamp; }).reduce(function (highestBlock, block) { return (highestBlock.timestamp - timestamp) < (block.timestamp - timestamp) ? highestBlock : block; });
|
|
183
|
-
imprecision = getPrecision(block);
|
|
184
|
-
blockImprecision = highBlock.number - lowBlock.number;
|
|
185
|
-
// debugLog(`chain: ${chain} block: ${block.number} #calls: ${i} imprecision: ${Number((imprecision)/60).toFixed(2)} (min) block diff: ${blockImprecision} Time Taken: ${Number((Date.now()-time)/1000).toFixed(2)} (in sec)`)
|
|
186
|
-
}
|
|
187
|
-
function getChainBlockTimeCache(chain) {
|
|
188
|
-
if (!blockTimeCache[chain])
|
|
189
|
-
blockTimeCache[chain] = {};
|
|
190
|
-
return blockTimeCache[chain];
|
|
191
|
-
}
|
|
192
|
-
function addBlockToCache(block) {
|
|
193
|
-
getChainBlockTimeCache(chain)[block.number] = block;
|
|
194
|
-
}
|
|
195
|
-
function getLowBlock() {
|
|
196
|
-
return Object.values(getChainBlockTimeCache(chain))
|
|
197
|
-
.filter(function (i) { return i.timestamp < timestamp; })
|
|
198
|
-
.sort(function (a, b) { return b.timestamp - a.timestamp; })[0];
|
|
199
|
-
}
|
|
200
|
-
function getHighBlock() {
|
|
201
|
-
return Object.values(getChainBlockTimeCache(chain))
|
|
202
|
-
.filter(function (i) { return i.timestamp > timestamp; })
|
|
203
|
-
.sort(function (a, b) { return a.timestamp - b.timestamp; })[0];
|
|
204
|
-
}
|
|
205
|
-
var chain, response, e_1, api, res, data, low, lowBlock, highBlock, block, i, time, allowedTimeRange, acceptableBlockImprecision, blockImprecision, imprecision, firstBlock, lastBlock, provider, firstBlockNum, blockDiff, timeDiff, avgBlockTime, closeBlock, midBlock, blocks, e_2;
|
|
206
|
-
var _j;
|
|
207
|
-
return __generator(this, function (_k) {
|
|
208
|
-
switch (_k.label) {
|
|
209
|
-
case 0:
|
|
210
|
-
chain = (_a = extraParams === null || extraParams === void 0 ? void 0 : extraParams.chain) !== null && _a !== void 0 ? _a : "ethereum";
|
|
211
|
-
if (!blockscoutEndpoints[chain]) return [3 /*break*/, 4];
|
|
212
|
-
_k.label = 1;
|
|
213
|
-
case 1:
|
|
214
|
-
_k.trys.push([1, 3, , 4]);
|
|
215
|
-
return [4 /*yield*/, getBlockscoutBlock(timestamp, chain)];
|
|
216
|
-
case 2:
|
|
217
|
-
response = _k.sent();
|
|
218
|
-
return [2 /*return*/, response];
|
|
219
|
-
case 3:
|
|
220
|
-
e_1 = _k.sent();
|
|
221
|
-
(0, debugLog_1.debugLog)('error fetching block from blockscout', e_1);
|
|
222
|
-
return [3 /*break*/, 4];
|
|
223
|
-
case 4:
|
|
224
|
-
if (!(chain === 'waves')) return [3 /*break*/, 7];
|
|
225
|
-
api = "https://nodes.wavesnodes.com/blocks/heightByTimestamp/".concat(timestamp);
|
|
226
|
-
return [4 /*yield*/, (0, node_fetch_1.default)(api)];
|
|
227
|
-
case 5:
|
|
228
|
-
res = _k.sent();
|
|
229
|
-
return [4 /*yield*/, res.json()];
|
|
230
|
-
case 6:
|
|
231
|
-
data = _k.sent();
|
|
232
|
-
return [2 /*return*/, {
|
|
233
|
-
timestamp: timestamp,
|
|
234
|
-
block: +data.height
|
|
235
|
-
}];
|
|
236
|
-
case 7:
|
|
237
|
-
low = (_b = intialBlocks[chain]) !== null && _b !== void 0 ? _b : 100;
|
|
238
|
-
lowBlock = getLowBlock();
|
|
239
|
-
highBlock = getHighBlock();
|
|
240
|
-
i = 0;
|
|
241
|
-
time = Date.now();
|
|
242
|
-
allowedTimeRange = 15 * 60 // how much imprecision is allowed (15 minutes now)
|
|
243
|
-
;
|
|
244
|
-
acceptableBlockImprecision = chain === 'ethereum' ? 20 : 200;
|
|
245
|
-
_k.label = 8;
|
|
246
|
-
case 8:
|
|
247
|
-
_k.trys.push([8, 17, , 18]);
|
|
248
|
-
firstBlock = void 0, lastBlock = void 0;
|
|
249
|
-
provider = getExtraProvider(chain);
|
|
250
|
-
if (!['evmos'].includes(chain)) return [3 /*break*/, 11];
|
|
251
|
-
return [4 /*yield*/, getBlock(provider, "latest", chain)];
|
|
252
|
-
case 9:
|
|
253
|
-
lastBlock = _k.sent();
|
|
254
|
-
firstBlockNum = lastBlock.number;
|
|
255
|
-
switch (chain) {
|
|
256
|
-
default: firstBlockNum -= 2 * 1e5; // evmos hold only the last 200k block data
|
|
257
|
-
}
|
|
258
|
-
return [4 /*yield*/, getBlock(provider, firstBlockNum, chain)];
|
|
259
|
-
case 10:
|
|
260
|
-
firstBlock = _k.sent();
|
|
261
|
-
return [3 /*break*/, 13];
|
|
262
|
-
case 11: return [4 /*yield*/, Promise.all([
|
|
263
|
-
getBlock(provider, "latest", chain),
|
|
264
|
-
lowBlock ? lowBlock : getBlock(provider, low, chain),
|
|
265
|
-
])];
|
|
266
|
-
case 12:
|
|
267
|
-
_j = _k.sent(), lastBlock = _j[0], firstBlock = _j[1];
|
|
268
|
-
_k.label = 13;
|
|
269
|
-
case 13:
|
|
270
|
-
lowBlock = firstBlock;
|
|
271
|
-
if (!highBlock)
|
|
272
|
-
highBlock = lastBlock;
|
|
273
|
-
if (lastBlock.timestamp - timestamp < -30 * 60) {
|
|
274
|
-
throw new Error("Last block of chain \"".concat(chain, "\" is further than 30 minutes into the past. Provider is \"").concat((_d = (_c = provider === null || provider === void 0 ? void 0 : provider.connection) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : (_h = (_g = (_f = ((_e = provider === null || provider === void 0 ? void 0 : provider.providerConfigs) !== null && _e !== void 0 ? _e : [])[0]) === null || _f === void 0 ? void 0 : _f.provider) === null || _g === void 0 ? void 0 : _g.connection) === null || _h === void 0 ? void 0 : _h.url, "\""));
|
|
275
|
-
}
|
|
276
|
-
if (Math.abs(highBlock.timestamp - timestamp) < 60 * 30) {
|
|
277
|
-
// Short-circuit in case we are trying to get the current block
|
|
278
|
-
return [2 /*return*/, {
|
|
279
|
-
block: highBlock.number,
|
|
280
|
-
timestamp: highBlock.timestamp
|
|
281
|
-
}];
|
|
282
|
-
}
|
|
283
|
-
updateBlock();
|
|
284
|
-
_k.label = 14;
|
|
285
|
-
case 14:
|
|
286
|
-
if (!(imprecision > allowedTimeRange && blockImprecision > acceptableBlockImprecision)) return [3 /*break*/, 16];
|
|
287
|
-
++i;
|
|
288
|
-
blockDiff = highBlock.number - lowBlock.number;
|
|
289
|
-
timeDiff = highBlock.timestamp - lowBlock.timestamp;
|
|
290
|
-
avgBlockTime = timeDiff / blockDiff;
|
|
291
|
-
closeBlock = Math.floor(lowBlock.number + (timestamp - lowBlock.timestamp) / avgBlockTime);
|
|
292
|
-
if (closeBlock > highBlock.number)
|
|
293
|
-
closeBlock = highBlock.number;
|
|
294
|
-
midBlock = Math.floor((lowBlock.number + highBlock.number) / 2);
|
|
295
|
-
return [4 /*yield*/, Promise.all([
|
|
296
|
-
getBlock(provider, closeBlock, chain),
|
|
297
|
-
getBlock(provider, midBlock, chain),
|
|
298
|
-
])];
|
|
299
|
-
case 15:
|
|
300
|
-
blocks = _k.sent();
|
|
301
|
-
updateBlock(blocks);
|
|
302
|
-
return [3 /*break*/, 14];
|
|
303
|
-
case 16:
|
|
304
|
-
(0, debugLog_1.debugLog)("chain: ".concat(chain, " block: ").concat(block.number, " #calls: ").concat(i, " imprecision: ").concat(Number((imprecision) / 60).toFixed(2), " (min) Time Taken: ").concat(Number((Date.now() - time) / 1000).toFixed(2), " (in sec)"));
|
|
305
|
-
if (chain !== "bsc" && // this check is there because bsc halted the chain for few days
|
|
306
|
-
Math.abs(block.timestamp - timestamp) > 3600) {
|
|
307
|
-
throw new Error("Block selected is more than 1 hour away from the requested timestamp");
|
|
308
|
-
}
|
|
309
|
-
return [2 /*return*/, {
|
|
310
|
-
block: block.number,
|
|
311
|
-
timestamp: block.timestamp
|
|
312
|
-
}];
|
|
313
|
-
case 17:
|
|
314
|
-
e_2 = _k.sent();
|
|
315
|
-
throw (0, generalUtil_1.formError)(e_2);
|
|
316
|
-
case 18: return [2 /*return*/];
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
exports.lookupBlock = lookupBlock;
|
|
322
74
|
// SMALL INCOMPATIBILITY: On the old API we don't return ids but we should
|
|
323
75
|
function getLogs(params) {
|
|
324
|
-
var _a;
|
|
76
|
+
var _a, _b, _c;
|
|
325
77
|
return __awaiter(this, void 0, void 0, function () {
|
|
326
|
-
var filter, logs, blockSpread, currentBlock, nextBlock, partLogs,
|
|
327
|
-
return __generator(this, function (
|
|
328
|
-
switch (
|
|
78
|
+
var filter, logs, blockSpread, currentBlock, nextBlock, logParams, partLogs, e_1, isBlockSpreadError, error;
|
|
79
|
+
return __generator(this, function (_d) {
|
|
80
|
+
switch (_d.label) {
|
|
329
81
|
case 0:
|
|
330
82
|
if (params.toBlock === undefined || params.fromBlock === undefined) {
|
|
331
83
|
throw new Error("toBlock and fromBlock need to be defined in all calls to getLogs");
|
|
@@ -339,28 +91,33 @@ function getLogs(params) {
|
|
|
339
91
|
logs = [];
|
|
340
92
|
blockSpread = params.toBlock - params.fromBlock;
|
|
341
93
|
currentBlock = params.fromBlock;
|
|
342
|
-
|
|
94
|
+
_d.label = 1;
|
|
343
95
|
case 1:
|
|
344
96
|
if (!(currentBlock < params.toBlock)) return [3 /*break*/, 6];
|
|
345
97
|
nextBlock = Math.min(params.toBlock, currentBlock + blockSpread);
|
|
346
|
-
|
|
98
|
+
logParams = __assign(__assign({}, filter), { fromBlock: currentBlock, toBlock: nextBlock });
|
|
99
|
+
_d.label = 2;
|
|
347
100
|
case 2:
|
|
348
|
-
|
|
349
|
-
return [4 /*yield*/, (0, general_1.getProvider)(params.chain, true).getLogs(
|
|
101
|
+
_d.trys.push([2, 4, , 5]);
|
|
102
|
+
return [4 /*yield*/, (0, general_1.getProvider)(params.chain, true).getLogs(logParams)];
|
|
350
103
|
case 3:
|
|
351
|
-
partLogs =
|
|
104
|
+
partLogs = _d.sent();
|
|
352
105
|
logs = logs.concat(partLogs);
|
|
353
106
|
currentBlock = nextBlock;
|
|
354
107
|
return [3 /*break*/, 5];
|
|
355
108
|
case 4:
|
|
356
|
-
|
|
357
|
-
|
|
109
|
+
e_1 = _d.sent();
|
|
110
|
+
(0, debugLog_1.debugLog)("Error fetching logs for chain ".concat(params.chain, " blockSpread: ").concat(blockSpread, ". ").concat((0, generalUtil_1.formError)(e_1)));
|
|
111
|
+
isBlockSpreadError = (_c = (_b = e_1 === null || e_1 === void 0 ? void 0 : e_1.message) === null || _b === void 0 ? void 0 : _b.includes) === null || _c === void 0 ? void 0 : _c.call(_b, "block range");
|
|
112
|
+
if (isBlockSpreadError && blockSpread >= 2e3) {
|
|
358
113
|
// We got too many results
|
|
359
114
|
// We could chop it up into 2K 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
|
|
360
115
|
blockSpread = Math.floor(blockSpread / 2);
|
|
361
116
|
}
|
|
362
117
|
else {
|
|
363
|
-
|
|
118
|
+
error = (0, generalUtil_1.formError)(e_1);
|
|
119
|
+
error.message = "[chain: ".concat(params.chain, "] ").concat(error === null || error === void 0 ? void 0 : error.message, " params: ").concat(JSON.stringify(logParams));
|
|
120
|
+
throw e_1;
|
|
364
121
|
}
|
|
365
122
|
return [3 /*break*/, 5];
|
|
366
123
|
case 5: return [3 /*break*/, 1];
|
|
@@ -381,22 +138,6 @@ function getLogs(params) {
|
|
|
381
138
|
});
|
|
382
139
|
}
|
|
383
140
|
exports.getLogs = getLogs;
|
|
384
|
-
function getTimestamp(height, chain) {
|
|
385
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
386
|
-
var provider, block;
|
|
387
|
-
return __generator(this, function (_a) {
|
|
388
|
-
switch (_a.label) {
|
|
389
|
-
case 0:
|
|
390
|
-
provider = getExtraProvider(chain);
|
|
391
|
-
return [4 /*yield*/, provider.getBlock(height)];
|
|
392
|
-
case 1:
|
|
393
|
-
block = _a.sent();
|
|
394
|
-
return [2 /*return*/, block.timestamp];
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
exports.getTimestamp = getTimestamp;
|
|
400
141
|
function normalizeAddress(address) {
|
|
401
142
|
// sol amd tezos case sensitive so no normalising
|
|
402
143
|
var prefix = address.substring(0, address.indexOf(":"));
|