Skip to content

Commit

Permalink
replace system token value with liquid
Browse files Browse the repository at this point in the history
  • Loading branch information
ttwishing committed Oct 17, 2024
1 parent cca8ad5 commit e6f19b3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/lib/state/client/account.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ export function preventDefault<TThis>(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}`
);
}
1 change: 1 addition & 0 deletions src/routes/[network]/api/account/[[name]]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e6f19b3

Please sign in to comment.