tulipv2-sdk icon indicating copy to clipboard operation
tulipv2-sdk copied to clipboard

Helper Functions To Add

Open bonedaddy opened this issue 3 years ago • 2 comments

  • 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 avatar Mar 08 '22 05:03 bonedaddy

addressed via sighashdb

bonedaddy avatar Jun 11 '22 08:06 bonedaddy


/// 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]))
}

bonedaddy avatar Oct 25 '22 03:10 bonedaddy