All files / app/hooks use-delegation-status.ts

0% Statements 0/22
0% Branches 0/22
0% Functions 0/2
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 58 59 60 61 62 63 64                                                                                                                               
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { selectCoreNodeInfo } from '../store/stacking/stacking.reducer';
import { useFetchDelegationStatus } from './use-fetch-delegation-status';
import { cvToString, hexToCV, ClarityType, cvToJSON } from '@stacks/transactions';
import { RootState } from '@store/index';
import { selectAddress } from '@store/keys';
import { selectPoxInfo } from '@store/stacking';
import BigNumber from 'bignumber.js';
import { useSelector } from 'react-redux';
 
interface DelegatedTrueStatus {
  delegated: true;
  amountMicroStx: BigNumber;
  untilBurnHeight: BigNumber;
  deadDelegation: boolean;
  delegatedTo: string;
  refetch(): Promise<any>;
}
 
interface DelegatedFalseStatus {
  delegated: false;
  refetch(): Promise<any>;
}
 
type DelegatedStatus = DelegatedTrueStatus | DelegatedFalseStatus;
 
export function useDelegationStatus(): DelegatedStatus {
  const { poxInfo, coreInfo, address } = useSelector((state: RootState) => ({
    poxInfo: selectPoxInfo(state),
    coreInfo: selectCoreNodeInfo(state),
    address: selectAddress(state),
  }));
 
  const { delegationStatus, refetch } = useFetchDelegationStatus(poxInfo?.contract_id, address);
 
  Iif (!delegationStatus || !delegationStatus?.data) return { delegated: false, refetch };
  const resp = hexToCV(delegationStatus.data);
  Iif (resp.type === ClarityType.OptionalSome && resp.value.type === ClarityType.Tuple) {
    const data = resp.value.data;
 
    const ustxAmount = (data['amount-ustx'] as any).value;
 
    const amountMicroStx = ustxAmount ? new BigNumber(ustxAmount) : new BigNumber(0);
 
    const untilBurnHeight =
      data['until-burn-ht'].type === ClarityType.OptionalSome
        ? cvToJSON(data['until-burn-ht']).value.value
        : null;
 
    const deadDelegation =
      untilBurnHeight && coreInfo ? coreInfo.burn_block_height > untilBurnHeight : false;
 
    return {
      delegated: true,
      amountMicroStx,
      untilBurnHeight,
      deadDelegation,
      delegatedTo: cvToString(data['delegated-to']),
      refetch,
    };
  }
  return { delegated: false, refetch };
}