correct token creation retries
This commit is contained in:
		
							parent
							
								
									1a897d780f
								
							
						
					
					
						commit
						bfb4a03b88
					
				| @ -127,8 +127,8 @@ impl TryFrom<KeysFile> for SolClient { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async fn wait_for_sol(client: &RpcClient, pubkey: &Pubkey) { | async fn wait_for_enough_sol(client: &RpcClient, pubkey: &Pubkey) { | ||||||
|     println!("Waiting to receive 0.01 SOL in address {pubkey}"); |     println!("Waiting for at least 0.01 SOL in address {pubkey}"); | ||||||
|     loop { |     loop { | ||||||
|         match client.get_balance(pubkey).await { |         match client.get_balance(pubkey).await { | ||||||
|             Ok(balance) => { |             Ok(balance) => { | ||||||
| @ -139,18 +139,33 @@ async fn wait_for_sol(client: &RpcClient, pubkey: &Pubkey) { | |||||||
|             } |             } | ||||||
|             Err(e) => { |             Err(e) => { | ||||||
|                 println!("Could not get balance: {e:?}"); |                 println!("Could not get balance: {e:?}"); | ||||||
|             }, |             } | ||||||
|         } |         } | ||||||
|         sleep(Duration::from_secs(30)).await; |         sleep(Duration::from_secs(30)).await; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async fn create_token(client: &RpcClient, keypair: &Keypair) -> Pubkey { | async fn create_token(client: &RpcClient, keypair: &Keypair) -> Pubkey { | ||||||
|     wait_for_sol(client, &keypair.pubkey()).await; |     loop { | ||||||
|  |         wait_for_enough_sol(client, &keypair.pubkey()).await; | ||||||
|  |         match create_token_int(client, keypair).await { | ||||||
|  |             None => sleep(Duration::from_secs(30)).await, | ||||||
|  |             Some(pubkey) => { | ||||||
|  |                 println!("Mint created: {pubkey}"); | ||||||
|  |                 return pubkey; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|  | async fn create_token_int(client: &RpcClient, keypair: &Keypair) -> Option<Pubkey> { | ||||||
|     let mint_keypair = Keypair::new(); |     let mint_keypair = Keypair::new(); | ||||||
|     let payer = Keypair::from_base58_string(&keypair.to_base58_string()); |     let payer = Keypair::from_base58_string(&keypair.to_base58_string()); | ||||||
|     let mint_rent = client.get_minimum_balance_for_rent_exemption(Mint::LEN).await.unwrap(); |     let mint_rent = client | ||||||
|  |         .get_minimum_balance_for_rent_exemption(Mint::LEN) | ||||||
|  |         .await | ||||||
|  |         .map_err(|e| println!("Can't get minimum balance for rent exemption: {e}")) | ||||||
|  |         .ok()?; | ||||||
| 
 | 
 | ||||||
|     let create_mint_account_ix = system_instruction::create_account( |     let create_mint_account_ix = system_instruction::create_account( | ||||||
|         &payer.pubkey(), |         &payer.pubkey(), | ||||||
| @ -162,9 +177,14 @@ async fn create_token(client: &RpcClient, keypair: &Keypair) -> Pubkey { | |||||||
| 
 | 
 | ||||||
|     let init_mint_ix = |     let init_mint_ix = | ||||||
|         initialize_mint(&spl_token::id(), &mint_keypair.pubkey(), &payer.pubkey(), None, 9) |         initialize_mint(&spl_token::id(), &mint_keypair.pubkey(), &payer.pubkey(), None, 9) | ||||||
|             .unwrap(); |             .map_err(|e| println!("Can't initialize mint: {e}")) | ||||||
|  |             .ok()?; | ||||||
| 
 | 
 | ||||||
|     let recent_blockhash = client.get_latest_blockhash().await.unwrap(); |     let recent_blockhash = client | ||||||
|  |         .get_latest_blockhash() | ||||||
|  |         .await | ||||||
|  |         .map_err(|e| println!("Can't get latest blockhash: {e}")) | ||||||
|  |         .ok()?; | ||||||
|     let tx = Transaction::new_signed_with_payer( |     let tx = Transaction::new_signed_with_payer( | ||||||
|         &[create_mint_account_ix, init_mint_ix], |         &[create_mint_account_ix, init_mint_ix], | ||||||
|         Some(&payer.pubkey()), |         Some(&payer.pubkey()), | ||||||
| @ -172,17 +192,13 @@ async fn create_token(client: &RpcClient, keypair: &Keypair) -> Pubkey { | |||||||
|         recent_blockhash, |         recent_blockhash, | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     loop { |     client | ||||||
|         match client.send_and_confirm_transaction(&tx).await { |         .send_and_confirm_transaction(&tx) | ||||||
|             Ok(signature) => { |         .await | ||||||
|                 println!("Mint created: {}", mint_keypair.pubkey()); |         .map_err(|e| println!("Can't execute transaction: {e}")) | ||||||
|                 println!("Transaction signature: {}", signature); |         .map(|s| { | ||||||
|                 return mint_keypair.pubkey(); |             println!("Transaction signature: {}", s); | ||||||
|             }, |             mint_keypair.pubkey() | ||||||
|             Err(e) => { |         }) | ||||||
|                 println!("Could not create mint: {e}"); |         .ok() | ||||||
|                 sleep(Duration::from_secs(30)).await; |  | ||||||
|             }, |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user