Skip to content

Instantly share code, notes, and snippets.

@crisog
Last active October 21, 2022 18:54
Show Gist options
  • Select an option

  • Save crisog/5503c2222dd0218e3838b4e3e23ff241 to your computer and use it in GitHub Desktop.

Select an option

Save crisog/5503c2222dd0218e3838b4e3e23ff241 to your computer and use it in GitHub Desktop.
import { Pocket, HttpRpcProvider, RpcError, JailedStatus, StakingStatus } from '@pokt-network/pocket-js';
import axios, { AxiosResponse } from 'axios';
const DISPATCH_URL = new URL("https://mainnet-1.nodes.pokt.network:4201")
const POCKET_RPC_URL = new URL("https://mainnet-1.nodes.pokt.network:4201")
async function main() {
const pocket = new Pocket([DISPATCH_URL], new HttpRpcProvider(POCKET_RPC_URL))
const queryNodesResponse = await pocket?.rpc()?.query.getNodes(StakingStatus.Staked, JailedStatus.NA, 74060n, "", 1, 30000, 60000, true)
if (!queryNodesResponse || queryNodesResponse instanceof RpcError) {
throw Error("Could not get nodes from Pocket Network")
}
const { nodes } = queryNodesResponse
let serviceURLs: string[] = []
for (const node of nodes) {
serviceURLs.push(node.serviceURL.toString())
}
const responses = await requestNodeVersions(serviceURLs, 10)
for (const response of responses) {
console.log(`${response.config.url},${response.data}`)
}
}
export const requestNodeVersions = async (
serviceURLs: string[],
retries: number,
delay = 1000
): Promise<any> => {
const promises = serviceURLs.map((serviceURL) => axios.get(`${serviceURL}v1`))
const results = await Promise.allSettled(promises);
const successResponses: AxiosResponse<any, any>[] = [];
results.forEach((promiseResult) => {
if (promiseResult.status === 'fulfilled') {
successResponses.push(promiseResult.value);
}
});
if (retries > 0 && successResponses.length !== promises.length) {
const failedServiceURLs: string[] = [];
results.forEach((promiseResult, index) => {
if (promiseResult.status === 'rejected') {
failedServiceURLs.push(serviceURLs[index]);
}
});
// Enable this to log the list of unavailable nodes
// for(const failedServiceURL of failedServiceURLs) {
// console.log(`Couldn't reach ${failedServiceURL}. Retrying...`);
// }
await new Promise((resolve) => setTimeout(resolve, delay));
const failedResults = await requestNodeVersions(
failedServiceURLs,
retries - 1
);
failedResults.forEach((promiseResult: any) => {
console.log(promiseResult)
});
}
return successResponses;
};
main().then().catch((err) => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment