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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use std::any::Any;
use solana_program::pubkey::Pubkey;
use clap::{Parser, Subcommand};
use common::init::{Matche};
use crate::CONFIG_FILE;

#[derive(Parser)]
#[clap(name = "aaa", author, version, about, long_about = None)]
#[clap(propagate_version = true)]
pub struct Cli {
    /// crema cli config, consistent with the solana cli config (default: $HOME/.config/solana/cli/config.yml)
    #[clap(long, short, global = true, takes_value = true,
    default_value_t = {
    if let Some(ref config_file) = * CONFIG_FILE {
    config_file.to_string()
    } else {
    String::new()
    }
    }
    )]
    pub config_file: String,
    /// owner key default from config file Keypair
    #[clap(long, short, global = true, takes_value = true)]
    pub owner: Option<String>,
    // config_file: String,
    // owner: String,
    #[clap(subcommand)]
    pub command: SwapCli,
}

impl Matche for Cli {
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn get_config_file(&self) -> &str {
        self.config_file.as_str()
    }
    fn get_owner(&self) -> &str {
        let s = self.owner.as_ref();
        if s.is_some() {
            s.unwrap().as_str()
        } else {
            ""
        }
    }
}

#[derive(Subcommand)]
pub enum SwapCli {
    /// pair command
    Pair {
        #[clap(subcommand)]
        command: PairCommands,
    },
    /// position command
    Pos {
        #[clap(subcommand)]
        command: PositionCommands,
    },
    /// math command
    Math {
        #[clap(subcommand)]
        command: MathCommands,
    },
    /// ticks command
    Ticks {
        #[clap(subcommand)]
        command: TicksCommands,
    },
}

#[derive(Subcommand)]
pub enum PairCommands {
    /// recalled by the new `ClmmConfig` authority to accept the authority
    AcceptProtocolFeeAuthority {
        /// The clmm_config is the config of new_authority to accept
        clmm_config: Pubkey,
    },
    /// collect the partner fee from ata controlled by partner pda
    CollectPartnerFee {
        /// The partner key
        partner: Pubkey,
        /// The clmmpool key
        clmmpool: Pubkey,
    },
    /// protocol_fee_claim_authority to collect the fee earn by the clmmpool
    CollectProtocolFee {
        /// The clmmpool key
        clmmpool: Pubkey,
    },
    /// create a yaml template for the create-pool
    CreatePoolTemplate {
        /// The config file output path (default: ./clmmpool-template.yaml)
        output_file: String,
    },
    /// create a clmm_pool
    CreatePool {
        /// The config file entry path (default: ./clmmpool-template.yaml)
        entry_file: String,
    },
    /// get clmm_pool info
    Info {
        /// pool key
        pair_key: Pubkey,
    },
    /// get clmm_pool list
    List {},
    /// create a yaml template for the init-config
    ConfigInitTemplate {
        /// The config file output path (default: ./clmm-config-template.yaml)
        output_file: String,
    },
    /// init clmm_config
    ConfigInit {
        /// The config file entry path (default: ./clmm-config-template.yaml)
        entry_file: String,
    },
    /// get config_info
    ConfigInfo {
        /// The config key
        config_key: Pubkey,
    },
    /// create a yaml template for the create-fee-tier
    FeeTierCreateTemplate {
        /// The config file output path (default: ./fee-tier-template.yaml)
        output_file: String,
    },
    /// create fee tier
    FeeTierCreate {
        /// The config file entry path (default: ./fee-tier-template.yaml)
        entry_file: String,
    },
    /// get fee_tier_info
    FeeTierInfo {
        /// The fee_tier key
        fee_tier: Pubkey,
    },
    /// create a yaml template for the create-partner
    PartnerCreateTemplate {
        /// The config file output path (default: ./partner-template.yaml)
        output_file: String,
    },
    /// create partner
    PartnerCreate {
        /// The config file entry path (default: ./partner-template.yaml)
        entry_file: String,
    },
    /// get partner_info
    PartnerInfo {
        /// The partner key
        partner: Pubkey,
    },
    /// create a yaml template for the create-tick-array
    TickArrayCreateTemplate {
        /// The config file output path (default: ./tick-array-template.yaml)
        output_file: String,
    },
    /// create tick array
    TickArrayCreate {
        /// The config file entry path (default: ./tick-array-template.yaml)
        entry_file: String,
    },
    /// protocol_fee_authority of the ClmmConfig to another new authority
    TransferProtocolFeeAuthority {
        /// The clmm_config to transfer to new_authority
        clmm_config: Pubkey,
        /// The new_authority will be the new authority of clmm_config
        new_authority: Pubkey,
    },
    /// update the protocol_fee_rate on ClmmConfig
    ConfigProtocolFeeRateUpdate {
        /// protocol_fee_authority is the authority to update protocol_fee_rate
        clmm_config: Pubkey,
        /// The new_protocol_fee_rate to update protocol config
        new_protocol_fee_rate: u16,
    },
    /// update the fee_rate on the clmmpool
    FeeRateUpdate {
        /// The fee_rate ot this fee_tier to update clmmpool
        fee_tier: Pubkey,
        /// The clmmpool to update the fee_rate
        clmmpool: Pubkey,
    },
    /// update partner fee_rate or claim authority
    PartnerUpdate {
        /// partner is the existed partner to update
        partner: Pubkey,
        /// The new fee rate to update partner
        new_fee_rate: Option<u16>,
        /// The new claim authority to update partner
        new_claim_authority: Option<Pubkey>,
    },
    /// update the protocol_fee_rate on ClmmPool
    ProtocolFeeRateUpdate {
        /// The clmmpool to update the fee_rate
        clmmpool: Pubkey,
    },

    /// swap
    Swap {
        /// The clmmpool
        clmmpool: Pubkey,
        /// swap amount
        amount: f64,
        /// swap direction, true: a->b, false: b->a
        #[clap(long, short = 'd', takes_value = true, default_value_t = true)]
        a_to_b: bool,
        /// swap by amount in, ture: in, false: out
        #[clap(long, short = 'i', takes_value = true, default_value_t = true)]
        by_amount_in: bool,
        /// swap price limit for tick
        #[clap(long, short, global = true, takes_value = true)]
        price_limit_tick: Option<i32>,
    },

    /// initialize rewarder
    InitRewarder {
        /// The pool key
        clmmpool: Pubkey,
        /// The rewarder authority private file(clmm_config.protocol_authority)
        rewarder_authority_file: String,
        /// The rewarder token mint
        rewarder_token_mint: Pubkey,
        /// The rewarder index
        rewarder_index: u8,
        /// The mint wrapper
        mint_wrapper: Pubkey,
        /// The minter
        minter: Pubkey,
    },
    /// update rewarder emission
    UpdateRewarderEmission {
        /// The pool key
        clmmpool: Pubkey,
        /// The rewarder authority private file(clmm_config.protocol_authority)
        rewarder_authority_file: String,
        /// The rewarder index
        rewarder_index: u8,
        /// The emissions per day
        emissions_per_day: f64,
    },

}

#[derive(Subcommand)]
pub enum PositionCommands {
    /// collect rewarder
    CollectRewarder {
        /// provider the position mint address
        mint: Pubkey,
        /// rewarder index for position rewarders
        rewarder_index: u8,
    },
    /// open a new position
    Open {
        /// pool key
        clmmpool: Pubkey,
        /// tick lower index
        tick_lower_index: i32,
        /// tick upper index
        tick_upper_index: i32,
    },
    /// provider the position mint address
    Remove {
        /// remove a position
        mint: Pubkey,
    },
    /// decrease liquidity to position
    Decrease {
        /// provider the position mint key
        mint: Pubkey,
        #[clap(long, short = 'a', global = true, takes_value = true, default_value_t = 0.0)]
        /// The token amount a
        amount_a: f64,
        #[clap(long, short = 'b', global = true, takes_value = true, default_value_t = 0.0)]
        /// The token amount b
        amount_b: f64,
        #[clap(long, short = 'l', global = true, takes_value = true, default_value_t = 0)]
        /// The liquidity
        liquidity: u128,
        #[clap(long, short = 's', global = true, takes_value = true, default_value_t = 0.01)]
        /// The slid rate (default 0.01)
        slid: f64,
    },
    /// increase liquidity to position
    Increase {
        /// provider the position mint key
        mint: Pubkey,
        #[clap(long, short = 'a', global = true, takes_value = true, default_value_t = 0.0)]
        /// The token amount a
        amount_a: f64,
        #[clap(long, short = 'b', global = true, takes_value = true, default_value_t = 0.0)]
        /// The token amount b
        amount_b: f64,
        #[clap(long, short = 's', global = true, takes_value = true, default_value_t = 0.01)]
        /// The slid rate (default 0.01)
        slid: f64,
    },
    IncreaseWithFixedToken {
        mint: Pubkey,
        #[clap(long, short = 'a', global = true, takes_value = true, default_value_t = 0.0)]
        amount: f64,
        #[clap(long, short = 's', global = true, takes_value = true, default_value_t = 0.01)]
        slid: f64,
        #[clap(long, short = 'f', global = true, takes_value = true, default_value_t = true)]
        is_a_fixed: bool,
    },
    /// get position list
    List {
        /// The clmm pool key
        #[clap(long, short = 'p', global = true, takes_value = true)]
        clmmpool: Option<Pubkey>,
        #[clap(long, short = 'a', global = true, takes_value = true)]
        /// The user owner key
        auth: Option<Pubkey>,
    },
    /// get position info
    Info {
        /// provider the position mint address to retreive the position details
        mint: Pubkey,
    },
    /// position owner to collect the fee the position owned
    CollectFee {
        /// provider the position mint address
        mint: Pubkey,
    },
}


#[derive(Subcommand)]
pub enum MathCommands {
    /// tick to sqrt_price
    TickToSqrtPrice {
        tick: i32,
    },
    /// sqrt_price to tick
    SqrtPriceToTick {
        price: u128,
    },
}

#[derive(Subcommand)]
pub enum TicksCommands {
    /// retrive one tick info
    Tick {
        /// pool key
        pool: Pubkey,
        /// tick index
        tick_index: i32,
    },
    /// tick array info
    Array {
        /// pool key
        pool: Pubkey,
        #[clap(long, short, global = true, takes_value = true)]
        /// the tick array contains this tick index
        tick_index: Option<i32>,
        #[clap(long, short, global = true, takes_value = true)]
        /// array index
        array_index: Option<u16>,
    },
    /// tick array map info
    Map {
        /// pool key
        pool: Pubkey,
        /// array index
        array_index: u16,
    },
    /// tick to price
    Price {
        tick: i32,
    },
    /// given tick index, return array_index and tick range
    Index {
        /// tick index
        tick: i32,
        /// tick spacing
        tick_spacing: u16,
    },
}