Skip to main content
12 votes
Accepted

Anchor Events -- What, Why, How?

What: log messages (anything you would do with msg!()). But they are not human readable as they are base64 encoded. Why: msg!() with string formatting is relatively expensive for compute units How: ...
Thomas Ruble's user avatar
9 votes

Can anchor client be used with @solana/web3.js 2.0rc?

With the release of @solana/web3.js version 2, we’ve introduced a tool for generating program clients using Codama IDLs (formerly Kinobi), fully compatible with this version. You can convert your ...
Loris Leiva's user avatar
8 votes

Initializing a vault: AccountDidNotDeserialize

Include an extra 8 bytes for the account discriminator that anchor adds when initializing a new account Try: #[account( init, payer = player, space = 8 + Vault::MAX_SIZE, ...
john's user avatar
  • 7,794
7 votes

Anchor Events -- What, Why, How?

They allow encoding of data into the transaction through the logs. They are more efficient that formatting a string (very expensive) and allow easy decoding through the anchor SDK They can be used for:...
Arowana's user avatar
  • 2,373
7 votes
Accepted

How to use Enum types as function argument in anchor test

You can pass it as { active: {} }. Enums with internal data pub enum AppState { Paused { reason: String } } You can pass it as { paused: { reason: "Not enough players" } } Reference: ...
acheron's user avatar
  • 2,909
6 votes
Accepted

Passing remaining_accounts using anchor client side SDK

Remaining accounts shouldn't be inside accounts, it's its own top level thing. Like so await program.rpc.sendMonies(new anchor.BN(0), new anchor.BN(100), { accounts: { baseAccount: ...
Dev Bharel'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
  • 356
5 votes
Accepted

How to verify data on chain with Solana

The solution is to use something called Program Derived Addresses. What Are They? Your program has its own unique address space to which it can allocate Program Derived Addresses. These are derived ...
HelmetFace's user avatar
  • 1,807
5 votes
Accepted

What is first 8 bytes of anchor generated instruction data?

Your assumption is correct. The first 8 bytes together form the method identifier which Anchor uses internally to determine what instruction to invoke. The method identifier is a unique id for each ...
Ademola's user avatar
  • 3,944
4 votes

What's the best way to use a u16 as a seed for a PDA?

Using a u16 as part of the seeds for a PDA works like this: Rust seeds = [.., x.to_le_bytes().as_ref()] Javascript x = 12; [.., new anchor.BN(x).toBuffer('le', 2)] We use the 2 argument because a ...
Ademola's user avatar
  • 3,944
4 votes
Accepted

how to deploy a program using clients like @solana/web3.js, @project-serum/anchor

A program on Solana is just an account whose data happens to be bytecode, and whose executable flag is set to true. To deploy programs, the Solana CLI sends instructions to the System Program and the ...
steveluscher's user avatar
  • 3,153
4 votes
Accepted

Stuck at Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0

0x0 means you're attempting to initialize an already initialized account. What's happening here is that because your PDAs have only constant seeds, the same address is generated for them on each run ...
Ademola's user avatar
  • 3,944
4 votes

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Program failed to complete

Can you do }).rpc({ skipPreflight:true }) This will not simulate the tx and you could get better errors
Pratik.js's user avatar
  • 1,682
4 votes

Is there a go package to create clients for anchor programs?

You can use anchor-go which takes the IDL created by anchor to generate clients.
Serban's user avatar
  • 1,486
4 votes

You cannot/should not initialize the payer account as a program account

It looks like you didn't provide the staking_pool account: #[account(init, payer = owner, space = StakingPool::LEN + 8)] pub staking_pool: Account<'info, StakingPool>, The account is ...
john's user avatar
  • 7,794
4 votes
Accepted

how can I get the return value of an instruction upon sent the Tx from JS client?

On Solana you would usually store data or state in an account and then read that from js after you changed it in a transaction. I would rethink the problem you are having and check if you really need ...
Jonas H.'s user avatar
  • 7,219
3 votes
Accepted

Using Fs to fetch idl error: "Module not found: Can't resolve 'fs'"

The fs module doesn't exist in the browser, it's a built-in node module for reading from the file system. Instead, try to import the file. import idl from "../idl/BigBazaar_IDL_Devnet.json"; ...
C.OG's user avatar
  • 2,907
3 votes

how to fetch list of all pda accounts in frontend? like sale account instance to display in the market? or should i be fetching from token account?

With PDAs, you can build a "hashmap-like" structure. but it's only hashmap-like. There is now way to iterate over all PDAs that were created. There are a few options on how to mitigate this. ...
AllBlooming's user avatar
  • 2,309
3 votes

Support of Anchor IDL for enums

For example if you have an account defined in Rust code #[account()] /// Stores meta information about the `Account` on chain pub struct User { // user data pub user_data : UserData, // ...
Galactus Godmode's user avatar
3 votes
Accepted

Communication between front end and smart contract

So typically how an Anchor application works is that you have the smart contract, a frontend for the user to interact with, and in-between that are calls to the smart contract using the @project-serum/...
Ademola's user avatar
  • 3,944
3 votes

What are the possible causes of `not enough signers`

What it means The program you called requires accounts to sign the transaction. The transaction you crafted and sent does not include all of them. Possible causes At the very least the fee payer needs ...
man0s's user avatar
  • 710
3 votes
Accepted

Testing Anchor program with solana-program-test crate

Try running the test with cargo test-bpf and specify the tests. So command is: cargo test-bpf --test initialize -- testing_iniitalize
hrs8's user avatar
  • 146
3 votes
Accepted

Query accounts with filters

There's an example usage of the memcmp filter in the anchor repo offset is 8 bytes (for the discriminator) + whatever other bytes come before the public key you're looking to filter based on. bytes ...
Henry E's user avatar
  • 2,949
3 votes

How the scope for memcmp works when the struct has multiple strings?

You need to pack them into fixed lengths byte arrays. This is the downside of memcmp: you can't have variable length fields. The following is an easy way to do so: pub fn pack_string_in_32_bytes(...
Albert Chen'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,364
3 votes
Accepted

Intitalize Solana Program through IDL

Unfortunately, anchor_client is not IDL-aware. It is intended for scenarios where you have access to the source code (or crate) of the program you are calling. You can see an example of this the ...
sohrab's user avatar
  • 1,851
3 votes

What's the best way to use a u16 as a seed for a PDA?

If you are trying to derive a PublicKey that involves a u16, it must still be provided as a Buffer to the PublicKey#findProgramAddress function. Based on the size of the number, in this case 2 bytes, ...
m_callens's user avatar
  • 909
3 votes
Accepted

Fetch all() isn't working in Anchor Client

Based on the comment above, it seems that the problem was due to the User account struct being modified in the program after some accounts were already created. For instance, suppose the User struct ...
john's user avatar
  • 7,794
3 votes

Getting insufficient balance error while trying 'anchor deploy' , but I have enough fund in my wallet. How to solve this?

You might not be aligned correctly with what your local solana config is setup as. Could you open your terminal and type in "solana config get" and see what it says? The problem here is ...
jajaMundo's user avatar
3 votes

Getting insufficient balance error while trying 'anchor deploy' , but I have enough fund in my wallet. How to solve this?

Make sure the [provider] section in your Anchor.toml file specifies cluster = "Localnet". If it doesn't, Anchor might be trying to deploy against a different cluster that isn't localnet ...
Ademola's user avatar
  • 3,944

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