Skip to content

Commit

Permalink
lang: Support legacy IDLs with declare_program! (#2997)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jun 3, 2024
1 parent 727aa4a commit e77a453
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions lang/attribute/program/src/declare_program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,17 @@ impl ToTokens for DeclareProgram {

fn get_idl(name: &syn::Ident) -> anyhow::Result<Idl> {
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 {
Expand Down
65 changes: 65 additions & 0 deletions tests/declare-program/idls/external_legacy.json
Original file line number Diff line number Diff line change
@@ -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 }]
}
]
}
3 changes: 3 additions & 0 deletions tests/declare-program/programs/declare-program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit e77a453

Please sign in to comment.