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
use std::ops::{Add, Mul};
use common::math::get_sqrt_price_at_tick;
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use spl_associated_token_account::get_associated_token_address;
use spl_token::ui_amount_to_amount;
use common::client::get_decimals;
use common::command::{CliConfig, ProcessResult};
use common::contract::instructions::swap_with_partner::new_swap_with_partner;
use common::contract::state::clmmpools::Clmmpool;
use common::program::SWAP_PROGRAM_ID;
use common::utils::send::send_tx;
#[allow(clippy::too_many_arguments)]
pub fn process(
rpc_client: &RpcClient,
config: &CliConfig,
clmmpool: &Pubkey,
partner: &Pubkey,
by_amount_in: bool,
price_limit_tick: Option<i32>,
a_to_b: bool,
amount: &f64,
slid: &f64,
) -> ProcessResult {
let price_limit_tick = if price_limit_tick.is_none() {
if a_to_b {
-443636
} else {
443636
}
} else {
price_limit_tick.unwrap()
};
let clmmpool_info = Clmmpool::get_info(rpc_client, clmmpool);
let token_decimal = if a_to_b == by_amount_in {
get_decimals(rpc_client, &clmmpool_info.token_a)
} else {
get_decimals(rpc_client, &clmmpool_info.token_b)
};
let amount_lamport = ui_amount_to_amount(*amount, token_decimal);
let amount_limit = amount.mul(slid.add(1_f64));
let _amount_limit_lamport = ui_amount_to_amount(amount_limit, token_decimal);
let sqrt_price_limit = get_sqrt_price_at_tick(price_limit_tick);
let account_a = get_associated_token_address(&config.pubkey().unwrap(), &clmmpool_info.token_a);
let account_b = get_associated_token_address(&config.pubkey().unwrap(), &clmmpool_info.token_b);
let (tick_array_map_pubkey, _) =
Pubkey::find_program_address(&[b"tick_array_map", clmmpool.as_ref()], &SWAP_PROGRAM_ID);
let partner_ata_a = get_associated_token_address(&partner, &clmmpool_info.token_a);
let partner_ata_b = get_associated_token_address(&partner, &clmmpool_info.token_b);
let amount_limit = if by_amount_in { 0 } else { u64::MAX };
let ixs = [new_swap_with_partner(
rpc_client,
&clmmpool_info.clmm_config,
clmmpool,
&clmmpool_info.token_a,
&clmmpool_info.token_b,
&account_a,
&account_b,
&clmmpool_info.token_a_vault,
&clmmpool_info.token_b_vault,
&tick_array_map_pubkey,
&partner,
&partner_ata_a,
&partner_ata_b,
a_to_b,
amount_lamport,
amount_limit,
sqrt_price_limit,
by_amount_in,
config.pubkey().unwrap(),
)];
let res = send_tx(rpc_client, config, &ixs)?;
Ok("signers : ".to_owned() + res.to_string().as_str())
}