From b643ca8eb89047160b5337502be58b989f251600 Mon Sep 17 00:00:00 2001 From: Nikhil B N Date: Tue, 22 Feb 2022 21:19:10 +0530 Subject: [PATCH 1/2] Allow anchor idl fetch to run outside workspace Signed-off-by: Nikhil B N --- cli/src/lib.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index a4d3d3a039..34528e4797 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1397,8 +1397,24 @@ pub enum BinVerificationState { // Fetches an IDL for the given program_id. fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result { - let cfg = Config::discover(cfg_override)?.expect("Inside a workspace"); - let url = cluster_url(&cfg); + let url = match Config::discover(cfg_override)? { + Some(cfg) => cluster_url(&cfg), + None => { + // If the command is not run inside a workspace, + // provider.cluster option has to be provided + if let Some(cluster) = cfg_override.cluster.clone() { + let is_localnet = cluster == Cluster::Localnet; + match is_localnet { + // If Cluster is Localnet, default url will be used + true => "http://localhost:8899".to_string(), + false => cluster.url().to_string(), + } + } else { + return Err(anyhow!("provider.cluster option required")); + } + } + }; + let client = RpcClient::new(url); let mut account = client From 4b39ef2996f00068a5ddb77cc8c4d2e22c1bcd9a Mon Sep 17 00:00:00 2001 From: Nikhil B N Date: Wed, 23 Feb 2022 20:15:08 +0530 Subject: [PATCH 2/2] Use default solana config for anchor idl fetch Signed-off-by: Nikhil B N --- CHANGELOG.md | 1 + Cargo.lock | 13 ++++--------- cli/Cargo.toml | 1 + cli/src/config.rs | 12 ++++++++++++ cli/src/lib.rs | 13 +++++-------- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c528d3e7cd..6fae7fbaf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ incremented for features. * lang: Add check that declared id == program id ([#1451](https://github.com/project-serum/anchor/pull/1451)). * ts: Added float types support ([#1425](https://github.com/project-serum/anchor/pull/1425)). * cli: Add `--skip-lint` option to disable check linting introduced in ([#1452](https://github.com/project-serum/anchor/pull/1452)) for rapid prototyping ([#1482](https://github.com/project-serum/anchor/pull/1482)). +* cli: Add support for `anchor idl fetch` to work outside anchor workspace ([#1509](https://github.com/project-serum/anchor/pull/1509)). ### Fixes diff --git a/Cargo.lock b/Cargo.lock index 0cde610c44..5953bc228e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,6 +165,7 @@ dependencies = [ "serde_json", "serum-common", "shellexpand", + "solana-cli-config", "solana-client", "solana-program", "solana-sdk", @@ -1089,12 +1090,6 @@ dependencies = [ "syn 0.15.44", ] -[[package]] -name = "dtoa" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" - [[package]] name = "ed25519" version = "1.2.0" @@ -2844,12 +2839,12 @@ dependencies = [ [[package]] name = "serde_yaml" -version = "0.8.21" +version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8c608a35705a5d3cdc9fbe403147647ff34b921f8e833e49306df898f9b20af" +checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0" dependencies = [ - "dtoa", "indexmap", + "ryu", "serde", "yaml-rust", ] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 2852399845..ecd31b6017 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -27,6 +27,7 @@ serde = { version = "1.0.122", features = ["derive"] } solana-sdk = "1.8.5" solana-program = "1.8.5" solana-client = "1.8.5" +solana-cli-config = "1.8.5" serum-common = { git = "https://github.com/project-serum/serum-dex", features = ["client"] } dirs = "3.0" heck = "0.3.1" diff --git a/cli/src/config.rs b/cli/src/config.rs index 0e828ff350..54455eab48 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -4,11 +4,13 @@ use anyhow::{anyhow, Error, Result}; use clap::{ArgEnum, Parser}; use heck::SnakeCase; use serde::{Deserialize, Serialize}; +use solana_cli_config::{Config as SolanaConfig, CONFIG_FILE}; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, Signer}; use std::collections::BTreeMap; use std::convert::TryFrom; use std::fs::{self, File}; +use std::io; use std::io::prelude::*; use std::ops::Deref; use std::path::Path; @@ -445,6 +447,16 @@ impl FromStr for Config { } } +pub fn get_solana_cfg_url() -> Result { + let config_file = CONFIG_FILE.as_ref().ok_or_else(|| { + io::Error::new( + io::ErrorKind::NotFound, + "Default Solana config was not found", + ) + })?; + SolanaConfig::load(config_file).map(|config| config.json_rpc_url) +} + fn ser_programs( programs: &BTreeMap>, ) -> BTreeMap> { diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 34528e4797..a7d403610f 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1401,16 +1401,13 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result { Some(cfg) => cluster_url(&cfg), None => { // If the command is not run inside a workspace, - // provider.cluster option has to be provided + // cluster_url will be used from default solana config + // provider.cluster option can be used to override this + if let Some(cluster) = cfg_override.cluster.clone() { - let is_localnet = cluster == Cluster::Localnet; - match is_localnet { - // If Cluster is Localnet, default url will be used - true => "http://localhost:8899".to_string(), - false => cluster.url().to_string(), - } + cluster.url().to_string() } else { - return Err(anyhow!("provider.cluster option required")); + config::get_solana_cfg_url()? } } };