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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

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

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay