1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::sync::Arc;
use borsh::BorshDeserialize;
use clap::ArgMatches;
use colored::Colorize;
use solana_clap_utils::keypair::DefaultSigner;
use solana_client::rpc_client::RpcClient;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use solana_sdk::pubkey::Pubkey;
use spl_token::amount_to_ui_amount;
use tabled::object::Segment;
use tabled::style::Color;
use tabled::{Alignment, Modify, Panel, Rotate, Style, Table};
use crate::check_and_update_err;
use common::client::get_decimals;
use common::command::{CliCommand, CliCommandInfo, CliError, ProcessResult};
use common::contract::instructions::tick::get_tick_info;
use common::contract::state::position::Position;
use common::contract::state::Clmmpool;
use common::contract::state::tick::Tick;
use common::program::SWAP_PROGRAM_ID;
pub fn parse_position_info<'a>(
matches: &ArgMatches,
default_signer: &DefaultSigner,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> Result<CliCommandInfo<'a>, CliError> {
let mint = matches.value_of("mint");
Ok(CliCommandInfo {
command: CliCommand::PositionInfo {
mint: mint.unwrap().parse::<Pubkey>().unwrap(),
},
signers: vec![check_and_update_err!(
default_signer.signer_from_path(matches, wallet_manager),
CliError::RpcRequestError("owner key is invalid".to_string())
)?],
})
}
pub fn process_position_info(rpc_client: &RpcClient, mint: &Pubkey) -> ProcessResult {
let (position, _) = Pubkey::find_program_address(
&[b"position", mint.as_ref()],
&SWAP_PROGRAM_ID,
);
let data = rpc_client.get_account_data(&position).unwrap();
let mut position_info: Position = Position::try_from_slice(&data[8..]).unwrap();
let mut pool_info = Clmmpool::get_info(rpc_client, &position_info.clmmpool);
let token_a_decimal = get_decimals(rpc_client, &pool_info.token_a);
let token_b_decimal = get_decimals(rpc_client, &pool_info.token_b);
let amount =
position_info.get_amount(pool_info.current_tick_index, pool_info.current_sqrt_price)?;
let amount_a_ui = amount_to_ui_amount(amount.0, token_a_decimal);
let amount_b_ui = amount_to_ui_amount(amount.1, token_b_decimal);
Ok(Table::builder(vec![position_info])
.build()
.with(Rotate::Bottom)
.with(Rotate::Right)
.with(Style::modern())
.with(Color::try_from(" ".cyan().to_string()).unwrap())
.with(Panel("Position Info", 0))
.with(Modify::new(Segment::all()).with(Alignment::center()))
.to_string()
+ "\n"
+ &Table::builder(&[
&["position", position.to_string().as_str()],
&["amount_a", amount_a_ui.to_string().as_str()],
&["amount_b", amount_b_ui.to_string().as_str()],
])
.build()
.with(Style::modern())
.with(Color::try_from(" ".cyan().to_string()).unwrap())
.with(Modify::new(Segment::all()).with(Alignment::center()))
.with(Panel("Position calculate", 0))
.to_string())
}