All files / app/store/address address.reducer.ts

0% Statements 0/28
0% Branches 0/4
0% Functions 0/8
0% Lines 0/21

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                                                                                             
import { RootState } from '..';
import { fetchAddressDone, updateAddressBalance } from './address.actions';
import { createReducer, createSelector } from '@reduxjs/toolkit';
import { AddressStxBalanceResponse } from '@stacks/stacks-blockchain-api-types';
import BigNumber from 'bignumber.js';
 
export interface AddressState {
  loading: boolean;
  details: AddressStxBalanceResponse | null;
}
 
const initialState: AddressState = {
  loading: true,
  details: null,
};
 
export const addressReducer = createReducer(initialState, builder =>
  builder
    .addCase(fetchAddressDone, (state, { payload }) => {
      state.loading = false;
      state.details = payload;
    })
    .addCase(updateAddressBalance, (state, action) => {
      Iif (state.details === null) return;
      state.details.balance = action.payload.balance;
    })
);
 
export const selectAddressState = (state: RootState) => state.address;
 
export const selectAddressBalance = createSelector(selectAddressState, state => state.details);
 
export const selectAvailableBalance = createSelector(selectAddressState, address => {
  Iif (address.details === null) return null;
  return new BigNumber(address.details.balance).minus(address.details.locked).toString();
});
 
export const selectTotalBalance = createSelector(selectAddressState, address => {
  Iif (address.details === null) return null;
  return new BigNumber(address.details.balance);
});
 
export const selectLockedBalance = createSelector(selectAddressState, address => {
  Iif (address.details === null) return null;
  return new BigNumber(address.details.locked);
});