Skip to content

Commit

Permalink
explorer command (#94)
Browse files Browse the repository at this point in the history
* explorer command
explorer get -k <address>
explorer get <label>

* match cluster with cli config

* newlines

* /address
  • Loading branch information
mwrites authored Nov 23, 2022
1 parent f775cf8 commit 01374c2
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ pub enum CliCommand {
worker_id: u64,
},

ExplorerGetThread {
id: Option<String>,
address: Option<Pubkey>,
},

Initialize {
mint: Pubkey,
},
Expand Down Expand Up @@ -228,6 +233,31 @@ pub fn app() -> Command<'static> {
),
),
)
.subcommand(
Command::new("explorer")
.about("Prints Explorer Urls")
.arg_required_else_help(true)
.subcommand(
Command::new("get")
.about("Prints thread explorer url")
.arg_required_else_help(true)
.arg(
Arg::new("id")
.index(1)
.takes_value(true)
.required(false)
.help("The label of the thread to lookup (only works if you \
are the signer of that thread)")
)
.arg(
Arg::new("address")
.short('k')
.long("address")
.takes_value(true)
.help("The address of the thread to lookup"),
),
)
)
.subcommand(
Command::new("initialize")
.about("Initialize the Clockwork network program")
Expand Down Expand Up @@ -374,7 +404,8 @@ pub fn app() -> Command<'static> {
.index(1)
.takes_value(true)
.required(false)
.help("The label of the thread to lookup")
.help("The label of the thread to lookup (only works if you \
are the signer of that thread)")
)
.arg(
Arg::new("address")
Expand Down
13 changes: 13 additions & 0 deletions cli/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl TryFrom<&ArgMatches> for CliCommand {
Some(("config", matches)) => parse_config_command(matches),
Some(("crontab", matches)) => parse_crontab_command(matches),
Some(("delegation", matches)) => parse_delegation_command(matches),
Some(("explorer", matches)) => parse_explorer_command(matches),
Some(("initialize", matches)) => parse_initialize_command(matches),
Some(("localnet", matches)) => parse_bpf_command(matches),
Some(("pool", matches)) => parse_pool_command(matches),
Expand Down Expand Up @@ -123,6 +124,18 @@ fn parse_delegation_command(matches: &ArgMatches) -> Result<CliCommand, CliError
}
}

fn parse_explorer_command(matches: &ArgMatches) -> Result<CliCommand, CliError> {
match matches.subcommand() {
Some(("get", matches)) => Ok(CliCommand::ExplorerGetThread {
id: parse_string("id", matches).ok(),
address: parse_pubkey("address", matches).ok(),
}),
_ => Err(CliError::CommandNotRecognized(
matches.subcommand().unwrap().0.into(),
)),
}
}

