All files transfer-utils.ts

88.88% Statements 72/81
55.81% Branches 24/43
100% Functions 12/12
95.31% Lines 61/64

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 1651x   1x 1x   1x 39x         1x 63x         1x 27x 27x   27x 27x   27x 27x   27x                               1x 8x 8x   8x 8x   8x                               1x 12x       12x 12x       12x 12x   12x     269x       12x                         1x 12x 12x   12x 35x 35x 25x 25x       12x     1x 12x 12x   12x   12x                           1x 12x 12x 12x   12x 35x 35x   35x 35x 28x   28x 15x   28x 12x       12x     1x 70x          
import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
import { ParsedInstruction, ParsedTransactionWithMeta } from "@solana/web3.js";
import { TOKENS, DEX_PROGRAMS } from "./constants";
import { TokenInfo, TransferData, convertToUiAmount, TradeInfo, DexInfo } from "./types";
 
export const isTransferCheck = (instruction: ParsedInstruction): boolean => {
    return (instruction.programId.equals(TOKEN_PROGRAM_ID) ||
        instruction.programId.equals(TOKEN_2022_PROGRAM_ID)) &&
        instruction.parsed.type.includes("transferChecked");
}
 
export const isTransfer = (instruction: ParsedInstruction): boolean => {
    return instruction.program === "spl-token" &&
        instruction.programId.equals(TOKEN_PROGRAM_ID) &&
        instruction.parsed.type === "transfer";
}
 
export const processTransfer = (instruction: ParsedInstruction, splTokenMap: Map<string, TokenInfo>, splDecimalsMap: Map<string, number>): TransferData | null => {
    const { info } = instruction.parsed;
    Iif (!info) return null;
 
    const mint = splTokenMap.get(info.destination)?.mint;
    Iif (!mint) return null;
 
    const decimals = splDecimalsMap.get(mint);
    Iif (typeof decimals === 'undefined') return null;
 
    return {
        type: 'transfer',
        info: {
            authority: info.authority || "",
            destination: info.destination || "",
            mint,
            source: info.source || "",
            tokenAmount: {
                amount: info.amount,
                decimals,
                uiAmount: convertToUiAmount(info.amount, decimals),
            },
        },
    };
}
 
export const processTransferCheck = (instruction: ParsedInstruction, splDecimalsMap: Map<string, number>): TransferData | null => {
    const { info } = instruction.parsed;
    Iif (!info) return null;
 
    const decimals = splDecimalsMap.get(info.mint);
    Iif (typeof decimals === 'undefined') return null;
 
    return {
        type: 'transferChecked',
        info: {
            authority: info.authority || "",
            destination: info.destination || "",
            mint: info.mint || "",
            source: info.source || "",
            tokenAmount: info.tokenAmount || {
                amount: info.amount,
                decimals,
                uiAmount: convertToUiAmount(info.amount, decimals),
            },
        },
    };
}
 
export const processSwapData = (txWithMeta: ParsedTransactionWithMeta, transfers: TransferData[], dexInfo: DexInfo): TradeInfo => {
    Iif (!transfers.length) {
        throw new Error("No swap data provided");
    }
 
    const uniqueTokens = extractUniqueTokens(transfers);
    Iif (uniqueTokens.length < 2) {
        throw new Error("Insufficient unique tokens for swap");
    }
 
    const { inputToken, outputToken } = calculateTokenAmounts(transfers, uniqueTokens);
    const tradeType = Object.values(TOKENS).includes(inputToken.mint) ? "SELL" : "BUY";
 
    let signer = txWithMeta.transaction.message.accountKeys[0].pubkey.toBase58();
 
    // containsDCAProgram checks if the transaction contains the Jupiter DCA program.
    Iif (txWithMeta.transaction.message.accountKeys.find((it) => it.pubkey.toBase58() == DEX_PROGRAMS.JUPITER_DCA.id)) {
        signer = txWithMeta.transaction.message.accountKeys[2].pubkey.toBase58();
    }
 
    return {
        type: tradeType,
        inputToken,
        outputToken,
        user: signer,
        programId: dexInfo.programId,
        amm: dexInfo.amm,
        slot: txWithMeta.slot,
        timestamp: txWithMeta.blockTime || 0,
        signature: txWithMeta.transaction.signatures[0],
    };
}
 
export const extractUniqueTokens = (transfers: TransferData[]): TokenInfo[] => {
    const uniqueTokens: TokenInfo[] = [];
    const seenTokens = new Set<string>();
 
    transfers.forEach(transfer => {
        const tokenInfo = getTransferTokenInfo(transfer);
        if (tokenInfo && !seenTokens.has(tokenInfo.mint)) {
            uniqueTokens.push(tokenInfo);
            seenTokens.add(tokenInfo.mint);
        }
    });
 
    return uniqueTokens;
}
 
export const calculateTokenAmounts = (transfers: TransferData[], uniqueTokens: TokenInfo[]) => {
    const inputToken = uniqueTokens[0];
    const outputToken = uniqueTokens[uniqueTokens.length - 1];
 
    const amounts = sumTokenAmounts(transfers, inputToken.mint, outputToken.mint);
 
    return {
        inputToken: {
            mint: inputToken.mint,
            amount: amounts.inputAmount,
            decimals: inputToken.decimals,
        },
        outputToken: {
            mint: outputToken.mint,
            amount: amounts.outputAmount,
            decimals: outputToken.decimals,
        },
    };
}
 
export const sumTokenAmounts = (transfers: TransferData[], inputMint: string, outputMint: string) => {
    const seenTransfers = new Set<string>();
    let inputAmount = 0;
    let outputAmount = 0;
 
    transfers.forEach(transfer => {
        const tokenInfo = getTransferTokenInfo(transfer);
        Iif (!tokenInfo) return;
 
        const key = `${tokenInfo.amount}-${tokenInfo.mint}`;
        if (seenTransfers.has(key)) return;
        seenTransfers.add(key);
 
        if (tokenInfo.mint === inputMint) {
            inputAmount += tokenInfo.amount;
        }
        if (tokenInfo.mint === outputMint) {
            outputAmount += tokenInfo.amount;
        }
    });
 
    return { inputAmount, outputAmount };
}
 
export const getTransferTokenInfo = (transfer: TransferData): TokenInfo | null => {
    return transfer?.info ? {
        mint: transfer.info.mint,
        amount: transfer.info.tokenAmount.uiAmount,
        decimals: transfer.info.tokenAmount.decimals,
    } : null;
}