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: ...
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 ...
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,
...
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:...
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: ...
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: ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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
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.
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 ...
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 ...
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";
...
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.
...
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,
// ...
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/...
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 ...
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
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 ...
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(...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
anchor-client × 102anchor × 76
solana-program × 33
anchor-lang × 20
web3.js × 17
rust × 14
anchor-test × 13
pda × 6
typescript × 5
testing × 4
anchor-program-idl × 4
solana-cli × 4
anchor-spl × 4
spl-token × 3
metaplex × 3
account × 3
solana-wallet-adapter × 3
idl × 3
borsh × 3
solana-sdk × 3
solana-client × 3
transactions × 2
phantom × 2
javascript × 2
program-test × 2