fn parse_initialize_command(matches: &ArgMatches) -> Result<CliCommand, CliError> {
Ok(CliCommand::Initialize {
mint: parse_pubkey("mint", matches)?,
Expand Down
17 changes: 17 additions & 0 deletions cli/src/processor/explorer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use {
crate::errors::CliError,
crate::config::CliConfig,
clockwork_utils::explorer::Explorer,

};

pub fn thread_url<T: std::fmt::Display>(thread: T, config: CliConfig) -> Result<(),
CliError> {
println!("thread: {}", explorer(config).thread_url(thread,
clockwork_client::thread::ID));
Ok(())
}

fn explorer(config: CliConfig) -> Explorer {
Explorer::from(config.json_rpc_url)
}
1 change: 1 addition & 0 deletions cli/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod config;
mod crontab;
mod delegation;
mod initialize;
mod explorer;
mod localnet;
mod pool;
mod process;
Expand Down
8 changes: 6 additions & 2 deletions cli/src/processor/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub fn process(matches: &ArgMatches) -> Result<(), CliError> {

// Build the RPC client
let payer = read_keypair_file(&config.keypair_path)
.map_err(|_| CliError::KeypairNotFound(config.keypair_path))?;
.map_err(|_| CliError::KeypairNotFound(config.keypair_path.clone()))?;

let client = Client::new(payer, config.json_rpc_url);
let client = Client::new(payer, config.json_rpc_url.clone());

// Process the command
match command {
Expand All @@ -51,6 +51,10 @@ pub fn process(matches: &ArgMatches) -> Result<(), CliError> {
delegation_id,
worker_id,
} => super::delegation::get(&client, delegation_id, worker_id),
CliCommand::ExplorerGetThread { id, address } => {
let pubkey = parse_pubkey_from_id_or_address(client.payer_pubkey(), id, address)?;
super::explorer::thread_url(pubkey, config)
}
CliCommand::Initialize { mint } => super::initialize::initialize(&client, mint),
CliCommand::Localnet { program_infos } => super::localnet::start(&client, program_infos),
CliCommand::PoolGet { id } => super::pool::get(&client, id),
Expand Down
4 changes: 3 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ name = "clockwork_sdk"

[dependencies]
chrono = { version = "0.4.19", default-features = false, features = ["alloc"] }
clockwork-utils = { path = "../utils", version = "1.3.8", optional = true }
clockwork-client = { path = "../client", version = "1.3.8", optional = true }
clockwork-thread-program = { path = "../programs/thread", features = ["cpi"], version = "1.3.8" }
nom = "~7"
Expand All @@ -23,4 +24,5 @@ once_cell = "1.5.2"
[features]
default = []
client = ["clockwork-client"]
thread = []
utils = ["clockwork-utils"]
thread = []
5 changes: 5 additions & 0 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ pub mod thread_program {
};
}
}

#[cfg(feature = "utils")]
pub mod utils {
pub use clockwork_utils::{explorer};
}
2 changes: 1 addition & 1 deletion utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ name = "clockwork_utils"

[dependencies]
anchor-lang = "0.25.0"
base64 = "0.13.1"
base64 = "~0.13"
static-pubkey = "1.0.3"

78 changes: 78 additions & 0 deletions utils/src/explorer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const EXPLORER_URL: &str = "https://explorer.solana.com";
const CK_EXPLORER_URL: &str = "https://explorer.clockwork.xyz";


#[derive(Default)]
pub struct Explorer {
cluster: String,
custom_rpc: Option<String>,
}

impl From<String> for Explorer {
fn from(json_rpc_url: String) -> Self {
match &json_rpc_url.to_lowercase() {
url if url.contains("devnet") => Explorer::devnet(),
url if url.contains("testnet") => Explorer::testnet(),
url if url.contains("mainnet") => Explorer::mainnet(),
_ => {
Explorer::custom(json_rpc_url)
}
}
}
}

impl Explorer {
pub fn mainnet() -> Self {
Self {
cluster: "mainnet-beta".into(),
..Default::default()
}
}

pub fn testnet() -> Self {
Self {
cluster: "testnet".into(),
..Default::default()
}
}

pub fn devnet() -> Self {
Self {
cluster: "devnet".into(),
..Default::default()
}
}

pub fn custom(custom_rpc: String) -> Self {
Self {
cluster: "custom".into(),
custom_rpc: Some(custom_rpc),
}
}

/// Ex: https://explorer.solana.com/tx/{tx}
/// ?cluster=custom
/// &customUrl=http://localhost:8899
pub fn tx_url<T: std::fmt::Display>(&self, tx: T) -> String {
let url = format!("{}/tx/{}?cluster={}", EXPLORER_URL, tx, self.cluster);
if self.cluster == "custom" {
url + "&customUrl=" + self.custom_rpc.as_ref().unwrap()
} else {
url
}
}

/// Ex: https://explorer.clockwork.xyz/thread/{thread}
/// ?network=custom
/// &customRPC=http://localhost:8899
pub fn thread_url<T: std::fmt::Display, U: std::fmt::Display>(&self, thread: T, program_id: U) -> String {
let url = format!("{}/address/{}?programID={}&network={}", CK_EXPLORER_URL,
thread, program_id, self
.cluster);
if self.cluster == "custom" {
url + "&customRPC=" + self.custom_rpc.as_ref().unwrap()
} else {
url
}
}
}
2 changes: 2 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod explorer;

use {
anchor_lang::{
prelude::borsh::BorshSchema,
Expand Down

0 comments on commit 01374c2

Please sign in to comment.