All files / app/utils tx-utils.ts

37.5% Statements 21/56
0% Branches 0/43
13.33% Functions 2/15
36% Lines 18/50

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128  1x 1x           1x       1x         1x         1x       1x               1x               1x               1x               1x       1x       1x 3x             1x                     1x                   1x                                       1x                        
import { StxTxDirection } from './get-stx-transfer-direction';
import { sumStxTxTotal } from './sum-stx-tx-total';
import { SEND_MANY_CONTRACT_ID } from '@constants/index';
import {
  Transaction,
  MempoolTransaction,
  ContractCallTransaction,
} from '@stacks/stacks-blockchain-api-types';
import BigNumber from 'bignumber.js';
 
type AnyTx = Transaction | MempoolTransaction;
 
export function hasMemo(tx: Transaction): boolean {
  Iif (tx.tx_type !== 'token_transfer') return false;
  return !!tx.token_transfer.memo;
}
 
export function getRecipientAddress(tx: Transaction) {
  Iif (tx.tx_type !== 'token_transfer') return null;
  return tx.token_transfer.recipient_address;
}
 
export function isContractCall(tx: AnyTx): tx is ContractCallTransaction {
  return tx.tx_type === 'contract_call';
}
 
export function isStackingTx(tx: AnyTx, poxContractId?: string) {
  return (
    isContractCall(tx) &&
    tx.contract_call.contract_id === poxContractId &&
    tx.contract_call.function_name === 'stack-stx'
  );
}
 
export function isDelegatedStackingTx(tx: AnyTx, poxContractId?: string) {
  return (
    isContractCall(tx) &&
    tx.contract_call.contract_id === poxContractId &&
    tx.contract_call.function_name === 'delegate-stack-stx'
  );
}
 
export function isDelegateStxTx(tx: AnyTx, poxContractId?: string) {
  return (
    isContractCall(tx) &&
    tx.contract_call.contract_id === poxContractId &&
    tx.contract_call.function_name === 'delegate-stx'
  );
}
 
export function isRevokingDelegationTx(tx: AnyTx, poxContractId?: string) {
  return (
    isContractCall(tx) &&
    tx.contract_call.contract_id === poxContractId &&
    tx.contract_call.function_name === 'revoke-delegate-stx'
  );
}
 
export function isMempoolTx(tx: AnyTx): tx is MempoolTransaction {
  return tx.tx_status === 'pending';
}
 
export function isSendManyTx(tx: AnyTx) {
  return tx.tx_type === 'contract_call' && tx.contract_call.contract_id === SEND_MANY_CONTRACT_ID;
}
 
export function sumTxsTotalSpentByAddress(txs: AnyTx[], address: string) {
  return txs.reduce((acc, tx) => acc.plus(sumStxTxTotal(address, tx)), new BigNumber(0));
}
 
interface InferSendManyTransferOperationReturn {
  direction: StxTxDirection;
  amount: BigNumber;
}
export function inferSendManyTransferOperation(
  sentAmount: string,
  receivedAmount: string
): InferSendManyTransferOperationReturn {
  const sent = new BigNumber(sentAmount);
  const received = new BigNumber(receivedAmount);
  const amount = sent.minus(received);
  const direction = amount.isNegative() ? 'received' : 'sent';
  return { amount: amount.absoluteValue(), direction };
}
 
export function shortenHex(hex: string, length = 4) {
  return `${hex.substring(0, length + 2)}…${hex.substring(hex.length - length)}`;
}
 
/**
 * truncateMiddle
 *
 * @param {string} input - the string to truncate
 * @param {number} offset - the number of chars to keep on either end
 */
export const truncateMiddle = (input: string, offset = 5): string => {
  Iif (!input) return '';
  // hashes
  Iif (input.startsWith('0x')) {
    return shortenHex(input, offset);
  }
  // for contracts
  if (input.includes('.')) {
    const parts = input.split('.');
    const start = parts[0]?.substr(0, offset);
    const end = parts[0]?.substr(parts[0].length - offset, parts[0].length);
    return `${start}…${end}.${parts[1]}`;
  } else {
    // everything else
    const start = input?.substr(0, offset);
    const end = input?.substr(input.length - offset, input.length);
    return `${start}…${end}`;
  }
};
 
export const getMemoForTx = (tx: Transaction, direction: StxTxDirection) => {
  Iif (tx.tx_type !== 'token_transfer' || direction !== 'sent') {
    return undefined;
  }
 
  const utf8Memo = Buffer.from(
    tx.token_transfer.memo.replace('0x', '').replace(/^(0{2})+|(0{2})+$/g, ''),
    'hex'
  ).toString('utf8');
 
  return utf8Memo;
};