DEV Community

chatgptnexus
chatgptnexus

Posted on

Solana Transactions Parser: Node.js Serverless Function on Vercel

In the digital expanse of blockchain, where every byte counts, we aim to parse Solana's transactions, seeking those that exceed 100 SOL. Here's how to do it with Node.js, deploying as a serverless function on Vercel.

Setting Up

First, ensure you have Node.js and npm on your machine. Initialize a project:

npm init -y
npm install @solana/web3.js
Enter fullscreen mode Exit fullscreen mode

Code for Solana Transactions

Create a directory named api and within it, a file solana-transactions.js:

// api/solana-transactions.js
import { Connection, PublicKey } from '@solana/web3.js';

export default async function handler(req, res) {
  const connection = new Connection('https://api.mainnet-beta.solana.com', 'confirmed');

  try {
    const latestBlock = await connection.getLatestBlockhashAndContext();
    const slot = latestBlock.context.slot;

    const blockData = await connection.getBlock(slot, { transactionDetails: "full" });

    const largeTransactions = blockData.transactions.filter(tx => {
      return tx.transaction.message.instructions.some(instruction => {
        if (instruction.parsed) {
          const { lamports } = instruction.parsed.info;
          return lamports && lamports >= 100 * 1e9; // Over 100 SOL
        }
        return false;
      });
    });

    const result = largeTransactions.map(tx => ({
      signature: tx.transaction.signatures[0],
      amount: tx.transaction.message.instructions[0].parsed.info.lamports / 1e9, // SOL conversion
      source: tx.transaction.message.instructions[0].parsed.info.source,
      destination: tx.transaction.message.instructions[0].parsed.info.destination
    }));

    res.status(200).json(result);
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'Failed to fetch or process transactions' });
  }
}
Enter fullscreen mode Exit fullscreen mode

Vercel Deployment

Configure Vercel with vercel.json in your project root:

{
  "version": 2,
  "routes": [
    {
      "src": "/api/(.*)",
      "dest": "api/$1"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Add a build script in package.json if needed:

{
  "scripts": {
    "build": "echo 'No build step needed'"
  }
}
Enter fullscreen mode Exit fullscreen mode

Deploy using Vercel CLI:

vercel login
vercel
Enter fullscreen mode Exit fullscreen mode

The Result

This function, once deployed on Vercel, will parse Solana's latest block, capturing transactions over 100 SOL. It's a simple yet effective way to monitor high-value transactions without the burden of maintaining a server.

Considerations

  • Performance: Cache results to reduce API calls.
  • Security: Handle errors gracefully and limit request rates.
  • Scalability: Be aware of Vercel's serverless function limitations.

In the vast sea of blockchain data, this tool acts as your sextant, guiding you to significant transactions with the simplicity and efficiency of serverless technology on Vercel.



Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay