orca_sol/accounts/
position.rs1use crate::types::PositionRewardInfo;
9use borsh::BorshDeserialize;
10use borsh::BorshSerialize;
11use solana_pubkey::Pubkey;
12
13#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct Position {
16 pub discriminator: [u8; 8],
17 #[cfg_attr(
18 feature = "serde",
19 serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
20 )]
21 pub whirlpool: Pubkey,
22 #[cfg_attr(
23 feature = "serde",
24 serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
25 )]
26 pub position_mint: Pubkey,
27 pub liquidity: u128,
28 pub tick_lower_index: i32,
29 pub tick_upper_index: i32,
30 pub fee_growth_checkpoint_a: u128,
31 pub fee_owed_a: u64,
32 pub fee_growth_checkpoint_b: u128,
33 pub fee_owed_b: u64,
34 pub reward_infos: [PositionRewardInfo; 3],
35}
36
37impl Position {
38 pub const LEN: usize = 216;
39
40 #[inline(always)]
41 pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
42 let mut data = data;
43 Self::deserialize(&mut data)
44 }
45}
46
47impl<'a> TryFrom<&solana_account_info::AccountInfo<'a>> for Position {
48 type Error = std::io::Error;
49
50 fn try_from(account_info: &solana_account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
51 let mut data: &[u8] = &(*account_info.data).borrow();
52 Self::deserialize(&mut data)
53 }
54}
55
56#[cfg(feature = "fetch")]
57pub fn fetch_position(
58 rpc: &solana_client::rpc_client::RpcClient,
59 address: &solana_pubkey::Pubkey,
60) -> Result<crate::shared::DecodedAccount<Position>, std::io::Error> {
61 let accounts = fetch_all_position(rpc, &[*address])?;
62 Ok(accounts[0].clone())
63}
64
65#[cfg(feature = "fetch")]
66pub fn fetch_all_position(
67 rpc: &solana_client::rpc_client::RpcClient,
68 addresses: &[solana_pubkey::Pubkey],
69) -> Result<Vec<crate::shared::DecodedAccount<Position>>, std::io::Error> {
70 let accounts = rpc
71 .get_multiple_accounts(addresses)
72 .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
73 let mut decoded_accounts: Vec<crate::shared::DecodedAccount<Position>> = Vec::new();
74 for i in 0..addresses.len() {
75 let address = addresses[i];
76 let account = accounts[i].as_ref().ok_or(std::io::Error::new(
77 std::io::ErrorKind::Other,
78 format!("Account not found: {}", address),
79 ))?;
80 let data = Position::from_bytes(&account.data)?;
81 decoded_accounts.push(crate::shared::DecodedAccount {
82 address,
83 account: account.clone(),
84 data,
85 });
86 }
87 Ok(decoded_accounts)
88}
89
90#[cfg(feature = "fetch")]
91pub fn fetch_maybe_position(
92 rpc: &solana_client::rpc_client::RpcClient,
93 address: &solana_pubkey::Pubkey,
94) -> Result<crate::shared::MaybeAccount<Position>, std::io::Error> {
95 let accounts = fetch_all_maybe_position(rpc, &[*address])?;
96 Ok(accounts[0].clone())
97}
98
99#[cfg(feature = "fetch")]
100pub fn fetch_all_maybe_position(
101 rpc: &solana_client::rpc_client::RpcClient,
102 addresses: &[solana_pubkey::Pubkey],
103) -> Result<Vec<crate::shared::MaybeAccount<Position>>, std::io::Error> {
104 let accounts = rpc
105 .get_multiple_accounts(addresses)
106 .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
107 let mut decoded_accounts: Vec<crate::shared::MaybeAccount<Position>> = Vec::new();
108 for i in 0..addresses.len() {
109 let address = addresses[i];
110 if let Some(account) = accounts[i].as_ref() {
111 let data = Position::from_bytes(&account.data)?;
112 decoded_accounts.push(crate::shared::MaybeAccount::Exists(
113 crate::shared::DecodedAccount {
114 address,
115 account: account.clone(),
116 data,
117 },
118 ));
119 } else {
120 decoded_accounts.push(crate::shared::MaybeAccount::NotFound(address));
121 }
122 }
123 Ok(decoded_accounts)
124}
125
126#[cfg(feature = "anchor")]
127impl anchor_lang::AccountDeserialize for Position {
128 fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
129 Ok(Self::deserialize(buf)?)
130 }
131}
132
133#[cfg(feature = "anchor")]
134impl anchor_lang::AccountSerialize for Position {}
135
136#[cfg(feature = "anchor")]
137impl anchor_lang::Owner for Position {
138 fn owner() -> Pubkey {
139 crate::WHIRLPOOL_ID
140 }
141}
142
143#[cfg(feature = "anchor-idl-build")]
144impl anchor_lang::IdlBuild for Position {}
145
146#[cfg(feature = "anchor-idl-build")]
147impl anchor_lang::Discriminator for Position {
148 const DISCRIMINATOR: [u8; 8] = [0; 8];
149}