Skip to content

Add an helper to create the instruction_sysvar account #100

Open
@L0STE

Description

@L0STE

Due to the nature of this sysvar, it's very hard to create an implementation that resemble the one currently used.

I don't have a "good" way of doing it at the moment, but this is the way that I currently do it:

//...
let (instruction_sysvar_pubkey, instruction_sysvar_account) = get_account_instructions_sysvar(&mut mollusk, &[]);
//..

fn get_account_instructions_sysvar(mollusk: &mut Mollusk, instructions: &[Instruction]) -> (Pubkey, AccountSharedData) {
    // Construct the instructions data from all instructions
    let mut data = construct_instructions_data(
        instructions.iter().map(|instruction| {
            BorrowedInstruction {
                program_id: &instruction.program_id,
                accounts: instruction.accounts
                    .iter()
                    .map(|meta| BorrowedAccountMeta {
                        pubkey: &meta.pubkey,
                        is_signer: meta.is_signer,
                        is_writable: meta.is_writable,
                    })
                    .collect(),
                data: &instruction.data,
            }
        }).collect::<Vec<_>>().as_slice()
    );

    // Find which instruction contains the sysvar account and at what position
    if let Some((ix_index, _)) = instructions.iter().enumerate()
        .find(|(_, instruction)| {
            instruction.accounts.iter()
                .any(|meta| meta.pubkey == INSTRUCTION_SYSVAR)
        }) 
    {
        store_current_index(&mut data, ix_index as u16);
    }

    // Set the account data
    let mut instruction_sysvar_account = AccountSharedData::new(
        mollusk
            .sysvars
            .rent
            .minimum_balance(data.len()),
        data.len(),
        &self::ID
    );
    instruction_sysvar_account.set_data_from_slice(&data);

    (INSTRUCTION_SYSVAR, instruction_sysvar_account)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions