Rust analyzer not working correctly and solana-test confusion
Rust analyzer not working correctly and solana-test confusion
Loading saved threads...
me_bruv · External communityPost link
External question — Solana Stack Exchange
Author: me_bruv
Original post: https://solana.stackexchange.com/questions/23127
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am new to solana and i am trying to write tests for program but rust-analyzer is not working for intitalize.rs file , i have tried various solution but none of them worked.
NOTE: This is for vault-program

here’s my toml file :
[package]
name = "program"
version = "0.1.0"
edition = "2021"
[features]
no-entrypoint = []
[dependencies]
solana-program = "2.3.0"
spl-token = { version = "8.0.0", features = ["no-entrypoint"] }
thiserror = "2.0.14"
[lib]
crate-type = ["cdylib", "lib"]
[dev-dependencies]
solana-program-test = "2.3.7"
solana-sdk = "2.3.1"
solana-system-interface = { version = "2.0.0", features = ["bincode"] }
tokio = { version = "1.47.1", features = ["full"] }
[[test]]
name = "integ_tests"
path = "tests/initialize.rs"
test = true
here’s the test file :
use solana_program_test::*;
use solana_sdk::{instruction::{AccountMeta, Instruction}, msg, program_option::COption, program_pack::Pack, pubkey::Pubkey, rent, signature::Keypair, transaction::Transaction};
use tokio;
use program::{entrypoint::process_instruction, error::VaultError, state::Vault};
#[tokio::test]
async fn test_intialize_vault(){
// - Start a Local Validator
// - first we would need to specify the program and its entry point
let program_id = Pubkey::new_unique();
let mut program_test = ProgramTest::new(
"vault_program",
program_id,
processor!(process_instruction)
);
let (mut banks_client,payer,recent_blockhash) = program_test.start().await;
let rent = banks_client.get_rent().await.unwrap();
// 0 : we would need intializer , the signer , the vault creator which would start the vault
// bank will acts as initializer in test environment
// 1 : we need underlying_token_mint , which we need to use as shares eg:USDC
let mint = Keypair::new();
// this ix just creates the mint account on ix , at this point it is nothing but just garbage account
msg!("mint create instruction");
let mint_create_ix = solana_system_interface::instruction::create_account(
&&payer.pubkey(),
&&mint.pubkey(),
rent.minimum_balance(spl_token::state::Mint::LEN),
spl_token::state::Mint::LEN as u64,
&spl_token::id()
);
// this ix sets the configuration of mint , which will make solana recognize it
msg!("initialize mint ix");
let mint_init_ix = spl_token::instruction::initialize_mint(
&spl_token::id(),
&&mint.pubkey(),
&&payer.pubkey(),
None,
6)?;
// 2 : token account which will be hold the deposited tokens
msg!("Deposit Token PDA");
let (deposit_token,deposit_token_bump) = Pubkey::find_program_address(&[b"deposit_token"], &program_id);
// 3 : Shares token mint which will be used by vault program as shares for giving as ownership
msg!("Share mint PDA");
let (share_mint,share_mint_bump) = Pubkey::find_program_address(&[b"share_mint"], &program_id);
// creating a vault account pda
msg!("Vault PDA");
let (vault_pda,vault_bump) = Pubkey::find_program_address(&[b"vault_pda"], &program_id);
msg!("Creating Transaction of creating mint and intializing acc");
let tx = Transaction::new_signed_with_payer(&[
mint_create_ix,
mint_init_ix,
],
Some(payer.pubkey()),
&[&payer,&mint], // Signatures
recent_blockhash);
msg!("Processing create mint Tx ....");
banks_client.process_transaction(tx).await.unwrap();
// This was the setup
// 5. Ab hume Init ix bhejni h to the network
msg!("Creating Init Ix");
let init_ix = Instruction {
program_id,
accounts:vec![
AccountMeta::new(&&payer.pubkey(), true),
AccountMeta::new(&&mint.pubkey(),false),
AccountMeta::new(deposit_token,false),
AccountMeta::new(share_mint,false),
AccountMeta::new(vault_pda,false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(rent::sysvar::id(), false),
],
data:vec![0u8] // Data ka pehla byte should specify what kind of instruction is it
};
// fetch fresh blockhash and send Init tx
msg!("Finding Recent BlockHash");
let recent_blockhash2 = banks_client.get_latest_blockhash().await.unwrap();
msg!("Creating init Transaction");
let init_tx = Transaction::new_signed_with_payer(&[
init_ix
],
Some(&&payer.pubkey()),
&[&payer], // Signatures
recent_blockhash2
);
msg!("Processing Init Tx ....");
banks_client.process_transaction(init_tx).await.unwrap();
msg!("Getting Intialized Vault Acc PDA");
let vault_acc = banks_client.get_account(vault_pda).await.unwrap().unwrap();
let vault_data = Vault::unpack(&vault_acc.data).unwrap();
if !vault_data.is_initialized {
msg!("Vault is not Intialized ?");
return Err(VaultError::VaultUnIntialized.into());
}
if vault_data.deposit_token_acc != deposit_token {
msg!("Deposit Token Account is invalid");
return Err(VaultError::InvalidDepositTokenAccount.into());
}
if vault_data.mint_acc != &&mint.pubkey() {
msg!("Mint Account is not invalid");
return Err(VaultError::InvalidMintAccount.into());
}
if vault_data.share_mint_acc != share_mint {
msg!("Share Mint Account is invalid");
return Err(VaultError::InvalidShareMintAccount.into());
}
msg!("Getting Deposit Token Account");
let deposit_token_acc = banks_client.get_account(deposit_token).await.unwrap().unwrap();
msg!("Unpacking Deposit Token Account Data");
let deposit_token_acc_data = spl_token::state::Account::unpack(&deposit_token_acc.data)?;
if *deposit_token_acc.owner != spl_token::id() {
msg!("deposit token account indeed is a not token");
return Err(VaultError::NotAToken.into());
}
if deposit_token_acc_data.owner != vault_pda {
msg!("Owner of deposit token account is not correct");
return Err(VaultError::InvalidTokenAuthority.into());
}
let share_mint_acc = banks_client.get_account(share_mint).await.unwrap().unwrap();
let share_mint_acc_data = spl_token::state::Mint::unpack(&share_mint_acc.data)?;
if !share_mint_acc_data.is_initialized {
msg!("Share Mint is Not Intialised");
return Err(VaultError::MintUnitialised.into());
}
if share_mint_acc_data.mint_authority != COption::Some(vault_pda) {
msg!("Owner of Share Mint is invalid");
return Err(VaultError::InvalidMintAuthority.into());
}
msg!("Vault initialized successfully!");
}
PS : Issue is resolved, i ran vscode in wsl and RA works correctly in that environment (in windows RA had some issue with perl )
Quote
Report
Aditya · External communityPost link
External answer — Solana Stack Exchange
Author: Aditya
Original post: https://solana.stackexchange.com/a/23131
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
in your anchor.toml instead of that
[[test]]
......
Also can you provide more info of the error.
Quote
Report
me_bruv · External communityPost link
External answer — Solana Stack Exchange
Author: me_bruv
Original post: https://solana.stackexchange.com/a/23136
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Solution
: I ran vscode in wsl and RA works correctly in that environment (in windows RA had some issue with perl )
Quote
Report
Post Reply
Checking account access…