Skip to content

Commit

Permalink
feat: add retry logic on rpc calls, remove unnecessary deps, fix warn…
Browse files Browse the repository at this point in the history
…ings, upgrade to rust 1.75+
  • Loading branch information
wiseaidev committed Jun 3, 2024
1 parent c0cf159 commit 92a1991
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions lib/client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cargo-features = ["workspace-inheritance"]

[package]
edition = "2021"
name = "openbook-v2-client"
Expand All @@ -19,9 +17,7 @@ anchor-spl = {workspace = true}
anyhow = "1.0"
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"
Expand All @@ -45,6 +41,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 }
5 changes: 2 additions & 3 deletions lib/client/src/account_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(async_fn_in_trait)]

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
Expand All @@ -15,7 +17,6 @@ use solana_sdk::pubkey::Pubkey;

use openbook_v2::state::OpenOrdersAccount;

#[async_trait::async_trait]
pub trait AccountFetcher: Sync + Send {
async fn fetch_raw_account(&self, address: &Pubkey) -> anyhow::Result<AccountSharedData>;
async fn fetch_raw_account_lookup_table(
Expand Down Expand Up @@ -57,7 +58,6 @@ pub struct RpcAccountFetcher {
pub rpc: RpcClientAsync,
}

#[async_trait::async_trait]
impl AccountFetcher for RpcAccountFetcher {
async fn fetch_raw_account(&self, address: &Pubkey) -> anyhow::Result<AccountSharedData> {
self.rpc
Expand Down Expand Up @@ -178,7 +178,6 @@ impl<T: AccountFetcher> CachedAccountFetcher<T> {
}
}

#[async_trait::async_trait]
impl<T: AccountFetcher + 'static> AccountFetcher for CachedAccountFetcher<T> {
#[allow(clippy::clone_on_copy)]
async fn fetch_raw_account(&self, address: &Pubkey) -> anyhow::Result<AccountSharedData> {
Expand Down
3 changes: 2 additions & 1 deletion lib/client/src/chain_data_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(async_fn_in_trait)]

use std::sync::{Arc, RwLock};
use std::thread;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -142,7 +144,6 @@ impl AccountFetcher {
}
}

#[async_trait::async_trait]
impl crate::AccountFetcher for AccountFetcher {
async fn fetch_raw_account(
&self,
Expand Down
23 changes: 17 additions & 6 deletions lib/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -744,18 +747,26 @@ impl<'a> TransactionBuilder<'a> {
pub async fn send(self, client: &Client) -> anyhow::Result<Signature> {
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<Signature> {
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
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/client/src/jup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<accounts_zerocopy::KeyedAccount> {
Expand Down
2 changes: 0 additions & 2 deletions programs/openbook-v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cargo-features = ["workspace-inheritance"]

[package]
description = "Created with Anchor"
edition = "2021"
Expand Down

0 comments on commit 92a1991

Please sign in to comment.