Skip to content

Commit

Permalink
ts: Add support for unnamed(tuple) enum in accounts (coral-xyz#2601)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto committed Aug 13, 2023
1 parent b5cf67f commit 454f1dd
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 74 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ The minor version will be incremented upon a breaking change and the patch versi
- avm: Add support for the `.anchorversion` file to facilitate switching between different versions of the `anchor-cli` ([#2553](https://github.com/coral-xyz/anchor/pull/2553)).
- ts: Add ability to access workspace programs independent of the casing used, e.g. `anchor.workspace.myProgram`, `anchor.workspace.MyProgram`... ([#2579](https://github.com/coral-xyz/anchor/pull/2579)).
- spl: Export `mpl-token-metadata` crate ([#2583](https://github.com/coral-xyz/anchor/pull/2583)).
- spl: Add `TokenRecordAccount` for pNFTs ([#2597](https://github.com/coral-xyz/anchor/pull/2597)).
- ts: Add support for unnamed(tuple) enum in accounts([#2601](https://github.com/coral-xyz/anchor/pull/2601)).

### Fixes

Expand Down
22 changes: 22 additions & 0 deletions tests/misc/programs/misc-optional/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub struct DataI16 {
}
size!(DataI16, 2);

#[account]
pub struct DataEnum {
pub data: TestEnum, // 1 + 16
}
size!(DataEnum, 17);

#[account(zero_copy)]
pub struct DataZeroCopy {
pub data: u16, // 2
Expand Down Expand Up @@ -73,3 +79,19 @@ pub struct DataConstCastArraySize {
pub struct DataMultidimensionalArrayConstSizes {
pub data: [[u8; MAX_SIZE_U8 as usize]; MAX_SIZE],
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub enum TestEnum {
First,
Second { x: u64, y: u64 },
TupleTest(u8, u8, u16, u16),
TupleStructTest(TestStruct),
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub struct TestStruct {
pub data1: u8,
pub data2: u16,
pub data3: u32,
pub data4: u64,
}
8 changes: 8 additions & 0 deletions tests/misc/programs/misc-optional/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ pub struct TestI16<'info> {
#[derive(Accounts)]
pub struct TestSimulate {}

#[derive(Accounts)]
pub struct TestAccountEnum<'info> {
#[account(init, payer = payer.as_ref().unwrap(), space = 8+ DataEnum::LEN )]
pub data: Option<Account<'info, DataEnum>>,
pub payer: Option<Signer<'info>>,
pub system_program: Option<Program<'info, System>>,
}

#[derive(Accounts)]
pub struct TestI8<'info> {
#[account(zero)]
Expand Down
18 changes: 2 additions & 16 deletions tests/misc/programs/misc-optional/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anchor_lang::prelude::*;

use crate::account::*;

pub const MAX_EVENT_SIZE: usize = 10;
pub const MAX_EVENT_SIZE_U8: u8 = 11;

Expand Down Expand Up @@ -33,22 +35,6 @@ pub struct E6 {
pub data: [u8; MAX_EVENT_SIZE_U8 as usize],
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub struct TestStruct {
pub data1: u8,
pub data2: u16,
pub data3: u32,
pub data4: u64,
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub enum TestEnum {
First,
Second { x: u64, y: u64 },
TupleTest(u8, u8, u16, u16),
TupleStructTest(TestStruct),
}

#[event]
pub struct E7 {
pub data: TestEnum,
Expand Down
36 changes: 26 additions & 10 deletions tests/misc/programs/misc-optional/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Misc optional example is a catchall program for testing unrelated features.
//! It's not too instructive/coherent by itself, so please see other examples.

use account::MAX_SIZE;
use account::*;
use anchor_lang::prelude::*;
use context::*;
use event::*;
Expand Down Expand Up @@ -62,11 +62,16 @@ pub mod misc_optional {
Ok(())
}

pub fn test_input_enum(ctx: Context<TestSimulate>, data: TestEnum) -> Result<()> {
pub fn test_input_enum(_ctx: Context<TestSimulate>, data: TestEnum) -> Result<()> {
emit!(E7 { data: data });
Ok(())
}

pub fn test_account_enum(ctx: Context<TestAccountEnum>, data: TestEnum) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data = data;
Ok(())
}

pub fn test_i8(ctx: Context<TestI8>, data: i8) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data = data;
Ok(())
Expand Down Expand Up @@ -171,7 +176,9 @@ pub mod misc_optional {
Ok(())
}

pub fn test_init_mint_with_token_program(_ctx: Context<TestInitMintWithTokenProgram>) -> Result<()> {
pub fn test_init_mint_with_token_program(
_ctx: Context<TestInitMintWithTokenProgram>,
) -> Result<()> {
Ok(())
}

Expand All @@ -182,11 +189,12 @@ pub mod misc_optional {
Ok(())
}

pub fn test_init_token_with_token_program(_ctx: Context<TestInitTokenWithTokenProgram>) -> Result<()> {
pub fn test_init_token_with_token_program(
_ctx: Context<TestInitTokenWithTokenProgram>,
) -> Result<()> {
Ok(())
}


pub fn test_composite_payer(ctx: Context<TestCompositePayer>) -> Result<()> {
ctx.accounts.composite.data.as_mut().unwrap().data = 1;
ctx.accounts.data.as_mut().unwrap().udata = 2;
Expand All @@ -201,7 +209,9 @@ pub mod misc_optional {
Ok(())
}

pub fn test_init_associated_token_with_token_program(ctx: Context<TestInitAssociatedTokenWithTokenProgram>) -> Result<()> {
pub fn test_init_associated_token_with_token_program(
_ctx: Context<TestInitAssociatedTokenWithTokenProgram>,
) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -261,7 +271,9 @@ pub mod misc_optional {
Ok(())
}

pub fn test_init_token_if_needed_with_token_program(_ctx: Context<TestInitTokenIfNeededWithTokenProgram>) -> Result<()> {
pub fn test_init_token_if_needed_with_token_program(
_ctx: Context<TestInitTokenIfNeededWithTokenProgram>,
) -> Result<()> {
Ok(())
}

Expand All @@ -277,7 +289,7 @@ pub mod misc_optional {
Ok(())
}

pub fn init_with_space(_ctx: Context<InitWithSpace>, data: u16) -> Result<()> {
pub fn init_with_space(_ctx: Context<InitWithSpace>, _data: u16) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -357,7 +369,9 @@ pub mod misc_optional {
Ok(())
}

pub fn test_only_token_program_constraint(_ctx: Context<TestOnlyTokenProgramConstraint>) -> Result<()> {
pub fn test_only_token_program_constraint(
_ctx: Context<TestOnlyTokenProgramConstraint>,
) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -401,7 +415,9 @@ pub mod misc_optional {
Ok(())
}

pub fn test_associated_token_with_token_program_constraint(_ctx: Context<TestAssociatedTokenWithTokenProgramConstraint>) -> Result<()> {
pub fn test_associated_token_with_token_program_constraint(
_ctx: Context<TestAssociatedTokenWithTokenProgramConstraint>,
) -> Result<()> {
Ok(())
}
}
22 changes: 22 additions & 0 deletions tests/misc/programs/misc/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub struct DataI16 {
}
size!(DataI16, 2);

#[account]
pub struct DataEnum {
pub data: TestEnum, // 1 + 16
}
size!(DataEnum, 17);

#[account(zero_copy)]
pub struct DataZeroCopy {
pub data: u16, // 2
Expand Down Expand Up @@ -94,3 +100,19 @@ pub enum CoolEnum {
some_slot: u64,
},
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub enum TestEnum {
First,
Second { x: u64, y: u64 },
TupleTest(u8, u8, u16, u16),
TupleStructTest(TestStruct),
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub struct TestStruct {
pub data1: u8,
pub data2: u16,
pub data3: u32,
pub data4: u64,
}
11 changes: 10 additions & 1 deletion tests/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ pub struct TestI16<'info> {
#[derive(Accounts)]
pub struct TestSimulate {}

#[derive(Accounts)]
pub struct TestAccountEnum<'info> {
#[account(init, payer = payer, space = 8 + DataEnum::LEN)]
pub data: Account<'info, DataEnum>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct TestI8<'info> {
#[account(zero)]
Expand Down Expand Up @@ -767,4 +776,4 @@ pub struct TestUsedIdentifiers<'info> {
)]
/// CHECK: ignore
pub test4: AccountInfo<'info>,
}
}
22 changes: 4 additions & 18 deletions tests/misc/programs/misc/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anchor_lang::prelude::*;

use crate::account::*;

pub const MAX_EVENT_SIZE: usize = 10;
pub const MAX_EVENT_SIZE_U8: u8 = 11;

Expand Down Expand Up @@ -33,23 +35,7 @@ pub struct E6 {
pub data: [u8; MAX_EVENT_SIZE_U8 as usize],
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub struct TestStruct {
pub data1: u8,
pub data2: u16,
pub data3: u32,
pub data4: u64,
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
pub enum TestEnum {
First,
Second {x: u64, y: u64},
TupleTest (u8, u8, u16, u16),
TupleStructTest (TestStruct),
}

#[event]
#[event]
pub struct E7 {
pub data: TestEnum,
}
}
35 changes: 26 additions & 9 deletions tests/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Misc example is a catchall program for testing unrelated features.
//! It's not too instructive/coherent by itself, so please see other examples.

use account::MAX_SIZE;
use account::*;
use anchor_lang::prelude::*;
use context::*;
use event::*;
Expand Down Expand Up @@ -71,6 +71,11 @@ pub mod misc {
Ok(())
}

pub fn test_account_enum(ctx: Context<TestAccountEnum>, data: TestEnum) -> Result<()> {
ctx.accounts.data.data = data;
Ok(())
}

pub fn test_i8(ctx: Context<TestI8>, data: i8) -> Result<()> {
ctx.accounts.data.data = data;
Ok(())
Expand Down Expand Up @@ -175,7 +180,9 @@ pub mod misc {
Ok(())
}

pub fn test_init_mint_with_token_program(_ctx: Context<TestInitMintWithTokenProgram>) -> Result<()> {
pub fn test_init_mint_with_token_program(
_ctx: Context<TestInitMintWithTokenProgram>,
) -> Result<()> {
Ok(())
}

Expand All @@ -184,7 +191,9 @@ pub mod misc {
Ok(())
}

pub fn test_init_token_with_token_program(_ctx: Context<TestInitTokenWithTokenProgram>) -> Result<()> {
pub fn test_init_token_with_token_program(
_ctx: Context<TestInitTokenWithTokenProgram>,
) -> Result<()> {
Ok(())
}

Expand All @@ -200,7 +209,9 @@ pub mod misc {
Ok(())
}

pub fn test_init_associated_token_with_token_program(_ctx: Context<TestInitAssociatedTokenWithTokenProgram>) -> Result<()> {
pub fn test_init_associated_token_with_token_program(
_ctx: Context<TestInitAssociatedTokenWithTokenProgram>,
) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -250,7 +261,7 @@ pub mod misc {
}

pub fn test_init_mint_if_needed_with_token_program(
_ctx: Context<TestInitMintIfNeededWithTokenProgram>
_ctx: Context<TestInitMintIfNeededWithTokenProgram>,
) -> Result<()> {
Ok(())
}
Expand All @@ -259,7 +270,9 @@ pub mod misc {
Ok(())
}

pub fn test_init_token_if_needed_with_token_program(_ctx: Context<TestInitTokenIfNeededWithTokenProgram>) -> Result<()> {
pub fn test_init_token_if_needed_with_token_program(
_ctx: Context<TestInitTokenIfNeededWithTokenProgram>,
) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -345,7 +358,9 @@ pub mod misc {
Ok(())
}

pub fn test_only_token_program_constraint(_ctx: Context<TestOnlyTokenProgramConstraint>) -> Result<()> {
pub fn test_only_token_program_constraint(
_ctx: Context<TestOnlyTokenProgramConstraint>,
) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -389,7 +404,9 @@ pub mod misc {
Ok(())
}

pub fn test_associated_token_with_token_program_constraint(_ctx: Context<TestAssociatedTokenWithTokenProgramConstraint>) -> Result<()> {
pub fn test_associated_token_with_token_program_constraint(
_ctx: Context<TestAssociatedTokenWithTokenProgramConstraint>,
) -> Result<()> {
Ok(())
}

Expand All @@ -399,7 +416,7 @@ pub mod misc {
program_id: u8,
accounts: u8,
ix_data: u8,
remaining_accounts: u8
remaining_accounts: u8,
) -> Result<()> {
Ok(())
}
Expand Down
Loading

0 comments on commit 454f1dd

Please sign in to comment.