Skip to main content
15 votes
Accepted

Uncaught ReferenceError: Buffer is not defined. Using Phantom Wallet, Solana and React to make a transaction

This is happening because the default bundler that comes with create react app(webpack 5) does not polyfill Buffer. The easiest way to fix this is including this code in your app: import * as buffer ...
acheron's user avatar
  • 2,679
6 votes
Accepted

How to run anchor tests partially?

You likely have something like this in your Anchor.toml file: [scripts] test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts" Change the wildcard (*) character to specify ...
Ademola's user avatar
  • 3,853
5 votes
Accepted

Anchor: How can I check specific Anchor Error Codes are caught in my test script if they are expected?

This is the general way of testing against errors in Anchor TS it('Expects Error', async () => { try { const tx = await program.methods.testErr().rpc(); assert.ok(false); ...
Muhammed Adil's user avatar
5 votes
Accepted

What is the more up to date way of waiting for a transaction?

In general, You can check out the documentation for the @solana/web3.js library in here Looking at the definition of the confirmTransaction function, you need to provide a recent blockhash, so it ...
nf0x's user avatar
  • 336
5 votes
Accepted

How to test a function that is expected to return error?

There are a few ways to assert errors. Here is one from Anchor's tic-tac-toe example: try { await program.methods.signUp("sajjad", "09146979106555555555", "")....
sohrab's user avatar
  • 1,781
5 votes
Accepted

How to include other programs and test CPIs on localnet using Anchor?

I assume you mean to test in your local test validator, using anchor test. You cannot simply copy-paste the spl_token program because it is not an Anchor program. There are a couple of ways to go ...
sohrab's user avatar
  • 1,781
5 votes

What are recommendations of things to do before publishing to mainnet?

Interesting question! Basically, the recommendations are the same as for releasing any high-quality software: test coverage, hand/auto tests on devnet and extensive code reviews. But also there are ...
Sergei Patrikeev's user avatar
4 votes

How to airdrop arbitrary token on devnet

You can use SPL token faucet to airdrop "dummy" SPL tokens on devnet
C.OG's user avatar
  • 2,837
4 votes
Accepted

Localnet on computer not confirming transactions

Check your local solana cli settings with: solana config get You should see something like: Config File: /Users/user/.config/solana/cli/config.yml RPC URL: http://localhost:8899 WebSocket URL: ws://...
Jacob Creech's user avatar
  • 3,874
4 votes
Accepted

Can't process_transaction twice within a test

This is quite an amusing issue that I ran into a few weeks ago while writing some new BPF tests for the Metaplex Candy Machine Program. The reason the second transaction is failing is almost certainly ...
Sammy's user avatar
  • 168
4 votes
Accepted

How to convert number to buffer in Anchor TS test

The seeds shoud be: const [gamePDA] = await PublicKey.findProgramAddress( [ Buffer.from("game"), user.publicKey.toBuffer(), new anchor.BN(fPlayer.gameCount).toArrayLike(Buffer, ...
James's user avatar
  • 309
3 votes
Accepted

Your configured rpc port: 8899 is already in use

Anchor Test start's a local validator deploys the program and then run's the test functions. If you want to just run the test's use the command anchor run test.If you wish to use anchor test then you ...
Pratik.js's user avatar
  • 1,632
3 votes
Accepted

what is the relation between Signers & Wallets in testing

What is the difference between provider.wallet.Pubkey , provider.Pubkey and anchor.web3.Keypair.generate().publicKey ... A crypto wallet is a device or application that stores a collection of keys ...
vicyyn's user avatar
  • 4,095
3 votes

Error: Your configured rpc port: 8899 is already in use

If you're running anchor test and you have a local validator already running, you can use the following command to skip the validator startup: anchor test --skip-local-validator Otherwise you can kill ...
Jacob Creech's user avatar
  • 3,874
3 votes

Uncaught ReferenceError: Buffer is not defined. Using Phantom Wallet, Solana and React to make a transaction

Thanks for trying to help me. I managed to get my code working finally. I've to do a couple of things I want to share: FIRST: Make sure you've all these dev dependencies installed "...
Mario Enrique Yahuarcani Salin's user avatar
3 votes
Accepted

What is the commonly accepted way to test using multiple users (signers) when testing anchor programs?

The usual way of using testing using a different user is, first you would create the keypair of the user. const newUser = anchor.web3.Keypair.generate(); Then you would have to fund this user account ...
Concise Labs's user avatar
  • 1,376
3 votes
Accepted

How to test token transfer?

There are a couple ways to load account data from mainnet (or devnet etc) into your localnet every time you run anchor test. 1. Cloning Under the hood, anchor test uses solana-test-validator. This has ...
HelmetFace's user avatar
  • 1,757
3 votes
Accepted

Does testing through ProgramTestContext have any disadvantage over `solana-test-validator`?

solana-program-test uses the same runtime as solana-test-validator, with a few differences: solana-program-test gives you a BanksClient rather than an RpcClient, which has a bit less functionality ...
Jon C's user avatar
  • 16.1k
3 votes

TryingToInitPayerAsProgramAccount. Error Number: 4101. You cannot/should not initialize the payer account as a program account

The different accounts defined in the Initialize<'info> struct of your Rust code are expected to be passed in by the client. Your on-chain code expects three accounts: the counter, the user, and ...
Ademola's user avatar
  • 3,853
3 votes

Instruction index: 0 Reason: A seeds constraint was violated. Getting this while testing in playground for the UpdateFund ctx

This is happening because in the create_fund instruction, you increase the fund_count by 1 with this line: // Incrementing total fund accounts user_profile.fund_count = user_profile.fund_count....
acheron's user avatar
  • 2,679
3 votes

Custom Program Error 0x0 When Running Test

In join_game, why are you initializing the PDA again? Presumably this already exists after game is created? Pass it in as an account without the initialize? You should already know the PDA address at ...
david lampach's user avatar
3 votes

Secret Key format issue in .env file

Different client side libraries expect different formats for secret keys. The three most common formats are: Raw bytes (usually in the form of a Uint8Array in Typescript/just an array of numbers in ...
Ahri's user avatar
  • 4,597
2 votes

What is the commonly accepted way to test using multiple users (signers) when testing anchor programs?

So it looks like @anon.sol was correct. I think the original difficulty was in dealing with passing in PDA accounts to transactions before they were created. Regardless, using anchor.web3.Keypair....
TovarishFin's user avatar
2 votes

"Unable to get latest blockhash. " when running "anchor test"

Alternatively, Run anchor clean To get rid of the build artefacts without deleting your deploy folder inside target
Jimii's user avatar
  • 3,695
2 votes
Accepted

"Unable to get latest blockhash. " when running "anchor test"

Solved this by deleting target folder and re-running anchor test. Error occurred due to renaming test file. Edit: An older earlier compiled file was running instead of the new one.
Arihant Bansal's user avatar
2 votes

Is there a way to get large amounts of SOL on Devnet/Testnet

Use a local validator for testing if you need these large sums. Also you're probably going to run serious activity which will be slow on either network and local validator will be faster.
Ohad Dahan's user avatar
2 votes
Accepted

PDA Anchor Test: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account

In the JS test code, replace this const [escrowPDA] = await anchor.web3.PublicKey.findProgramAddress( [utf8.encode('escrow')], program.programId ); with this const [escrowPDA] ...
AllBlooming's user avatar
  • 2,269
2 votes

How to test a function that is expected to return error?

try { await program.methods .signUp("sajjad", "09146979106555555555", "") .accounts({ user: secondUser.publicKey, authority: provider.wallet....
Ademola's user avatar
  • 3,853
2 votes

Anchor Testing Workflow?

I mainly use cargo test for unit tests, and anchor run <script in Anchor.toml> for integration tests. Scripts within Anchor.toml can be defined as follows: [scripts] my-tests = "ts-mocha -p ...
Federico García's user avatar
2 votes
Accepted

How to generate new wallets in anchor typescript tests

Another user in your case is any Keypair You can generate another Keypair using: const user1 = anchor.web3.Keypair.generate(); const user2 = anchor.web3.Keypair.generate(); Then you can call ...
C.OG's user avatar
  • 2,837

Only top scored, non community-wiki answers of a minimum length are eligible