copy_trading/position_manager/
set_wallet_configs_from_pl_df.rs

1use crate::types::position_config::PositionConfig;
2use crate::types::position_manager::PositionManager;
3use polars::prelude::DataFrame;
4use solana_sdk::pubkey::Pubkey;
5
6/// Set wallet configurations from a Polars DataFrame. Clears existing wallet configs and loads new
7/// configurations from a DataFrame containing wallet addresses and their position configuration
8/// parameters.
9pub fn set_wallet_configs_from_pl_df(position_manager: &mut PositionManager, df: &DataFrame) {
10  // Clear existing wallet configs this datafarme will overwrite everything
11  position_manager.wallet_configs.clear();
12
13  let rows = df.height();
14  // Extract columns from the dataframe, fail with error message if any are missing
15  let wallet_address_col = df
16    .column("wallet_address")
17    .expect("set_wallet_configs_from_pl_df: Missing wallet_address column")
18    .str()
19    .expect("set_wallet_configs_from_pl_df: Column wallet_address is not of expected type str");
20  let buy_amount_lp_col = df
21    .column("buy_amount_lp")
22    .expect("set_wallet_configs_from_pl_df: Missing buy_amount_lp column")
23    .u64()
24    .expect("set_wallet_configs_from_pl_df: Column buy_amount_lp is not of expected type u64");
25  let jito_tip_buy_lp_col = df
26    .column("jito_tip_buy_lp")
27    .expect("set_wallet_configs_from_pl_df: Missing jito_tip_buy_lp column")
28    .u64()
29    .expect("set_wallet_configs_from_pl_df: Column jito_tip_buy_lp is not of expected type u64");
30  let hold_time_ms_col = df
31    .column("hold_time_ms")
32    .expect("set_wallet_configs_from_pl_df: Missing hold_time_ms column")
33    .u32()
34    .expect("set_wallet_configs_from_pl_df: Column hold_time_ms is not of expected type u32");
35  let jito_tip_sell_lp_col = df
36    .column("jito_tip_sell_lp")
37    .expect("set_wallet_configs_from_pl_df: Missing jito_tip_sell_lp column")
38    .u64()
39    .expect("set_wallet_configs_from_pl_df: Column jito_tip_sell_lp is not of expected type u64");
40  let use_stop_loss_col = df
41    .column("use_stop_loss")
42    .expect("set_wallet_configs_from_pl_df: Missing use_stop_loss column")
43    .bool()
44    .expect("set_wallet_configs_from_pl_df: Column use_stop_loss is not of expected type bool");
45  let trailing_stop_loss_col = df
46    .column("trailing_stop_loss")
47    .expect("set_wallet_configs_from_pl_df: Missing trailing_stop_loss column")
48    .bool()
49    .expect(
50      "set_wallet_configs_from_pl_df: Column trailing_stop_loss is not of expected type bool",
51    );
52  let stop_loss_threshold_lp_col = df
53    .column("stop_loss_threshold_lp")
54    .expect("set_wallet_configs_from_pl_df: Missing stop_loss_threshold_lp column")
55    .u64()
56    .expect(
57      "set_wallet_configs_from_pl_df: Column stop_loss_threshold_lp is not of expected type u64",
58    );
59  let slippage_lps_col = df
60    .column("slippage_lps")
61    .expect("set_wallet_configs_from_pl_df: Missing slippage_lps column")
62    .u64()
63    .expect("set_wallet_configs_from_pl_df: Column slippage_lps  is not of expected type u64");
64
65  // Iterate through each row and create PositionConfig instances
66  for i in 0..rows {
67    // Extract wallet_address and convert to Pubkey
68    let wallet_address = wallet_address_col
69      .get(i)
70      .ok_or(format!(
71        "set_wallet_configs_from_pl_df: Missing wallet_address row value at index {}",
72        i
73      ))
74      .unwrap();
75    let wallet_address = Pubkey::from_str_const(wallet_address);
76
77    // Extract all other fields
78    let buy_amount_lp = buy_amount_lp_col.get(i).ok_or("missing row value").unwrap();
79    let jito_tip_buy_lp = jito_tip_buy_lp_col
80      .get(i)
81      .ok_or("missing row value")
82      .unwrap();
83    let hold_time_ms = hold_time_ms_col.get(i).ok_or("missing row value").unwrap();
84    let jito_tip_sell_lp = jito_tip_sell_lp_col
85      .get(i)
86      .ok_or("missing row value")
87      .unwrap();
88    let use_stop_loss = use_stop_loss_col.get(i).ok_or("").unwrap();
89    let trailing_stop_loss = trailing_stop_loss_col
90      .get(i)
91      .ok_or("missing row value")
92      .unwrap();
93    let stop_loss_threshold_lp = stop_loss_threshold_lp_col
94      .get(i)
95      .ok_or("missing row value")
96      .unwrap();
97    let slippage_lps = slippage_lps_col.get(i).ok_or("missing row value").unwrap();
98
99    // Create PositionConfig instance
100    let position_config = PositionConfig {
101      buy_amount_lp,
102      jito_tip_buy_lp,
103      hold_time_ms,
104      jito_tip_sell_lp,
105      use_stop_loss,
106      trailing_stop_loss,
107      stop_loss_threshold_lp,
108      wallet_copying: wallet_address,
109      slippage_lps,
110    };
111
112    // Insert into the HashMap with Arc wrapper
113    position_manager
114      .wallet_configs
115      .insert(wallet_address, position_config);
116  }
117}