Skip to content

Commit

Permalink
Fix default asset symbol in overview page
Browse files Browse the repository at this point in the history
  • Loading branch information
apporc committed Oct 24, 2024
1 parent 002813d commit fef390e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 41 deletions.
6 changes: 2 additions & 4 deletions src/routes/[network]/(account)/(staking)/staking/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
const { data } = $props();
const networkName = String(data.network);
let staked: Asset = $derived(
context.account ? getStakedBalance(data.network, context.account) : Asset.from(0, '0,UNKNOWN')
);
let staked: Asset = $derived(getStakedBalance(data.network, context.account));
let unstaking: Array<UnstakingRecord> = $derived(
context.account ? getUnstakingBalances(data.network, context.account) : []
getUnstakingBalances(data.network, context.account)
);
let apy = $derived(getAPY(data.network));
let usdValue = $derived(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ export class StakeManager {
public error: string = $state('');
public txid: string = $state('');

public stakable: Asset = $derived(
this.account && this.network ? getStakableBalance(this.network, this.account) : defaultQuantity
);
public stakable: Asset = $derived(getStakableBalance(this.network, this.account));
public apy: string = $derived(this.network ? getAPY(this.network) : '0');
public estimateYield: Asset = $derived(
this.network
this.network && this.network.loaded
? Asset.from(
(this.assetValue.value * parseFloat(this.apy)) / 100,
this.network.chain.systemToken!.symbol
Expand All @@ -39,6 +37,7 @@ export class StakeManager {

constructor(network: NetworkState) {
this.network = network;
this.assetValue = this.zeroValue;
}

get zeroValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ export class UnstakeManager {
public txid: string = $state('');

public unstaking: Array<UnstakingRecord> = $derived(
this.account && this.network ? getUnstakingBalances(this.network, this.account) : []
getUnstakingBalances(this.network, this.account)
);
public unstakable: Asset = $derived(
this.account && this.network
? getUnstakableBalance(this.network, this.account, this.unstaking)
: defaultQuantity
getUnstakableBalance(this.network, this.account, this.unstaking)
);

constructor(network: NetworkState) {
this.network = network;
this.assetValue = this.zeroValue;
}

get zeroValue() {
Expand Down
53 changes: 34 additions & 19 deletions src/routes/[network]/(account)/(staking)/staking/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ export interface UnstakingRecord {
const defaultSymbol = Asset.Symbol.from('0,UNKNOWN');
export const defaultQuantity = Asset.fromUnits(0, defaultSymbol);

export function getStakableBalance(network: NetworkState, account: AccountState): Asset {
export function getStakableBalance(network?: NetworkState, account?: AccountState): Asset {
const balance = Int64.from(0);
if (account && account.balance) {
if (account.balance && account.balance.liquid) {
balance.add(account.balance.liquid.units);
}
}
return Asset.fromUnits(balance, network.chain.systemToken!.symbol);
return Asset.fromUnits(balance, network ? network.chain.systemToken!.symbol : defaultSymbol);
}

export function getStakedBalance(network: NetworkState, account: AccountState): Asset {
export function getStakedBalance(network?: NetworkState, account?: AccountState): Asset {
const staked = Int64.from(0);
if (account && account.loaded) {
if (account && account.loaded && network && network.loaded) {
if (account.account?.data.rex_info) {
staked.add(network.rexToToken(account.account.data.rex_info.rex_balance).units);
}
if (account.sources.rexfund) {
staked.add(Asset.from(account.sources.rexfund.balance).units);
}
}
return Asset.fromUnits(staked, network.chain.systemToken!.symbol);
return Asset.fromUnits(staked, network ? network.chain.systemToken!.symbol : defaultSymbol);
}

export function getClaimableBalance(
network: NetworkState,
account: AccountState,
unstaking: Array<UnstakingRecord> | undefined
network?: NetworkState,
account?: AccountState,
unstaking?: Array<UnstakingRecord>
): Asset {
// claimable buckets, rex to be sold
const claimable = Int64.from(0);
Expand All @@ -54,24 +54,37 @@ export function getClaimableBalance(
claimable.add(sum);
}

return Asset.fromUnits(claimable, network.chain.systemToken!.symbol);
return Asset.fromUnits(
claimable,
network && network.loaded ? network.chain.systemToken!.symbol : defaultSymbol
);
}

export function getWithdrawableBalance(network: NetworkState, account: AccountState): Asset {
export function getWithdrawableBalance(network?: NetworkState, account?: AccountState): Asset {
const withdrawable = Int64.from(0);
if (account && account.loaded && account.sources.rexfund && account.sources.rexfund.balance) {
if (
network &&
network.loaded &&
account &&
account.loaded &&
account.sources.rexfund &&
account.sources.rexfund.balance
) {
withdrawable.add(Asset.from(account.sources.rexfund.balance).units);
}
return Asset.fromUnits(withdrawable, network.chain.systemToken!.symbol);
return Asset.fromUnits(
withdrawable,
network && network.loaded ? network.chain.systemToken!.symbol : defaultSymbol
);
}

export function getUnstakingBalances(
network: NetworkState,
account: AccountState
network?: NetworkState,
account?: AccountState
): Array<UnstakingRecord> {
// matured_rex + claimable buckets
const records: Array<UnstakingRecord> = [];
if (account && account.loaded && account.account?.data.rex_info) {
if (network && network.loaded && account && account.loaded && account.account?.data.rex_info) {
const rexInfo = account.account.data.rex_info;
if (rexInfo.matured_rex && rexInfo.matured_rex.gt(Int64.from(0))) {
// construct matured as one rex bucket
Expand Down Expand Up @@ -108,15 +121,17 @@ export function getUnstakingBalances(
}

export function getUnstakableBalance(
network: NetworkState,
account: AccountState,
unstaking: Array<UnstakingRecord> | undefined
network?: NetworkState,
account?: AccountState,
unstaking?: Array<UnstakingRecord>
): Asset {
if (!unstaking) {
unstaking = getUnstakingBalances(network, account);
}
const savings = unstaking.find((r) => r.savings);
return savings ? savings.balance : Asset.from(0, network.chain.systemToken!.symbol);
return savings
? savings.balance
: Asset.from(0, network && network.loaded ? network.chain.systemToken!.symbol : defaultSymbol);
}

export function getAPY(network: NetworkState): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@ export class WithdrawManager {
public txid: string = $state('');

public unstaking: Array<UnstakingRecord> = $derived(
this.account && this.network ? getUnstakingBalances(this.network, this.account) : []
getUnstakingBalances(this.network, this.account)
);
public claimable: Asset = $derived(
this.account && this.network
? getClaimableBalance(this.network, this.account, this.unstaking)
: defaultQuantity
);
public withdrawable: Asset = $derived(
this.account && this.network
? getWithdrawableBalance(this.network, this.account)
: defaultQuantity
getClaimableBalance(this.network, this.account, this.unstaking)
);
public withdrawable: Asset = $derived(getWithdrawableBalance(this.network, this.account));
public total: Asset = $derived(
this.network
this.network && this.network.loaded
? Asset.fromUnits(
this.claimable.units.adding(this.withdrawable.units),
this.network.chain.systemToken!.symbol
Expand Down

0 comments on commit fef390e

Please sign in to comment.