From 720d35df822d02fb4142cb57c8b7a3ad0899652c Mon Sep 17 00:00:00 2001 From: liyukun Date: Fri, 17 Nov 2023 11:32:01 +0800 Subject: [PATCH] feat: consume-ack retry --- examples/sudt-transfer.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/examples/sudt-transfer.rs b/examples/sudt-transfer.rs index 5624384..9dd5aed 100644 --- a/examples/sudt-transfer.rs +++ b/examples/sudt-transfer.rs @@ -136,7 +136,7 @@ async fn main() -> Result<()> { Commands::CreateStCell { sudt } => { create_st_cell(config, sk, sender_lock_script, sudt).await } - Commands::ConsumeAck => consume_ack(&config, sk, sender_lock_script).await, + Commands::ConsumeAck => consume_ack_retry(&config, sk, sender_lock_script).await, Commands::Send { sudt, receiver, @@ -145,6 +145,30 @@ async fn main() -> Result<()> { } } +async fn consume_ack_retry( + config: &Config, + sk: secp256k1::SecretKey, + sender_lock_script: packed::Script, +) -> Result<()> { + loop { + match consume_ack(&config, sk, sender_lock_script.clone()).await { + Ok(()) => return Ok(()), + Err(e) => { + if let Some(re) = e.downcast_ref::() { + match re { + ckb_sdk::RpcError::Rpc(re) if should_retry_code(re.code.code()) => { + println!("Will retry, error: {e:#}"); + tokio::time::sleep(Duration::from_secs(5)).await; + continue; + } + _ => return Err(e), + } + } + } + } + } +} + async fn consume_ack( config: &Config, sk: secp256k1::SecretKey,