lang: Support legacy IDLs with `declare_program!` (#2997)
This commit is contained in:
parent
727aa4a601
commit
e77a453ac3
|
@ -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
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 }]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -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::*;
|
||||
|
|
Loading…
Reference in New Issue