From bfb4a03b888880ed54e66eba5aa3496d17921582 Mon Sep 17 00:00:00 2001 From: Valentyn Faychuk Date: Fri, 10 Jan 2025 05:06:27 +0200 Subject: [PATCH] correct token creation retries --- src/solana.rs | 56 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/solana.rs b/src/solana.rs index 9e540d4..c8be706 100644 --- a/src/solana.rs +++ b/src/solana.rs @@ -127,8 +127,8 @@ impl TryFrom for SolClient { } } -async fn wait_for_sol(client: &RpcClient, pubkey: &Pubkey) { - println!("Waiting to receive 0.01 SOL in address {pubkey}"); +async fn wait_for_enough_sol(client: &RpcClient, pubkey: &Pubkey) { + println!("Waiting for at least 0.01 SOL in address {pubkey}"); loop { match client.get_balance(pubkey).await { Ok(balance) => { @@ -139,18 +139,33 @@ async fn wait_for_sol(client: &RpcClient, pubkey: &Pubkey) { } Err(e) => { println!("Could not get balance: {e:?}"); - }, + } } sleep(Duration::from_secs(30)).await; } } 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 { let mint_keypair = Keypair::new(); 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( &payer.pubkey(), @@ -162,9 +177,14 @@ async fn create_token(client: &RpcClient, keypair: &Keypair) -> Pubkey { let init_mint_ix = 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( &[create_mint_account_ix, init_mint_ix], Some(&payer.pubkey()), @@ -172,17 +192,13 @@ async fn create_token(client: &RpcClient, keypair: &Keypair) -> Pubkey { recent_blockhash, ); - loop { - match client.send_and_confirm_transaction(&tx).await { - Ok(signature) => { - println!("Mint created: {}", mint_keypair.pubkey()); - println!("Transaction signature: {}", signature); - return mint_keypair.pubkey(); - }, - Err(e) => { - println!("Could not create mint: {e}"); - sleep(Duration::from_secs(30)).await; - }, - } - } + client + .send_and_confirm_transaction(&tx) + .await + .map_err(|e| println!("Can't execute transaction: {e}")) + .map(|s| { + println!("Transaction signature: {}", s); + mint_keypair.pubkey() + }) + .ok() }