Skip to content

Commit

Permalink
feat(io): use paginated gateways API
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jul 10, 2024
1 parent 99a36fe commit 85a737f
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IO, Gateway, IO_TESTNET_PROCESS_ID } from "@ar.io/sdk/web";
import { IO, Gateway, IO_TESTNET_PROCESS_ID, WalletAddress, AoGateway } from "@ar.io/sdk/web";

export type OnlineGateway = Gateway & {
online?: boolean;
Expand Down Expand Up @@ -196,7 +196,7 @@ async function isGatewayOnline(gateway: Gateway): Promise<boolean> {
]);
return (response as Response).ok;
} catch (error) {
console.log((error as Error).message); // Log the error
console.error(error);
return false;
}
}
Expand All @@ -214,7 +214,6 @@ async function refreshOnlineGateways(): Promise<Record<string, OnlineGateway>> {
});

const results = await Promise.allSettled(promises);
console.log(results);
results.forEach((result) => {
if (result.status === "fulfilled") {
garCache[result.value.address] = result.value.gateway;
Expand All @@ -234,7 +233,8 @@ async function getGatewayAddressRegistry(arIO: any): Promise<void> {
"Getting the gateways with the SDK and Process Id: ",
processId
);
const garCache = await arIO.getGateways();

const garCache = await fetchAllGateways();
await chrome.storage.local.set({ garCache });
console.log(
`Found ${
Expand All @@ -256,6 +256,25 @@ async function getGatewayAddressRegistry(arIO: any): Promise<void> {
}
}

/**
* Fetch all gateways from the AR.IO SDK.
*/
const fetchAllGateways = async (): Promise<Record<WalletAddress, AoGateway>> => {
const gateways: Record<WalletAddress, AoGateway> = {};
let hasNextPage = true;
let page = 1;
while (hasNextPage) {
const response = await arIO.getGateways({ page });
for (const gateway of response.items) {
const { gatewayAddress, ...gatewayData } = gateway;
gateways[gatewayAddress] = gatewayData;
}
hasNextPage = response.hasNextPage;
page++;
}
return gateways;
}

/**
* Get an online gateway based on the configured routing method.
* @returns A promise that resolves to a gateway object.
Expand Down

0 comments on commit 85a737f

Please sign in to comment.