Skip to content

Instantly share code, notes, and snippets.

@nixxholas
Created June 5, 2021 16:35
Show Gist options
  • Save nixxholas/bee7cc4b9bf139a93349d3cedf3e6691 to your computer and use it in GitHub Desktop.
Save nixxholas/bee7cc4b9bf139a93349d3cedf3e6691 to your computer and use it in GitHub Desktop.
So I wasted my time on processing an instruction rather than reading it
use std::str::FromStr;
use diesel::PgConnection;
use rayon::prelude::*;
use solana_client::rpc_client::RpcClient;
use solana_program::account_info::{AccountInfo, IntoAccountInfo};
use solana_sdk::pubkey::Pubkey;
use spl_associated_token_account::processor::process_instruction;
use crate::models::instruction_properties::NewInstructionProperty;
pub fn process_native_associated_token_account(
conn: &PgConnection,
rpc_url: String,
// The transaction that has this instruction.
transaction_hash: &String,
// The unique index of the instruction for the transaction involved.
instruction_index: Option<&i32>,
// Program's core instruction type
core_instruction_type: &crate::models::instruction_types::InstructionType,
// The data relating to this instruction
data: &[u8],
// The parent instruction, if any; Frequently used for InnerInstructions
parent_instruction_id: Option<&i64>,
// All the other involved account indices
account_indices: &Vec<u8>,
// All the pubkeys in this transaction
account_keys: &Vec<Pubkey>,
) -> Result<crate::models::instructions::Instruction, String> {
// Get the RpcClient up
let rpc_client = RpcClient::new(rpc_url);
// Get the accounts that are involved only?
let mut involved_accounts = Vec::new();
for i in account_indices {
if (i + 1) as usize <= account_keys.len() {
involved_accounts.push(account_keys[*i as usize]);
}
}
// Ensure the number of accounts involved = account_indices
if involved_accounts.len() == account_indices.len() {
// First, attempt to gather the account data
let accounts_result = rpc_client.get_multiple_accounts(account_keys);
// Since account data is in,
if let Ok(mut account_data) = accounts_result {
// Convert the pubkeys and accounts into AccountInfo
let mut accounts = Vec::new();
for (idx, ad) in account_data.iter().enumerate() {
if let Some(mut a) = ad {
accounts
.push((account_keys[account_indices[idx] as usize], a).into_account_info());
}
}
// Unpack the instruction via the spl_token_swap library
let process_result = process_instruction(
&Pubkey::from_str(&core_instruction_type.program.to_string()).unwrap(),
accounts.as_slice(),
data,
);
// Process the transaction
if let Ok(ata_instruction) = process_result {}
return Err(
"[processors/programs/native_associated_token_account] FATAL: Unrecognised instruction."
.to_string(),
);
}
}
Err(
"[processors/programs/native_associated_token_account] FATAL: Failed to gather account data \
via RPC.".to_string(),
)
}
@nixxholas
Copy link
Author

I spent 4 hours realising the fact that i was doing a tombalek to myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment