@@ -65,40 +65,70 @@ async fn main() -> Result<(), anyhow::Error> {
65
65
66
66
// Try different approaches to match what the faucet expects
67
67
let client = reqwest:: Client :: new ( ) ;
68
+ let pub_key_hex = hex:: encode ( sender. public_key ( ) . to_bytes ( ) ) ;
68
69
69
- // First try GET with query parameters (some faucets prefer this)
70
+ // First try POST with pub_key (preferred)
71
+ let mut funded = false ;
70
72
let response = client
71
- . get ( & format ! ( "{}/mint" , faucet_url) )
72
- . query ( & [
73
- ( "address " , sender . address ( ) . to_string ( ) ) ,
73
+ . post ( & format ! ( "{}/mint" , faucet_url) )
74
+ . form ( & [
75
+ ( "pub_key " , pub_key_hex . clone ( ) ) ,
74
76
( "amount" , "1000000" . to_string ( ) ) ,
77
+ ( "return_txns" , "true" . to_string ( ) ) ,
75
78
] )
76
79
. send ( )
77
80
. await
78
- . context ( "Failed to send faucet GET request" ) ?;
79
-
81
+ . context ( "Failed to send faucet POST request with pub_key" ) ?;
80
82
let status = response. status ( ) ;
81
- if !status. is_success ( ) {
82
- // If GET fails, try POST with form data
83
- println ! ( "GET request failed with status {}, trying POST with form data..." , status) ;
84
-
83
+ if status. is_success ( ) {
84
+ funded = true ;
85
+ } else {
86
+ let error_text = response. text ( ) . await . unwrap_or_default ( ) ;
87
+ println ! ( "[WARN] Faucet POST(pub_key) failed {}: {}" , status, error_text) ;
88
+ // Try GET with address as fallback
85
89
let response = client
86
- . post ( & format ! ( "{}/mint" , faucet_url) )
87
- . form ( & [
90
+ . get ( & format ! ( "{}/mint" , faucet_url) )
91
+ . query ( & [
88
92
( "address" , sender. address ( ) . to_string ( ) ) ,
89
93
( "amount" , "1000000" . to_string ( ) ) ,
94
+ ( "return_txns" , "true" . to_string ( ) ) ,
90
95
] )
91
96
. send ( )
92
97
. await
93
- . context ( "Failed to send faucet POST request" ) ?;
94
-
98
+ . context ( "Failed to send faucet GET request with address" ) ?;
95
99
let status = response. status ( ) ;
96
- if !status. is_success ( ) {
100
+ if status. is_success ( ) {
101
+ funded = true ;
102
+ } else {
97
103
let error_text = response. text ( ) . await . unwrap_or_default ( ) ;
98
104
return Err ( anyhow:: anyhow!( "Faucet request failed with status {}: {}" , status, error_text) ) ;
99
105
}
100
106
}
101
107
108
+ if funded {
109
+ println ! ( "Sender account funded request accepted by faucet" ) ;
110
+ }
111
+
112
+ // Wait until the account exists on-chain (poll a few times)
113
+ let mut created = false ;
114
+ for attempt in 1 ..=10 {
115
+ match rest_client. get_account ( sender. address ( ) ) . await {
116
+ Ok ( account_info) => {
117
+ println ! ( "Account now exists on-chain with sequence {} (attempt {})" , account_info. inner( ) . sequence_number, attempt) ;
118
+ created = true ;
119
+ break ;
120
+ }
121
+ Err ( _) => {
122
+ println ! ( "Waiting for account creation on-chain... attempt {}" , attempt) ;
123
+ tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( 500 ) ) . await ;
124
+ }
125
+ }
126
+ }
127
+
128
+ if !created {
129
+ return Err ( anyhow:: anyhow!( "Sender account not found on-chain after faucet funding" ) ) ;
130
+ }
131
+
102
132
println ! ( "Sender account funded successfully via faucet" ) ;
103
133
104
134
// Create the beneficiary account (just create, no funding needed for this test)
@@ -241,10 +271,41 @@ async fn main() -> Result<(), anyhow::Error> {
241
271
// Execute test transaction
242
272
println ! ( "Executing test transaction..." ) ;
243
273
244
- let test_txn = coin_client
245
- . transfer ( & mut sender, beneficiary. address ( ) , 1_000 , None )
246
- . await
247
- . context ( "Failed to submit test transaction" ) ?;
274
+ // Debug: Check account sequence number and other details
275
+ println ! ( "Sender account details:" ) ;
276
+ println ! ( " Address: {}" , sender. address( ) ) ;
277
+ println ! ( " Sequence number: {}" , sender. sequence_number( ) ) ;
278
+ println ! ( " Public key: {:?}" , sender. public_key( ) ) ;
279
+
280
+ // Try to get account info from the chain
281
+ match rest_client. get_account ( sender. address ( ) ) . await {
282
+ Ok ( account_info) => {
283
+ println ! ( " On-chain sequence number: {}" , account_info. inner( ) . sequence_number) ;
284
+ println ! ( " On-chain authentication key: {:?}" , account_info. inner( ) . authentication_key) ;
285
+ }
286
+ Err ( e) => {
287
+ println ! ( " [WARN] Could not get on-chain account info: {}" , e) ;
288
+ }
289
+ }
290
+
291
+ println ! ( "Beneficiary address: {}" , beneficiary. address( ) ) ;
292
+
293
+ // Try the transaction with better error handling
294
+ let test_txn = match coin_client. transfer ( & mut sender, beneficiary. address ( ) , 1_000 , None ) . await {
295
+ Ok ( txn) => {
296
+ println ! ( "Transaction submitted successfully with hash: {:?}" , txn) ;
297
+ txn
298
+ }
299
+ Err ( e) => {
300
+ println ! ( "[ERROR] Transaction submission failed: {}" , e) ;
301
+ println ! ( "[DEBUG] This might be due to:" ) ;
302
+ println ! ( " - Account not properly funded" ) ;
303
+ println ! ( " - Sequence number mismatch" ) ;
304
+ println ! ( " - Network connectivity issues" ) ;
305
+ println ! ( " - Gas price/limit issues" ) ;
306
+ return Err ( e. context ( "Failed to submit test transaction" ) ) ;
307
+ }
308
+ } ;
248
309
249
310
rest_client
250
311
. wait_for_transaction ( & test_txn)
0 commit comments