123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- //! 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::*;
- use anchor_spl::token::{Mint, TokenAccount};
- use misc2::misc2::MyState as Misc2State;
- use misc2::Auth;
- #[program]
- pub mod misc {
- use super::*;
- 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![] })
- }
- pub fn remaining_accounts(&mut self, ctx: Context<RemainingAccounts>) -> ProgramResult {
- if ctx.remaining_accounts.len() != 1 {
- return Err(ProgramError::Custom(1)); // Arbitrary error.
- }
- Ok(())
- }
- }
- pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> ProgramResult {
- ctx.accounts.data.udata = udata;
- ctx.accounts.data.idata = idata;
- Ok(())
- }
- pub fn test_owner(_ctx: Context<TestOwner>) -> ProgramResult {
- Ok(())
- }
- pub fn test_executable(_ctx: Context<TestExecutable>) -> ProgramResult {
- Ok(())
- }
- 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)
- }
- pub fn test_init_associated_account(
- ctx: Context<TestInitAssociatedAccount>,
- data: u64,
- ) -> ProgramResult {
- ctx.accounts.my_account.data = data;
- Ok(())
- }
- pub fn test_associated_account(
- ctx: Context<TestAssociatedAccount>,
- data: u64,
- ) -> ProgramResult {
- ctx.accounts.my_account.data = data;
- Ok(())
- }
- pub fn test_u16(ctx: Context<TestU16>, data: u16) -> ProgramResult {
- ctx.accounts.my_account.data = data;
- Ok(())
- }
- pub fn test_simulate(_ctx: Context<TestSimulate>, data: u32) -> ProgramResult {
- emit!(E1 { data });
- emit!(E2 { data: 1234 });
- emit!(E3 { data: 9 });
- Ok(())
- }
- pub fn test_simulate_associated_account(
- ctx: Context<TestSimulateAssociatedAccount>,
- data: u32,
- ) -> ProgramResult {
- let associated_account = *ctx.accounts.my_account.to_account_info().key;
- emit!(E1 { data });
- emit!(E2 { data: 1234 });
- emit!(E3 { data: 9 });
- emit!(E4 {
- data: associated_account
- });
- Ok(())
- }
- 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(())
- }
- pub fn test_close(_ctx: Context<TestClose>) -> ProgramResult {
- Ok(())
- }
- 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(())
- }
- pub fn test_token_seeds_init(_ctx: Context<TestTokenSeedsInit>, _nonce: u8) -> ProgramResult {
- Ok(())
- }
- pub fn default<'info>(
- _program_id: &Pubkey,
- _accounts: &[AccountInfo<'info>],
- _data: &[u8],
- ) -> ProgramResult {
- Err(ProgramError::Custom(1234))
- }
- }
- #[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,
- )]
- my_pda: ProgramAccount<'info, DataU16>,
- my_payer: AccountInfo<'info>,
- foo: AccountInfo<'info>,
- rent: Sysvar<'info, Rent>,
- 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)]
- my_pda: Loader<'info, DataZeroCopy>,
- my_payer: AccountInfo<'info>,
- rent: Sysvar<'info, Rent>,
- system_program: AccountInfo<'info>,
- }
- #[derive(Accounts)]
- pub struct TestPdaMutZeroCopy<'info> {
- #[account(mut, seeds = [b"my-seed".as_ref(), &[my_pda.load()?.bump]])]
- my_pda: Loader<'info, DataZeroCopy>,
- my_payer: AccountInfo<'info>,
- }
- #[derive(Accounts)]
- pub struct Ctor {}
- #[derive(Accounts)]
- pub struct RemainingAccounts {}
- #[derive(Accounts)]
- pub struct Initialize<'info> {
- #[account(init)]
- data: ProgramAccount<'info, Data>,
- rent: Sysvar<'info, Rent>,
- }
- #[derive(Accounts)]
- pub struct TestOwner<'info> {
- #[account(owner = misc)]
- data: AccountInfo<'info>,
- misc: AccountInfo<'info>,
- }
- #[derive(Accounts)]
- pub struct TestExecutable<'info> {
- #[account(executable)]
- program: AccountInfo<'info>,
- }
- #[derive(Accounts)]
- pub struct TestStateCpi<'info> {
- #[account(signer)]
- authority: AccountInfo<'info>,
- #[account(mut, state = misc2_program)]
- cpi_state: CpiState<'info, Misc2State>,
- #[account(executable)]
- misc2_program: AccountInfo<'info>,
- }
- #[derive(Accounts)]
- pub struct TestClose<'info> {
- #[account(mut, close = sol_dest)]
- 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")]
- my_account: ProgramAccount<'info, TestData>,
- #[account(mut, signer)]
- authority: AccountInfo<'info>,
- state: ProgramState<'info, MyState>,
- data: ProgramAccount<'info, Data>,
- rent: Sysvar<'info, Rent>,
- system_program: AccountInfo<'info>,
- }
- #[derive(Accounts)]
- pub struct TestAssociatedAccount<'info> {
- #[account(mut, associated = authority, with = state, with = data, with = b"my-seed")]
- my_account: ProgramAccount<'info, TestData>,
- #[account(mut, signer)]
- authority: AccountInfo<'info>,
- state: ProgramState<'info, MyState>,
- data: ProgramAccount<'info, Data>,
- }
- #[derive(Accounts)]
- pub struct TestU16<'info> {
- #[account(init)]
- my_account: ProgramAccount<'info, DataU16>,
- rent: Sysvar<'info, Rent>,
- }
- #[derive(Accounts)]
- pub struct TestI16<'info> {
- #[account(init)]
- data: ProgramAccount<'info, DataI16>,
- rent: Sysvar<'info, Rent>,
- }
- #[derive(Accounts)]
- pub struct TestSimulate {}
- #[derive(Accounts)]
- pub struct TestSimulateAssociatedAccount<'info> {
- #[account(init, associated = authority)]
- my_account: ProgramAccount<'info, TestData>,
- #[account(mut, signer)]
- authority: AccountInfo<'info>,
- rent: Sysvar<'info, Rent>,
- system_program: AccountInfo<'info>,
- }
- #[derive(Accounts)]
- pub struct TestI8<'info> {
- #[account(init)]
- data: ProgramAccount<'info, DataI8>,
- rent: Sysvar<'info, Rent>,
- }
- #[associated]
- #[derive(Default)]
- pub struct TestData {
- data: u64,
- }
- #[account]
- pub struct Data {
- udata: u128,
- idata: i128,
- }
- #[account]
- #[derive(Default)]
- pub struct DataU16 {
- data: u16,
- }
- #[account]
- pub struct DataI8 {
- data: i8,
- }
- #[account]
- pub struct DataI16 {
- data: i16,
- }
- #[account(zero_copy)]
- #[derive(Default)]
- pub struct DataZeroCopy {
- data: u16,
- bump: u8,
- }
- #[event]
- pub struct E1 {
- data: u32,
- }
- #[event]
- pub struct E2 {
- data: u32,
- }
- #[event]
- pub struct E3 {
- data: u32,
- }
- #[event]
- pub struct E4 {
- data: Pubkey,
- }
|