The portfolio value tracking system provides real-time updates of wallet balances and token holdings. Let's examine the key components:
The sumAssets function calculates the total amount of a specific asset across UTxOs:
// src/store/slices/walletSlice.ts
// Sums assets across UTxOs for a given asset class
export const sumAssets = (utxos: St.Utxo[], assetClass: string): Big => {
let sum = Big(0);
for (const utxo of utxos) {
const amountString = utxo.assets[assetClass];
const quantity = amountString === undefined ? Big(0) : Big(amountString);
sum = sum.add(quantity);
}
return sum;
};
The selectWalletLovelaceAmount selector retrieves the total lovelace amount in the wallet:
// src/store/slices/walletSlice.ts
// Selects total lovelace amount in wallet
export const selectWalletLovelaceAmount = (state: RootState): Big => {
const utxos = state.wallet.partialWallet.utxos;
return sumAssets(utxos, "lovelace");
};
The selectWalletAdaAmount selector converts lovelace to ADA:
// src/store/slices/walletSlice.ts
// Selects total ADA amount in wallet
export const selectWalletAdaAmount = (state: RootState): Big => {
return lovelaceToAda(selectWalletLovelaceAmount(state));
};
Token-specific balance selectors for different assets: