All files / app/hooks use-websocket-integration.ts

0% Statements 0/39
0% Branches 0/23
0% Functions 0/7
0% Lines 0/35

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                                                                                                                               
import { useApi } from './use-api';
import { useDelegationStatus } from './use-delegation-status';
import { useMempool } from './use-mempool';
import { connectWebSocketClient, StacksApiWebSocketClient } from '@stacks/blockchain-api-client';
import { updateAddressBalance } from '@store/address';
import { RootState } from '@store/index';
import { selectAddress } from '@store/keys';
import { selectPoxInfo } from '@store/stacking';
import { safeAwait } from '@utils/safe-await';
import { isDelegatedStackingTx, isRevokingDelegationTx, isDelegateStxTx } from '@utils/tx-utils';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import urljoin from 'url-join';
 
export function useWebSocketIntegration() {
  const delegationStatus = useDelegationStatus();
  const mempool = useMempool();
  const dispatch = useDispatch();
 
  const { address, poxInfo } = useSelector((state: RootState) => ({
    address: selectAddress(state),
    poxInfo: selectPoxInfo(state),
  }));
 
  const api = useApi();
  useEffect(() => {
    const wsUrl = new URL(api.baseUrl);
    wsUrl.protocol = 'ws:';
    let client: null | StacksApiWebSocketClient;
 
    async function run() {
      try {
        Iif (!address) return;
        client = await connectWebSocketClient(urljoin(wsUrl.toString(), 'extended', 'v1', 'ws'));
        await client.subscribeAddressBalanceUpdates(address, async ({ address, balance }) => {
          await safeAwait(mempool.refetch());
          dispatch(updateAddressBalance({ address, balance }));
        });
        await client.subscribeAddressTransactions(address, async ({ tx_id }) => {
          const newTx = await api.getTxDetails(tx_id);
          Iif (
            isDelegatedStackingTx(newTx.data, poxInfo?.contract_id) ||
            isRevokingDelegationTx(newTx.data, poxInfo?.contract_id) ||
            isDelegateStxTx(newTx.data, poxInfo?.contract_id)
          ) {
            await safeAwait(delegationStatus.refetch());
          }
          Iif (newTx.data.tx_status !== 'success') return;
          // commented out as not particularly needed given polling for new tx
          // dispatch(addNewTransaction(newTx.data));
          // dispatch(pendingTransactionSuccessful(newTx.data));
        });
      } catch {
        // ignore
      }
    }
    void run();
    return () => {
      Iif (client) client.webSocket.close();
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [address, dispatch, api, poxInfo?.contract_id]);
}