Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow direct accounts manipulation #142

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

Ikrk
Copy link
Collaborator

@Ikrk Ikrk commented Mar 11, 2024

This PR extends the FuzzClient trait and the functionality of AccountsStorage in order to have more flexibility to create and store an arbitrary account. This removes the previous limitation that only basic accounts operations were possible.

Now, one can create and store any kind of account. For example:

  • to add account that uses the #[account(zero)] anchor constraint (must be rent exempt, owned by our program, with empty data)
let state = fuzz_accounts
            .state
            .storage()
            .entry(self.accounts.state)
            .or_insert_with(|| {
                let space = State::SIZE;
                let rent_exempt_lamports = client.get_rent().unwrap().minimum_balance(space);
                let keypair = Keypair::new();
                let account = AccountSharedData::new_data_with_space::<[u8; 0]>(
                    rent_exempt_lamports,
                    &[],
                    space,
                    &my_program::id(),
                )
                .unwrap();
                client.set_account_custom(&keypair.pubkey(), &account);
                keypair
            });
  • to add a new system owned account with a specific PDAdress
let rent_exempt_for_token_acc = client
            .get_rent()
            .unwrap()
            .minimum_balance(anchor_spl::token::spl_token::state::Account::LEN);

let reserve_pda = fuzz_accounts
            .reserve_pda
            .storage()
            .entry(self.accounts.reserve_pda)
            .or_insert_with(|| {
                let seeds = &[b"some-seeds"];
                let pda = Pubkey::find_program_address(seeds, &my_program::id()).0;
                let account = AccountSharedData::new_data_with_space::<[u8; 0]>(
                    rent_exempt_for_token_acc,
                    &[],
                    0,
                    &SYSTEM_PROGRAM_ID,
                )
                .unwrap();
                client.set_account_custom(&pda, &account);
                let vec_of_seeds: Vec<Vec<u8>> = seeds.iter().map(|&seed| seed.to_vec()).collect();
                PdaStore {
                    pubkey: pda,
                    seeds: vec_of_seeds,
                }
            })
            .pubkey();

@Ikrk Ikrk force-pushed the feat/allow-direct-account-manipulation branch from b601de3 to e985275 Compare March 11, 2024 15:25
@Ikrk Ikrk marked this pull request as ready for review March 11, 2024 15:26
@Ikrk Ikrk requested a review from lukacan March 11, 2024 15:26
@Ikrk Ikrk force-pushed the feat/allow-direct-account-manipulation branch from e985275 to 34e7765 Compare March 12, 2024 10:31
@lukacan lukacan force-pushed the feat/allow-direct-account-manipulation branch from 34e7765 to 3c54693 Compare March 12, 2024 12:52
@lukacan lukacan merged commit 0fa4618 into develop Mar 12, 2024
8 checks passed
@lukacan lukacan deleted the feat/allow-direct-account-manipulation branch March 12, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants