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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use crate::app::{Cli, MathCommands, PairCommands, PositionCommands, SwapCli, TicksCommands};
use crate::command::fee_tier::fee_tier::process_fee_tier_info;
use crate::command::math::price::{process_sqrt_price_to_tick, process_tick_to_sqrt_price};
use crate::command::{pair, position, ticks};
use common::command::{CliConfig, ProcessResult};
use common::init::Matche;
use solana_client::rpc_client::RpcClient;

pub fn process_command(
    config: &mut CliConfig,
    rpc_client: RpcClient,
    app: Box<dyn Matche>,
) -> ProcessResult {
    let app = app.as_any().downcast_ref::<Cli>().unwrap();
    match &app.command {
        SwapCli::Pair { command } => match command {
            PairCommands::AcceptProtocolFeeAuthority { clmm_config } => {
                pair::accept_protocol_fee_authority::process(&rpc_client, config, &clmm_config)
            }
            PairCommands::CollectPartnerFee { partner, clmmpool } => {
                pair::collect_partner_fee::process(&rpc_client, config, &partner, &clmmpool)
            }
            PairCommands::CollectProtocolFee { clmmpool } => {
                pair::collect_protocol_fee::process(&rpc_client, config, &clmmpool)
            }
            PairCommands::CreatePool {} => pair::create_pool::process(&rpc_client, config),
            PairCommands::PausePool { clmmpool } => {
                pair::pause_clmmpool::process(&rpc_client, config, clmmpool)
            }
            PairCommands::UnpausePool { clmmpool } => {
                pair::unpause_clmmpool::process(&rpc_client, config, clmmpool)
            }
            PairCommands::CreatePartner {} => pair::create_partner::process(&rpc_client, config),
            PairCommands::PartnerInfo { partner } => pair::partner::process(&rpc_client, *partner),

            PairCommands::CreateTickArray {} => {
                pair::create_tick_array::process(&rpc_client, config)
            }
            PairCommands::Info { pair_key } => pair::pair::process(&rpc_client, &pair_key),
            PairCommands::List {} => pair::pair_list::process(&rpc_client),
            PairCommands::InitConfig {} => pair::init_config::process(&rpc_client, config),
            PairCommands::ConfigInfo {} => pair::config::process(&rpc_client),

            PairCommands::CreateFeeTier {} => pair::create_fee_tier::process(&rpc_client, config),
            PairCommands::FeeTierInfo { fee_tier } => process_fee_tier_info(&rpc_client, *fee_tier),
            PairCommands::UpdateConfig {
                clmm_config,
                new_protocol_fee_rate,
                create_pool_authority,
                claim_authority,
            } => pair::update_config::process(
                &rpc_client,
                config,
                &clmm_config,
                new_protocol_fee_rate,
                create_pool_authority,
                claim_authority,
            ),
            PairCommands::UpdateFeeRate {
                new_fee_rate,
                clmmpool,
            } => pair::update_fee_rate::process(&rpc_client, config, *new_fee_rate, &clmmpool),
            PairCommands::UpdatePartner {
                partner,
                new_fee_rate,
                new_claim_authority,
                start_time,
                end_time,
            } => pair::update_partner::process(
                &rpc_client,
                &config,
                &partner,
                &new_fee_rate,
                &new_claim_authority,
                &start_time,
                &end_time,
            ),
            PairCommands::Swap {
                clmmpool,
                a_to_b,
                by_amount_in,
                price_limit_tick,
                amount,
            } => pair::swap::process(
                &rpc_client,
                config,
                &clmmpool,
                by_amount_in.unwrap_or(true),
                *price_limit_tick,
                a_to_b.unwrap_or(true),
                &amount,
                &0f64,
            ),
            PairCommands::SwapWithPartner {
                clmmpool,
                partner,
                amount,
                a_to_b,
                by_amount_in,
                price_limit_tick,
            } => pair::swap_with_partner::process(
                &rpc_client,
                config,
                &clmmpool,
                &partner,
                by_amount_in.unwrap_or(true),
                *price_limit_tick,
                a_to_b.unwrap_or(true),
                &amount,
                &0f64,
            ),
            PairCommands::InitRewarder {
                clmmpool,
                rewarder_token_mint,
                rewarder_index,
                mint_wrapper,
                minter,
            } => pair::initialize_rewarder::process(
                &rpc_client,
                config,
                *clmmpool,
                *rewarder_token_mint,
                *rewarder_index,
                *mint_wrapper,
                *minter,
            ),
            PairCommands::UpdateRewarderEmission {
                clmmpool,
                rewarder_index,
                emissions_per_day,
            } => pair::update_rewarder_emission::process(
                &rpc_client,
                config,
                *clmmpool,
                *rewarder_index,
                *emissions_per_day,
            ),
            PairCommands::TransferProtocolFeeAuthority {
                clmm_config,
                new_authority,
            } => pair::transfer_protocol_fee_authority::process(
                &rpc_client,
                config,
                &clmm_config,
                &new_authority,
            ),
            PairCommands::TransferPartnerClaimAuthority {
                partner,
                new_authority,
            } => pair::transfer_partner_claim_authority::process(
                &rpc_client,
                config,
                &partner,
                &new_authority,
            ),
            PairCommands::AcceptPartnerClaimAuthority { partner } => {
                pair::accept_partner_claim_authority::process(&rpc_client, config, partner)
            }

            PairCommands::CreateMetadata {} => pair::create_metadata::process(&rpc_client, config),

            PairCommands::FetchTicks { clmmpool } => {
                pair::fetch_ticks::process(&rpc_client, clmmpool)
            }
        },

        SwapCli::Pos { command } => match command {
            PositionCommands::CollectRewarder {
                mint,
                rewarder_index,
            } => position::collect_rewarder::process(&rpc_client, config, *mint, *rewarder_index),
            PositionCommands::Increase {
                mint,
                amount_a,
                amount_b,
                slid,
            } => position::increase_liquidity::process(
                &rpc_client,
                config,
                &mint,
                &amount_a,
                &amount_b,
                &slid,
            ),
            PositionCommands::IncreaseWithFixedToken {
                mint,
                amount,
                slid,
                is_a_fixed,
            } => position::increase_liquidity_with_fixed_token::process(
                &rpc_client,
                config,
                &mint,
                *amount,
                &slid,
                is_a_fixed.unwrap_or(true),
            ),
            PositionCommands::Decrease {
                mint,
                amount_a,
                amount_b,
                liquidity,
                slid,
            } => position::decrease_liquidity::process(
                &rpc_client,
                config,
                &mint,
                &amount_a,
                &liquidity,
                &amount_b,
                &slid,
            ),
            PositionCommands::Open {
                clmmpool,
                tick_lower_index,
                tick_upper_index,
            } => position::open_position::process(
                &rpc_client,
                config,
                &clmmpool,
                &tick_lower_index,
                &tick_upper_index,
            ),
            PositionCommands::Remove { mint } => {
                position::remove_position::process(&rpc_client, config, &mint)
            }
            PositionCommands::Info { mint } => position::position_info::process(&rpc_client, &mint),
            PositionCommands::CollectFee { mint } => {
                position::collect_fee::process(&rpc_client, config, &mint)
            }
            PositionCommands::List { clmmpool, auth } => {
                position::position_list::process(&rpc_client, *clmmpool, *auth)
            }
        },

        SwapCli::Math { command } => match command {
            MathCommands::TickToSqrtPrice { tick } => process_tick_to_sqrt_price(*tick),
            MathCommands::SqrtPriceToTick { price } => process_sqrt_price_to_tick(*price),
        },

        SwapCli::Ticks { command } => match command {
            TicksCommands::Array {
                pool,
                tick_index,
                array_index,
            } => ticks::tick_array::process(&rpc_client, *tick_index, *array_index, &pool),
            TicksCommands::Tick { pool, tick_index } => {
                ticks::tick::process(&rpc_client, *tick_index, &pool)
            }
            TicksCommands::Map { pool, array_index } => {
                ticks::tick_map::process(&rpc_client, *array_index, &pool)
            }
            TicksCommands::Price { tick } => ticks::price::process(&rpc_client, *tick),
            TicksCommands::Index { tick, tick_spacing } => {
                ticks::array_index::process(*tick, *tick_spacing)
            }
        },
    }
}