@defillama/sdk 2.3.111 → 2.3.113
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/computeTVL/index.d.ts +1 -0
- package/build/computeTVL/index.js +9 -4
- package/build/util/index.d.ts +7 -0
- package/build/util/index.js +62 -16
- package/package.json +3 -4
|
@@ -45,6 +45,7 @@ var humanizeNumber_1 = require("./humanizeNumber");
|
|
|
45
45
|
var prices_1 = require("./prices");
|
|
46
46
|
var node_fetch_1 = __importDefault(require("node-fetch"));
|
|
47
47
|
var generalUtil_1 = require("../generalUtil");
|
|
48
|
+
var index_1 = require("./../util/index");
|
|
48
49
|
function tokenMulticall(addresses, abi, chain) {
|
|
49
50
|
return abi_1.multiCall({
|
|
50
51
|
abi: abi,
|
|
@@ -189,7 +190,7 @@ function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingec
|
|
|
189
190
|
if (getCoingeckoLock === void 0) { getCoingeckoLock = function () { return Promise.resolve(); }; }
|
|
190
191
|
if (coingeckoMaxRetries === void 0) { coingeckoMaxRetries = 3; }
|
|
191
192
|
return __awaiter(this, void 0, void 0, function () {
|
|
192
|
-
var balances, callTokenDecimals_1, chainIds, _i, chains_1, chain, normalizedBalances, _a, _b, tokenAddressOrName, normalizedAddressOrName, normalizedBalance, chain, symbolsAndDecimals, allChainTokenPrices, usdTokenBalances, tokenBalances, usdAmounts, usdTvl;
|
|
193
|
+
var balances, callTokenDecimals_1, chainIds, _i, chains_1, chain, normalizedBalances, _a, _b, tokenAddressOrName, normalizedAddressOrName, normalizedBalance, chain, symbolsAndDecimals, allChainTokenPrices, usdTokenBalances, tokenBalances, rawTokenBalances, usdAmounts, usdTvl;
|
|
193
194
|
var _this = this;
|
|
194
195
|
return __generator(this, function (_c) {
|
|
195
196
|
switch (_c.label) {
|
|
@@ -285,6 +286,7 @@ function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingec
|
|
|
285
286
|
case 7:
|
|
286
287
|
usdTokenBalances = {};
|
|
287
288
|
tokenBalances = {};
|
|
289
|
+
rawTokenBalances = {};
|
|
288
290
|
usdAmounts = Object.entries(normalizedBalances).map(function (_a) {
|
|
289
291
|
var address = _a[0], balance = _a[1];
|
|
290
292
|
return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -298,11 +300,11 @@ function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingec
|
|
|
298
300
|
chains.forEach(function (chain) {
|
|
299
301
|
if (address.startsWith(chain)) {
|
|
300
302
|
chainSelector_1 = chain;
|
|
301
|
-
normalizedAddress_1 = address.slice(chain.length + 1);
|
|
303
|
+
normalizedAddress_1 = index_1.normalizeAddress(address.slice(chain.length + 1));
|
|
302
304
|
}
|
|
303
305
|
});
|
|
304
306
|
chainTokenPrices = (_b = allChainTokenPrices[chainSelector_1]) !== null && _b !== void 0 ? _b : {};
|
|
305
|
-
chainAddress_1 = chainSelector_1 + ":" + normalizedAddress_1
|
|
307
|
+
chainAddress_1 = chainSelector_1 + ":" + normalizedAddress_1;
|
|
306
308
|
coinData = symbolsAndDecimals.find(function (coin) { return coin.coin === chainAddress_1; });
|
|
307
309
|
tokenSymbol = (_c = coinData === null || coinData === void 0 ? void 0 : coinData.symbol) === null || _c === void 0 ? void 0 : _c.toUpperCase();
|
|
308
310
|
if (tokenSymbol === undefined || tokenSymbol === null) {
|
|
@@ -315,12 +317,14 @@ function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingec
|
|
|
315
317
|
else {
|
|
316
318
|
amount = Number(balance) / Math.pow(10, Number(tokenDecimals));
|
|
317
319
|
}
|
|
318
|
-
price = (_d = chainTokenPrices[normalizedAddress_1
|
|
320
|
+
price = (_d = chainTokenPrices[normalizedAddress_1]) === null || _d === void 0 ? void 0 : _d.usd;
|
|
321
|
+
addTokenBalance(rawTokenBalances, normalizedAddress_1, amount);
|
|
319
322
|
}
|
|
320
323
|
else {
|
|
321
324
|
tokenSymbol = address;
|
|
322
325
|
price = (_e = allChainTokenPrices["coingecko"][address.toLowerCase()]) === null || _e === void 0 ? void 0 : _e.usd;
|
|
323
326
|
amount = Number(balance);
|
|
327
|
+
addTokenBalance(rawTokenBalances, "coingecko:" + address.toLowerCase(), amount);
|
|
324
328
|
}
|
|
325
329
|
if (price === undefined) {
|
|
326
330
|
if (verbose) {
|
|
@@ -362,6 +366,7 @@ function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingec
|
|
|
362
366
|
usdTvl: usdTvl,
|
|
363
367
|
usdTokenBalances: usdTokenBalances,
|
|
364
368
|
tokenBalances: tokenBalances,
|
|
369
|
+
rawTokenBalances: rawTokenBalances
|
|
365
370
|
}];
|
|
366
371
|
}
|
|
367
372
|
});
|
package/build/util/index.d.ts
CHANGED
|
@@ -33,3 +33,10 @@ export declare function getLogs(params: {
|
|
|
33
33
|
} | {
|
|
34
34
|
output: Log[];
|
|
35
35
|
}>;
|
|
36
|
+
export declare function normalizeAddress(address: string): string;
|
|
37
|
+
export declare function normalizePrefixes(address: string): string;
|
|
38
|
+
export declare function normalizeBalances(balances: {
|
|
39
|
+
[address: string]: string;
|
|
40
|
+
}): {
|
|
41
|
+
[address: string]: string;
|
|
42
|
+
};
|
package/build/util/index.js
CHANGED
|
@@ -50,10 +50,12 @@ 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.getLogs = exports.kyberTokens = exports.lookupBlock = exports.getLatestBlock = void 0;
|
|
53
|
+
exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.getLogs = exports.kyberTokens = exports.lookupBlock = exports.getLatestBlock = void 0;
|
|
54
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
54
55
|
var general_1 = require("../general");
|
|
55
56
|
var node_fetch_1 = __importDefault(require("node-fetch"));
|
|
56
57
|
var ethers_1 = require("ethers");
|
|
58
|
+
var generalUtil_1 = require("../generalUtil");
|
|
57
59
|
var kavaBlockProvider = {
|
|
58
60
|
getBlock: function (height) { return __awaiter(void 0, void 0, void 0, function () {
|
|
59
61
|
return __generator(this, function (_a) {
|
|
@@ -61,10 +63,10 @@ var kavaBlockProvider = {
|
|
|
61
63
|
.then(function (res) { return res.json(); })
|
|
62
64
|
.then(function (block) { return ({
|
|
63
65
|
number: Number(block.block.header.height),
|
|
64
|
-
timestamp: Math.round(Date.parse(block.block.header.time) / 1000)
|
|
66
|
+
timestamp: Math.round(Date.parse(block.block.header.time) / 1000)
|
|
65
67
|
}); })];
|
|
66
68
|
});
|
|
67
|
-
}); }
|
|
69
|
+
}); }
|
|
68
70
|
};
|
|
69
71
|
var terraBlockProvider = {
|
|
70
72
|
getBlock: function (height) { return __awaiter(void 0, void 0, void 0, function () {
|
|
@@ -73,10 +75,10 @@ var terraBlockProvider = {
|
|
|
73
75
|
.then(function (res) { return res.json(); })
|
|
74
76
|
.then(function (block) { return ({
|
|
75
77
|
number: Number(block.block.header.height),
|
|
76
|
-
timestamp: Math.round(Date.parse(block.block.header.time) / 1000)
|
|
78
|
+
timestamp: Math.round(Date.parse(block.block.header.time) / 1000)
|
|
77
79
|
}); })];
|
|
78
80
|
});
|
|
79
|
-
}); }
|
|
81
|
+
}); }
|
|
80
82
|
};
|
|
81
83
|
function getBlock(provider, height, chain) {
|
|
82
84
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -87,7 +89,7 @@ function getBlock(provider, height, chain) {
|
|
|
87
89
|
case 1:
|
|
88
90
|
block = _a.sent();
|
|
89
91
|
if (block === null) {
|
|
90
|
-
throw new Error("Can't get block of chain " + (chain !== null && chain !== void 0 ? chain :
|
|
92
|
+
throw new Error("Can't get block of chain " + (chain !== null && chain !== void 0 ? chain : "ethereum"));
|
|
91
93
|
}
|
|
92
94
|
return [2 /*return*/, block];
|
|
93
95
|
}
|
|
@@ -130,14 +132,15 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
130
132
|
return [4 /*yield*/, getBlock(provider, "latest", extraParams.chain)];
|
|
131
133
|
case 1:
|
|
132
134
|
lastBlock = _f.sent();
|
|
133
|
-
if (extraParams.chain !==
|
|
135
|
+
if (extraParams.chain !== "bsc" &&
|
|
136
|
+
lastBlock.timestamp - timestamp < -30 * 60) {
|
|
134
137
|
throw new Error("Last block of chain \"" + extraParams.chain + "\" is further than 30 minutes into the past. Provider is \"" + ((_b = (_a = provider) === null || _a === void 0 ? void 0 : _a.connection) === null || _b === void 0 ? void 0 : _b.url) + "\"");
|
|
135
138
|
}
|
|
136
139
|
if (Math.abs(lastBlock.timestamp - timestamp) < 60) {
|
|
137
140
|
// Short-circuit in case we are trying to get the current block
|
|
138
141
|
return [2 /*return*/, {
|
|
139
142
|
block: lastBlock.number,
|
|
140
|
-
timestamp: lastBlock.timestamp
|
|
143
|
+
timestamp: lastBlock.timestamp
|
|
141
144
|
}];
|
|
142
145
|
}
|
|
143
146
|
high = lastBlock.number;
|
|
@@ -160,17 +163,18 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
160
163
|
if (high - low > 4) return [3 /*break*/, 2];
|
|
161
164
|
_f.label = 5;
|
|
162
165
|
case 5:
|
|
163
|
-
if (extraParams.chain !==
|
|
166
|
+
if (extraParams.chain !== "bsc" &&
|
|
167
|
+
Math.abs(block.timestamp - timestamp) > 3600) {
|
|
164
168
|
throw new Error("Block selected is more than 1 hour away from the requested timestamp");
|
|
165
169
|
}
|
|
166
170
|
return [2 /*return*/, {
|
|
167
171
|
block: block.number,
|
|
168
|
-
timestamp: block.timestamp
|
|
172
|
+
timestamp: block.timestamp
|
|
169
173
|
}];
|
|
170
174
|
case 6:
|
|
171
175
|
e_1 = _f.sent();
|
|
172
176
|
console.log(e_1);
|
|
173
|
-
throw new Error("Couldn't find block height for chain " + ((_e = extraParams.chain) !== null && _e !== void 0 ? _e :
|
|
177
|
+
throw new Error("Couldn't find block height for chain " + ((_e = extraParams.chain) !== null && _e !== void 0 ? _e : "ethereum") + ", RPC node rugged");
|
|
174
178
|
case 7: return [2 /*return*/];
|
|
175
179
|
}
|
|
176
180
|
});
|
|
@@ -190,12 +194,12 @@ function kyberTokens() {
|
|
|
190
194
|
acc[pair.contractAddress] = {
|
|
191
195
|
symbol: pair.symbol,
|
|
192
196
|
decimals: pair.decimals,
|
|
193
|
-
ethPrice: pair.currentPrice
|
|
197
|
+
ethPrice: pair.currentPrice
|
|
194
198
|
};
|
|
195
199
|
return acc;
|
|
196
200
|
}, {});
|
|
197
201
|
return [2 /*return*/, {
|
|
198
|
-
output: tokens
|
|
202
|
+
output: tokens
|
|
199
203
|
}];
|
|
200
204
|
}
|
|
201
205
|
});
|
|
@@ -217,7 +221,7 @@ function getLogs(params) {
|
|
|
217
221
|
address: params.target,
|
|
218
222
|
topics: (_a = params.topics) !== null && _a !== void 0 ? _a : [ethers_1.utils.id(params.topic)],
|
|
219
223
|
fromBlock: params.fromBlock,
|
|
220
|
-
toBlock: params.toBlock
|
|
224
|
+
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
|
|
221
225
|
};
|
|
222
226
|
logs = [];
|
|
223
227
|
blockSpread = params.toBlock - params.fromBlock;
|
|
@@ -253,14 +257,56 @@ function getLogs(params) {
|
|
|
253
257
|
throw new Error("Unsupported");
|
|
254
258
|
}
|
|
255
259
|
return [2 /*return*/, {
|
|
256
|
-
output: logs.map(function (log) { return log.topics; })
|
|
260
|
+
output: logs.map(function (log) { return log.topics; })
|
|
257
261
|
}];
|
|
258
262
|
}
|
|
259
263
|
return [2 /*return*/, {
|
|
260
|
-
output: logs
|
|
264
|
+
output: logs
|
|
261
265
|
}];
|
|
262
266
|
}
|
|
263
267
|
});
|
|
264
268
|
});
|
|
265
269
|
}
|
|
266
270
|
exports.getLogs = getLogs;
|
|
271
|
+
function normalizeAddress(address) {
|
|
272
|
+
// sol amd tezos case sensitive so no normalising
|
|
273
|
+
var prefix = address.substring(0, address.indexOf(":"));
|
|
274
|
+
if (["solana", "tezos"].includes(prefix))
|
|
275
|
+
return address;
|
|
276
|
+
return address.toLowerCase();
|
|
277
|
+
}
|
|
278
|
+
exports.normalizeAddress = normalizeAddress;
|
|
279
|
+
function normalizePrefixes(address) {
|
|
280
|
+
var normalizedAddress = normalizeAddress(address);
|
|
281
|
+
if (address == normalizedAddress)
|
|
282
|
+
return address;
|
|
283
|
+
return address.startsWith("0x")
|
|
284
|
+
? "ethereum:" + address.toLowerCase()
|
|
285
|
+
: !address.includes(":")
|
|
286
|
+
? "coingecko:" + address.toLowerCase()
|
|
287
|
+
: address.toLowerCase();
|
|
288
|
+
}
|
|
289
|
+
exports.normalizePrefixes = normalizePrefixes;
|
|
290
|
+
var ethereumAddress = "ethereum:0x0000000000000000000000000000000000000000";
|
|
291
|
+
var weth = "ethereum:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
|
|
292
|
+
function normalizeBalances(balances) {
|
|
293
|
+
var _a;
|
|
294
|
+
Object.keys(balances).map(function (key) {
|
|
295
|
+
if (+balances[key] === 0) {
|
|
296
|
+
delete balances[key];
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
var normalisedKey = normalizePrefixes(key);
|
|
300
|
+
if (normalisedKey == key)
|
|
301
|
+
return;
|
|
302
|
+
generalUtil_1.sumSingleBalance(balances, normalisedKey, balances[key]);
|
|
303
|
+
delete balances[key];
|
|
304
|
+
});
|
|
305
|
+
var eth = balances[ethereumAddress];
|
|
306
|
+
if (eth !== undefined) {
|
|
307
|
+
balances[weth] = new bignumber_js_1.default((_a = balances[weth]) !== null && _a !== void 0 ? _a : 0).plus(eth).toFixed(0);
|
|
308
|
+
delete balances[ethereumAddress];
|
|
309
|
+
}
|
|
310
|
+
return balances;
|
|
311
|
+
}
|
|
312
|
+
exports.normalizeBalances = normalizeBalances;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defillama/sdk",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.113",
|
|
4
4
|
"description": "SDK for calculating TVL",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,11 +18,10 @@
|
|
|
18
18
|
"author": "",
|
|
19
19
|
"license": "ISC",
|
|
20
20
|
"types": "build/index.d.ts",
|
|
21
|
-
"files": [
|
|
22
|
-
"build"
|
|
23
|
-
],
|
|
21
|
+
"files": ["build"],
|
|
24
22
|
"dependencies": {
|
|
25
23
|
"@supercharge/promise-pool": "^2.1.0",
|
|
24
|
+
"bignumber.js": "^9.1.0",
|
|
26
25
|
"ethers": "^5.4.5",
|
|
27
26
|
"graphql": "^15.5.0",
|
|
28
27
|
"graphql-request": "^3.4.0",
|