1

I need to detect buy transactions on Solana, how can I do this? Does anyone have an example of how it could work? I created something, but without success so far, my biggest difficulty is identifying a buy transaction.

async function watchForBuys(params: any) {
    try {
        const connection = new Connection('https://solana-mainnet.core.chainstack.com/myapikey', 'confirmed');

        const socket = new WebSocket(params.websocketUrl);
        socket.on('open', () => {
            console.log('WebSocket open');
            socket.send(JSON.stringify({
                jsonrpc: '2.0',
                id: 1,
                method: 'logsSubscribe',
                params: [{ "mentions": [params.address] }, { "commitment": 'confirmed' }]
            }));
        });

        socket.on('message', async (data) => {
            const jsonData = JSON.parse(data.toString());
            if (jsonData.method === 'logsNotification') {
                const { result } = jsonData.params;
                if (result && result.value) {
                    //console.log("result", result)

                    const transaction: any = await connection.getTransaction(result.value.signature, {
                        //commitment: 'confirmed'
                        "maxSupportedTransactionVersion": 0
                    });

                    //console.log("transaction", transaction)

                    if (transaction) {
                        const isBuyTransaction = isBuyTransactionForToken(transaction.meta.logMessages, params.address);

                        console.log("isBuyTransaction", isBuyTransaction)
                        /* if (isBuyTransaction) {
                            const buyer = getTransactionParticipant(transaction, 'buyer');
                            const seller = getTransactionParticipant(transaction, 'seller');
                            const value = getTransactionValue(transaction);

                            console.log('Buyer:', buyer);
                            console.log('Seller:', seller);
                            console.log('Amount:', value);
                        } */
                    } else {
                        console.error('Transaction not valid.');
                    }
                }
            }
        });

        socket.on('close', (event) => {
            console.log('WebSocket close:', event);
        });
    } catch (error: any) {
        console.error('Error:', error.message);
    }
}

function isBuyTransactionForToken(logs: any, tokenAddress: any) {
    return logs.some((log: string | string[]) => log.includes('Instruction: Transfer') && log.includes(tokenAddress));
}

function getTransactionParticipant(transaction: any, role: any) {

}

function getTransactionValue(transaction: any) {
    
}

1 Answer 1

3

To preface this response I would just like to mention that I work for Helius Labs.

Helius offers webhooks that track various events across the Solana ecosystem. One of these are transactions that involve token swaps (buy/sell). The webhook can be set up to monitor a specific wallet, token or program. This webhook will then send updates whenever that type of transaction occurs. You can view all the transaction types that we have parsed and offer webhooks for in our docs: https://docs.helius.dev/resources/transaction-types

You can set up a webhook for free through our developer portal: https://dev.helius.xyz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.