Skip to content

Commit

Permalink
fix account balances parse
Browse files Browse the repository at this point in the history
  • Loading branch information
ttwishing committed Oct 4, 2024
1 parent 97d36be commit f7be983
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
44 changes: 21 additions & 23 deletions src/lib/state/client/account.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { calculateValue } from '$lib/utils';

const defaultDataSources = {
get_account: undefined,
light_account: [],
balances: [],
delegated: [],
rex: undefined,
rexfund: undefined
Expand Down Expand Up @@ -79,7 +79,7 @@ export class AccountState {
this.last_update = new Date();
this.sources = {
get_account: json.account_data,
light_account: json.balances,
balances: json.balances,
delegated: json.delegated,
rex: json.rex,
rexfund: json.rexfund
Expand Down Expand Up @@ -218,29 +218,27 @@ export function getBalances(
chain: Checksum256,
tokenmeta?: TokenMeta[]
): TokenBalance[] {
if (sources.light_account) {
if (sources.balances) {
const balances: TokenBalance[] = [];
sources.light_account.forEach((lightAccount) => {
lightAccount.balances?.forEach((balance) => {
const asset = Asset.from(`${balance.amount} ${balance.currency}`);
const contract = Name.from(balance.contract);
const id = TokenIdentifier.from({
chain: chain,
contract: contract,
symbol: asset.symbol
});
const metadata =
tokenmeta && tokenmeta.length > 0
? tokenmeta.find((meta) => meta.id.equals(id))
: undefined;
balances.push(
TokenBalance.from({
asset,
contract,
metadata: metadata || TokenMeta.from({ id: { chain, contract, symbol: asset.symbol } })
})
);
sources.balances?.forEach((balance) => {
const asset = Asset.from(`${balance.amount} ${balance.currency}`);
const contract = Name.from(balance.contract);
const id = TokenIdentifier.from({
chain: chain,
contract: contract,
symbol: asset.symbol
});
const metadata =
tokenmeta && tokenmeta.length > 0
? tokenmeta.find((meta) => meta.id.equals(id))
: undefined;
balances.push(
TokenBalance.from({
asset,
contract,
metadata: metadata || TokenMeta.from({ id: { chain, contract, symbol: asset.symbol } })
})
);
});

return balances;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ActivityAction extends Struct {

export interface DataSources {
get_account?: API.v1.AccountObject | undefined;
light_account: LightAPIBalanceResponse[];
balances: LightAPIBalanceRow[];
delegated: SystemContract.Types.delegated_bandwidth[];
rex?: SystemContract.Types.rex_balance;
rexfund?: SystemContract.Types.rex_fund;
Expand Down

0 comments on commit f7be983

Please sign in to comment.