@@ -11,16 +11,32 @@ use solana_sdk::{
1111
1212/// Input for Solana transaction - either build from instructions or use pre-built
1313#[ derive( Debug , Clone , Serialize , Deserialize , utoipa:: ToSchema ) ]
14- #[ serde( rename_all = "camelCase" , untagged) ]
14+ #[ serde( untagged) ]
1515pub enum SolanaTransactionInput {
1616 /// Build transaction from instructions
17- Instructions {
18- instructions : Vec < SolanaInstructionData > ,
19- } ,
17+ Instructions ( SolanaTransactionInputInstructions ) ,
2018 /// Use pre-built serialized VersionedTransaction (base64)
21- Serialized {
22- transaction : String ,
23- } ,
19+ Serialized ( SolanaTransactionInputSerialized ) ,
20+ }
21+
22+ impl SolanaTransactionInput {
23+ pub fn new_with_instructions ( instructions : Vec < SolanaInstructionData > ) -> Self {
24+ Self :: Instructions ( SolanaTransactionInputInstructions { instructions } )
25+ }
26+
27+ pub fn new_with_serialized ( transaction : String ) -> Self {
28+ Self :: Serialized ( SolanaTransactionInputSerialized { transaction } )
29+ }
30+ }
31+
32+ #[ derive( Debug , Clone , Serialize , Deserialize , utoipa:: ToSchema ) ]
33+ pub struct SolanaTransactionInputInstructions {
34+ pub instructions : Vec < SolanaInstructionData > ,
35+ }
36+
37+ #[ derive( Debug , Clone , Serialize , Deserialize , utoipa:: ToSchema ) ]
38+ pub struct SolanaTransactionInputSerialized {
39+ pub transaction : String ,
2440}
2541
2642/// Solana instruction data provided by the user
@@ -142,11 +158,11 @@ impl SolanaTransaction {
142158 recent_blockhash : solana_sdk:: hash:: Hash ,
143159 ) -> Result < VersionedTransaction , SolanaTransactionError > {
144160 match & self . input {
145- SolanaTransactionInput :: Instructions { instructions } => {
146- self . build_from_instructions ( instructions, payer, recent_blockhash)
161+ SolanaTransactionInput :: Instructions ( i ) => {
162+ self . build_from_instructions ( & i . instructions , payer, recent_blockhash)
147163 }
148- SolanaTransactionInput :: Serialized { transaction } => {
149- self . deserialize_transaction ( transaction, payer)
164+ SolanaTransactionInput :: Serialized ( t ) => {
165+ self . deserialize_transaction ( & t . transaction , payer)
150166 }
151167 }
152168 }
@@ -175,15 +191,12 @@ impl SolanaTransaction {
175191 inst_list. push ( inst. to_instruction ( ) ?) ;
176192 }
177193
178- let message = v0:: Message :: try_compile (
179- & payer,
180- & inst_list,
181- & [ ] ,
182- recent_blockhash,
183- )
184- . map_err ( |e| SolanaTransactionError :: MessageCompilationFailed {
185- error : e. to_string ( ) ,
186- } ) ?;
194+ let message =
195+ v0:: Message :: try_compile ( & payer, & inst_list, & [ ] , recent_blockhash) . map_err ( |e| {
196+ SolanaTransactionError :: MessageCompilationFailed {
197+ error : e. to_string ( ) ,
198+ }
199+ } ) ?;
187200
188201 let message = VersionedMessage :: V0 ( message) ;
189202 let num_signatures = message. header ( ) . num_required_signatures as usize ;
@@ -203,17 +216,19 @@ impl SolanaTransaction {
203216 tx_base64 : & str ,
204217 expected_payer : Pubkey ,
205218 ) -> Result < VersionedTransaction , SolanaTransactionError > {
206- let tx_bytes = Base64Engine . decode ( tx_base64)
207- . map_err ( |e| SolanaTransactionError :: DeserializationFailed {
219+ let tx_bytes = Base64Engine . decode ( tx_base64) . map_err ( |e| {
220+ SolanaTransactionError :: DeserializationFailed {
208221 error : format ! ( "Invalid base64: {}" , e) ,
209- } ) ?;
222+ }
223+ } ) ?;
210224
211225 // Deserialize from binary wire format using bincode
212226 let ( transaction, _) : ( VersionedTransaction , _ ) =
213- bincode:: serde:: decode_from_slice ( & tx_bytes, bincode:: config:: standard ( ) )
214- . map_err ( |e| SolanaTransactionError :: DeserializationFailed {
227+ bincode:: serde:: decode_from_slice ( & tx_bytes, bincode:: config:: standard ( ) ) . map_err (
228+ |e| SolanaTransactionError :: DeserializationFailed {
215229 error : format ! ( "Failed to deserialize VersionedTransaction: {}" , e) ,
216- } ) ?;
230+ } ,
231+ ) ?;
217232
218233 // Verify fee payer
219234 let fee_payer = transaction. message . static_account_keys ( ) [ 0 ] ;
@@ -245,10 +260,10 @@ pub enum SolanaTransactionError {
245260
246261 #[ error( "Invalid blockhash: {error}" ) ]
247262 InvalidBlockhash { error : String } ,
248-
263+
249264 #[ error( "Failed to deserialize transaction: {error}" ) ]
250265 DeserializationFailed { error : String } ,
251-
266+
252267 #[ error( "Fee payer mismatch: expected {expected}, got {got}" ) ]
253268 FeePayerMismatch { expected : String , got : String } ,
254269}
0 commit comments