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
use std::process::exit;
mod args;
mod ops;
mod utils;

use {
    args::VyperCliArgs,
    clap::Parser,
    args::Vyper,
    ops::core_ops::handle_core_command,
    ops::redeem_logic_plugin_ops::{
        redeem_logic_forward_ops::handle_redeem_logic_forward_command,
    },
    ops::rate_plugin_ops::{
        rate_switchboard_ops::handle_rate_switchboard_command
    },
    anchor_client::{
        Client,
        Cluster,
        solana_sdk::{
            signer::keypair::read_keypair_file,
            pubkey:: Pubkey
        },
    },
    solana_cli_config::Config,
    std::rc::Rc,
    utils::{
        println_error,
        get_solana_config
    }
};


const VYPER_CORE_ID: &str = "vyPErCcGJKQQBeeQ59gXcWrDyU4vBrq8qQfacwmsAsp";
const REDEEM_LOGIC_FORWARD: &str = "BrpV1re8MshA8qskKVxcEG8zXG3vf2uLX6myeTKAyhsK";
const RATE_SWITCHBOARD: &str  = "2hGXiH1oEQwjCXRx8bNdHTi49ScZp7Mj2bxcjxtULKe1";

fn main() {

    // parsing arguments
    let args = VyperCliArgs::parse();

    // getting the config file
    let mut cli_config:Option<Config> = None;

    let current_cluster = match args.config_override.cluster {
        Some(cluster) => cluster,
        None => {
            cli_config = Some(get_solana_config());
            match &cli_config {
                Some(solana_config) => Cluster::Custom(solana_config.json_rpc_url.clone(),solana_config.websocket_url.clone()),
                None=> {
                    println_error("Could not find a config file or --cluster option");
                    exit(1);
                }
            }
        }
    };

    let keypair_path = match args.config_override.wallet {
        Some(keypair) => keypair,
        None => {
            match &cli_config {
                Some(solana_config)=> solana_config.keypair_path.clone(),
                None => {
                    get_solana_config().keypair_path
                }
            }
        }
    };

    let key_pair = read_keypair_file(&keypair_path);
    let key_pair = match key_pair {
        Ok(keys) => keys,
        Err(_) => {
            println_error("Could not find keypair in the config file");
            exit(1);
        }
    };


    let client = Client::new(current_cluster, Rc::new(key_pair));
    match args.vyper {
        Vyper::Core(core) => {
            // vyper core program
            let core_program_id: Pubkey = Pubkey::new(&bs58::decode(&VYPER_CORE_ID).into_vec().expect("Invalid vyper core program id"));
            let core_program = client.program(core_program_id);
            // core command handler
            handle_core_command(core,&core_program);
        },
        Vyper::RedeemLogicForward(redeem_logic_command) => {
            // redem logic forward program
            let redeem_logic_forward_program_id: Pubkey = Pubkey::new(&bs58::decode(&REDEEM_LOGIC_FORWARD).into_vec().expect("Invalid redeem logic forward program id"));
            let redeem_logic_forward_program = client.program(redeem_logic_forward_program_id);
            // command handler
            handle_redeem_logic_forward_command(redeem_logic_command, &redeem_logic_forward_program);
        },
        Vyper::RateSwitchboard(rate_switchboard_command) => {
             // rate switchboard program
             let rate_switchboard_program_id: Pubkey = Pubkey::new(&bs58::decode(&RATE_SWITCHBOARD).into_vec().expect("Invalid rate switchboard program id"));
             let rate_switchboard_program = client.program(rate_switchboard_program_id);
             // command handler
             handle_rate_switchboard_command(rate_switchboard_command, &rate_switchboard_program);
        }
    }
}