forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.ts
More file actions
40 lines (35 loc) · 957 Bytes
/
Copy pathstellar.ts
File metadata and controls
40 lines (35 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { horizonUrl } from '@/config';
export interface AccountBalance {
asset_type: string;
balance: string;
asset_code?: string;
asset_issuer?: string;
}
/**
* Fetch balances for a Stellar account from Horizon.
* Returns an empty array if the account does not exist (404).
*/
export async function fetchAccountBalances(
publicKey: string,
): Promise<AccountBalance[]> {
try {
const response = await fetch(
`${horizonUrl.replace(/\/$/, '')}/accounts/${publicKey}`,
);
if (!response.ok) {
if (response.status === 404) {
return [];
}
throw new Error(
`Unable to fetch Stellar balance from Horizon (${response.status}).`,
);
}
const account = (await response.json()) as {
balances?: AccountBalance[];
};
return account.balances || [];
} catch (error) {
console.error(`[Stellar] Error fetching balances for ${publicKey}:`, error);
throw error;
}
}