Skip to content

Commit 07ef069

Browse files
authored
Fallback to Zerion Balance Fetching on Failed Safe Attempt (#18)
We are hitting some failed balance fetching on Avalanche: ``` Error fetching Safe balances: TypeError: fetch failed at node:internal/deps/undici/undici:13185:13 at processTicksAndRejections (node:internal/process/task_queues:95:5) at runNextTicks (node:internal/process/task_queues:64:3) at process.processImmediate (node:internal/timers:454:9) at process.callbackTrampoline (node:internal/async_hooks:130:17) at async getSafeBalances (/var/task/node_modules/@bitte-ai/agent-sdk/dist/cjs/evm/safe.js:40:26) at async Promise.all (index 0) { [cause]: ConnectTimeoutError: Connect Timeout Error (attempted addresses: 108.138.85.41:443) at onConnectTimeout (node:internal/deps/undici/undici:2331:28) at node:internal/deps/undici/undici:2283:50 at Immediate._onImmediate (node:internal/deps/undici/undici:2315:13) at process.processImmediate (node:internal/timers:483:21) at process.callbackTrampoline (node:internal/async_hooks:130:17) { code: 'UND_ERR_CONNECT_TIMEOUT' } } ``` for this request: https://safe-transaction-avalanche.safe.global/api/v1/safes/0x454F89b3Ba39A15C198F95c2C92A20dD9D102144/balances/?trusted=false&exclude_spam=true Attempting to temporarily navigate the Safe Balance fetching failures by using zerion on error. Asked about rate limiting on [Safe Discord](https://discord.com/channels/907968493134155807/960898410037272616/1328708351516872778)
1 parent b744d13 commit 07ef069

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

packages/agent-sdk/src/evm/safe.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,39 @@ export async function getSafeBalances(
7474
if (!response.ok) {
7575
if (response.status === 404) {
7676
console.warn(`Safe not found for ${address}`);
77-
if (!zerionKey) {
78-
return [];
79-
}
80-
console.info("Zerion Key provided - using Zerion");
81-
// Not a Safe, try Zerion
82-
try {
83-
const zerion = new ZerionAPI(zerionKey);
84-
// TODO(bh2smith): This is not super efficient, but it works for now.
85-
// Zerion API has its own network filter (but its not by chainID).
86-
const balances = await zerion.ui.getUserBalances(address, {
87-
options: { supportedChains: [chainId] },
88-
});
89-
return zerionToTokenBalances(balances.tokens);
90-
} catch (error) {
91-
console.error("Error fetching Zerion balances:", error);
92-
return [];
93-
}
9477
}
9578
throw new Error(`HTTP error! status: ${response.status}`);
9679
}
80+
const data: TokenBalance[] = await response.json();
81+
console.log(`Retrieved ${data.length} balances`);
82+
return data;
83+
} catch (error) {
84+
console.warn("Error fetching Safe balances:", error);
85+
return getZerionBalances(chainId, address, zerionKey);
86+
}
87+
}
9788

98-
return await response.json();
89+
async function getZerionBalances(
90+
chainId: number,
91+
address: Address,
92+
zerionKey?: string,
93+
): Promise<TokenBalance[]> {
94+
if (!zerionKey) {
95+
return [];
96+
}
97+
console.info("Zerion Key provided - using Zerion");
98+
// Not a Safe, try Zerion
99+
try {
100+
const zerion = new ZerionAPI(zerionKey);
101+
// TODO(bh2smith): This is not super efficient, but it works for now.
102+
// Zerion API has its own network filter (but its not by chainID).
103+
const balances = await zerion.ui.getUserBalances(address, {
104+
options: { supportedChains: [chainId] },
105+
});
106+
return zerionToTokenBalances(balances.tokens);
99107
} catch (error) {
100-
console.error("Error fetching Safe balances:", error);
101-
throw error;
108+
console.error("Error fetching Zerion balances:", error);
109+
return [];
102110
}
103111
}
104112

0 commit comments

Comments
 (0)