0

I'm currently trying to fetch-transactions in a serverless function using vercel and getting this error:

    Error in GET /fetch-transactions: TypeError: s.Wallet is not a constructor

Is there a special way to connect when doing so from a server?

Here is my code:

    export const runtime = 'nodejs';
    export const dynamic = 'force-dynamic';
    
    import { NextResponse } from 'next/server';
    import { getPrismaClient } from '../utils/prisma';
    import * as anchor from '@coral-xyz/anchor';
    
    export async function GET(request) {
      const prisma = getPrismaClient();
      const { searchParams } = new URL(request.url);
      const address = searchParams.get('address');
    
      if (!address) {
        return NextResponse.json({ error: 'Address is required' }, { status: 400 });
      }
    
      try {
        const rpcEndpoint = process.env.NEXT_PUBLIC_RPC_ENDPOINT;
        const serverKeypairBase64 = process.env.SERVER_KEYPAIR_BASE64;
    
        if (!rpcEndpoint) {
          throw new Error('RPC endpoint is not defined in NEXT_PUBLIC_RPC_ENDPOINT');
        }
    
        if (!serverKeypairBase64) {
          throw new Error('Server keypair is not defined in SERVER_KEYPAIR_BASE64');
        }
    
        const serverKeypairUint8Array = Uint8Array.from(
          JSON.parse(Buffer.from(serverKeypairBase64, 'base64').toString())
        );
    
        console.log('DEBUG CHECK 1');
        const serverKeypair = anchor.web3.Keypair.fromSecretKey(serverKeypairUint8Array);
        console.log('DEBUG CHECK 2');
        const wallet = new anchor.Wallet(serverKeypair);
        console.log('DEBUG CHECK 3');
        const connection = new anchor.web3.Connection(rpcEndpoint, 'confirmed');
        console.log('DEBUG CHECK 4');
        const provider = new anchor.AnchorProvider(connection, wallet, {});
        anchor.setProvider(provider);
        const programId = new anchor.web3.PublicKey('PROGRAM_ID');
        const idl = await anchor.Program.fetchIdl(programId, provider);
        const program = new anchor.Program(idl, programId, provider);

The server logs:

DEBUG CHECK 1

DEBUG CHECK 2 

and then the error

Thanks for your help!

1
  • what version of @coral-xyz/anchor are you using?
    – Jimii
    Commented Nov 1 at 14:07

1 Answer 1

0

Consider updating the wallet constructor (use web3 instead of anchor):

import { Keypair, Connection } from '@solana/web3.js';
import { AnchorProvider, setProvider, Program } from '@coral-xyz/anchor';

// Replace anchor.Wallet with Keypair-based wallet for server
const serverKeypair = Keypair.fromSecretKey(serverKeypairUint8Array);
const connection = new Connection(rpcEndpoint, 'confirmed');

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.