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

Helper Functions To Add #3

Open
bonedaddy opened this issue Mar 8, 2022 · 2 comments
Open

Helper Functions To Add #3

bonedaddy opened this issue Mar 8, 2022 · 2 comments

Comments

@bonedaddy
Copy link
Member

  • parse anchor instruction data for the sighash

add hex = "0.4.3" to cargo.toml

    pub fn anchor_instruction_data_to_sighash(ix_data: &str) -> Result<[u8; 8]> {
        let ix_data_decoded = match hex::decode(ix_data)?;
        let mut sighash: [u8; 8] = [0_u8; 8];
        for (idx, val) in ix_data_decoded.iter().enumerate() {
            if idx >= 8 {
                break;
            }
            sighash[idx] = *val;
        }
        Ok(sighash)
    }
@bonedaddy
Copy link
Member Author

addressed via sighashdb

@bonedaddy
Copy link
Member Author

bonedaddy commented Oct 25, 2022

/// given `account`, attempts to perform zero-copy deserialization of the account farm_key
/// however due to the nature of this function, no validation of the account discriminator is performed
/// and as such it's technically possibly for this function to produce valid results on accounts
/// which aren't actually vault accounts
pub fn farm_accessor(account: &Account) -> Result<Farm> {
    if account.data.len() <  448 {
        return Err(anyhow!("account data length too smal"));
    }
    let farm_information = &account.data[8 + 424..8 + 424 + 16];
    let (part_one, part_two) = farm_information.split_at(8);
    let part_one = {
        let mut bytes: [u8; 8] = [0u8; 8];
        bytes.copy_from_slice(part_one);
        u64::from_le_bytes(bytes)
    };
    let part_two = {
        let mut bytes: [u8; 8] = [0u8; 8];
        bytes.copy_from_slice(part_two);
        u64::from_le_bytes(bytes)
    };
    Ok(Farm::from([part_one, part_two]))
}

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

No branches or pull requests

1 participant