Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 7994

Trying to get the amounts of SOL swapped into a tx of a Token Account

$
0
0

For 3 days I've been trying to create a function that fetches all the swap txs made on a Token Account.

I'm able to get back all the txs made but not to fetch inside it to get back the swap details.

When I console.logs the tx details are quite hard to understand and create something that will work with all the structure differences.

If you guys can tell me if it's possible or not and what I'm doing wrong if you know please.

I'm also searching for a solanaweb3js community but I didn't find any.

Thanks!

async function getTokenTransfers(walletAddress, tokenAddress, limit) {    console.log("Fetching token transfers...");    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=cf515ae3-98b8-498a-8dfe-6e4dc7d83d39', 'confirmed');    try {        const tokenAccount = await getAssociatedTokenAddress(tokenAddress, walletAddress);        const signatures = await connection.getConfirmedSignaturesForAddress2(tokenAccount, { limit });        console.log(`${signatures.length} signatures found`);        const transactions = await connection.getParsedTransactions(            signatures.map(sig => sig.signature),            { maxSupportedTransactionVersion: 0 }        );        console.log(`${transactions.length} transactions fetched`);        const transfers = transactions            .filter(tx => tx !== null)            .flatMap(tx =>                 tx.transaction.message.instructions.filter(                    instruction => 'parsed' in instruction &&                        instruction.program === 'spl-token'&&                        ['transfer', 'transferChecked'].includes(instruction.parsed.type)                ).map(instruction => {                    if ('parsed' in instruction && instruction.parsed.info) {                        const { info } = instruction.parsed;                        return {                            signature: tx.transaction.signatures[0],                            blockTime: tx.blockTime,                            sender: new PublicKey(info.authority),                            amount: info.amount || info.tokenAmount.uiAmount,                            source: new PublicKey(info.source),                            destination: new PublicKey(info.destination)                        };                    }                    return null;                }).filter(transfer => transfer !== null)            );        console.log(`${transfers.length} transfers processed`);        return transfers;    } catch (error) {        console.error('Error fetching token transfers:', error);        throw error; // Rethrow to allow caller to handle or log the error as needed.    }}

Viewing all articles
Browse latest Browse all 7994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>