2021-02-10 21:24:29 -08:00
|
|
|
//! Misc example is a catchall program for testing unrelated features.
|
|
|
|
//! It's not too instructive/coherent by itself, so please see other examples.
|
|
|
|
|
2022-03-20 12:15:04 -07:00
|
|
|
use account::MAX_SIZE;
|
2021-02-10 21:24:29 -08:00
|
|
|
use anchor_lang::prelude::*;
|
2021-07-16 14:36:32 -07:00
|
|
|
use context::*;
|
|
|
|
use event::*;
|
2021-04-11 17:23:43 -07:00
|
|
|
use misc2::Auth;
|
2021-02-10 21:24:29 -08:00
|
|
|
|
2021-07-16 14:36:32 -07:00
|
|
|
mod account;
|
|
|
|
mod context;
|
|
|
|
mod event;
|
|
|
|
|
2022-03-26 17:28:55 -07:00
|
|
|
declare_id!("3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh");
|
2021-09-07 13:06:15 -07:00
|
|
|
|
2021-12-03 15:49:38 -08:00
|
|
|
#[constant]
|
|
|
|
pub const BASE: u128 = 1_000_000;
|
|
|
|
#[constant]
|
|
|
|
pub const DECIMALS: u8 = 6;
|
|
|
|
pub const NO_IDL: u16 = 55;
|
|
|
|
|
2021-02-10 21:24:29 -08:00
|
|
|
#[program]
|
|
|
|
pub mod misc {
|
|
|
|
use super::*;
|
2021-03-24 10:35:51 -07:00
|
|
|
|
|
|
|
pub const SIZE: u64 = 99;
|
|
|
|
|
|
|
|
#[state(SIZE)]
|
|
|
|
pub struct MyState {
|
|
|
|
pub v: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MyState {
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn new(_ctx: Context<Ctor>) -> Result<Self> {
|
2021-03-24 10:35:51 -07:00
|
|
|
Ok(Self { v: vec![] })
|
|
|
|
}
|
2021-06-11 09:31:59 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn remaining_accounts(&mut self, ctx: Context<RemainingAccounts>) -> Result<()> {
|
2021-06-11 09:31:59 -07:00
|
|
|
if ctx.remaining_accounts.len() != 1 {
|
2022-02-20 14:28:24 -08:00
|
|
|
return Err(ProgramError::Custom(1).into()); // Arbitrary error.
|
2021-06-11 09:31:59 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-24 10:35:51 -07:00
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> Result<()> {
|
2021-02-10 21:24:29 -08:00
|
|
|
ctx.accounts.data.udata = udata;
|
|
|
|
ctx.accounts.data.idata = idata;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-03 15:13:12 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn initialize_no_rent_exempt(ctx: Context<InitializeNoRentExempt>) -> Result<()> {
|
2021-12-30 12:25:10 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn initialize_skip_rent_exempt(ctx: Context<InitializeSkipRentExempt>) -> Result<()> {
|
2021-12-30 12:25:10 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_owner(_ctx: Context<TestOwner>) -> Result<()> {
|
2021-04-11 21:54:35 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_executable(_ctx: Context<TestExecutable>) -> Result<()> {
|
2021-04-03 15:13:12 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-11 17:23:43 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_state_cpi(ctx: Context<TestStateCpi>, data: u64) -> Result<()> {
|
2021-04-11 17:23:43 -07:00
|
|
|
let cpi_program = ctx.accounts.misc2_program.clone();
|
|
|
|
let cpi_accounts = Auth {
|
|
|
|
authority: ctx.accounts.authority.clone(),
|
|
|
|
};
|
|
|
|
let ctx = ctx.accounts.cpi_state.context(cpi_program, cpi_accounts);
|
|
|
|
misc2::cpi::state::set_data(ctx, data)
|
|
|
|
}
|
2021-04-13 11:47:54 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_u16(ctx: Context<TestU16>, data: u16) -> Result<()> {
|
2021-05-07 11:20:15 -07:00
|
|
|
ctx.accounts.my_account.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-05-08 21:31:55 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_simulate(_ctx: Context<TestSimulate>, data: u32) -> Result<()> {
|
2021-05-08 21:31:55 -07:00
|
|
|
emit!(E1 { data });
|
|
|
|
emit!(E2 { data: 1234 });
|
|
|
|
emit!(E3 { data: 9 });
|
2022-03-11 15:05:19 -08:00
|
|
|
emit!(E5 {
|
|
|
|
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
|
|
});
|
|
|
|
emit!(E6 {
|
|
|
|
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
|
|
|
|
});
|
2021-05-08 21:31:55 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-05-15 15:21:16 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_i8(ctx: Context<TestI8>, data: i8) -> Result<()> {
|
2021-05-15 15:21:16 -07:00
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_i16(ctx: Context<TestI16>, data: i16) -> Result<()> {
|
2021-05-15 15:21:16 -07:00
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-09 15:40:43 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_const_array_size(ctx: Context<TestConstArraySize>, data: u8) -> Result<()> {
|
2021-12-22 20:13:46 -08:00
|
|
|
ctx.accounts.data.data[0] = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-20 12:15:04 -07:00
|
|
|
pub fn test_const_ix_data_size(
|
|
|
|
ctx: Context<TestConstIxDataSize>,
|
|
|
|
data: [u8; MAX_SIZE],
|
|
|
|
) -> Result<()> {
|
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_close(_ctx: Context<TestClose>) -> Result<()> {
|
2021-06-09 15:40:43 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-15 17:15:51 -07:00
|
|
|
|
|
|
|
pub fn test_instruction_constraint(
|
|
|
|
_ctx: Context<TestInstructionConstraint>,
|
|
|
|
_nonce: u8,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-06-15 17:15:51 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_pda_init(
|
|
|
|
ctx: Context<TestPdaInit>,
|
|
|
|
_domain: String,
|
|
|
|
_seed: Vec<u8>,
|
|
|
|
_bump: u8,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-06-15 17:15:51 -07:00
|
|
|
ctx.accounts.my_pda.data = 6;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>) -> Result<()> {
|
2021-06-15 17:15:51 -07:00
|
|
|
let mut acc = ctx.accounts.my_pda.load_init()?;
|
|
|
|
acc.data = 9;
|
2022-02-07 10:26:05 -08:00
|
|
|
acc.bump = *ctx.bumps.get("my_pda").unwrap();
|
2021-06-15 17:15:51 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_pda_mut_zero_copy(ctx: Context<TestPdaMutZeroCopy>) -> Result<()> {
|
2021-06-15 17:15:51 -07:00
|
|
|
let mut acc = ctx.accounts.my_pda.load_mut()?;
|
|
|
|
acc.data = 1234;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-27 13:17:05 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_token_seeds_init(_ctx: Context<TestTokenSeedsInit>) -> Result<()> {
|
2021-06-27 13:17:05 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-02 17:33:48 -07:00
|
|
|
|
|
|
|
pub fn default<'info>(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
_accounts: &[AccountInfo<'info>],
|
|
|
|
_data: &[u8],
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
|
|
|
Err(ProgramError::Custom(1234).into())
|
2021-07-02 17:33:48 -07:00
|
|
|
}
|
2021-08-29 14:25:38 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init(ctx: Context<TestInit>) -> Result<()> {
|
2021-08-29 14:25:38 -07:00
|
|
|
ctx.accounts.data.data = 3;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init_zero_copy(ctx: Context<TestInitZeroCopy>) -> Result<()> {
|
2021-08-29 14:25:38 -07:00
|
|
|
let mut data = ctx.accounts.data.load_init()?;
|
|
|
|
data.data = 10;
|
|
|
|
data.bump = 2;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init_mint(ctx: Context<TestInitMint>) -> Result<()> {
|
2021-08-29 14:25:38 -07:00
|
|
|
assert!(ctx.accounts.mint.decimals == 6);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init_token(ctx: Context<TestInitToken>) -> Result<()> {
|
2021-08-29 14:25:38 -07:00
|
|
|
assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-21 12:14:15 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_composite_payer(ctx: Context<TestCompositePayer>) -> Result<()> {
|
2021-09-21 12:14:15 -07:00
|
|
|
ctx.accounts.composite.data.data = 1;
|
|
|
|
ctx.accounts.data.udata = 2;
|
|
|
|
ctx.accounts.data.idata = 3;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-23 17:06:38 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init_associated_token(ctx: Context<TestInitAssociatedToken>) -> Result<()> {
|
2021-09-23 17:06:38 -07:00
|
|
|
assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-25 07:22:12 -07:00
|
|
|
|
2021-10-07 13:57:47 -07:00
|
|
|
pub fn test_validate_associated_token(
|
|
|
|
_ctx: Context<TestValidateAssociatedToken>,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-10-07 13:57:47 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_fetch_all(ctx: Context<TestFetchAll>, filterable: Pubkey) -> Result<()> {
|
2021-09-25 07:22:12 -07:00
|
|
|
ctx.accounts.data.authority = ctx.accounts.authority.key();
|
|
|
|
ctx.accounts.data.filterable = filterable;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-09 12:21:15 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init_with_empty_seeds(ctx: Context<TestInitWithEmptySeeds>) -> Result<()> {
|
2021-10-09 12:21:15 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_empty_seeds_constraint(ctx: Context<TestEmptySeedsConstraint>) -> Result<()> {
|
2021-10-09 12:21:15 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-21 16:05:16 -07:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init_if_needed(ctx: Context<TestInitIfNeeded>, data: u16) -> Result<()> {
|
2021-10-21 16:05:16 -07:00
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-02 11:32:24 -07:00
|
|
|
|
2021-12-30 12:25:10 -08:00
|
|
|
pub fn test_init_if_needed_checks_owner(
|
|
|
|
ctx: Context<TestInitIfNeededChecksOwner>,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-12-08 13:50:08 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:25:10 -08:00
|
|
|
pub fn test_init_if_needed_checks_seeds(
|
|
|
|
ctx: Context<TestInitIfNeededChecksSeeds>,
|
|
|
|
seed_data: String,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-12-08 13:50:08 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:25:10 -08:00
|
|
|
pub fn test_init_mint_if_needed(
|
|
|
|
ctx: Context<TestInitMintIfNeeded>,
|
|
|
|
decimals: u8,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-12-08 13:50:08 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_init_token_if_needed(ctx: Context<TestInitTokenIfNeeded>) -> Result<()> {
|
2021-12-08 13:50:08 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:25:10 -08:00
|
|
|
pub fn test_init_associated_token_if_needed(
|
|
|
|
ctx: Context<TestInitAssociatedTokenIfNeeded>,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-12-08 13:50:08 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn init_with_space(ctx: Context<InitWithSpace>, data: u16) -> Result<()> {
|
2021-12-08 13:50:08 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-02 11:32:24 -07:00
|
|
|
pub fn test_multidimensional_array(
|
|
|
|
ctx: Context<TestMultidimensionalArray>,
|
|
|
|
data: [[u8; 10]; 10],
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2021-11-02 11:32:24 -07:00
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-12-30 12:25:10 -08:00
|
|
|
|
2022-03-11 15:05:19 -08:00
|
|
|
pub fn test_multidimensional_array_const_sizes(
|
|
|
|
ctx: Context<TestMultidimensionalArrayConstSizes>,
|
|
|
|
data: [[u8; 11]; 10],
|
|
|
|
) -> Result<()> {
|
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_no_rent_exempt(ctx: Context<NoRentExempt>) -> Result<()> {
|
2021-12-30 12:25:10 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn test_enforce_rent_exempt(ctx: Context<EnforceRentExempt>) -> Result<()> {
|
2021-12-30 12:25:10 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-06 12:31:36 -08:00
|
|
|
|
2022-02-20 14:28:24 -08:00
|
|
|
pub fn init_decrease_lamports(ctx: Context<InitDecreaseLamports>) -> Result<()> {
|
2022-01-06 12:31:36 -08:00
|
|
|
**ctx.accounts.data.try_borrow_mut_lamports()? -= 1;
|
|
|
|
**ctx.accounts.user.try_borrow_mut_lamports()? += 1;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-07 10:26:05 -08:00
|
|
|
|
|
|
|
pub fn init_if_needed_checks_rent_exemption(
|
|
|
|
_ctx: Context<InitIfNeededChecksRentExemption>,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2022-01-06 12:31:36 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-07 10:26:05 -08:00
|
|
|
|
2022-01-11 07:51:22 -08:00
|
|
|
pub fn test_program_id_constraint(
|
|
|
|
_ctx: Context<TestProgramIdConstraint>,
|
|
|
|
_bump: u8,
|
2022-02-07 10:26:05 -08:00
|
|
|
_second_bump: u8,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2022-01-11 07:51:22 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_program_id_constraint_find_pda(
|
|
|
|
_ctx: Context<TestProgramIdConstraintUsingFindPda>,
|
2022-02-20 14:28:24 -08:00
|
|
|
) -> Result<()> {
|
2022-01-11 07:51:22 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-27 13:17:05 -07:00
|
|
|
}
|