4

As the title suggests the goal is to listen to all Raydium swaps that happen live, preferably via websockets and be able to decode meaningful info. I have a code snippet that to my understanding does half of the equation but I have some questions regarding the decoding part.

    console.log("Monitoring logs for program:", programAddress.toString());
    connection.onLogs(
        programAddress,
        ({ logs, err, signature }) => {
            if (err) return;

            if (logs && logs.some(log => log.includes("swap"))) {
                console.log("Signature for 'swap':", signature);
                fetchAndDecodeSwapInstruction(signature, connection);
            }
        },
        "confirmed"
    );

Inside the fetchAndDecodeSwapInstruction I can parse the logs and deduce some info but I want a more general way to handle all types of Raydium swaps and decode amounts in and out, token addresses and pair address. I have a snippet where I check if the instruction's programId is the RAYDIUM_PUBLIC_KEY like so

    for (const instruction of instructions) {
                if (instruction.programId.toBase58() === RAYDIUM_PUBLIC_KEY) {
                   // do something
                }
    }

But it never triggers which is confusing to me. I am familiar with how ETH works but not much with Solana so I would appreciate it if anyone can point me to the right direction or even better some example code in Javascript.

2

2 Answers 2

0

you have a small problem that I see in the code and that is that although it is true that some purchase transactions have swap, not all comply with that, transfer is more general and will help you with what you want, I would recommend adding that they are searched with swap or transfer, but also excluding certain instructions such as MintTo, InitializeAccount3, etc. that you know will not appear in a purchase instruction.

On the other hand to find more information what I would do is use getTransaction, pass the obtained signature, and there if decode all the data, also remember that you must filter the transactions well because if you only have with swap you will see too many transactions and the RPC is overloaded, which will make you not find anything and can cause problems that.

0

Using onLogs on the Raydium program is a ton of data, about 30-40gb last I checked. Might be worth listening to only the Raydium fee receiver using onLogs, then decoding the "ray_log" program event string that gets emitted. This will miss some pools that are initialized via CPI I think but to give you an idea, Try something like this (find the other raydium log data layouts by searching the Raydium org for "ray_log" and then converting the structs from rust to javascript in the first result):

import { PublicKey, Connection} from "@solana/web3.js"
import { u64 } from "@solana/buffer-layout-utils"
import { struct, u8 } from '@solana/buffer-layout';

const connection = new Connection('https://api.mainnet-beta.solana.com')
const swapBaseInLog = struct([u8('type'), u64('inn'), u64('out'), u64('direction'), u64('source'), u64('base'), u64('quote'), u64('delta')])

const ray = new PublicKey('7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5')
connection.onLogs(ray, async (logs) => {
for (const log of logs.logs) {
if (log.includes("ray_log")) {
const rayLog = log.split(" ").pop().replace("'", "")
const len = Buffer.from(rayLog, "base64").length
if (len === 57) { console.log(swapBaseInLog.decode(Buffer.from(rayLog, "base64"))) } } } } )

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.