Skip to content

Commit 358c7ed

Browse files
authored
skip dead chains in test (#673)
1 parent dac325d commit 358c7ed

File tree

1 file changed

+51
-43
lines changed

1 file changed

+51
-43
lines changed

src/adapters/peggedAssets/test.ts

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import peggedAssets from "../../peggedData/peggedData";
77
import { PeggedAssetIssuance, PeggedTokenBalance } from "../../types";
88
import { extractIssuanceFromSnapshot, getClosestSnapshotForChain } from "../../utils/extrapolatedCacheFallback";
99
import { PeggedIssuanceAdapter } from "./peggedAsset.type";
10+
import { DEAD_CHAINS } from "../../utils/deadChains";
1011
const {
1112
humanizeNumber,
1213
} = require("@defillama/sdk/build/computeTVL/humanizeNumber");
@@ -49,27 +50,33 @@ async function getPeggedAsset(
4950
) {
5051
const maxRetries = 3;
5152
const timeoutMs = 3 * 60 * 1000; // 3 minutes
52-
53+
5354
for (let i = 0; i < maxRetries; i++) {
5455
try {
5556
const chainApi = new sdk.ChainApi({ chain })
56-
const balance = await Promise.race([
57-
issuanceFunction(
58-
chainApi,
59-
ethBlock,
60-
chainBlocks
61-
) as Promise<PeggedTokenBalance>,
62-
new Promise<never>((_, reject) =>
63-
setTimeout(() => reject(new Error(`Issuance function for chain ${chain} exceeded the timeout limit`)), timeoutMs)
64-
),
65-
]);
66-
57+
let balance: PeggedTokenBalance | null = null;
58+
if (!DEAD_CHAINS.has(chain))
59+
balance = await Promise.race([
60+
issuanceFunction(
61+
chainApi,
62+
ethBlock,
63+
chainBlocks
64+
) as Promise<PeggedTokenBalance>,
65+
new Promise<never>((_, reject) =>
66+
setTimeout(() => reject(new Error(`Issuance function for chain ${chain} exceeded the timeout limit`)), timeoutMs)
67+
),
68+
])
69+
else {
70+
console.log(`[${adapterLabel}] Skipping RPC calls for dead chain ${chain}`);
71+
balance = {} as PeggedTokenBalance;
72+
}
73+
6774
if (balance && Object.keys(balance).length === 0) {
6875
peggedBalances[chain] = peggedBalances[chain] || {};
6976
peggedBalances[chain][issuanceType] = { [pegType]: 0 } as any;
7077
return balance;
7178
}
72-
79+
7380
if (!balance) {
7481
throw new Error(`Could not get pegged balance on chain ${chain}`);
7582
}
@@ -89,20 +96,20 @@ async function getPeggedAsset(
8996
Bridge data not found on chain ${chain}. Use sumSingleBalance from helper/generalUtil to add bridge data.`
9097
);
9198
}
92-
99+
93100
peggedBalances[chain] = peggedBalances[chain] || {};
94101
peggedBalances[chain][issuanceType] = balance as PeggedTokenBalance;
95-
102+
96103
if (issuanceType !== "minted" && issuanceType !== "unreleased") {
97104
bridgedFromMapping[issuanceType] = bridgedFromMapping[issuanceType] || [];
98105
bridgedFromMapping[issuanceType].push(balance as PeggedTokenBalance);
99106
}
100-
107+
101108
return balance as PeggedTokenBalance;
102-
109+
103110
} catch (error) {
104111
const errorMessage = error instanceof Error ? error.message : String(error);
105-
112+
106113
if (i >= maxRetries - 1) {
107114
console.warn(`[${adapterLabel}] Chain ${chain} failed after ${maxRetries} attempts:`, errorMessage);
108115
console.log(`[${adapterLabel}] Using snapshot fallback for failed chain ${chain}`);
@@ -145,7 +152,7 @@ async function getPeggedAsset(
145152
console.log(`[${adapterLabel}] No cached snapshot found for chain ${chain} (issuance: ${issuanceType})`);
146153
peggedBalances[chain] = peggedBalances[chain] || {};
147154
(peggedBalances as any)[chain][issuanceType] = { [pegType]: null as any };
148-
155+
149156
console.error(`Getting ${issuanceType} on chain ${chain} failed.`);
150157
return null;
151158
} catch (cacheError) {
@@ -172,7 +179,7 @@ async function calcCirculating(
172179
async (chain) => {
173180
let circulating: PeggedTokenBalance = { [pegType]: 0 } as any;
174181
const chainIssuances = peggedBalances[chain];
175-
182+
176183
Object.entries(chainIssuances).map(
177184
([issuanceType, peggedTokenBalance]) => {
178185
const balance = (peggedTokenBalance as any)[pegType];
@@ -188,7 +195,7 @@ async function calcCirculating(
188195
}
189196
}
190197
);
191-
198+
192199
if (bridgedFromMapping[chain]) {
193200
bridgedFromMapping[chain].forEach((peggedTokenBalance) => {
194201
const balance = (peggedTokenBalance as any)[pegType];
@@ -199,7 +206,7 @@ async function calcCirculating(
199206
(circulating as any)[pegType]! -= balance;
200207
});
201208
}
202-
209+
203210
if ((circulating as any)[pegType]! < 0) {
204211
throw new Error(
205212
`Pegged asset on chain ${chain} has negative circulating amount`
@@ -215,7 +222,7 @@ async function calcCirculating(
215222
(peggedBalances as any)["totalCirculating"]["unreleased"] = { [pegType]: 0 } as any;
216223
let peggedTotalPromises = Object.keys(peggedBalances).map((chain) => {
217224
const circulating = (peggedBalances as any)[chain].circulating || { [pegType]: 0 };
218-
const unreleased = (peggedBalances as any)[chain].unreleased || { [pegType]: 0 };
225+
const unreleased = (peggedBalances as any)[chain].unreleased || { [pegType]: 0 };
219226
if (chain !== "totalCirculating") {
220227
(peggedBalances as any)["totalCirculating"]["circulating"][pegType]! +=
221228
circulating[pegType] || 0;
@@ -233,25 +240,25 @@ if (process.argv.length < 3) {
233240
}
234241

235242
const passedFile = path.resolve(process.cwd(), process.argv[2]);
236-
const dummyFn = () => {};
243+
const dummyFn = () => { };
237244
const INTERNAL_CACHE_FILE = 'pegged-assets-cache/sdk-cache.json';
238245

239246
function getStablecoinIdFromPath(filePath: string): string {
240247
const pathParts = filePath.split(path.sep);
241248
const stablecoinDir = pathParts[pathParts.length - 1];
242-
249+
243250
if (/^\d+$/.test(stablecoinDir)) {
244251
return stablecoinDir;
245252
}
246-
253+
247254
const peggedAsset = peggedAssets.find((pegged) => {
248255
return pegged.gecko_id === stablecoinDir;
249256
});
250-
257+
251258
if (peggedAsset) {
252259
return peggedAsset.id;
253260
}
254-
261+
255262
console.warn(`[WARNING] Could not determine stablecoin ID for path: ${filePath}, using folder name: ${stablecoinDir}`);
256263
return stablecoinDir;
257264
}
@@ -289,7 +296,7 @@ function getAdapterLabelFromPath(filePath: string): string {
289296
if (!chains.includes("ethereum")) {
290297
chains.push("ethereum");
291298
}
292-
299+
293300
const ethBlock = chainBlocks.ethereum;
294301

295302
let pegType = process.argv[3];
@@ -298,7 +305,7 @@ function getAdapterLabelFromPath(filePath: string): string {
298305
}
299306
let peggedBalances: PeggedAssetIssuance = {};
300307
let bridgedFromMapping: BridgeMapping = {};
301-
308+
302309
const extrapolationMetadata = {
303310
extrapolated: false,
304311
extrapolatedChains: [] as Array<{ chain: string; timestamp: number }>
@@ -346,7 +353,7 @@ function getAdapterLabelFromPath(filePath: string): string {
346353
}
347354
);
348355
await Promise.all(peggedBalancesPromises);
349-
356+
350357
await Promise.race([
351358
calcCirculating(peggedBalances, bridgedFromMapping, pegType),
352359
new Promise<never>((_, reject) =>
@@ -369,12 +376,12 @@ function getAdapterLabelFromPath(filePath: string): string {
369376
if (chain === "extrapolated" || chain === "extrapolatedChains") {
370377
return;
371378
}
372-
373-
const item: any = { chain}
379+
380+
const item: any = { chain }
374381
if (chain !== "totalCirculating") {
375382
displayTable.push(item)
376383
console.log(`--- ${chain} ---`);
377-
384+
378385
if (issuances && typeof issuances === 'object') {
379386
Object.entries(issuances)
380387
.filter(([_, issuance]) => issuance && typeof issuance === 'object')
@@ -399,20 +406,20 @@ function getAdapterLabelFromPath(filePath: string): string {
399406
console.log(`------ Total Circulating ------`);
400407
const totalItem: any = { chain: "Total Circulating" }
401408
Object.entries((peggedBalances as any).totalCirculating).forEach(
402-
([issuanceType, issuance]) =>{
409+
([issuanceType, issuance]) => {
403410
totalItem[issuanceType] = humanizeNumber((issuance as any)[pegType]);
404411
console.log(
405412
`Total ${issuanceType}`.padEnd(25, " "),
406413
humanizeNumber((issuance as any)[pegType])
407414
)
408415
}
409416
);
410-
417+
411418
if (extrapolationMetadata.extrapolated) {
412419
console.log(`\n------ EXTRAPOLATION INFO ------`);
413420
console.log(`⚠️ Some chains used extrapolated data from cache`);
414421
console.log(`Extrapolated chains details:`);
415-
422+
416423
extrapolationMetadata.extrapolatedChains?.forEach((extrapolatedChain: any) => {
417424
const d = new Date(extrapolatedChain.timestamp * 1000);
418425
const formattedDate = d.toISOString().slice(0, 10); // YYYY-MM-DD
@@ -422,12 +429,13 @@ function getAdapterLabelFromPath(filePath: string): string {
422429
console.log(`\n------ NO EXTRAPOLATION ------`);
423430
console.log(`✅ All chains used real-time data`);
424431
}
425-
426-
console.log(`\n[DEBUG] Final extrapolation state:`, {
427-
extrapolated: extrapolationMetadata.extrapolated,
428-
extrapolatedChainsCount: extrapolationMetadata.extrapolatedChains?.length || 0,
429-
extrapolationMetadata: extrapolationMetadata.extrapolatedChains
430-
});
432+
433+
if (extrapolationMetadata?.extrapolated)
434+
console.log(`\n[DEBUG] Final extrapolation state:`, {
435+
extrapolated: extrapolationMetadata.extrapolated,
436+
extrapolatedChainsCount: extrapolationMetadata.extrapolatedChains?.length || 0,
437+
extrapolationMetadata: extrapolationMetadata.extrapolatedChains
438+
});
431439
displayTable.push(totalItem)
432440
console.table(displayTable);
433441
process.exit(0);

0 commit comments

Comments
 (0)