diff --git a/CHANGELOG.md b/CHANGELOG.md index 176b2348e..383ee16c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ The minor version will be incremented upon a breaking change and the patch versi - ts: Extract Anchor error codes into their own package ([#2983](https://github.com/coral-xyz/anchor/pull/2983)). - cli: Add additional solana arguments to the `upgrade` command ([#2998](https://github.com/coral-xyz/anchor/pull/2998)). - spl: Export `spl-associated-token-account` crate ([#2999](https://github.com/coral-xyz/anchor/pull/2999)). +- lang: Support legacy IDLs with `declare_program!` ([#2997](https://github.com/coral-xyz/anchor/pull/2997)). ### Fixes diff --git a/lang/attribute/program/Cargo.toml b/lang/attribute/program/Cargo.toml index 3bd2a3dad..2d70f3bb5 100644 --- a/lang/attribute/program/Cargo.toml +++ b/lang/attribute/program/Cargo.toml @@ -16,7 +16,7 @@ idl-build = ["anchor-syn/idl-build"] interface-instructions = ["anchor-syn/interface-instructions"] [dependencies] -anchor-lang-idl = { path = "../../../idl", version = "0.1.0" } +anchor-lang-idl = { path = "../../../idl", version = "0.1.0", features = ["convert"] } anchor-syn = { path = "../../syn", version = "0.30.0" } anyhow = "1" bs58 = "0.5" diff --git a/lang/attribute/program/src/declare_program/mod.rs b/lang/attribute/program/src/declare_program/mod.rs index bca2c15ec..b6e5df3f2 100644 --- a/lang/attribute/program/src/declare_program/mod.rs +++ b/lang/attribute/program/src/declare_program/mod.rs @@ -35,19 +35,17 @@ impl ToTokens for DeclareProgram { fn get_idl(name: &syn::Ident) -> anyhow::Result { let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get manifest dir"); - let path = std::path::Path::new(&manifest_dir) + std::path::Path::new(&manifest_dir) .ancestors() .find_map(|ancestor| { let idl_dir = ancestor.join("idls"); - std::fs::metadata(&idl_dir).map(|_| idl_dir).ok() + idl_dir.exists().then_some(idl_dir) }) .ok_or_else(|| anyhow!("`idls` directory not found")) - .map(|idl_dir| idl_dir.join(name.to_string()).with_extension("json"))?; - - std::fs::read(path) - .map_err(|e| anyhow!("Failed to read IDL: {e}")) - .map(|idl| serde_json::from_slice(&idl))? - .map_err(|e| anyhow!("Failed to parse IDL: {e}")) + .map(|idl_dir| idl_dir.join(name.to_string()).with_extension("json")) + .map(std::fs::read)? + .map_err(|e| anyhow!("Failed to read IDL `{name}`: {e}")) + .map(|buf| Idl::from_slice_with_conversion(&buf))? } fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream { diff --git a/tests/declare-program/idls/external_legacy.json b/tests/declare-program/idls/external_legacy.json new file mode 100644 index 000000000..20da7a7fa --- /dev/null +++ b/tests/declare-program/idls/external_legacy.json @@ -0,0 +1,65 @@ +{ + "version": "0.1.0", + "name": "external", + "metadata": { + "address": "Externa111111111111111111111111111111111111" + }, + "instructions": [ + { + "name": "init", + "accounts": [ + { "name": "authority", "isMut": true, "isSigner": true }, + { "name": "myAccount", "isMut": true, "isSigner": false }, + { "name": "systemProgram", "isMut": false, "isSigner": false } + ], + "args": [] + }, + { + "name": "update", + "accounts": [ + { "name": "authority", "isMut": false, "isSigner": true }, + { "name": "myAccount", "isMut": true, "isSigner": false } + ], + "args": [{ "name": "value", "type": "u32" }] + }, + { + "name": "updateComposite", + "accounts": [ + { + "name": "update", + "accounts": [ + { "name": "authority", "isMut": false, "isSigner": true }, + { "name": "myAccount", "isMut": true, "isSigner": false } + ] + } + ], + "args": [{ "name": "value", "type": "u32" }] + }, + { + "name": "testCompilationDefinedTypeParam", + "accounts": [{ "name": "signer", "isMut": false, "isSigner": true }], + "args": [{ "name": "myAccount", "type": { "defined": "MyAccount" } }] + }, + { + "name": "testCompilationReturnType", + "accounts": [{ "name": "signer", "isMut": false, "isSigner": true }], + "args": [], + "returns": "bool" + } + ], + "accounts": [ + { + "name": "MyAccount", + "type": { + "kind": "struct", + "fields": [{ "name": "field", "type": "u32" }] + } + } + ], + "events": [ + { + "name": "MyEvent", + "fields": [{ "name": "value", "type": "u32", "index": false }] + } + ] +} diff --git a/tests/declare-program/programs/declare-program/src/lib.rs b/tests/declare-program/programs/declare-program/src/lib.rs index 5272dea55..01df3b22a 100644 --- a/tests/declare-program/programs/declare-program/src/lib.rs +++ b/tests/declare-program/programs/declare-program/src/lib.rs @@ -5,6 +5,9 @@ declare_id!("Dec1areProgram11111111111111111111111111111"); declare_program!(external); use external::program::External; +// Compilation check for legacy IDL (pre Anchor `0.30`) +declare_program!(external_legacy); + #[program] pub mod declare_program { use super::*;