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.
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-09-07 13:06:15 -07:00
|
|
|
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
|
|
|
|
|
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 {
|
|
|
|
pub fn new(_ctx: Context<Ctor>) -> Result<Self, ProgramError> {
|
|
|
|
Ok(Self { v: vec![] })
|
|
|
|
}
|
2021-06-11 09:31:59 -07:00
|
|
|
|
|
|
|
pub fn remaining_accounts(&mut self, ctx: Context<RemainingAccounts>) -> ProgramResult {
|
|
|
|
if ctx.remaining_accounts.len() != 1 {
|
|
|
|
return Err(ProgramError::Custom(1)); // Arbitrary error.
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-24 10:35:51 -07:00
|
|
|
}
|
|
|
|
|
2021-02-10 21:24:29 -08:00
|
|
|
pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> ProgramResult {
|
|
|
|
ctx.accounts.data.udata = udata;
|
|
|
|
ctx.accounts.data.idata = idata;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-03 15:13:12 -07:00
|
|
|
|
2021-04-11 21:54:35 -07:00
|
|
|
pub fn test_owner(_ctx: Context<TestOwner>) -> ProgramResult {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:23:43 -07:00
|
|
|
pub fn test_executable(_ctx: Context<TestExecutable>) -> ProgramResult {
|
2021-04-03 15:13:12 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-11 17:23:43 -07:00
|
|
|
|
|
|
|
pub fn test_state_cpi(ctx: Context<TestStateCpi>, data: u64) -> ProgramResult {
|
|
|
|
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
|
|
|
|
2021-05-07 11:20:15 -07:00
|
|
|
pub fn test_u16(ctx: Context<TestU16>, data: u16) -> ProgramResult {
|
|
|
|
ctx.accounts.my_account.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-05-08 21:31:55 -07:00
|
|
|
|
|
|
|
pub fn test_simulate(_ctx: Context<TestSimulate>, data: u32) -> ProgramResult {
|
|
|
|
emit!(E1 { data });
|
|
|
|
emit!(E2 { data: 1234 });
|
|
|
|
emit!(E3 { data: 9 });
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-05-15 15:21:16 -07:00
|
|
|
|
|
|
|
pub fn test_i8(ctx: Context<TestI8>, data: i8) -> ProgramResult {
|
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_i16(ctx: Context<TestI16>, data: i16) -> ProgramResult {
|
|
|
|
ctx.accounts.data.data = data;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-09 15:40:43 -07:00
|
|
|
|
|
|
|
pub fn test_close(_ctx: Context<TestClose>) -> ProgramResult {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-15 17:15:51 -07:00
|
|
|
|
|
|
|
pub fn test_instruction_constraint(
|
|
|
|
_ctx: Context<TestInstructionConstraint>,
|
|
|
|
_nonce: u8,
|
|
|
|
) -> ProgramResult {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_pda_init(
|
|
|
|
ctx: Context<TestPdaInit>,
|
|
|
|
_domain: String,
|
|
|
|
_seed: Vec<u8>,
|
|
|
|
_bump: u8,
|
|
|
|
) -> ProgramResult {
|
|
|
|
ctx.accounts.my_pda.data = 6;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>, bump: u8) -> ProgramResult {
|
|
|
|
let mut acc = ctx.accounts.my_pda.load_init()?;
|
|
|
|
acc.data = 9;
|
|
|
|
acc.bump = bump;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_pda_mut_zero_copy(ctx: Context<TestPdaMutZeroCopy>) -> ProgramResult {
|
|
|
|
let mut acc = ctx.accounts.my_pda.load_mut()?;
|
|
|
|
acc.data = 1234;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-27 13:17:05 -07:00
|
|
|
|
2021-08-14 18:18:28 -07:00
|
|
|
pub fn test_token_seeds_init(
|
|
|
|
_ctx: Context<TestTokenSeedsInit>,
|
|
|
|
_token_bump: u8,
|
|
|
|
_mint_bump: u8,
|
|
|
|
) -> ProgramResult {
|
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],
|
|
|
|
) -> ProgramResult {
|
|
|
|
Err(ProgramError::Custom(1234))
|
|
|
|
}
|
2021-08-29 14:25:38 -07:00
|
|
|
|
|
|
|
pub fn test_init(ctx: Context<TestInit>) -> ProgramResult {
|
|
|
|
ctx.accounts.data.data = 3;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_init_zero_copy(ctx: Context<TestInitZeroCopy>) -> ProgramResult {
|
|
|
|
let mut data = ctx.accounts.data.load_init()?;
|
|
|
|
data.data = 10;
|
|
|
|
data.bump = 2;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_init_mint(ctx: Context<TestInitMint>) -> ProgramResult {
|
|
|
|
assert!(ctx.accounts.mint.decimals == 6);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_init_token(ctx: Context<TestInitToken>) -> ProgramResult {
|
|
|
|
assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-27 13:17:05 -07:00
|
|
|
}
|