From 75c5ec465f26f377f872e390b71e72ceef39d0e8 Mon Sep 17 00:00:00 2001 From: wiseaidev Date: Mon, 3 Jun 2024 10:20:07 +0300 Subject: [PATCH] feat: add retry logic on rpc calls, remove unnecessary deps, fix warnings --- Cargo.lock | 14 +++++++++++++- lib/client/Cargo.toml | 4 +--- lib/client/src/client.rs | 23 +++++++++++++++++------ lib/client/src/jup.rs | 2 +- programs/openbook-v2/Cargo.toml | 2 -- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 644d45a06..5ff274b8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -701,6 +701,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "backon" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d67782c3f868daa71d3533538e98a8e13713231969def7536e8039606fc46bf0" +dependencies = [ + "fastrand", + "futures-core", + "pin-project", + "tokio", +] + [[package]] name = "backtrace" version = "0.3.71" @@ -3572,8 +3584,8 @@ dependencies = [ "async-channel", "async-once-cell", "async-trait", + "backon", "base64 0.13.1", - "bincode", "fixed", "futures 0.3.29", "itertools", diff --git a/lib/client/Cargo.toml b/lib/client/Cargo.toml index 861cc7cb4..85d80cfcd 100644 --- a/lib/client/Cargo.toml +++ b/lib/client/Cargo.toml @@ -1,5 +1,3 @@ -cargo-features = ["workspace-inheritance"] - [package] edition = "2021" name = "openbook-v2-client" @@ -21,7 +19,6 @@ async-channel = "1.6" async-once-cell = {version = "0.4.2", features = ["unpin"]} async-trait = "0.1.52" base64 = "0.13.0" -bincode = "1.3.3" fixed = {workspace = true, features = ["serde", "borsh"]} futures = "0.3.25" itertools = "0.10.3" @@ -45,6 +42,7 @@ thiserror = "1.0.31" tokio = {version = "1", features = ["full"]} tokio-stream = {version = "0.1.9"} jupiter-amm-interface = "0.1.1" +backon = "0.4.4" [dev-dependencies] solana-program-test = { workspace = true } diff --git a/lib/client/src/client.rs b/lib/client/src/client.rs index b505a2e8e..c8c5b58a0 100644 --- a/lib/client/src/client.rs +++ b/lib/client/src/client.rs @@ -33,6 +33,9 @@ use solana_sdk::instruction::Instruction; use solana_sdk::signature::{Keypair, Signature}; use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey, signer::Signer}; +use backon::ExponentialBuilder; +use backon::Retryable; + // very close to anchor_client::Client, which unfortunately has no accessors or Clone #[derive(Clone, Debug)] pub struct Client { @@ -744,18 +747,26 @@ impl<'a> TransactionBuilder<'a> { pub async fn send(self, client: &Client) -> anyhow::Result { let rpc = client.rpc_async(); let tx = self.transaction(&rpc).await?; - rpc.send_transaction_with_config(&tx, client.rpc_send_transaction_config) - .await - .map_err(prettify_solana_client_error) + (|| async { + rpc.send_transaction_with_config(&tx, client.rpc_send_transaction_config) + .await + .map_err(prettify_solana_client_error) + }) + .retry(&ExponentialBuilder::default()) + .await } pub async fn send_and_confirm(self, client: &Client) -> anyhow::Result { let rpc = client.rpc_async(); let tx = self.transaction(&rpc).await?; // TODO: Wish we could use client.rpc_send_transaction_config here too! - rpc.send_and_confirm_transaction(&tx) - .await - .map_err(prettify_solana_client_error) + (|| async { + rpc.send_and_confirm_transaction(&tx) + .await + .map_err(prettify_solana_client_error) + }) + .retry(&ExponentialBuilder::default()) + .await } } diff --git a/lib/client/src/jup.rs b/lib/client/src/jup.rs index 99efc53da..9ce202c37 100644 --- a/lib/client/src/jup.rs +++ b/lib/client/src/jup.rs @@ -108,7 +108,7 @@ impl Amm for OpenBookMarket { EventHeap::try_deserialize_from_slice(&mut event_heap_data.data.as_slice()).unwrap(); let clock_data = account_map.get(&clock::ID).unwrap(); - let clock: Clock = bincode::deserialize(clock_data.data.as_slice())?; + let clock: Clock = serde_json::from_slice(clock_data.data.as_slice())?; let oracle_acc = |nonzero_pubkey: NonZeroPubkeyOption| -> Option { diff --git a/programs/openbook-v2/Cargo.toml b/programs/openbook-v2/Cargo.toml index f81f38de2..362014ae8 100644 --- a/programs/openbook-v2/Cargo.toml +++ b/programs/openbook-v2/Cargo.toml @@ -1,5 +1,3 @@ -cargo-features = ["workspace-inheritance"] - [package] description = "Created with Anchor" edition = "2021"