2

I am currently trying to figure out how to decode data from a transaction instruction, but whatever i try it doesn't work.

Example data base58:

2K7nL28PxCW8ejnyCeuMpbWSTLdurgKFpsogNMAfkxaivrGSbx5pnrFeWkZY5EUDZL26WSoMa5kTRAuZiW7s3HifSXN9sWnRp1XnD71rTSxG3GNXxguzpgsXeDQfJx7A5TCPx9t7r5B3KcWsvbxXAivnaY6i82epDwEiCiZ7T2oLg3QsrcZqFjun6QF1

What i should be getting as values in an object:

{
  "mint": {
    "type": "publicKey",
    "data": "6Y6uxUWa2AtijqprubRwx2amJnZkXiK2FM7kJ6E6pump"
  },
  "solAmount": {
    "type": "u64",
    "data": "43099058"
  },
  "tokenAmount": {
    "type": "u64",
    "data": "1219512195121"
  },
  "isBuy": {
    "type": "bool",
    "data": true
  },
  "user": {
    "type": "publicKey",
    "data": "Bzen1deTMBZXh7etJheznKZtDF9KWfgacGSwWV6DPPZ2"
  },
  "timestamp": {
    "type": "i64",
    "data": "1718285628"
  },
  "virtualSolReserves": {
    "type": "u64",
    "data": "33750388325"
  },
  "virtualTokenReserves": {
    "type": "u64",
    "data": "953766803940863"
  }
}

Does anyone have an idea how to decode it?

2 Answers 2

2

Im using this repo: https://github.com/rckprtr/pumpdotfun-sdk

import bs58 from "bs58";
import idl from "../../src/IDL/pump-fun.json";
import {
  BorshCoder,
} from "@coral-xyz/anchor";
import { base64 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";

const main = async () => {
  let base58Data =
    "2K7nL28PxCW8ejnyCeuMpbWSTLdurgKFpsogNMAfkxaivrGSbx5pnrFeWkZY5EUDZL26WSoMa5kTRAuZiW7s3HifSXN9sWnRp1XnD71rTSxG3GNXxguzpgsXeDQfJx7A5TCPx9t7r5B3KcWsvbxXAivnaY6i82epDwEiCiZ7T2oLg3QsrcZqFjun6QF1";

  let buffer = Buffer.from(bs58.decode(base58Data));
  //remove first 8 bytes for the event cpi
  buffer = buffer.slice(8);
  
  let coder = new BorshCoder(idl as any);
  let args = coder.events.decode(base64.encode(buffer));
  console.log(args);
};

main();

Results:

{
  data: {
    mint: PublicKey [PublicKey(6Y6uxUWa2AtijqprubRwx2amJnZkXiK2FM7kJ6E6pump)] {
      _bn: <BN: 52422028108045a3dfbc44fb5428a6cee7ea2f55bdd87164929a93663d3145ff>
    },
    solAmount: <BN: 291a3b2>,
    tokenAmount: <BN: 11bf0975831>,
    isBuy: true,
    user: PublicKey [PublicKey(Bzen1deTMBZXh7etJheznKZtDF9KWfgacGSwWV6DPPZ2)] {
      _bn: <BN: a359b05b86c4036aafbfaa3bda65369edf96fd8630852ee5d38e7fe79199b429>
    },
    timestamp: <BN: 666af53c>,
    virtualSolReserves: <BN: 7dbae0e65>,
    virtualTokenReserves: <BN: 36372238c25ff>,
    realSolReserves: <BN: df8a6265>,
    realTokenReserves: <BN: 264e0d7798dff>
  },
  name: 'TradeEvent'
}
1
  • 1
    life saver lol thank you Commented Jun 13 at 16:25
0

for python, you can buffer 16 bytes and start with mint = buffer[0:32]

import base58
def decode_transaction_data(base58_data):
    buffer = base58.b58decode(base58_data)

    buffer = buffer[8:]

    mint = buffer[8:40]  
    sol_amount = int.from_bytes(buffer[40:48], "little") 
    token_amount = int.from_bytes(buffer[48:56], "little") 
    is_buy = bool(buffer[56])  
    user = buffer[57:89]  
    timestamp = int.from_bytes(buffer[89:97], "little", signed=True)  
    virtual_sol_reserves = int.from_bytes(buffer[97:105], "little") 
    virtual_token_reserves = int.from_bytes(buffer[105:113], "little") 
    real_sol_reserves = int.from_bytes(buffer[113:121], "little") 
    real_token_reserves = int.from_bytes(buffer[121:129], "little") 

    mint_base58 = base58.b58encode(mint).decode()
    user_base58 = base58.b58encode(user).decode()

    return {
        "mint": mint_base58,
        "sol_amount": sol_amount,
        "token_amount": token_amount,
        "is_buy": is_buy,
        "user": user_base58,
        "timestamp": timestamp,
        "virtual_sol_reserves": virtual_sol_reserves,
        "virtual_token_reserves": virtual_token_reserves,
        "real_sol_reserves": real_sol_reserves,
        "real_token_reserves": real_token_reserves,
    }

base58_data = "2K7nL28PxCW8ejnyCeuMpbWSTLdurgKFpsogNMAfkxaivrGSbx5pnrFeWkZY5EUDZL26WSoMa5kTRAuZiW7s3HifSXN9sWnRp1XnD71rTSxG3GNXxguzpgsXeDQfJx7A5TCPx9t7r5B3KcWsvbxXAivnaY6i82epDwEiCiZ7T2oLg3QsrcZqFjun6QF1"
decoded_data = decode_transaction_data(base58_data)

for key, value in decoded_data.items():
    print(f"{key}: {value}")

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.