sugar_cli/
setup.rs

1use std::rc::Rc;
2
3use anchor_client::{
4    solana_sdk::{
5        commitment_config::CommitmentConfig,
6        signature::{keypair::Keypair, read_keypair_file},
7    },
8    Client, Cluster,
9};
10use anyhow::{anyhow, Result};
11use console::style;
12use tracing::error;
13
14use crate::{config::data::SugarConfig, constants::DEFAULT_KEYPATH, parse::*};
15
16pub fn setup_client(sugar_config: &SugarConfig) -> Result<Client> {
17    let rpc_url = sugar_config.rpc_url.clone();
18    let ws_url = rpc_url.replace("http", "ws");
19    let cluster = Cluster::Custom(rpc_url, ws_url);
20
21    let key_bytes = sugar_config.keypair.to_bytes();
22    let signer = Rc::new(Keypair::from_bytes(&key_bytes)?);
23
24    let opts = CommitmentConfig::confirmed();
25    Ok(Client::new_with_options(cluster, signer, opts))
26}
27
28pub fn sugar_setup(
29    keypair_opt: Option<String>,
30    rpc_url_opt: Option<String>,
31) -> Result<SugarConfig> {
32    let sol_config_option = parse_solana_config();
33
34    let rpc_url = get_rpc_url(rpc_url_opt);
35
36    let keypair = match keypair_opt {
37        Some(keypair_path) => match read_keypair_file(&keypair_path) {
38            Ok(keypair) => keypair,
39            Err(e) => {
40                error!("Failed to read keypair file: {}", e);
41                return Err(anyhow!(
42                    "Failed to read keypair file: {}, {}",
43                    keypair_path,
44                    e
45                ));
46            }
47        },
48
49        None => match sol_config_option {
50            Some(ref sol_config) => match read_keypair_file(&sol_config.keypair_path) {
51                Ok(keypair) => keypair,
52                Err(e) => {
53                    error!(
54                        "Failed to read keypair file: {}, {}",
55                        &sol_config.keypair_path, e
56                    );
57                    return Err(anyhow!(
58                        "Failed to read keypair file: {}, {}",
59                        &sol_config.keypair_path,
60                        e
61                    ));
62                }
63            },
64            None => match read_keypair_file(&*shellexpand::tilde(DEFAULT_KEYPATH)) {
65                Ok(keypair) => keypair,
66                Err(e) => {
67                    error!("Failed to read keypair file: {}, {}", DEFAULT_KEYPATH, e);
68                    return Err(anyhow!(
69                        "Failed to read keypair file: {}, {}",
70                        DEFAULT_KEYPATH,
71                        e
72                    ));
73                }
74            },
75        },
76    };
77
78    Ok(SugarConfig { rpc_url, keypair })
79}
80
81pub fn get_rpc_url(rpc_url_opt: Option<String>) -> String {
82    let sol_config_option = parse_solana_config();
83
84    match rpc_url_opt {
85        Some(rpc_url) => rpc_url,
86        None => match sol_config_option {
87            Some(ref sol_config) => sol_config.json_rpc_url.clone(),
88            None => {
89                println!(
90                    "{}",
91                    style("No RPCL URL found in Miraland config file.")
92                        .bold()
93                        .red(),
94                );
95                std::process::exit(1);
96            }
97        },
98    }
99}