From e6f19b327d5e4d64442595d492d318ceedf0cd80 Mon Sep 17 00:00:00 2001 From: kurt Date: Thu, 17 Oct 2024 11:25:04 +0800 Subject: [PATCH] replace system token value with liquid --- src/lib/state/client/account.svelte.ts | 27 ++++++++++++++++--- src/lib/utils/index.ts | 7 +++++ .../[network]/api/account/[[name]]/+server.ts | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/lib/state/client/account.svelte.ts b/src/lib/state/client/account.svelte.ts index f8b165b21..eba9d24f2 100644 --- a/src/lib/state/client/account.svelte.ts +++ b/src/lib/state/client/account.svelte.ts @@ -43,7 +43,13 @@ export class AccountState { ); public balances = $derived.by(() => this.network - ? getBalances(this.sources, this.network.chain.id, this.network.tokenmeta) + ? getBalances( + this.network, + this.sources, + this.network.chain.id, + this.network.tokenmeta, + this.balance + ) : undefined ); public delegations = $derived(getDelegations(this.sources)); @@ -214,14 +220,29 @@ export function getBalance(network: NetworkState, sources: DataSources): Balance } export function getBalances( + network: NetworkState, sources: DataSources, chain: Checksum256, - tokenmeta?: TokenMeta[] + tokenmeta?: TokenMeta[], + balance?: Balance ): TokenBalance[] { if (sources.light_account) { const balances: TokenBalance[] = []; + + //If the value of system token is 0, + //for example, the chain does not support lightapi. + //replace it with the value of liquid sources.light_account?.forEach((lightAccount) => { - const asset = Asset.from(`${lightAccount.amount} ${lightAccount.currency}`); + let amount = lightAccount.amount; + if ( + !Number(amount) && + String(network.chain.systemToken?.contract) === lightAccount.contract && + String(network.chain.systemToken?.symbol.name) === lightAccount.currency && + balance?.liquid + ) { + amount = String(balance.liquid.value); + } + const asset = Asset.from(`${amount} ${lightAccount.currency}`); const contract = Name.from(lightAccount.contract); const id = TokenIdentifier.from({ chain: chain, diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 04bd86544..06a62ab60 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -31,3 +31,10 @@ export function preventDefault(fn: (this: TThis, event: Event) => void) { fn.call(this, event); }; } + + +export function isSystemToken(balance: Asset, currency: Asset): Asset { + return Asset.from( + `${(currency.value * balance.value).toFixed(currency.symbol.precision)} ${currency.symbol.code}` + ); +} \ No newline at end of file diff --git a/src/routes/[network]/api/account/[[name]]/+server.ts b/src/routes/[network]/api/account/[[name]]/+server.ts index 87389e284..48bc142f3 100644 --- a/src/routes/[network]/api/account/[[name]]/+server.ts +++ b/src/routes/[network]/api/account/[[name]]/+server.ts @@ -65,6 +65,7 @@ async function loadBalances( if (network.supports('lightapi')) { const result = await f(`https://balances.unicove.com/api/balances/${network}/${account}`); const json: LightAPIBalanceResponse = await result.json(); + console.log("json = ", json); balances.push(...json.balances); } return balances;