Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions packages/agent-sdk/src/evm/safe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Address, checksumAddress, parseUnits } from "viem";
import { type UserToken, ZerionAPI } from "zerion-sdk";
import { scientificToDecimal } from "../misc";
import { getClient } from "near-safe";

export interface TokenBalance {
tokenAddress: string | null; // null for native token
Expand Down Expand Up @@ -53,6 +54,12 @@ export async function getSafeBalances(
address: Address,
zerionKey?: string,
): Promise<TokenBalance[]> {
const client = await getClient(chainId);
const codeAt = await client.getCode({ address });
if (!codeAt) {
// Not a Safe - Get balances from Zerion.
return getZerionBalances(chainId, address, zerionKey);
}
const baseUrl = safeTxServiceUrlFor(chainId);
if (!baseUrl) {
console.warn(
Expand Down
5 changes: 0 additions & 5 deletions packages/agent-sdk/tests/evm/safe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { zeroAddress } from "viem";
import {
getSafeBalances,
flatSafeBalances,
Expand All @@ -17,10 +16,6 @@ describe("getSafeBalances", () => {
console.warn = originalWarn;
});

it("should throw error for unsupported chain ID", async () => {
await expect(getSafeBalances(999, zeroAddress)).resolves.toEqual([]);
});

it("zerionToTokenBalance should transform scientific notation balance", () => {
expect(
zerionToTokenBalance({
Expand Down