-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcli.rs
More file actions
84 lines (74 loc) · 2.21 KB
/
cli.rs
File metadata and controls
84 lines (74 loc) · 2.21 KB
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
use std::{path::PathBuf, str::FromStr};
use aurora_sdk_rs::near::{
crypto::{InMemorySigner, Signer},
primitives::types::AccountId,
};
use clap::{Parser, ValueEnum, command};
use crate::command::Command;
#[derive(Debug, Clone, ValueEnum)]
pub enum Network {
Localnet,
Mainnet,
Testnet,
}
impl Network {
pub fn rpc_url(&self) -> &str {
match self {
Network::Localnet => "http://localhost:3030",
Network::Mainnet => "https://rpc.mainnet.near.org",
Network::Testnet => "https://rpc.testnet.near.org",
}
}
}
#[derive(Parser)]
#[command(author, long_about = None)]
pub struct Cli {
/// Near network ID
#[arg(long, value_enum, default_value_t = Network::Localnet)]
pub network: Network,
/// Aurora EVM account
#[arg(long, value_name = "ACCOUNT_ID", default_value = "aurora")]
pub engine: AccountId,
/// The way output of a command would be formatted
#[arg(long, default_value = "plain")]
pub output_format: OutputFormat,
/// Path to file with NEAR account id and secret key in JSON format
#[arg(long)]
pub near_key_path: PathBuf,
/// Block height to use for the view command
#[arg(short, long)]
pub block_height: Option<u64>,
#[clap(subcommand)]
pub command: Command,
}
impl Cli {
pub(crate) fn signer(&self) -> anyhow::Result<Signer> {
InMemorySigner::from_file(&self.near_key_path).map_err(Into::into)
}
pub(crate) fn root_contract_id(&self) -> anyhow::Result<AccountId> {
let account = match self.network {
Network::Testnet => "testnet",
Network::Mainnet => "near",
Network::Localnet => {
anyhow::bail!("Account creation is only supported for mainnet or testnet")
}
};
account.parse().map_err(Into::into)
}
}
#[derive(Default, Clone, ValueEnum)]
pub enum OutputFormat {
#[default]
Plain,
Json,
}
impl FromStr for OutputFormat {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"plain" => Ok(Self::Plain),
"json" => Ok(Self::Json),
_ => anyhow::bail!("unknown output format: {s}"),
}
}
}