Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions rust/chains/tw_solana/src/modules/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,30 @@ impl SolanaTransaction {
.tw_err(SigningErrorType::Error_internal)
}

pub fn add_instruction(encoded_tx: &str, instruction: &str) -> SigningResult<String> {
pub fn insert_instruction(
encoded_tx: &str,
insert_at: i32,
instruction: &str,
) -> SigningResult<String> {
let tx_bytes = base64::decode(encoded_tx, STANDARD)?;
let mut tx: VersionedTransaction =
bincode::deserialize(&tx_bytes).map_err(|_| SigningErrorType::Error_input_parse)?;

let instruction: Instruction =
serde_json::from_str(instruction).map_err(|_| SigningErrorType::Error_input_parse)?;

tx.message.push_instruction(
if insert_at >= 0 && insert_at as usize > tx.message.instructions().len() {
return Err(SigningError::from(SigningErrorType::Error_invalid_params));
}

let final_insert_at = if insert_at < 0 {
tx.message.instructions().len() // Append to the end if negative
} else {
insert_at as usize // Use the specified position
};

tx.message.insert_instruction(
final_insert_at,
instruction.program_id,
instruction.accounts,
instruction.data,
Expand Down
Loading
Loading