2
2
crate :: parse_instruction:: {
3
3
check_num_accounts, ParsableProgram , ParseInstructionError , ParsedInstructionEnum ,
4
4
} ,
5
+ borsh:: BorshDeserialize ,
5
6
serde_json:: json,
6
7
solana_sdk:: { instruction:: CompiledInstruction , message:: AccountKeys , pubkey:: Pubkey } ,
8
+ spl_associated_token_account:: instruction:: AssociatedTokenAccountInstruction ,
7
9
} ;
8
10
9
11
// A helper function to convert spl_associated_token_account::id() as spl_sdk::pubkey::Pubkey
@@ -25,19 +27,42 @@ pub fn parse_associated_token(
25
27
) ) ;
26
28
}
27
29
}
28
- check_num_associated_token_accounts ( & instruction. accounts , 7 ) ?;
29
- Ok ( ParsedInstructionEnum {
30
- instruction_type : "create" . to_string ( ) ,
31
- info : json ! ( {
32
- "source" : account_keys[ instruction. accounts[ 0 ] as usize ] . to_string( ) ,
33
- "account" : account_keys[ instruction. accounts[ 1 ] as usize ] . to_string( ) ,
34
- "wallet" : account_keys[ instruction. accounts[ 2 ] as usize ] . to_string( ) ,
35
- "mint" : account_keys[ instruction. accounts[ 3 ] as usize ] . to_string( ) ,
36
- "systemProgram" : account_keys[ instruction. accounts[ 4 ] as usize ] . to_string( ) ,
37
- "tokenProgram" : account_keys[ instruction. accounts[ 5 ] as usize ] . to_string( ) ,
38
- "rentSysvar" : account_keys[ instruction. accounts[ 6 ] as usize ] . to_string( ) ,
39
- } ) ,
40
- } )
30
+ if instruction. data . is_empty ( ) {
31
+ check_num_associated_token_accounts ( & instruction. accounts , 7 ) ?;
32
+ Ok ( ParsedInstructionEnum {
33
+ instruction_type : "create" . to_string ( ) ,
34
+ info : json ! ( {
35
+ "source" : account_keys[ instruction. accounts[ 0 ] as usize ] . to_string( ) ,
36
+ "account" : account_keys[ instruction. accounts[ 1 ] as usize ] . to_string( ) ,
37
+ "wallet" : account_keys[ instruction. accounts[ 2 ] as usize ] . to_string( ) ,
38
+ "mint" : account_keys[ instruction. accounts[ 3 ] as usize ] . to_string( ) ,
39
+ "systemProgram" : account_keys[ instruction. accounts[ 4 ] as usize ] . to_string( ) ,
40
+ "tokenProgram" : account_keys[ instruction. accounts[ 5 ] as usize ] . to_string( ) ,
41
+ "rentSysvar" : account_keys[ instruction. accounts[ 6 ] as usize ] . to_string( ) ,
42
+ } ) ,
43
+ } )
44
+ } else {
45
+ let ata_instruction = AssociatedTokenAccountInstruction :: try_from_slice ( & instruction. data )
46
+ . map_err ( |_| {
47
+ ParseInstructionError :: InstructionNotParsable ( ParsableProgram :: SplToken )
48
+ } ) ?;
49
+ match ata_instruction {
50
+ AssociatedTokenAccountInstruction :: Create => {
51
+ check_num_associated_token_accounts ( & instruction. accounts , 6 ) ?;
52
+ Ok ( ParsedInstructionEnum {
53
+ instruction_type : "create" . to_string ( ) ,
54
+ info : json ! ( {
55
+ "source" : account_keys[ instruction. accounts[ 0 ] as usize ] . to_string( ) ,
56
+ "account" : account_keys[ instruction. accounts[ 1 ] as usize ] . to_string( ) ,
57
+ "wallet" : account_keys[ instruction. accounts[ 2 ] as usize ] . to_string( ) ,
58
+ "mint" : account_keys[ instruction. accounts[ 3 ] as usize ] . to_string( ) ,
59
+ "systemProgram" : account_keys[ instruction. accounts[ 4 ] as usize ] . to_string( ) ,
60
+ "tokenProgram" : account_keys[ instruction. accounts[ 5 ] as usize ] . to_string( ) ,
61
+ } ) ,
62
+ } )
63
+ }
64
+ }
65
+ }
41
66
}
42
67
43
68
fn check_num_associated_token_accounts (
@@ -49,14 +74,17 @@ fn check_num_associated_token_accounts(
49
74
50
75
#[ cfg( test) ]
51
76
mod test {
77
+ #[ allow( deprecated) ]
78
+ use spl_associated_token_account:: create_associated_token_account as create_associated_token_account_deprecated;
52
79
use {
53
80
super :: * ,
54
81
solana_account_decoder:: parse_token:: pubkey_from_spl_token,
55
82
spl_associated_token_account:: {
56
- create_associated_token_account, get_associated_token_address,
83
+ get_associated_token_address,
84
+ instruction:: create_associated_token_account,
57
85
solana_program:: {
58
86
instruction:: CompiledInstruction as SplAssociatedTokenCompiledInstruction ,
59
- message:: Message , pubkey:: Pubkey as SplAssociatedTokenPubkey ,
87
+ message:: Message , pubkey:: Pubkey as SplAssociatedTokenPubkey , sysvar ,
60
88
} ,
61
89
} ,
62
90
} ;
@@ -84,14 +112,48 @@ mod test {
84
112
}
85
113
86
114
#[ test]
87
- fn test_parse_associated_token ( ) {
115
+ fn test_parse_associated_token_deprecated ( ) {
88
116
let funder = Pubkey :: new_unique ( ) ;
89
117
let wallet_address = Pubkey :: new_unique ( ) ;
90
118
let mint = Pubkey :: new_unique ( ) ;
91
119
let associated_account_address =
92
120
get_associated_token_address ( & convert_pubkey ( wallet_address) , & convert_pubkey ( mint) ) ;
93
- let rent_sysvar = solana_sdk:: sysvar:: rent:: id ( ) ;
121
+ #[ allow( deprecated) ]
122
+ let create_ix = create_associated_token_account_deprecated (
123
+ & convert_pubkey ( funder) ,
124
+ & convert_pubkey ( wallet_address) ,
125
+ & convert_pubkey ( mint) ,
126
+ ) ;
127
+ let message = Message :: new ( & [ create_ix] , None ) ;
128
+ let compiled_instruction = convert_compiled_instruction ( & message. instructions [ 0 ] ) ;
129
+ assert_eq ! (
130
+ parse_associated_token(
131
+ & compiled_instruction,
132
+ & AccountKeys :: new( & convert_account_keys( & message) , None )
133
+ )
134
+ . unwrap( ) ,
135
+ ParsedInstructionEnum {
136
+ instruction_type: "create" . to_string( ) ,
137
+ info: json!( {
138
+ "source" : funder. to_string( ) ,
139
+ "account" : associated_account_address. to_string( ) ,
140
+ "wallet" : wallet_address. to_string( ) ,
141
+ "mint" : mint. to_string( ) ,
142
+ "systemProgram" : solana_sdk:: system_program:: id( ) . to_string( ) ,
143
+ "tokenProgram" : spl_token:: id( ) . to_string( ) ,
144
+ "rentSysvar" : sysvar:: rent:: id( ) . to_string( ) ,
145
+ } )
146
+ }
147
+ ) ;
148
+ }
94
149
150
+ #[ test]
151
+ fn test_parse_associated_token ( ) {
152
+ let funder = Pubkey :: new_unique ( ) ;
153
+ let wallet_address = Pubkey :: new_unique ( ) ;
154
+ let mint = Pubkey :: new_unique ( ) ;
155
+ let associated_account_address =
156
+ get_associated_token_address ( & convert_pubkey ( wallet_address) , & convert_pubkey ( mint) ) ;
95
157
let create_ix = create_associated_token_account (
96
158
& convert_pubkey ( funder) ,
97
159
& convert_pubkey ( wallet_address) ,
@@ -114,7 +176,6 @@ mod test {
114
176
"mint" : mint. to_string( ) ,
115
177
"systemProgram" : solana_sdk:: system_program:: id( ) . to_string( ) ,
116
178
"tokenProgram" : spl_token:: id( ) . to_string( ) ,
117
- "rentSysvar" : rent_sysvar. to_string( ) ,
118
179
} )
119
180
}
120
181
) ;
0 commit comments