correct token creation retries

This commit is contained in:
Valentyn Faychuk 2025-01-10 05:06:27 +02:00
parent 1a897d780f
commit bfb4a03b88
Signed by: valy
GPG Key ID: F1AB995E20FEADC5

@ -127,8 +127,8 @@ impl TryFrom<KeysFile> 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<Pubkey> {
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()
}