@defillama/sdk 2.3.117 → 2.3.118

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.
@@ -3,16 +3,17 @@ export declare const chainsForBlocks: Chain[];
3
3
  export declare function getChainBlocks(timestamp: number, chains?: Chain[]): Promise<{
4
4
  [chain: string]: number;
5
5
  }>;
6
- export declare function getBlocks(timestamp: number): Promise<{
7
- ethereumBlock: number;
6
+ export declare function getBlocks(timestamp: number, chains?: Chain[] | undefined): Promise<{
7
+ ethereumBlock: any;
8
8
  chainBlocks: {
9
9
  [chain: string]: number;
10
10
  };
11
11
  }>;
12
- export declare function getCurrentBlocks(): Promise<{
12
+ export declare function getCurrentBlocks(chains?: Chain[] | undefined): Promise<{
13
13
  timestamp: number;
14
14
  ethereumBlock: number;
15
15
  chainBlocks: {
16
16
  [chain: string]: number;
17
17
  };
18
18
  }>;
19
+ export declare function getBlock(chain: Chain, timestamp?: number | undefined): Promise<any>;
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.getCurrentBlocks = exports.getBlocks = exports.getChainBlocks = exports.chainsForBlocks = void 0;
39
+ exports.getBlock = exports.getCurrentBlocks = exports.getBlocks = exports.getChainBlocks = exports.chainsForBlocks = void 0;
40
40
  var general_1 = require("../general");
41
41
  var index_1 = require("../util/index");
42
42
  exports.chainsForBlocks = ["avax", "bsc", "polygon", "xdai", "fantom", "arbitrum"];
@@ -44,45 +44,22 @@ var blockRetries = 5;
44
44
  function getChainBlocks(timestamp, chains) {
45
45
  if (chains === void 0) { chains = exports.chainsForBlocks; }
46
46
  return __awaiter(this, void 0, void 0, function () {
47
- var chainBlocks;
47
+ var chainBlocks, setBlock;
48
48
  var _this = this;
49
49
  return __generator(this, function (_a) {
50
50
  switch (_a.label) {
51
51
  case 0:
52
52
  chainBlocks = {};
53
- return [4 /*yield*/, Promise.all(chains.map(function (chain) { return __awaiter(_this, void 0, void 0, function () {
54
- var i, _a, _b, e_1;
55
- return __generator(this, function (_c) {
56
- switch (_c.label) {
57
- case 0:
58
- i = 0;
59
- _c.label = 1;
60
- case 1:
61
- if (!(i < blockRetries)) return [3 /*break*/, 6];
62
- _c.label = 2;
63
- case 2:
64
- _c.trys.push([2, 4, , 5]);
65
- _a = chainBlocks;
66
- _b = chain;
67
- return [4 /*yield*/, index_1.lookupBlock(timestamp, {
68
- chain: chain,
69
- }).then(function (block) { return block.block; })];
70
- case 3:
71
- _a[_b] = _c.sent();
72
- return [3 /*break*/, 6];
73
- case 4:
74
- e_1 = _c.sent();
75
- if (i === blockRetries - 1) {
76
- throw e_1;
77
- }
78
- return [3 /*break*/, 5];
79
- case 5:
80
- i++;
81
- return [3 /*break*/, 1];
82
- case 6: return [2 /*return*/];
83
- }
84
- });
85
- }); }))];
53
+ setBlock = function (chain) { return __awaiter(_this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) {
54
+ switch (_c.label) {
55
+ case 0:
56
+ _a = chainBlocks;
57
+ _b = chain;
58
+ return [4 /*yield*/, getBlock(chain, timestamp)];
59
+ case 1: return [2 /*return*/, _a[_b] = (_c.sent()).block];
60
+ }
61
+ }); }); };
62
+ return [4 /*yield*/, Promise.all(chains.map(setBlock))];
86
63
  case 1:
87
64
  _a.sent();
88
65
  return [2 /*return*/, chainBlocks];
@@ -91,12 +68,13 @@ function getChainBlocks(timestamp, chains) {
91
68
  });
92
69
  }
93
70
  exports.getChainBlocks = getChainBlocks;
94
- function getBlocks(timestamp) {
71
+ function getBlocks(timestamp, chains) {
72
+ if (chains === void 0) { chains = undefined; }
95
73
  return __awaiter(this, void 0, void 0, function () {
96
74
  var _a, ethBlock, chainBlocks;
97
75
  return __generator(this, function (_b) {
98
76
  switch (_b.label) {
99
- case 0: return [4 /*yield*/, Promise.all([index_1.lookupBlock(timestamp), getChainBlocks(timestamp)])];
77
+ case 0: return [4 /*yield*/, Promise.all([getBlock('ethereum', timestamp), getChainBlocks(timestamp, chains)])];
100
78
  case 1:
101
79
  _a = _b.sent(), ethBlock = _a[0], chainBlocks = _a[1];
102
80
  chainBlocks['ethereum'] = ethBlock.block;
@@ -109,21 +87,20 @@ function getBlocks(timestamp) {
109
87
  });
110
88
  }
111
89
  exports.getBlocks = getBlocks;
112
- function getCurrentBlocks() {
90
+ function getCurrentBlocks(chains) {
91
+ if (chains === void 0) { chains = undefined; }
113
92
  return __awaiter(this, void 0, void 0, function () {
114
- var provider, lastBlockNumber, block, chainBlocks;
93
+ var block, chainBlocks;
115
94
  return __generator(this, function (_a) {
116
95
  switch (_a.label) {
117
96
  case 0:
118
- provider = general_1.getProvider("ethereum");
119
- return [4 /*yield*/, provider.getBlockNumber()];
97
+ if (chains)
98
+ chains = chains.filter(function (i) { return i !== "ethereum"; });
99
+ return [4 /*yield*/, getBlock('ethereum')];
120
100
  case 1:
121
- lastBlockNumber = _a.sent();
122
- return [4 /*yield*/, provider.getBlock(lastBlockNumber - 5)];
123
- case 2:
124
101
  block = _a.sent();
125
- return [4 /*yield*/, getChainBlocks(block.timestamp)];
126
- case 3:
102
+ return [4 /*yield*/, getChainBlocks(block.timestamp, chains)];
103
+ case 2:
127
104
  chainBlocks = _a.sent();
128
105
  chainBlocks['ethereum'] = block.number;
129
106
  return [2 /*return*/, {
@@ -136,3 +113,75 @@ function getCurrentBlocks() {
136
113
  });
137
114
  }
138
115
  exports.getCurrentBlocks = getCurrentBlocks;
116
+ var blockCache = {
117
+ current: {}
118
+ };
119
+ function getBlock(chain, timestamp) {
120
+ if (timestamp === void 0) { timestamp = undefined; }
121
+ return __awaiter(this, void 0, void 0, function () {
122
+ function getCurrentEthBlock() {
123
+ return __awaiter(this, void 0, void 0, function () {
124
+ var provider, lastBlockNumber;
125
+ return __generator(this, function (_a) {
126
+ switch (_a.label) {
127
+ case 0:
128
+ provider = general_1.getProvider("ethereum");
129
+ return [4 /*yield*/, provider.getBlockNumber()];
130
+ case 1:
131
+ lastBlockNumber = _a.sent();
132
+ return [2 /*return*/, provider.getBlock(lastBlockNumber - 5)]; // To allow indexers to catch up
133
+ }
134
+ });
135
+ });
136
+ }
137
+ function _getBlock() {
138
+ return __awaiter(this, void 0, void 0, function () {
139
+ var i, block, e_1;
140
+ return __generator(this, function (_a) {
141
+ switch (_a.label) {
142
+ case 0:
143
+ i = 0;
144
+ _a.label = 1;
145
+ case 1:
146
+ if (!(i < blockRetries)) return [3 /*break*/, 6];
147
+ _a.label = 2;
148
+ case 2:
149
+ _a.trys.push([2, 4, , 5]);
150
+ return [4 /*yield*/, index_1.lookupBlock(timestamp, { chain: chain, })];
151
+ case 3:
152
+ block = (_a.sent()).block;
153
+ return [2 /*return*/, block];
154
+ case 4:
155
+ e_1 = _a.sent();
156
+ if (i === blockRetries - 1) {
157
+ throw e_1;
158
+ }
159
+ return [3 /*break*/, 5];
160
+ case 5:
161
+ i++;
162
+ return [3 /*break*/, 1];
163
+ case 6: return [2 /*return*/];
164
+ }
165
+ });
166
+ });
167
+ }
168
+ return __generator(this, function (_a) {
169
+ if (!timestamp) {
170
+ if (chain !== 'ethereum')
171
+ throw new Error('Timestamp is missing!!!');
172
+ if (!blockCache.current)
173
+ blockCache.current = {};
174
+ if (!blockCache.current.ethereum) {
175
+ blockCache.current.ethereum = getCurrentEthBlock();
176
+ }
177
+ return [2 /*return*/, blockCache.current.ethereum];
178
+ }
179
+ if (!blockCache[timestamp])
180
+ blockCache[timestamp] = {};
181
+ if (!blockCache[timestamp][chain])
182
+ blockCache[timestamp][chain] = _getBlock();
183
+ return [2 /*return*/, blockCache[timestamp][chain]];
184
+ });
185
+ });
186
+ }
187
+ exports.getBlock = getBlock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "2.3.117",
3
+ "version": "2.3.118",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {