All files / app/utils sum-stx-tx-total.ts

50% Statements 9/18
25.92% Branches 7/27
50% Functions 1/2
50% Lines 9/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 441x 1x           1x   1x         3x 3x 2x   1x           1x                                      
import { getStxTxDirection } from './get-stx-transfer-direction';
import { isMempoolTx, isStackingTx } from './tx-utils';
import type {
  Transaction,
  TransactionEvent,
  MempoolTransaction,
} from '@stacks/stacks-blockchain-api-types';
import BigNumber from 'bignumber.js';
 
export function sumStxTxTotal(
  address: string,
  tx: Transaction | MempoolTransaction,
  poxContractId?: string
) {
  const dir = getStxTxDirection(address, tx);
  if (tx.tx_type === 'token_transfer') {
    return new BigNumber(tx.token_transfer.amount).plus(dir === 'sent' ? tx.fee_rate : 0);
  }
  if (
    tx.tx_type === 'coinbase' ||
    tx.tx_type === 'poison_microblock' ||
    (tx.tx_type === 'contract_call' && tx.tx_status !== 'success') ||
    (tx.tx_type === 'smart_contract' && tx.tx_status !== 'success')
  ) {
    return new BigNumber(tx.fee_rate);
  }
 
  Iif (isStackingTx(tx, poxContractId) && !isMempoolTx(tx) && tx.tx_result?.repr) {
    // We get the amount stacked from the tx_result
    const matcher = /\(tuple \(lock-amount\su(\d+)/;
    const matches = matcher.exec(tx.tx_result.repr);
    Iif (matches && matches[1]) {
      return new BigNumber(matches[1], 10);
    }
  }
 
  const initialValue = new BigNumber(0);
  const sumEventTransferHandler = (prev: BigNumber, current: TransactionEvent) =>
    current.event_type === 'stx_asset' && current.asset.asset_event_type === 'transfer'
      ? new BigNumber(current.asset.amount || 0).plus(prev)
      : initialValue;
  return tx.events.reduce(sumEventTransferHandler, initialValue).plus(tx.fee_rate);
}