coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.65k stars 1.34k forks source link

Unable to fetch Program Accounts on server side in Next.js 13 #3189

Closed adityanithariya closed 2 months ago

adityanithariya commented 2 months ago

For fetching program accounts, provider must be there, but in server side, there's no transaction being made, and only connection should be sufficient in that case, question arises is what should be the best way to fetch program accounts data in anchor on server side in Next.js

Tried using AnchorProvider.env(), and ANCHOR_WALLET points to ./config/id.json, but when deployed on vercel, it's unable to find it even when it's there. Apart from this, tried AnchorProvider.local(), but same issue with it.

For using new AnchorProvider(connection, wallet), we need wallet object which can be fetched from useAnchorWallet(), but unusable on server side.

I would like to propose that if we want to sign a transaction we need wallet, but when reading accounts, we only need connection, so wallet can be made optional or something like that. Otherwise, adding a constructor to Wallet class constructable from Keypair object would be better.

Let me know if you have better solution than this, or we have anything already implemented on it. I would love to raise a PR for implementing a solution for this! Thanks in Advance!!

acheroncrypto commented 2 months ago

I would like to propose that if we want to sign a transaction we need wallet, but when reading accounts, we only need connection, so wallet can be made optional or something like that. Otherwise, adding a constructor to Wallet class constructable from Keypair object would be better.

Wallet is not a class, it's just an interface, so you can just pass {} as Wallet and it should work as long as you never use wallet functionality.

adityanithariya commented 2 months ago

I would like to propose that if we want to sign a transaction we need wallet, but when reading accounts, we only need connection, so wallet can be made optional or something like that. Otherwise, adding a constructor to Wallet class constructable from Keypair object would be better.

Wallet is not a class, it's just an interface, so you can just pass {} as Wallet and it should work as long as you never use wallet functionality.

Thanks, it worked!