@@ -26,7 +26,7 @@ use composite::instruction as composite_instruction;
2626use composite:: { DummyA , DummyB } ;
2727use optional:: account:: { DataAccount , DataPda } ;
2828use std:: ops:: Deref ;
29- use std:: rc :: Rc ;
29+ use std:: sync :: Arc ;
3030use std:: time:: Duration ;
3131use tokio:: sync:: mpsc;
3232use tokio:: time:: sleep;
@@ -43,14 +43,15 @@ pub async fn main() -> Result<()> {
4343 ) ;
4444
4545 // Client.
46- let payer = Rc :: new ( payer) ;
46+ let payer = Arc :: new ( payer) ;
4747 let client =
4848 Client :: new_with_options ( url. clone ( ) , payer. clone ( ) , CommitmentConfig :: processed ( ) ) ;
4949
5050 println ! ( "\n Starting async test..." ) ;
5151 composite ( & client, opts. composite_pid ) . await ?;
5252 basic_2 ( & client, opts. basic_2_pid ) . await ?;
5353 basic_4 ( & client, opts. basic_4_pid ) . await ?;
54+ test_tokio ( client, opts. basic_2_pid ) . await ?;
5455
5556 // Can also use references, since they deref to a signer
5657 let payer: & Keypair = & payer;
@@ -61,6 +62,42 @@ pub async fn main() -> Result<()> {
6162 Ok ( ( ) )
6263}
6364
65+ pub async fn test_tokio ( client : Client < Arc < Keypair > > , pid : Pubkey ) -> Result < ( ) > {
66+ tokio:: spawn ( async move {
67+ let program = client. program ( pid) . unwrap ( ) ;
68+
69+ // `Create` parameters.
70+ let counter = Arc :: new ( Keypair :: new ( ) ) ;
71+ let counter_pubkey = counter. pubkey ( ) ;
72+ let authority = program. payer ( ) ;
73+
74+ // Build and send a transaction.
75+ program
76+ . request ( )
77+ . signer ( counter)
78+ . accounts ( basic_2_accounts:: Create {
79+ counter : counter_pubkey,
80+ user : authority,
81+ system_program : system_program:: ID ,
82+ } )
83+ . args ( basic_2_instruction:: Create { authority } )
84+ . send ( )
85+ . await
86+ . unwrap ( ) ;
87+
88+ let counter_account: Counter = program. account ( counter_pubkey) . await . unwrap ( ) ;
89+
90+ assert_eq ! ( counter_account. authority, authority) ;
91+ assert_eq ! ( counter_account. count, 0 ) ;
92+ } )
93+ . await
94+ . unwrap ( ) ;
95+
96+ println ! ( "Tokio success!" ) ;
97+
98+ Ok ( ( ) )
99+ }
100+
64101pub async fn composite < C : Deref < Target = impl Signer > + Clone > (
65102 client : & Client < C > ,
66103 pid : Pubkey ,
@@ -69,8 +106,8 @@ pub async fn composite<C: Deref<Target = impl Signer> + Clone>(
69106 let program = client. program ( pid) ?;
70107
71108 // `Initialize` parameters.
72- let dummy_a = Keypair :: new ( ) ;
73- let dummy_b = Keypair :: new ( ) ;
109+ let dummy_a = Arc :: new ( Keypair :: new ( ) ) ;
110+ let dummy_b = Arc :: new ( Keypair :: new ( ) ) ;
74111
75112 // Build and send a transaction.
76113 program
@@ -95,8 +132,8 @@ pub async fn composite<C: Deref<Target = impl Signer> + Clone>(
95132 500 ,
96133 & program. id ( ) ,
97134 ) )
98- . signer ( & dummy_a)
99- . signer ( & dummy_b)
135+ . signer ( dummy_a. clone ( ) )
136+ . signer ( dummy_b. clone ( ) )
100137 . accounts ( Initialize {
101138 dummy_a : dummy_a. pubkey ( ) ,
102139 dummy_b : dummy_b. pubkey ( ) ,
@@ -147,13 +184,13 @@ pub async fn basic_2<C: Deref<Target = impl Signer> + Clone>(
147184 let program = client. program ( pid) ?;
148185
149186 // `Create` parameters.
150- let counter = Keypair :: new ( ) ;
187+ let counter = Arc :: new ( Keypair :: new ( ) ) ;
151188 let authority = program. payer ( ) ;
152189
153190 // Build and send a transaction.
154191 program
155192 . request ( )
156- . signer ( & counter)
193+ . signer ( counter. clone ( ) )
157194 . accounts ( basic_2_accounts:: Create {
158195 counter : counter. pubkey ( ) ,
159196 user : authority,
@@ -253,13 +290,13 @@ pub async fn optional<C: Deref<Target = impl Signer> + Clone>(
253290 let program = client. program ( pid) ?;
254291
255292 // `Initialize` parameters.
256- let data_account_keypair = Keypair :: new ( ) ;
293+ let data_account_keypair = Arc :: new ( Keypair :: new ( ) ) ;
257294
258295 let data_account_key = data_account_keypair. pubkey ( ) ;
259296
260297 let data_pda_seeds = & [ DataPda :: PREFIX . as_ref ( ) , data_account_key. as_ref ( ) ] ;
261298 let data_pda_key = Pubkey :: find_program_address ( data_pda_seeds, & pid) . 0 ;
262- let required_keypair = Keypair :: new ( ) ;
299+ let required_keypair = Arc :: new ( Keypair :: new ( ) ) ;
263300 let value: u64 = 10 ;
264301
265302 // Build and send a transaction.
@@ -276,8 +313,8 @@ pub async fn optional<C: Deref<Target = impl Signer> + Clone>(
276313 DataAccount :: LEN as u64 ,
277314 & program. id ( ) ,
278315 ) )
279- . signer ( & data_account_keypair)
280- . signer ( & required_keypair)
316+ . signer ( data_account_keypair. clone ( ) )
317+ . signer ( required_keypair. clone ( ) )
281318 . accounts ( OptionalInitialize {
282319 payer : Some ( program. payer ( ) ) ,
283320 required : required_keypair. pubkey ( ) ,
0 commit comments