All files / app/store/pending-transaction pending-transaction.reducer.ts

0% Statements 0/24
0% Branches 0/2
0% Functions 0/9
0% Lines 0/20

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                                                                                                                 
import { RootState } from '..';
import { fetchTransactionsDone, pendingTransactionSuccessful } from '../transaction';
import {
  createEntityAdapter,
  EntityState,
  createSlice,
  createSelector,
  PayloadAction,
} from '@reduxjs/toolkit';
import { MempoolTransaction, Transaction } from '@stacks/stacks-blockchain-api-types';
 
export type PendingTransactionState = EntityState<MempoolTransaction>;
 
const pendingTransactionAdapter = createEntityAdapter<MempoolTransaction>({
  selectId: pendingTx => pendingTx.tx_id,
  sortComparer: (pTx1, pTx2) => pTx2.receipt_time - pTx1.receipt_time,
});
 
const initialState = pendingTransactionAdapter.getInitialState();
 
export const pendingTransactionSlice = createSlice({
  name: 'pendingTransactions',
  initialState,
  reducers: {
    addPendingTransaction: pendingTransactionAdapter.addOne,
    removePendingTransaction: pendingTransactionAdapter.removeOne,
  },
  extraReducers: {
    [fetchTransactionsDone.toString()]: (state, action: PayloadAction<Transaction[]>) => {
      Iif (state.ids.length === 0) return;
      state.ids.forEach(pendingId => {
        Iif (action.payload.map(tx => tx.tx_id).includes(pendingId as string)) {
          pendingTransactionAdapter.removeOne(state, pendingId);
        }
      });
    },
    // cannot import owing to circular dependency
    'transactions/pending-transaction-successful': (
      state,
      action: ReturnType<typeof pendingTransactionSuccessful>
    ) => pendingTransactionAdapter.removeOne(state, action.payload.tx.tx_id),
  },
});
 
export const pendingTransactionReducer = pendingTransactionSlice.reducer;
 
const selectPendingTransactionState = (state: RootState) => state.pendingTransaction;
const selectors = pendingTransactionAdapter.getSelectors(selectPendingTransactionState);
 
export const selectPendingTransactions = selectors.selectAll;
export const selectIsStackingCallPending = createSelector(selectors.selectAll, state =>
  state.some(tx => (tx as any).isStackingCall)
);
 
export const addPendingTransaction = pendingTransactionSlice.actions.addPendingTransaction;
export const removePendingTransaction = pendingTransactionSlice.actions.removePendingTransaction;