import { CompiledInstruction, PublicKey, TransactionResponse } from '@solana/web3.js'; import { SOLCIAL_MINT, SOLCIAL_SWAP } from '../config'; import b58 from 'b58'; export enum programOperationTypes { SWAP_CREATION = 8, SWAP = 4, UNKNOWN = 0, } export interface SwapInstructions extends CompiledInstruction { inner: CompiledInstruction[]; accounts: number[]; decoded: Uint8Array; } /** * @function detectSwapOrSwapCreation * @description Detects if a transaction is a swap or a swap creation. * @param tx * @param solcialSwapId */ export function detectSwapOrSwapCreation( tx: TransactionResponse, solcialSwapId: PublicKey = SOLCIAL_SWAP, ): { operationType: programOperationTypes; instructions?: SwapInstructions; } { const accounts = tx.transaction.message.accountKeys; const swapIdx = accounts.findIndex((a) => a.toBase58() == solcialSwapId.toBase58()); if (swapIdx == -1) { return { operationType: programOperationTypes.UNKNOWN }; } if (tx.meta.innerInstructions) { for (const inner of tx.meta.innerInstructions) { tx.transaction.message.instructions[inner.index]['inner'] = inner.instructions; } } const instructions = tx.transaction.message.instructions .filter((i) => i.programIdIndex == swapIdx) .map((i) => ({ ...i, decoded: b58.decode(i.data) })); const creations = instructions.filter((i) => i.decoded[0] == 0) as SwapInstructions[]; if (creations.length !== 0) { return { operationType: programOperationTypes.SWAP_CREATION, instructions: creations[0], }; } const trades = instructions .filter((i) => i.decoded[0] == 0x01) .filter((i) => i['inner'] != null && i['inner'].length == 2) as SwapInstructions[]; if (trades.length !== 0) { return { operationType: programOperationTypes.SWAP, instructions: trades[0], }; } return { operationType: programOperationTypes.UNKNOWN, }; } export async function parseSwap( transaction: TransactionResponse, solcialSwapId: PublicKey = SOLCIAL_SWAP, solcialMint = SOLCIAL_MINT, ) { const accounts = transaction.transaction.message.accountKeys; const swapIdx = accounts.findIndex((a) => a.toBase58() == solcialSwapId.toBase58()); if (swapIdx == -1) { console.error('Not a swap instruction'); return []; } if (transaction.meta.innerInstructions) { for (const inner of transaction.meta.innerInstructions) { transaction.transaction.message.instructions[inner.index]['inner'] = inner.instructions; } } const instructions = transaction.transaction.message.instructions .filter((i) => i.programIdIndex == swapIdx) .map((i) => ({ ...i, decoded: b58.decode(i.data) })); const creations = instructions.filter((i) => i.decoded[0] == 0); const trades = instructions .filter((i) => i.decoded[0] == 0x01) // 0x01 : swap .filter((i) => i['inner'] != null && i['inner'].length == 2) // swap should have inner transacition .map(async (i) => { const tradeAccounts = { authority_info: accounts[i.accounts[0]], user_transfer_authority_info: accounts[i.accounts[1]], source_info: accounts[i.accounts[2]], swap_source_info: accounts[i.accounts[3]], swap_destination_info: accounts[i.accounts[4]], destination_info: accounts[i.accounts[5]], pool_mint_info: accounts[i.accounts[6]], pool_fee_account_info: accounts[i.accounts[7]], source_token_program_info: accounts[i.accounts[8]], destination_token_program_info: accounts[i.accounts[9]], pool_token_program_info: accounts[i.accounts[10]], }; // inner should be token transfer const tokenTransferIn = i['inner'][0]; const accountsIn = { source: accounts[tokenTransferIn.accounts[0]], mint: accounts[tokenTransferIn.accounts[1]], dest: accounts[tokenTransferIn.accounts[2]], owner: accounts[tokenTransferIn.accounts[3]], }; const dvTokenIn = new DataView(new Uint8Array(b58.decode(tokenTransferIn.data)).buffer); const amountIn = dvTokenIn.getBigUint64(1, true); const tokenTransferOut = i['inner'][1]; const accountsOut = { source: accounts[tokenTransferOut.accounts[0]], mint: accounts[tokenTransferOut.accounts[1]], dest: accounts[tokenTransferOut.accounts[2]], owner: accounts[tokenTransferIn.accounts[3]], }; const dvTokenOut = new DataView(new Uint8Array(b58.decode(tokenTransferOut.data)).buffer); const amountOut = dvTokenOut.getBigUint64(1, true); const sell = accountsOut.mint.toBase58() === solcialMint.toBase58(); const buy = accountsIn.mint.toBase58() === solcialMint.toBase58(); let price = BigInt(0); if (buy) { const slclAmount = amountIn; const usrAmount = amountOut; price = (((usrAmount * BigInt(1e9)) / slclAmount) * BigInt(1e9)) / BigInt(1e9); } if (sell) { const slclAmount = amountOut; const usrAmount = amountIn; price = (((usrAmount * BigInt(1e9)) / slclAmount) * BigInt(1e9)) / BigInt(1e9); } return { swap: tradeAccounts.authority_info, amountIn, amountOut, accountsIn, accountsOut, tradeAccounts, sell, buy, price, }; }); return Promise.all(trades); }