lang: Fix using defined types in instruction parameters with `declare_program!` (#2959)

This commit is contained in:
acheron 2024-05-11 11:59:27 +02:00 committed by GitHub
parent d8d007eb89
commit 460a16171a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 54 additions and 3 deletions

View File

@ -25,6 +25,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: add filename to 'Unable to read keypair file' errors ([#2932](https://github.com/coral-xyz/anchor/pull/2932)).
- idl: Fix path resolution of the `Cargo.lock` of the project when generating idls for external types ([#2946](https://github.com/coral-xyz/anchor/pull/2946)).
- idl: Fix potential panic on external type resolution ([#2954](https://github.com/coral-xyz/anchor/pull/2954)).
- lang: Fix using defined types in instruction parameters with `declare_program!` ([#2959](https://github.com/coral-xyz/anchor/pull/2959)).
### Breaking

View File

@ -73,6 +73,9 @@ fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream {
#docs
pub mod #name {
use anchor_lang::prelude::*;
use accounts::*;
use events::*;
use types::*;
#id
#program_mod

View File

@ -110,7 +110,7 @@ pub fn gen_accounts_mod(idl: &Idl) -> proc_macro2::TokenStream {
quote! {
/// Program account type definitions.
pub mod accounts {
use super::{*, types::*};
use super::*;
#(#accounts)*
}

View File

@ -37,7 +37,7 @@ pub fn gen_events_mod(idl: &Idl) -> proc_macro2::TokenStream {
quote! {
/// Program event type definitions.
pub mod events {
use super::{*, types::*};
use super::*;
#(#events)*
}

View File

@ -32,7 +32,7 @@ fn gen_event(idl: &Idl) -> proc_macro2::TokenStream {
});
quote! {
use super::{*, events::*};
use super::*;
/// An enum that includes all events of the declared program as a tuple variant.
///

View File

@ -81,6 +81,47 @@
}
]
},
{
"name": "update_all",
"discriminator": [
205,
139,
239,
66,
134,
131,
110,
182
],
"accounts": [
{
"name": "authority",
"signer": true
},
{
"name": "my_account",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "authority"
}
]
}
}
],
"args": [
{
"name": "my_account",
"type": {
"defined": {
"name": "MyAccount"
}
}
}
]
},
{
"name": "update_composite",
"discriminator": [

View File

@ -19,6 +19,12 @@ pub mod external {
ctx.accounts.update.my_account.field = value;
Ok(())
}
// Compilation test for whether a defined type (an account in this case) can be used in `cpi` client.
pub fn update_all(ctx: Context<Update>, my_account: MyAccount) -> Result<()> {
*ctx.accounts.my_account = my_account;
Ok(())
}
}
#[derive(Accounts)]