Skip to content

Commit

Permalink
lang/syn: Parse entire crate for IDL (coral-xyz#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
suscd authored Jul 16, 2021
1 parent e92bfbf commit f34d073
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 380 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ incremented for features.
* lang: Adds `require` macro for specifying assertions that return error codes on failure ([#483](https://github.com/project-serum/anchor/pull/483)).
* lang: Allow one to specify arbitrary programs as the owner when creating PDA ([#483](https://github.com/project-serum/anchor/pull/483)).
* lang: A new `bump` keyword is added to the accounts constraints, which is used to add an optional bump seed to the end of a `seeds` array. When used in conjunction with *both* `init` and `seeds`, then the program executes `find_program_address` to assert that the given bump is the canonical bump ([#483](https://github.com/project-serum/anchor/pull/483)).
* lang: IDLs are now parsed from the entire crate ([#517](https://github.com/project-serum/anchor/pull/517)).

### Fixes

Expand Down
36 changes: 36 additions & 0 deletions examples/misc/programs/misc/src/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use anchor_lang::prelude::*;

#[associated]
#[derive(Default)]
pub struct TestData {
pub data: u64,
}

#[account]
pub struct Data {
pub udata: u128,
pub idata: i128,
}

#[account]
#[derive(Default)]
pub struct DataU16 {
pub data: u16,
}

#[account]
pub struct DataI8 {
pub data: i8,
}

#[account]
pub struct DataI16 {
pub data: i16,
}

#[account(zero_copy)]
#[derive(Default)]
pub struct DataZeroCopy {
pub data: u16,
pub bump: u8,
}
172 changes: 172 additions & 0 deletions examples/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use crate::account::*;
use crate::misc::MyState;
use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, TokenAccount};
use misc2::misc2::MyState as Misc2State;

#[derive(Accounts)]
#[instruction(nonce: u8)]
pub struct TestTokenSeedsInit<'info> {
#[account(
init,
token = mint,
authority = authority,
seeds = [b"my-token-seed".as_ref(), &[nonce]],
payer = authority,
space = TokenAccount::LEN,
)]
pub my_pda: CpiAccount<'info, TokenAccount>,
pub mint: CpiAccount<'info, Mint>,
pub authority: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub token_program: AccountInfo<'info>,
}

#[derive(Accounts)]
#[instruction(nonce: u8)]
pub struct TestInstructionConstraint<'info> {
#[account(seeds = [b"my-seed", my_account.key.as_ref(), &[nonce]])]
pub my_pda: AccountInfo<'info>,
pub my_account: AccountInfo<'info>,
}

#[derive(Accounts)]
#[instruction(domain: String, seed: Vec<u8>, bump: u8)]
pub struct TestPdaInit<'info> {
#[account(
init,
seeds = [b"my-seed", domain.as_bytes(), foo.key.as_ref(), &seed, &[bump]],
payer = my_payer,
)]
pub my_pda: ProgramAccount<'info, DataU16>,
pub my_payer: AccountInfo<'info>,
pub foo: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
}

#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct TestPdaInitZeroCopy<'info> {
#[account(init, seeds = [b"my-seed".as_ref(), &[bump]], payer = my_payer)]
pub my_pda: Loader<'info, DataZeroCopy>,
pub my_payer: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct TestPdaMutZeroCopy<'info> {
#[account(mut, seeds = [b"my-seed".as_ref(), &[my_pda.load()?.bump]])]
pub my_pda: Loader<'info, DataZeroCopy>,
pub my_payer: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct Ctor {}

#[derive(Accounts)]
pub struct RemainingAccounts {}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init)]
pub data: ProgramAccount<'info, Data>,
pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
pub struct TestOwner<'info> {
#[account(owner = misc)]
pub data: AccountInfo<'info>,
pub misc: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct TestExecutable<'info> {
#[account(executable)]
pub program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct TestStateCpi<'info> {
#[account(signer)]
pub authority: AccountInfo<'info>,
#[account(mut, state = misc2_program)]
pub cpi_state: CpiState<'info, Misc2State>,
#[account(executable)]
pub misc2_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct TestClose<'info> {
#[account(mut, close = sol_dest)]
pub data: ProgramAccount<'info, Data>,
sol_dest: AccountInfo<'info>,
}

// `my_account` is the associated token account being created.
// `authority` must be a `mut` and `signer` since it will pay for the creation
// of the associated token account. `state` is used as an association, i.e., one
// can *optionally* identify targets to be used as seeds for the program
// derived address by using `with` (and it doesn't have to be a state account).
// For example, the SPL token program uses a `Mint` account. Lastly,
// `rent` and `system_program` are *required* by convention, since the
// accounts are needed when creating the associated program address within
// the program.
#[derive(Accounts)]
pub struct TestInitAssociatedAccount<'info> {
#[account(init, associated = authority, with = state, with = data, with = b"my-seed")]
pub my_account: ProgramAccount<'info, TestData>,
#[account(mut, signer)]
pub authority: AccountInfo<'info>,
pub state: ProgramState<'info, MyState>,
pub data: ProgramAccount<'info, Data>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct TestAssociatedAccount<'info> {
#[account(mut, associated = authority, with = state, with = data, with = b"my-seed")]
pub my_account: ProgramAccount<'info, TestData>,
#[account(mut, signer)]
pub authority: AccountInfo<'info>,
pub state: ProgramState<'info, MyState>,
pub data: ProgramAccount<'info, Data>,
}

#[derive(Accounts)]
pub struct TestU16<'info> {
#[account(init)]
pub my_account: ProgramAccount<'info, DataU16>,
pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
pub struct TestI16<'info> {
#[account(init)]
pub data: ProgramAccount<'info, DataI16>,
pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
pub struct TestSimulate {}

#[derive(Accounts)]
pub struct TestSimulateAssociatedAccount<'info> {
#[account(init, associated = authority)]
pub my_account: ProgramAccount<'info, TestData>,
#[account(mut, signer)]
pub authority: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct TestI8<'info> {
#[account(init)]
pub data: ProgramAccount<'info, DataI8>,
pub rent: Sysvar<'info, Rent>,
}
21 changes: 21 additions & 0 deletions examples/misc/programs/misc/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use anchor_lang::prelude::*;

#[event]
pub struct E1 {
pub data: u32,
}

#[event]
pub struct E2 {
pub data: u32,
}

#[event]
pub struct E3 {
pub data: u32,
}

#[event]
pub struct E4 {
pub data: Pubkey,
}
Loading

0 comments on commit f34d073

Please sign in to comment.