Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions packages/agent-sdk/src/evm/safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,39 @@ export async function getSafeBalances(
if (!response.ok) {
if (response.status === 404) {
console.warn(`Safe not found for ${address}`);
if (!zerionKey) {
return [];
}
console.info("Zerion Key provided - using Zerion");
// Not a Safe, try Zerion
try {
const zerion = new ZerionAPI(zerionKey);
// TODO(bh2smith): This is not super efficient, but it works for now.
// Zerion API has its own network filter (but its not by chainID).
const balances = await zerion.ui.getUserBalances(address, {
options: { supportedChains: [chainId] },
});
return zerionToTokenBalances(balances.tokens);
} catch (error) {
console.error("Error fetching Zerion balances:", error);
return [];
}
}
throw new Error(`HTTP error! status: ${response.status}`);
}
const data: TokenBalance[] = await response.json();
console.log(`Retrieved ${data.length} balances`);
return data;
} catch (error) {
console.warn("Error fetching Safe balances:", error);
return getZerionBalances(chainId, address, zerionKey);
}
}

return await response.json();
async function getZerionBalances(
chainId: number,
address: Address,
zerionKey?: string,
): Promise<TokenBalance[]> {
if (!zerionKey) {
return [];
}
console.info("Zerion Key provided - using Zerion");
// Not a Safe, try Zerion
try {
const zerion = new ZerionAPI(zerionKey);
// TODO(bh2smith): This is not super efficient, but it works for now.
// Zerion API has its own network filter (but its not by chainID).
const balances = await zerion.ui.getUserBalances(address, {
options: { supportedChains: [chainId] },
});
return zerionToTokenBalances(balances.tokens);
} catch (error) {
console.error("Error fetching Safe balances:", error);
throw error;
console.error("Error fetching Zerion balances:", error);
return [];
}
}

Expand Down
Loading