@@ -22,7 +22,7 @@ use spl_token_2022::{
22
22
} ;
23
23
use std:: {
24
24
fmt, io,
25
- sync:: Arc ,
25
+ sync:: { Arc , RwLock } ,
26
26
time:: { Duration , Instant } ,
27
27
} ;
28
28
use thiserror:: Error ;
@@ -124,6 +124,7 @@ pub struct Token<T, S> {
124
124
pubkey : Pubkey ,
125
125
payer : S ,
126
126
program_id : Pubkey ,
127
+ memo : Arc < RwLock < Option < String > > > ,
127
128
}
128
129
129
130
impl < T , S > fmt:: Debug for Token < T , S >
@@ -134,6 +135,7 @@ where
134
135
f. debug_struct ( "Token" )
135
136
. field ( "pubkey" , & self . pubkey )
136
137
. field ( "payer" , & self . payer . pubkey ( ) )
138
+ . field ( "memo" , & self . memo . read ( ) . unwrap ( ) )
137
139
. finish ( )
138
140
}
139
141
}
@@ -154,6 +156,7 @@ where
154
156
pubkey : * address,
155
157
payer,
156
158
program_id : * program_id,
159
+ memo : Arc :: new ( RwLock :: new ( None ) ) ,
157
160
}
158
161
}
159
162
@@ -168,9 +171,16 @@ where
168
171
pubkey : self . pubkey ,
169
172
payer,
170
173
program_id : self . program_id ,
174
+ memo : Arc :: new ( RwLock :: new ( None ) ) ,
171
175
}
172
176
}
173
177
178
+ pub fn with_memo < M : AsRef < str > > ( & self , memo : M ) -> & Self {
179
+ let mut w_memo = self . memo . write ( ) . unwrap ( ) ;
180
+ * w_memo = Some ( memo. as_ref ( ) . to_string ( ) ) ;
181
+ self
182
+ }
183
+
174
184
pub async fn get_new_latest_blockhash ( & self ) -> TokenResult < Hash > {
175
185
let blockhash = self
176
186
. client
@@ -206,16 +216,22 @@ where
206
216
207
217
pub async fn process_ixs < S2 : Signers > (
208
218
& self ,
209
- instructions : & [ Instruction ] ,
219
+ token_instructions : & [ Instruction ] ,
210
220
signing_keypairs : & S2 ,
211
221
) -> TokenResult < T :: Output > {
222
+ let mut instructions = vec ! [ ] ;
223
+ let mut w_memo = self . memo . write ( ) . unwrap ( ) ;
224
+ if let Some ( memo) = w_memo. take ( ) {
225
+ instructions. push ( spl_memo:: build_memo ( memo. as_bytes ( ) , & [ ] ) ) ;
226
+ }
227
+ instructions. extend_from_slice ( token_instructions) ;
212
228
let latest_blockhash = self
213
229
. client
214
230
. get_latest_blockhash ( )
215
231
. await
216
232
. map_err ( TokenError :: Client ) ?;
217
233
218
- let mut tx = Transaction :: new_with_payer ( instructions, Some ( & self . payer . pubkey ( ) ) ) ;
234
+ let mut tx = Transaction :: new_with_payer ( & instructions, Some ( & self . payer . pubkey ( ) ) ) ;
219
235
tx. try_partial_sign ( & [ & self . payer ] , latest_blockhash)
220
236
. map_err ( |error| TokenError :: Client ( error. into ( ) ) ) ?;
221
237
tx. try_sign ( signing_keypairs, latest_blockhash)
@@ -280,12 +296,12 @@ where
280
296
) -> TokenResult < Self > {
281
297
let token = Self :: new ( client, program_id, & native_mint:: id ( ) , payer) ;
282
298
token
283
- . process_ixs (
299
+ . process_ixs :: < [ & dyn Signer ; 0 ] > (
284
300
& [ instruction:: create_native_mint (
285
301
program_id,
286
302
& token. payer . pubkey ( ) ,
287
303
) ?] ,
288
- & [ & token . payer ] ,
304
+ & [ ] ,
289
305
)
290
306
. await ?;
291
307
@@ -299,14 +315,14 @@ where
299
315
300
316
/// Create and initialize the associated account.
301
317
pub async fn create_associated_token_account ( & self , owner : & Pubkey ) -> TokenResult < Pubkey > {
302
- self . process_ixs (
318
+ self . process_ixs :: < [ & dyn Signer ; 0 ] > (
303
319
& [ create_associated_token_account (
304
320
& self . payer . pubkey ( ) ,
305
321
owner,
306
322
& self . pubkey ,
307
323
& self . program_id ,
308
324
) ] ,
309
- & [ & self . payer ] ,
325
+ & [ ] ,
310
326
)
311
327
. await
312
328
. map ( |_| self . get_associated_token_address ( owner) )
@@ -359,7 +375,7 @@ where
359
375
owner,
360
376
) ?,
361
377
] ,
362
- & [ & self . payer , account] ,
378
+ & [ account] ,
363
379
)
364
380
. await
365
381
. map ( |_| account. pubkey ( ) )
@@ -753,13 +769,13 @@ where
753
769
& self ,
754
770
sources : & [ & Pubkey ] ,
755
771
) -> TokenResult < T :: Output > {
756
- self . process_ixs (
772
+ self . process_ixs :: < [ & dyn Signer ; 0 ] > (
757
773
& [ transfer_fee:: instruction:: harvest_withheld_tokens_to_mint (
758
774
& self . program_id ,
759
775
& self . pubkey ,
760
776
sources,
761
777
) ?] ,
762
- & [ & self . payer ] ,
778
+ & [ ] ,
763
779
)
764
780
. await
765
781
}
0 commit comments