All files / src/services BalanceTracker.ts

100% Statements 26/26
100% Branches 2/2
100% Functions 15/15
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77  43x   43x 43x   43x 1605x     43x       125x 4x       43x       125x 4x     43x       125x                     11x                       771x 1806x     43x     257x                              
import { BalanceTracker, DelegationTracker, TransactionalObservables } from './types';
import { Cardano, coalesceValueQuantities } from '@cardano-sdk/core';
 
import { Observable, combineLatest, distinctUntilChanged, map } from 'rxjs';
import { utxoEquals } from './util';
 
const mapUtxoValue = map<Cardano.Utxo[], Cardano.Value>((utxo) =>
  coalesceValueQuantities(utxo.map(([_, txOut]) => txOut.value))
);
 
const computeDepositCoin = (
  protocolParameters$: Observable<Cardano.ProtocolParameters>,
  numDeposits$: Observable<number>
) =>
  combineLatest([numDeposits$, protocolParameters$]).pipe(
    map(([registeredAccounts, { stakeKeyDeposit }]) => BigInt(registeredAccounts * stakeKeyDeposit)),
    distinctUntilChanged()
  );
 
const numRewardAccountsWithKeyStatus = (
  delegationTracker: DelegationTracker,
  keyStatuses: Cardano.StakeCredentialStatus[]
) =>
  delegationTracker.rewardAccounts$.pipe(
    map((accounts) => accounts.filter((account) => keyStatuses.includes(account.credentialStatus)).length)
  );
 
export const createBalanceTracker = (
  protocolParameters$: Observable<Cardano.ProtocolParameters>,
  utxoTracker: TransactionalObservables<Cardano.Utxo[]>,
  delegationTracker: DelegationTracker
): BalanceTracker => ({
  rewardAccounts: {
    // 'Unregistering' balance will be reflected in utxo
    deposit$: computeDepositCoin(
      protocolParameters$,
      numRewardAccountsWithKeyStatus(delegationTracker, [
        Cardano.StakeCredentialStatus.Registered,
        Cardano.StakeCredentialStatus.Registering
      ])
    ),
    rewards$: delegationTracker.rewardAccounts$.pipe(
      map((accounts) => accounts.reduce((sum, { rewardBalance }) => sum + rewardBalance, 0n)),
      distinctUntilChanged()
    )
  },
  utxo: {
    available$: utxoTracker.available$.pipe(mapUtxoValue),
    total$: utxoTracker.total$.pipe(mapUtxoValue),
    unspendable$: utxoTracker.unspendable$.pipe(mapUtxoValue)
  }
});
 
/** Returns utxos filtered by the txOut.addresses or all utxos if addresses are undefined or empty array */
const filterUtxosByAddress = (addresses?: Cardano.PaymentAddress[]) => (utxos$: Observable<Cardano.Utxo[]>) =>
  utxos$.pipe(map((utxos) => utxos.filter(([, txOut]) => !addresses?.length || addresses.includes(txOut.address))));
 
/** Creates utxo balance aggregated by txOut.address. If addresses are undefined or an empty array, it creates a single balance from all utxos */
export const createUtxoBalanceByAddressTracker = (
  utxoTracker: TransactionalObservables<Cardano.Utxo[]>,
  addresses?: Cardano.PaymentAddress[]
): Pick<BalanceTracker, 'utxo'> => ({
  utxo: {
    available$: utxoTracker.available$.pipe(
      filterUtxosByAddress(addresses),
      distinctUntilChanged(utxoEquals),
      mapUtxoValue
    ),
    total$: utxoTracker.total$.pipe(filterUtxosByAddress(addresses), distinctUntilChanged(utxoEquals), mapUtxoValue),
    unspendable$: utxoTracker.unspendable$.pipe(
      filterUtxosByAddress(addresses),
      distinctUntilChanged(utxoEquals),
      mapUtxoValue
    )
  }
});