-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirrorNodeClient.js
49 lines (39 loc) · 1.27 KB
/
mirrorNodeClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import axios from "axios";
class MirrorNodeClient {
constructor() {
this.mirrorNodeRestUrl = process.env.MIRROR_NODE_REST_URL;
this.NODE_TIMEOUT = process.env.NODE_TIMEOUT;
}
async getAccountData(accountId) {
const url = `${this.mirrorNodeRestUrl}/api/v1/accounts/${accountId}`;
return this.retryUntilData(url);
}
async getBalanceData() {
const url = `${this.mirrorNodeRestUrl}/api/v1/balances`;
return this.retryUntilData(url);
}
async getTokenData(tokenId) {
const url = `${this.mirrorNodeRestUrl}/api/v1/tokens/${tokenId}`;
return this.retryUntilData(url);
}
async retryUntilData(url) {
const maxRetries = Math.floor(this.NODE_TIMEOUT / 1000); // retry once per second
let retries = 0;
while (retries < maxRetries) {
try {
const response = await axios.get(url);
if (response.data) {
return response.data;
}
} catch (error) {
// Uncomment if you want to see the error
// console.error(error);
}
// If the array is empty, delay for a second before the next try
await new Promise((resolve) => setTimeout(resolve, 1000));
retries++;
}
throw new Error("Max retries reached without data");
}
}
export default new MirrorNodeClient();