Skip to content

Commit

Permalink
lang: Fix constant bytes declarations when using declare_program! (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Sep 30, 2024
1 parent aa3ace3 commit 143893f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Use closures for `init` constraints to reduce the stack usage of `try_accounts` ([#2939](https://github.com/coral-xyz/anchor/pull/2939)).
- lang: Allow the `cfg` attribute above the instructions ([#2339](https://github.com/coral-xyz/anchor/pull/2339)).
- idl: Log output with `ANCHOR_LOG` on failure and improve build error message ([#3284](https://github.com/coral-xyz/anchor/pull/3284)).
- lang: Fix constant bytes declarations when using `declare_program!` ([#3287](https://github.com/coral-xyz/anchor/pull/3287)).

### Breaking

Expand Down
17 changes: 8 additions & 9 deletions lang/attribute/program/src/declare_program/mods/constants.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use anchor_lang_idl::types::{Idl, IdlType};
use quote::{format_ident, quote, ToTokens};

use super::common::convert_idl_type_to_str;
use super::common::convert_idl_type_to_syn_type;

pub fn gen_constants_mod(idl: &Idl) -> proc_macro2::TokenStream {
let constants = idl.constants.iter().map(|c| {
let name = format_ident!("{}", c.name);
let ty = match &c.ty {
IdlType::String => quote!(&str),
_ => parse_expr_ts(&convert_idl_type_to_str(&c.ty)),
let val = syn::parse_str::<syn::Expr>(&c.value)
.unwrap()
.to_token_stream();
let (ty, val) = match &c.ty {
IdlType::Bytes => (quote!(&[u8]), quote! { &#val }),
IdlType::String => (quote!(&str), val),
_ => (convert_idl_type_to_syn_type(&c.ty).to_token_stream(), val),
};
let val = parse_expr_ts(&c.value);

// TODO: Docs
quote! { pub const #name: #ty = #val; }
Expand All @@ -23,7 +26,3 @@ pub fn gen_constants_mod(idl: &Idl) -> proc_macro2::TokenStream {
}
}
}

fn parse_expr_ts(s: &str) -> proc_macro2::TokenStream {
syn::parse_str::<syn::Expr>(s).unwrap().to_token_stream()
}
7 changes: 7 additions & 0 deletions tests/declare-program/idls/external.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,12 @@
]
}
}
],
"constants": [
{
"name": "MASTER_SEED",
"type": "bytes",
"value": "[109, 97, 115, 116, 101, 114]"
}
]
}
111 changes: 93 additions & 18 deletions tests/declare-program/idls/external_legacy.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,109 @@
{
"version": "0.1.0",
"name": "external",
"metadata": {
"address": "Externa111111111111111111111111111111111111"
},
"constants": [
{
"name": "MASTER_SEED",
"type": "bytes",
"value": "[109, 97, 115, 116, 101, 114]"
}
],
"instructions": [
{
"name": "init",
"accounts": [
{ "name": "authority", "isMut": true, "isSigner": true },
{ "name": "myAccount", "isMut": true, "isSigner": false },
{ "name": "systemProgram", "isMut": false, "isSigner": false }
{
"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 }
{
"name": "authority",
"isMut": false,
"isSigner": true
},
{
"name": "myAccount",
"isMut": true,
"isSigner": false
}
],
"args": [{ "name": "value", "type": "u32" }]
"args": [
{
"name": "value",
"type": "u32"
}
]
},
{
"name": "updateComposite",
"accounts": [
{
"name": "update",
"accounts": [
{ "name": "authority", "isMut": false, "isSigner": true },
{ "name": "myAccount", "isMut": true, "isSigner": false }
{
"name": "authority",
"isMut": false,
"isSigner": true
},
{
"name": "myAccount",
"isMut": true,
"isSigner": false
}
]
}
],
"args": [{ "name": "value", "type": "u32" }]
"args": [
{
"name": "value",
"type": "u32"
}
]
},
{
"name": "testCompilationDefinedTypeParam",
"accounts": [{ "name": "signer", "isMut": false, "isSigner": true }],
"args": [{ "name": "myAccount", "type": { "defined": "MyAccount" } }]
"accounts": [
{
"name": "signer",
"isMut": false,
"isSigner": true
}
],
"args": [
{
"name": "myAccount",
"type": {
"defined": "MyAccount"
}
}
]
},
{
"name": "testCompilationReturnType",
"accounts": [{ "name": "signer", "isMut": false, "isSigner": true }],
"accounts": [
{
"name": "signer",
"isMut": false,
"isSigner": true
}
],
"args": [],
"returns": "bool"
}
Expand All @@ -52,14 +113,28 @@
"name": "MyAccount",
"type": {
"kind": "struct",
"fields": [{ "name": "field", "type": "u32" }]
"fields": [
{
"name": "field",
"type": "u32"
}
]
}
}
],
"events": [
{
"name": "MyEvent",
"fields": [{ "name": "value", "type": "u32", "index": false }]
"fields": [
{
"name": "value",
"type": "u32",
"index": false
}
]
}
]
],
"metadata": {
"address": "Externa111111111111111111111111111111111111"
}
}
3 changes: 3 additions & 0 deletions tests/declare-program/programs/external/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use anchor_lang::prelude::*;

declare_id!("Externa111111111111111111111111111111111111");

#[constant]
pub const MASTER_SEED: &[u8] = b"master";

#[program]
pub mod external {
use super::*;
Expand Down

0 comments on commit 143893f

Please sign in to comment.