Skip to content

Commit

Permalink
Merge pull request #22 from ar-io/paginated-gateways
Browse files Browse the repository at this point in the history
feat(io): use paginated gateways API
  • Loading branch information
vilenarios authored Jul 11, 2024
2 parents 99a36fe + c33735f commit 116cb02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
},
"homepage": "https://github.com/ar-io/wayfinder#readme",
"dependencies": {
"@ar.io/sdk": "2.0.0-alpha.2"
"@ar.io/sdk": "^2.0.0"
}
}
25 changes: 21 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,23 @@ 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 cursor;
do {
const response = await arIO.getGateways({ cursor });
for (const gateway of response.items) {
const { gatewayAddress, ...gatewayData } = gateway;
gateways[gatewayAddress] = gatewayData;
}
cursor = response.nextCursor;
} while (cursor)
return gateways;
}

/**
* Get an online gateway based on the configured routing method.
* @returns A promise that resolves to a gateway object.
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"@jridgewell/gen-mapping" "^0.3.5"
"@jridgewell/trace-mapping" "^0.3.24"

"@ar.io/sdk@2.0.0-alpha.2":
version "2.0.0-alpha.2"
resolved "https://registry.yarnpkg.com/@ar.io/sdk/-/sdk-2.0.0-alpha.2.tgz#30cc90ebafb16f700e3a3b27c391794999a7b73e"
integrity sha512-LQ1zRunqJrrshWFHDn22t7k6Mm1Pi85Q4tJZ1oc1af+B7B3FlUKUtYhX5cjshRYSVAooporgR+Lo+enI9O+nkg==
"@ar.io/sdk@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@ar.io/sdk/-/sdk-2.0.0.tgz#8725d1ece71b19dd7bfe44e846190bd458164206"
integrity sha512-at40lht1K6BOegoHoTezgL3z8y+Wj3VTEHMjNcoAywAV4R5l69xOTkJqEDL1uyLahspmxYGgX7rf50XFaVxMOw==
dependencies:
"@permaweb/aoconnect" "^0.0.55"
arbundles "0.11.0"
Expand Down

0 comments on commit 116cb02

Please sign in to comment.