carbon_core/transformers.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
//! Provides utility functions to transform transaction data into various representations
//! within the `carbon-core` framework.
//!
//! This module includes functions for extracting transaction metadata, parsing instructions,
//! and nesting instructions based on stack depth. It also offers transformations for Solana
//! transaction components into suitable formats for the framework, enabling flexible processing
//! of transaction data.
//!
//! ## Key Components
//!
//! - **Metadata Extraction**: Extracts essential transaction metadata for processing.
//! - **Instruction Parsing**: Parses both top-level and nested instructions from transactions.
//! - **Account Metadata**: Converts account data into a standardized format for transactions.
//!
//! ## Notes
//!
//! - The module supports both legacy and v0 transactions, including handling of loaded
//! addresses and inner instructions.
use crate::{
datasource::TransactionUpdate,
error::{CarbonResult, Error},
instruction::{InstructionMetadata, NestedInstruction},
transaction::TransactionMetadata,
};
use solana_sdk::{
instruction::{AccountMeta, CompiledInstruction},
message::{
v0::{LoadedAddresses, LoadedMessage},
VersionedMessage,
},
pubkey::Pubkey,
reserved_account_keys::ReservedAccountKeys,
transaction_context::TransactionReturnData,
};
use solana_transaction_status::{
option_serializer::OptionSerializer, InnerInstruction, InnerInstructions, Reward,
TransactionStatusMeta, TransactionTokenBalance, UiInstruction, UiLoadedAddresses,
UiTransactionStatusMeta,
};
use std::{collections::HashSet, str::FromStr};
/// Extracts the metadata from a transaction update.
///
/// This function retrieves core metadata such as the transaction's slot, signature, and
/// fee payer from the transaction's message. It ensures that these details are available
/// and ready for further processing.
///
/// # Parameters
///
/// - `transaction_update`: The `TransactionUpdate` containing the transaction details.
///
/// # Returns
///
/// A `CarbonResult<TransactionMetadata>` which includes the slot, signature, and fee payer of the transaction.
///
/// # Errors
///
/// Returns an error if the fee payer cannot be extracted from the transaction's account keys.
pub fn extract_transaction_metadata(
transaction_update: &TransactionUpdate,
) -> CarbonResult<TransactionMetadata> {
log::trace!(
"extract_transaction_metadata(transaction_update: {:?})",
transaction_update
);
let message = transaction_update.transaction.message.clone();
let accounts = message.static_account_keys();
Ok(TransactionMetadata {
slot: transaction_update.slot,
signature: transaction_update.signature,
fee_payer: *accounts.get(0).ok_or(Error::MissingFeePayer)?,
})
}
/// Extracts instructions with metadata from a transaction update.
///
/// This function parses both top-level and inner instructions, associating them with
/// metadata such as stack height and account information. It provides a detailed
/// breakdown of each instruction, useful for further processing.
///
/// # Parameters
///
/// - `transaction_metadata`: Metadata about the transaction from which instructions are extracted.
/// - `transaction_update`: The `TransactionUpdate` containing the transaction's data and message.
///
/// # Returns
///
/// A `CarbonResult<Vec<(InstructionMetadata, solana_sdk::instruction::Instruction)>>` containing
/// instructions along with their associated metadata.
///
/// # Errors
///
/// Returns an error if any account metadata required for instruction processing is missing.
pub fn extract_instructions_with_metadata(
transaction_metadata: &TransactionMetadata,
transaction_update: &TransactionUpdate,
) -> CarbonResult<Vec<(InstructionMetadata, solana_sdk::instruction::Instruction)>> {
log::trace!(
"extract_instructions_with_metadata(transaction_metadata: {:?}, transaction_update: {:?})",
transaction_metadata,
transaction_update
);
let message = transaction_update.transaction.message.clone();
let meta = transaction_update.meta.clone();
let mut instructions_with_metadata =
Vec::<(InstructionMetadata, solana_sdk::instruction::Instruction)>::new();
match message {
VersionedMessage::Legacy(legacy) => {
for (i, compiled_instruction) in legacy.instructions.iter().enumerate() {
let program_id = *legacy
.account_keys
.get(compiled_instruction.program_id_index as usize)
.unwrap_or(&Pubkey::default());
let accounts: Vec<_> = compiled_instruction
.accounts
.iter()
.filter_map(|account_index| {
let account_pubkey = legacy.account_keys.get(*account_index as usize)?;
Some(AccountMeta {
pubkey: *account_pubkey,
is_writable: legacy.is_maybe_writable(*account_index as usize, None),
is_signer: legacy.is_signer(*account_index as usize),
})
})
.collect();
instructions_with_metadata.push((
InstructionMetadata {
transaction_metadata: transaction_metadata.clone(),
stack_height: 1,
},
solana_sdk::instruction::Instruction {
program_id,
accounts,
data: compiled_instruction.data.clone(),
},
));
if let Some(inner_instructions) = &meta.inner_instructions {
for inner_instructions_per_tx in inner_instructions {
if inner_instructions_per_tx.index == i as u8 {
for inner_instruction in inner_instructions_per_tx.instructions.iter() {
let program_id = *legacy
.account_keys
.get(inner_instruction.instruction.program_id_index as usize)
.unwrap_or(&Pubkey::default());
let accounts: Vec<_> = inner_instruction
.instruction
.accounts
.iter()
.filter_map(|account_index| {
let account_pubkey =
legacy.account_keys.get(*account_index as usize)?;
return Some(AccountMeta {
pubkey: *account_pubkey,
is_writable: legacy
.is_maybe_writable(*account_index as usize, None),
is_signer: legacy.is_signer(*account_index as usize),
});
})
.collect();
instructions_with_metadata.push((
InstructionMetadata {
transaction_metadata: transaction_metadata.clone(),
stack_height: inner_instruction.stack_height.unwrap_or(1),
},
solana_sdk::instruction::Instruction {
program_id,
accounts,
data: inner_instruction.instruction.data.clone(),
},
));
}
}
}
}
}
}
VersionedMessage::V0(v0) => {
let loaded_addresses = LoadedAddresses {
writable: meta
.loaded_addresses
.writable
.iter()
.map(|key| key.clone())
.collect(),
readonly: meta
.loaded_addresses
.readonly
.iter()
.map(|key| key.clone())
.collect(),
};
let loaded_message = LoadedMessage::new(
v0.clone(),
loaded_addresses,
&ReservedAccountKeys::empty_key_set(),
);
for (i, compiled_instruction) in v0.instructions.iter().enumerate() {
let program_id = *loaded_message
.account_keys()
.get(compiled_instruction.program_id_index as usize)
.unwrap_or(&Pubkey::default());
let accounts: Vec<AccountMeta> = compiled_instruction
.accounts
.iter()
.filter_map(|account_index| {
let account_pubkey =
loaded_message.account_keys().get(*account_index as usize);
return Some(AccountMeta {
pubkey: account_pubkey.map(|acc| acc.clone()).unwrap_or_default(),
is_writable: loaded_message.is_writable(*account_index as usize),
is_signer: loaded_message.is_signer(*account_index as usize),
});
})
.collect();
instructions_with_metadata.push((
InstructionMetadata {
transaction_metadata: transaction_metadata.clone(),
stack_height: 1,
},
solana_sdk::instruction::Instruction {
program_id,
accounts,
data: compiled_instruction.data.clone(),
},
));
if let Some(inner_instructions) = &meta.inner_instructions {
for inner_instructions_per_tx in inner_instructions {
if inner_instructions_per_tx.index == i as u8 {
for inner_instruction in inner_instructions_per_tx.instructions.iter() {
let program_id = *loaded_message
.account_keys()
.get(inner_instruction.instruction.program_id_index as usize)
.unwrap_or(&Pubkey::default());
let accounts: Vec<AccountMeta> = inner_instruction
.instruction
.accounts
.iter()
.filter_map(|account_index| {
let account_pubkey = loaded_message
.account_keys()
.get(*account_index as usize)
.map(|pubkey| pubkey.clone())
.unwrap_or_default();
return Some(AccountMeta {
pubkey: account_pubkey.clone(),
is_writable: loaded_message
.is_writable(*account_index as usize),
is_signer: loaded_message
.is_signer(*account_index as usize),
});
})
.collect();
instructions_with_metadata.push((
InstructionMetadata {
transaction_metadata: transaction_metadata.clone(),
stack_height: inner_instruction.stack_height.unwrap_or(1),
},
solana_sdk::instruction::Instruction {
program_id,
accounts,
data: inner_instruction.instruction.data.clone(),
},
));
}
}
}
}
}
}
}
Ok(instructions_with_metadata)
}
/// Extracts account metadata from a compiled instruction and transaction message.
///
/// This function converts each account index within the instruction into an `AccountMeta`
/// struct, providing details on account keys, signer status, and write permissions.
///
/// # Parameters
///
/// - `compiled_instruction`: The compiled instruction to extract accounts from.
/// - `message`: The transaction message containing the account keys.
///
/// # Returns
///
/// A `CarbonResult<Vec<solana_sdk::instruction::AccountMeta>>` containing metadata
/// for each account involved in the instruction.
///
/// # Errors
///
/// Returns an error if any referenced account key is missing from the transaction.
pub fn extract_account_metas(
compiled_instruction: &solana_sdk::instruction::CompiledInstruction,
message: &solana_sdk::message::VersionedMessage,
) -> CarbonResult<Vec<solana_sdk::instruction::AccountMeta>> {
log::trace!(
"extract_account_metas(compiled_instruction: {:?}, message: {:?})",
compiled_instruction,
message
);
let mut accounts = Vec::<solana_sdk::instruction::AccountMeta>::new();
for account_index in compiled_instruction.accounts.iter() {
accounts.push(solana_sdk::instruction::AccountMeta {
pubkey: *message
.static_account_keys()
.get(*account_index as usize)
.ok_or(Error::MissingAccountInTransaction)?,
is_signer: message.is_signer(*account_index as usize),
is_writable: message.is_maybe_writable(
*account_index as usize,
Some(
&message
.static_account_keys()
.into_iter()
.map(|pubkey| pubkey.clone())
.collect::<HashSet<_>>(),
),
),
});
}
Ok(accounts)
}
/// Nests instructions based on stack height, producing a hierarchy of `NestedInstruction`.
///
/// This function organizes instructions into a nested structure, enabling hierarchical
/// transaction analysis. Instructions are nested according to their stack height,
/// forming a tree-like structure.
///
/// # Parameters
///
/// - `instructions`: A list of tuples containing `InstructionMetadata` and instructions.
///
/// # Returns
///
/// A vector of `NestedInstruction`, representing the instructions organized by stack depth.
pub fn nest_instructions(
instructions: Vec<(InstructionMetadata, solana_sdk::instruction::Instruction)>,
) -> Vec<NestedInstruction> {
log::trace!("nest_instructions(instructions: {:?})", instructions);
let mut result = Vec::<NestedInstruction>::new();
let mut stack = Vec::<(Vec<usize>, usize)>::new();
for (metadata, instruction) in instructions {
let nested_instruction = NestedInstruction {
metadata: metadata.clone(),
instruction,
inner_instructions: Vec::new(),
};
while let Some((_, parent_stack_height)) = stack.last() {
if metadata.stack_height as usize > *parent_stack_height {
break;
}
stack.pop();
}
if let Some((path_to_parent, _)) = stack.last() {
let mut current_instructions = &mut result;
for &index in path_to_parent {
current_instructions = &mut current_instructions[index].inner_instructions;
}
current_instructions.push(nested_instruction);
let mut new_path = path_to_parent.clone();
new_path.push(current_instructions.len() - 1);
stack.push((new_path, metadata.stack_height as usize));
} else {
result.push(nested_instruction);
let new_path = vec![result.len() - 1];
stack.push((new_path, metadata.stack_height as usize));
}
}
result
}
/// Converts UI transaction metadata into `TransactionStatusMeta`.
///
/// This function transforms the user interface format of transaction metadata into
/// a more comprehensive `TransactionStatusMeta` structure suitable for backend processing.
///
/// # Parameters
///
/// - `meta_original`: The original UI format of transaction status metadata.
///
/// # Returns
///
/// A `CarbonResult<TransactionStatusMeta>` representing the full transaction status
/// with nested instructions, token balances, and rewards.
///
/// # Notes
///
/// This function handles various metadata fields, including inner instructions, token
/// balances, and rewards, providing a complete view of the transaction's effects.
pub fn transaction_metadata_from_original_meta(
meta_original: UiTransactionStatusMeta,
) -> CarbonResult<TransactionStatusMeta> {
log::trace!(
"transaction_metadata_from_original_meta(meta_original: {:?})",
meta_original
);
Ok(TransactionStatusMeta {
status: meta_original.status,
fee: meta_original.fee,
pre_balances: meta_original.pre_balances,
post_balances: meta_original.post_balances,
inner_instructions: Some(
meta_original
.inner_instructions
.unwrap_or_else(|| vec![])
.iter()
.map(|inner_instruction_group| InnerInstructions {
index: inner_instruction_group.index,
instructions: inner_instruction_group
.instructions
.iter()
.map(|ui_instruction| match ui_instruction {
UiInstruction::Compiled(compiled_ui_instruction) => {
let decoded_data =
bs58::decode(compiled_ui_instruction.data.clone())
.into_vec()
.unwrap_or_else(|_| vec![]);
InnerInstruction {
instruction: CompiledInstruction {
program_id_index: compiled_ui_instruction.program_id_index,
accounts: compiled_ui_instruction.accounts.clone(),
data: decoded_data,
},
stack_height: compiled_ui_instruction.stack_height,
}
}
_ => {
log::error!("Unsupported instruction type encountered");
InnerInstruction {
instruction: CompiledInstruction {
program_id_index: 0,
accounts: vec![],
data: vec![],
},
stack_height: None,
}
}
})
.collect::<Vec<InnerInstruction>>(),
})
.collect::<Vec<InnerInstructions>>(),
),
log_messages: Some(meta_original.log_messages.unwrap_or_else(|| vec![])),
pre_token_balances: Some(
meta_original
.pre_token_balances
.unwrap_or_else(|| vec![])
.iter()
.filter_map(|transaction_token_balance| {
if let (OptionSerializer::Some(owner), OptionSerializer::Some(program_id)) = (
transaction_token_balance.owner.as_ref(),
transaction_token_balance.program_id.as_ref(),
) {
Some(TransactionTokenBalance {
account_index: transaction_token_balance.account_index,
mint: transaction_token_balance.mint.clone(),
ui_token_amount: transaction_token_balance.ui_token_amount.clone(),
owner: owner.to_string(),
program_id: program_id.to_string(),
})
} else {
None
}
})
.collect::<Vec<TransactionTokenBalance>>(),
),
post_token_balances: Some(
meta_original
.post_token_balances
.unwrap_or_else(|| vec![])
.iter()
.filter_map(|transaction_token_balance| {
if let (OptionSerializer::Some(owner), OptionSerializer::Some(program_id)) = (
transaction_token_balance.owner.as_ref(),
transaction_token_balance.program_id.as_ref(),
) {
Some(TransactionTokenBalance {
account_index: transaction_token_balance.account_index,
mint: transaction_token_balance.mint.clone(),
ui_token_amount: transaction_token_balance.ui_token_amount.clone(),
owner: owner.to_string(),
program_id: program_id.to_string(),
})
} else {
None
}
})
.collect::<Vec<TransactionTokenBalance>>(),
),
rewards: Some(
meta_original
.rewards
.unwrap_or_else(|| vec![])
.iter()
.map(|rewards| Reward {
pubkey: rewards.pubkey.clone(),
lamports: rewards.lamports,
post_balance: rewards.post_balance,
reward_type: rewards.reward_type,
commission: rewards.commission,
})
.collect::<Vec<Reward>>(),
),
loaded_addresses: {
let loaded = meta_original
.loaded_addresses
.unwrap_or_else(|| UiLoadedAddresses {
writable: vec![],
readonly: vec![],
});
LoadedAddresses {
writable: loaded
.writable
.iter()
.map(|w| Pubkey::from_str(&w).unwrap_or_default())
.collect::<Vec<Pubkey>>(),
readonly: loaded
.readonly
.iter()
.map(|r| Pubkey::from_str(&r).unwrap_or_default())
.collect::<Vec<Pubkey>>(),
}
},
return_data: meta_original
.return_data
.map(|return_data| TransactionReturnData {
program_id: return_data.program_id.parse().unwrap_or_default(),
data: return_data.data.0.as_bytes().to_vec(),
}),
compute_units_consumed: meta_original
.compute_units_consumed
.map(|compute_unit_consumed| compute_unit_consumed)
.or(None),
})
}