Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Add support for fetching legacy IDLs #3324

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Add short alias for the `idl build` command ([#3283](https://github.com/coral-xyz/anchor/pull/3283)).
- cli: Add `--program-id` option to `idl convert` command ([#3309](https://github.com/coral-xyz/anchor/pull/3309)).
- lang: Generate documentation of constants in `declare_program!` ([#3311](https://github.com/coral-xyz/anchor/pull/3311)).
- cli: Add support for fetching legacy IDLs ([#3324](https://github.com/coral-xyz/anchor/pull/3324)).

### Fixes

Expand Down
20 changes: 11 additions & 9 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,7 @@ fn verify(
// Verify IDL (only if it's not a buffer account).
let local_idl = generate_idl(&cfg, true, false, &cargo_args)?;
if bin_ver.state != BinVerificationState::Buffer {
let deployed_idl = fetch_idl(cfg_override, program_id)?;
let deployed_idl = fetch_idl(cfg_override, program_id).map(serde_json::from_value)??;
if local_idl != deployed_idl {
println!("Error: IDLs don't match");
std::process::exit(1);
Expand Down Expand Up @@ -2315,15 +2315,16 @@ fn idl(cfg_override: &ConfigOverride, subcmd: IdlCommand) -> Result<()> {
}

/// Fetch an IDL for the given program id.
fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result<Idl> {
///
/// Intentionally returns [`serde_json::Value`] rather than [`Idl`] to also support legacy IDLs.
fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result<serde_json::Value> {
let url = match Config::discover(cfg_override)? {
Some(cfg) => cluster_url(&cfg, &cfg.test_validator),
None => {
// If the command is not run inside a workspace,
// 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() {
if let Some(cluster) = cfg_override.cluster.as_ref() {
cluster.url().to_string()
} else {
config::get_solana_cfg_url()?
Expand Down Expand Up @@ -2803,12 +2804,13 @@ fn generate_idl(
}

fn idl_fetch(cfg_override: &ConfigOverride, address: Pubkey, out: Option<String>) -> Result<()> {
let idl = fetch_idl(cfg_override, address)?;
let out = match out {
None => OutFile::Stdout,
Some(out) => OutFile::File(PathBuf::from(out)),
let idl = fetch_idl(cfg_override, address).map(|idl| serde_json::to_string_pretty(&idl))??;
match out {
Some(out) => fs::write(out, idl)?,
_ => println!("{idl}"),
};
write_idl(&idl, out)

Ok(())
}

fn idl_convert(path: String, out: Option<String>, program_id: Option<Pubkey>) -> Result<()> {
Expand Down
Loading