Skip to content

Commit fbec27e

Browse files
committed
feat(auth, networks): add minimum 2 retries on fetchBlockchainData method
1 parent 3f741b9 commit fbec27e

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed
Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
import { EthBlockhashInfo } from '@lit-protocol/types';
22

3+
const RETRY_ATTEMPTS = 2; // total attempts = RETRY_ATTEMPTS + 1
4+
const RETRY_DELAY_MS = 250;
5+
6+
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
7+
38
export const fetchBlockchainData = async () => {
4-
try {
5-
const resp = await fetch(
6-
'https://block-indexer.litgateway.com/get_most_recent_valid_block'
7-
);
8-
if (!resp.ok) {
9-
throw new Error(`Primary fetch failed with status: ${resp.status}`); // Or a custom error
10-
}
9+
let lastError: Error | undefined;
1110

12-
const blockHashBody: EthBlockhashInfo = await resp.json();
13-
const { blockhash, timestamp } = blockHashBody;
11+
for (let attempt = 0; attempt <= RETRY_ATTEMPTS; attempt++) {
12+
try {
13+
const resp = await fetch(
14+
'https://block-indexer.litgateway.com/get_most_recent_valid_block'
15+
);
16+
if (!resp.ok) {
17+
throw new Error(`Primary fetch failed with status: ${resp.status}`);
18+
}
1419

15-
if (!blockhash || !timestamp) {
16-
throw new Error('Invalid data from primary blockhash source');
17-
}
20+
const blockHashBody: EthBlockhashInfo = await resp.json();
21+
const { blockhash, timestamp } = blockHashBody;
1822

19-
return blockhash;
20-
} catch (error) {
21-
if (error instanceof Error) {
22-
throw new Error(error.message);
23+
if (!blockhash || !timestamp) {
24+
throw new Error('Invalid data from primary blockhash source');
25+
}
26+
27+
return blockhash;
28+
} catch (error) {
29+
lastError = error instanceof Error ? error : new Error(String(error));
30+
if (attempt === RETRY_ATTEMPTS) {
31+
throw new Error(lastError.message);
32+
}
33+
34+
await delay(RETRY_DELAY_MS * (attempt + 1));
2335
}
24-
throw new Error(String(error));
2536
}
37+
38+
throw new Error(lastError?.message ?? 'Unknown fetchBlockchainData error');
2639
};
Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
import { EthBlockhashInfo } from '@lit-protocol/types';
22

3+
const RETRY_ATTEMPTS = 2; // total attempts = RETRY_ATTEMPTS + 1
4+
const RETRY_DELAY_MS = 250;
5+
6+
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
7+
38
export const fetchBlockchainData = async () => {
4-
try {
5-
const resp = await fetch(
6-
'https://block-indexer.litgateway.com/get_most_recent_valid_block'
7-
);
8-
if (!resp.ok) {
9-
throw new Error(`Primary fetch failed with status: ${resp.status}`); // Or a custom error
10-
}
9+
let lastError: Error | undefined;
1110

12-
const blockHashBody: EthBlockhashInfo = await resp.json();
13-
const { blockhash, timestamp } = blockHashBody;
11+
for (let attempt = 0; attempt <= RETRY_ATTEMPTS; attempt++) {
12+
try {
13+
const resp = await fetch(
14+
'https://block-indexer.litgateway.com/get_most_recent_valid_block'
15+
);
16+
if (!resp.ok) {
17+
throw new Error(`Primary fetch failed with status: ${resp.status}`);
18+
}
1419

15-
if (!blockhash || !timestamp) {
16-
throw new Error('Invalid data from primary blockhash source');
17-
}
20+
const blockHashBody: EthBlockhashInfo = await resp.json();
21+
const { blockhash, timestamp } = blockHashBody;
1822

19-
return blockhash;
20-
} catch (error) {
21-
if (error instanceof Error) {
22-
throw new Error(error.message);
23+
if (!blockhash || !timestamp) {
24+
throw new Error('Invalid data from primary blockhash source');
25+
}
26+
27+
return blockhash;
28+
} catch (error) {
29+
lastError = error instanceof Error ? error : new Error(String(error));
30+
if (attempt === RETRY_ATTEMPTS) {
31+
throw new Error(lastError.message);
32+
}
33+
34+
await delay(RETRY_DELAY_MS * (attempt + 1));
2335
}
24-
throw new Error(String(error));
2536
}
37+
38+
throw new Error(lastError?.message ?? 'Unknown fetchBlockchainData error');
2639
};

0 commit comments

Comments
 (0)