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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::ops::{Add, Mul};
use std::str::FromStr;
use std::sync::Arc;

use clap::ArgMatches;
use common::math::get_sqrt_price_at_tick;
use solana_clap_utils::keypair::DefaultSigner;
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use spl_associated_token_account::get_associated_token_address;
use spl_token::ui_amount_to_amount;

use crate::check_and_update_err;
use common::client::get_decimals;
use common::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use common::contract::instructions::swap::new_swap;
use common::contract::state::clmmpools::Clmmpool;
use common::program::SWAP_PROGRAM_ID;
use common::utils::send::send_tx;

pub fn parse_swap<'a>(
    matches: &'a ArgMatches,
    default_signer: &DefaultSigner,
    wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> Result<CliCommandInfo<'a>, CliError> {
    let clmmpool = matches.value_of("clmmpool");
    let a_to_b = matches.value_of("a_to_b");
    let amount = matches.value_of("amount");
    let by_amount_in = matches.value_of("by_amount_in");
    let price_limit_tick = matches.value_of("price_limit_tick");
    //let slid = matches.value_of("slid");
    let a_to_b = a_to_b.unwrap().parse::<bool>().unwrap();
    let price_limit_tick = if price_limit_tick.is_none() {
        if a_to_b {
            -443636
        } else {
            443636
        }
    } else {
        price_limit_tick.unwrap().parse::<i32>().unwrap()
    };
    Ok(CliCommandInfo {
        command: CliCommand::PairSwap {
            clmmpool: Pubkey::from_str(clmmpool.unwrap()).unwrap(),
            a_to_b,
            by_amount_in: by_amount_in.unwrap().parse::<bool>().unwrap(),
            price_limit_tick,
            amount: amount.unwrap().parse::<f64>().unwrap(),
            slid: 0 as f64, //slid.unwrap().parse::<f64>().unwrap(),
        },
        signers: vec![check_and_update_err!(
            default_signer.signer_from_path(matches, wallet_manager),
            CliError::RpcRequestError("owner key is invalid".to_string())
        )?],
    })
}

#[allow(clippy::too_many_arguments)]
pub fn process_swap(
    rpc_client: &RpcClient,
    config: &CliConfig,
    clmmpool: &Pubkey,
    by_amount_in: bool,
    price_limit_tick: i32,
    a_to_b: bool,
    amount: &f64,
    slid: &f64,
) -> ProcessResult {
    let clmmpool_info = Clmmpool::get_info(rpc_client, clmmpool);
    let token_decimal = if a_to_b {
        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 = clmmpool_info
    //     .current_sqrt_price
    //     .mul(slid.add(1_f64) as u128);
    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 amount_limit = if by_amount_in { 0 } else { u64::MAX };
    let ixs = [new_swap(
        rpc_client,
        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,
        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())
}