0

I currently have a demand for a project that needs to listen to SWAP transactions between tokens, preferably via websocket.

Currently I only found the programSubscribe method on the nodes, but it gives me transaction information in a coded form, like the one below:

{
  jsonrpc: "2.0",
  method: "programNotification",
  params: {
    result: {
      context: { slot: 240418289 },
      value: {
        pubkey: "8ZxRqWLQ7rdp9Y354krM3PHx1MbdC7JKN7u3ew77zX8R ",
        account: {
          lamports: 6124800,
          data: [
            "BgAAAAAAAAD+AAAAAAAAAAcAAAAAAAAAwAAAAAAAAAIAAAAAAAAAAkAAAAAAAAAAQAAAAAAAAAAAAAAAAAAKCGAQAAAAAA9AEAAAAAAABAS0wAAAAAABAnAAAAAAAAoIYBAAAAAAABAAAAAAAAAADKmjsAAAAAAMqaOwAAAAAFAAAAAAAAABAnAAAAAAAGQAAAAAAAAAQJ wAAAAAAAAAwAAAAAAAAAZAAAAAAAZAAAAAAAAABAnAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABk45dlAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFiHtgyBPocCAAAAAAAAAABndpeydAAAAAAAAAAAA2UPJTwAAAADftnmqfAAAAAAAAAAAAAAAG5Ct/zQFMQMAAAAAAAAAAONCjXs8ngEAfb6UWLDXGRn9fLOO yaE1RL3P1f88SOi/0uzC3iWV5tUZ3gMmtETy3rLZtotJl8BUuU0LBVbU5dxLKdBjnOnB2JQFPty3wQ1hV0tpnjbr2VNroHEaXbCwC+f+Qx3KL0FcBpuIV/6rgYT7aH9jRhjANdrEOdwa6ztV mKDwAAAAAAGSCBfgAivwg9yl06mIFns7yaitmbCIl+ByYjLvR4JlqdOqPI4Bcf7VzvUpDEXtH2k+pH/kA8hWf0xZM2kgCw9yuARDwGJH/VfGJAOiPXWt0albGhOM+uKqaSuchF7q0jINB1GoKC2mEwX+KZw 3uZjlhHHbETUDcxD4vhBFpgr27sQODi4QaL1jU/ZzCbeybkDLZ5K/3dB2lx1HU8INflY7AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOW2K2XLO72m9WiI5m/ujmTcVWAZnA +IsR/ic70Fnoqhx208k3gdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
            "base64",
          ],
          owner: "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8",
          executable: false,
          rentEpoch: 0,
          space: 752,
        },
      },
    },
    subscription: 194814,
  },
};

Does anyone have any idea what I can do? If there is any other way, or how could I decode these transactions above?

1
  • 1
    You'll need some sort of decoder for the accounts of this program. Check on GitHub to see if there's an official package provided by the project. This program, the Raydium AMM, does not have an Anchor IDL published on-chain, so you'll have to use a Raydium package to decode their accounts and structs. github.com/raydium-io/raydium-contract-instructions may be a good place to start
    – Jon C
    Commented Jan 11 at 11:46

2 Answers 2

0

I will go with logs subscribe with the raydium key that you have there. Find in logs init_pc_amount or initialize2 and then decode that info so you can have the poolkeys need it for swap.

1
  • Although the answer does answer the question. It can be improved by providing code/JSON example and providing snippet to decode the data which websocket returns. Commented Feb 16 at 18:23
0

You’re on the right track by using programSubscribe to stream swaps. Each time an account owned by the subscribed program triggers an event, you’ll receive Account Data. This Account Data is a long string (BgAAAAAAAAD...) encoded in base64. By decoding this base64 string, you’ll get all the information necessary to calculate price fluctuations.

You're essentially trying to convert this encoded string into human-readable text. For example, this is how market data appears for a random pool on the Raydium AMM program:

https://solscan.io/account/E29cf49XTYTtqwnMXDUEaqKBnuUpDsZYB5z41vutCcqB#data You’ll find addresses, quantities, and other relevant data there under marketdata.

Now that you have an idea of the data you need, here are the steps to decode it into a human-readable format:

  1. Define a Struct or Class: You need to create a struct or class that represents the market data fields seen on Solscan. Make sure the fields are in the exact order and use the correct data types for your programming language.
  2. Convert Base64 String to Byte Array: You need to convert the base64 string into a byte array.
  3. Map Byte Array to Struct or Class: This is the trickiest part. You need a method to map the byte array to your struct or class. The mapping process may differ between programs, so the exact details will depend on the specific program you're working with.

Here’s a concise example in C# that demonstrates the approach:

public struct AccountData { public ulong Status { get; set; } public ulong Nonce { get; set; } public ulong MaxOrder { get; set; } }

public AccountData Parse(byte[] data) { using var reader = new BinaryReader(new MemoryStream(data));

AccountData accountData = new AccountData();

accountData.Status = reader.ReadUInt64();
accountData.Nonce = reader.ReadUInt64();
accountData.MaxOrder = reader.ReadUInt64();
// Continue reading all the fields...

return accountData;

}

Depending on the program you’re decoding, you might need to skip some offsets while reading the data. For example:

if (reader.BaseStream.Position == 304) reader.BaseStream.Seek(8, SeekOrigin.Current);

This is how I’ve handled this for the Raydium program. The mapping may differ for other programs, so you’ll need to adjust it accordingly.

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.