From fa7dae78c77351ece1459c97591375a4e9b37d6f Mon Sep 17 00:00:00 2001 From: soundsonacid Date: Thu, 21 Mar 2024 14:04:05 -0500 Subject: [PATCH] clippy --- src/dlob/dlob.rs | 13 ++++++------- src/dlob/dlob_builder.rs | 7 ++----- src/dlob/dlob_node.rs | 2 +- src/dlob/market.rs | 8 +++----- src/dlob/order_list.rs | 10 +++++----- src/lib.rs | 8 ++++---- src/marketmap.rs | 2 +- src/oraclemap.rs | 19 ++++++++----------- src/usermap.rs | 6 +++--- src/websocket_account_subscriber.rs | 12 +++++------- 10 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/dlob/dlob.rs b/src/dlob/dlob.rs index 0689b16..ad55014 100644 --- a/src/dlob/dlob.rs +++ b/src/dlob/dlob.rs @@ -4,8 +4,7 @@ use drift::state::user::{MarketType, Order}; use rayon::prelude::*; use solana_sdk::pubkey::Pubkey; use std::any::Any; -use std::cell::Cell; -use std::collections::{BinaryHeap, HashMap}; +use std::collections::BinaryHeap; use std::str::FromStr; use std::sync::Arc; @@ -99,14 +98,14 @@ impl DLOB { .get_mut(&market_index) .expect(format!("Market index {} not found", market_index).as_str()); - let (order_list, subtype, node_type) = market.get_info_for_order_insert(&order, slot); + let (order_list, subtype, node_type) = market.get_info_for_order_insert(order, slot); - let node = create_node(node_type, order.clone(), user_account); + let node = create_node(node_type, *order, user_account); if let Some(order_list) = order_list { match subtype { - SubType::Bid => order_list.insert_bid(node.clone()), - SubType::Ask => order_list.insert_ask(node.clone()), + SubType::Bid => order_list.insert_bid(node), + SubType::Ask => order_list.insert_ask(node), _ => {} } } else { @@ -118,7 +117,7 @@ impl DLOB { let order_signature = get_order_signature(order_id, user_account); for order_list in get_order_lists(&self.exchange) { if let Some(node) = order_list.get_node(&order_signature) { - return Some(node.get_order().clone()); + return Some(*node.get_order()); } } diff --git a/src/dlob/dlob_builder.rs b/src/dlob/dlob_builder.rs index bbde4ec..3104acf 100644 --- a/src/dlob/dlob_builder.rs +++ b/src/dlob/dlob_builder.rs @@ -1,9 +1,6 @@ use crate::{ - dlob::dlob::DLOB, - event_emitter::{Event, EventEmitter}, - slot_subscriber::SlotSubscriber, - usermap::UserMap, - SdkResult, + dlob::dlob::DLOB, event_emitter::EventEmitter, slot_subscriber::SlotSubscriber, + usermap::UserMap, SdkResult, }; pub struct DLOBBuilder { diff --git a/src/dlob/dlob_node.rs b/src/dlob/dlob_node.rs index a3219d8..8a2972e 100644 --- a/src/dlob/dlob_node.rs +++ b/src/dlob/dlob_node.rs @@ -298,7 +298,7 @@ pub(crate) fn create_node(node_type: NodeType, order: Order, user_account: Pubke } pub(crate) fn get_order_signature(order_id: u32, user_account: Pubkey) -> String { - format!("{}-{}", order_id, user_account.to_string()) + format!("{}-{}", order_id, user_account) } #[cfg(test)] diff --git a/src/dlob/market.rs b/src/dlob/market.rs index ce1b759..0d5c409 100644 --- a/src/dlob/market.rs +++ b/src/dlob/market.rs @@ -61,12 +61,10 @@ impl Market { NodeType::Market } else if order.oracle_price_offset != 0 { NodeType::FloatingLimit + } else if is_resting_limit_order(order, slot) { + NodeType::RestingLimit } else { - if is_resting_limit_order(order, slot) { - NodeType::RestingLimit - } else { - NodeType::TakingLimit - } + NodeType::TakingLimit }; let order_list = match node_type { diff --git a/src/dlob/order_list.rs b/src/dlob/order_list.rs index 921af38..5a5c48e 100644 --- a/src/dlob/order_list.rs +++ b/src/dlob/order_list.rs @@ -26,20 +26,20 @@ impl Orderlist { pub fn insert_bid(&mut self, node: Node) { let order_sig = get_order_signature(node.get_order().order_id, node.get_user_account()); - self.order_sigs.insert(order_sig.clone(), node.clone()); + self.order_sigs.insert(order_sig.clone(), node); let directional = DirectionalNode::new(node, self.bid_sort_direction); self.bids.push(directional); } pub fn insert_ask(&mut self, node: Node) { let order_sig = get_order_signature(node.get_order().order_id, node.get_user_account()); - self.order_sigs.insert(order_sig.clone(), node.clone()); + self.order_sigs.insert(order_sig.clone(), node); let directional = DirectionalNode::new(node, self.ask_sort_direction); self.asks.push(directional); } pub fn get_best_bid(&mut self) -> Option { - if let Some(node) = self.bids.pop().map(|node| node.node.clone()) { + if let Some(node) = self.bids.pop().map(|node| node.node) { let order_sig = get_order_signature(node.get_order().order_id, node.get_user_account()); if self.order_sigs.contains_key(&order_sig) { self.order_sigs.remove(&order_sig); @@ -50,7 +50,7 @@ impl Orderlist { } pub fn get_best_ask(&mut self) -> Option { - if let Some(node) = self.asks.pop().map(|node| node.node.clone()) { + if let Some(node) = self.asks.pop().map(|node| node.node) { let order_sig = get_order_signature(node.get_order().order_id, node.get_user_account()); if self.order_sigs.contains_key(&order_sig) { self.order_sigs.remove(&order_sig); @@ -61,7 +61,7 @@ impl Orderlist { } pub fn get_node(&self, order_sig: &String) -> Option { - self.order_sigs.get(order_sig).map(|node| node.clone()) + self.order_sigs.get(order_sig).map(|node| *node) } pub fn bids_empty(&self) -> bool { diff --git a/src/lib.rs b/src/lib.rs index 2bd4e15..9478757 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ //! Drift SDK -use std::{borrow::Cow, sync::Arc, time::Duration}; +use std::{borrow::Cow, rc::Rc, sync::Arc, time::Duration}; use anchor_lang::{AccountDeserialize, Discriminator, InstructionData, ToAccountMetas}; use async_utils::{retry_policy, spawn_retry_task}; @@ -668,7 +668,7 @@ pub struct DriftClientBackend { program_data: ProgramData, perp_market_map: MarketMap, spot_market_map: MarketMap, - oracle_map: Arc, + oracle_map: Rc, } impl DriftClientBackend { @@ -709,7 +709,7 @@ impl DriftClientBackend { program_data: ProgramData::uninitialized(), perp_market_map, spot_market_map, - oracle_map: Arc::new(oracle_map), + oracle_map: Rc::new(oracle_map), }; let lookup_table_address = market_lookup_table(context); @@ -1831,7 +1831,7 @@ mod tests { program_data: ProgramData::uninitialized(), perp_market_map, spot_market_map, - oracle_map: Arc::new(OracleMap::new( + oracle_map: Rc::new(OracleMap::new( CommitmentConfig::processed(), DEVNET_ENDPOINT.to_string(), true, diff --git a/src/marketmap.rs b/src/marketmap.rs index 2ad084d..9fc9034 100644 --- a/src/marketmap.rs +++ b/src/marketmap.rs @@ -99,7 +99,7 @@ impl MarketMap SdkResult<()> { - if let Some(_) = self.sync_lock { + if self.sync_lock.is_some() { self.sync().await?; } diff --git a/src/oraclemap.rs b/src/oraclemap.rs index d7dbda1..9685b6e 100644 --- a/src/oraclemap.rs +++ b/src/oraclemap.rs @@ -90,7 +90,7 @@ impl OracleMap { } pub async fn subscribe(&self) -> SdkResult<()> { - if let Some(_) = self.sync_lock { + if self.sync_lock.is_some() { self.sync().await?; } @@ -139,7 +139,7 @@ impl OracleMap { update.data.rent_epoch, ); match get_oracle_price( - &oracle_source.value(), + oracle_source.value(), &oracle_account_info, update.slot, ) { @@ -235,8 +235,8 @@ impl OracleMap { for (i, account) in response.value.iter().enumerate() { if let Some(oracle_account) = account { - let oracle_info = oracle_infos[i].clone(); - let oracle_pubkey = oracle_info.0.clone(); + let oracle_info = oracle_infos[i]; + let oracle_pubkey = oracle_info.0; let mut oracle_components = (oracle_pubkey, oracle_account.clone()); let account_info = oracle_components.into_account_info(); let price_data = get_oracle_price(&oracle_info.1, &account_info, slot) @@ -267,22 +267,19 @@ impl OracleMap { } pub fn current_perp_oracle(&self, market_index: u16) -> Option { - self.perp_oracles.get(&market_index).map(|x| x.clone()) + self.perp_oracles.get(&market_index).map(|x| *x) } pub fn current_spot_oracle(&self, market_index: u16) -> Option { - self.spot_oracles.get(&market_index).map(|x| x.clone()) + self.spot_oracles.get(&market_index).map(|x| *x) } pub fn get(&self, key: &str) -> Option { - self.oraclemap.get(key).map(|v| v.clone()) + self.oraclemap.get(key).map(|v| *v) } pub fn values(&self) -> Vec { - self.oraclemap - .iter() - .map(|x| x.value().data.clone()) - .collect() + self.oraclemap.iter().map(|x| x.value().data).collect() } pub async fn add_oracle(&self, oracle: Pubkey, source: OracleSource) -> SdkResult<()> { diff --git a/src/usermap.rs b/src/usermap.rs index cd399a6..1677408 100644 --- a/src/usermap.rs +++ b/src/usermap.rs @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use crate::event_emitter::EventEmitter; -use crate::memcmp::{get_non_idle_user_filter, get_user_filter, get_user_with_order_filter}; +use crate::memcmp::{get_non_idle_user_filter, get_user_filter}; use crate::utils::{decode, get_ws_url}; use crate::websocket_program_account_subscriber::{ ProgramAccountUpdate, WebsocketProgramAccountOptions, WebsocketProgramAccountSubscriber, @@ -71,7 +71,7 @@ impl UserMap { } pub async fn subscribe(&mut self) -> SdkResult<()> { - if let Some(_) = self.sync_lock { + if self.sync_lock.is_some() { self.sync().await?; } @@ -130,7 +130,7 @@ impl UserMap { .get_account_data(&Pubkey::from_str(pubkey).unwrap()) .await?; let user = User::try_deserialize(&mut user_data.as_slice()).unwrap(); - self.usermap.insert(pubkey.to_string(), user.clone()); + self.usermap.insert(pubkey.to_string(), user); Ok(self.get(pubkey).unwrap()) } } diff --git a/src/websocket_account_subscriber.rs b/src/websocket_account_subscriber.rs index 7dd6483..1bf5db6 100644 --- a/src/websocket_account_subscriber.rs +++ b/src/websocket_account_subscriber.rs @@ -111,13 +111,11 @@ impl WebsocketAccountSubscriber { } } unsub = unsub_rx.recv() => { - match unsub { - Some(_) => { - log::debug!("unsubscribing"); - account_unsubscribe().await; - break; - } - None => {} + if let Some(_) = unsub { + log::debug!("{}: Unsubscribing from account stream", susbscription_name); + account_unsubscribe().await; + break; + } } }