copy_trading/types/position.rs
1use crate::types::position_close_reason::PositionCloseReason;
2use crate::types::position_config::PositionConfig;
3use crate::types::position_status::PositionStatus;
4use solana_central::PoolTrait;
5use solana_sdk::pubkey::Pubkey;
6use solana_sdk::signature::Keypair;
7use std::sync::{Arc, RwLock};
8
9/// Represents a single copy trading position tracking a token purchase from a monitored wallet.
10/// Contains all state for a position including entry/exit prices, timestamps, stop-loss settings,
11/// and lifecycle status. Positions are managed through a state machine with transitions:
12/// Created -> Opening -> Open -> Closing -> Closed.
13pub struct Position {
14 pub config: PositionConfig,
15 pub token_address: Pubkey,
16 pub pool: Arc<RwLock<dyn PoolTrait>>,
17 pub status: PositionStatus,
18 pub migrated: bool,
19 pub pool_address: Pubkey,
20 // Unix timestamp in ms of position open
21 pub open_time: u64,
22 pub buy_confirmation_time: u64,
23 // Unix timestamp in ms of position close
24 pub close_time: u64,
25 // The reason why the position was closed
26 pub close_reason: PositionCloseReason,
27 pub sell_confirmation_time: u64,
28 /*
29 Entry price in lamports per token. Will ALWAYS be in sol/token regardless of whether or not SOL
30 is token A or B. It is recorded as the execution price of the actual trade and is done by doing
31 swapped sol amount in / swapped token amount received for buys.
32 */
33 pub entry_price_lp: u128,
34 // Execution price at which position was closed
35 pub exit_price_lp: u128,
36 /*
37 The amount of the other token in the token's units that was purchased.
38 */
39 pub tokens_purchased: u64,
40 pub wallet: Arc<Keypair>,
41 // Is sol the token A? If this boolean is false, then sol is token B
42 pub sol_token_a: bool,
43 /*
44 The base stop loss price in lamports per token. Also will always be in sol/token. This is what
45 the current market price is compared to to determine if we should close the position due to
46 exceeding the loss threshold.
47 */
48 pub stop_loss_price_lp: u128,
49 /*
50 The lowest acceptable price in lamports per token. This is the price that we will close the
51 position at if the market price drops below this price. It is updated along with the stop loss
52 price itself.
53 */
54 pub lowest_acceptable_price_lp: u128,
55 /*
56 The amount of sol received from the sell in lamports.
57 */
58 pub sell_amount_lp: u64,
59}