forked from solana-foundation/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
142 lines (112 loc) · 4.63 KB
/
lib.rs
File metadata and controls
142 lines (112 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use anchor_lang::prelude::*;
declare_id!("Dec1areProgram11111111111111111111111111111");
declare_program!(external);
use external::program::External;
// Compilation check for legacy IDL (pre Anchor `0.30`)
declare_program!(external_legacy);
// Compilation check for the Raydium AMM v3 program (Anchor v0.29.0)
// https://github.com/raydium-io/raydium-idl/blob/c8507c78618eda1de96ff5e43bd29daefa7e9307/raydium_clmm/amm_v3.json
declare_program!(amm_v3);
#[program]
pub mod declare_program {
use super::*;
pub fn cpi(ctx: Context<Cpi>, value: u32) -> Result<()> {
let cpi_my_account = &mut ctx.accounts.cpi_my_account;
require_keys_eq!(external::accounts::MyAccount::owner(), external::ID);
require_eq!(cpi_my_account.field, 0);
let cpi_ctx = CpiContext::new(
ctx.accounts.external_program.key(),
external::cpi::accounts::Update {
authority: ctx.accounts.authority.to_account_info(),
my_account: cpi_my_account.to_account_info(),
},
);
external::cpi::update(cpi_ctx, value)?;
cpi_my_account.reload()?;
require_eq!(cpi_my_account.field, value);
Ok(())
}
pub fn cpi_composite(ctx: Context<Cpi>, value: u32) -> Result<()> {
let cpi_my_account = &mut ctx.accounts.cpi_my_account;
// Composite accounts that's also an instruction
let cpi_ctx = CpiContext::new(
ctx.accounts.external_program.key(),
external::cpi::accounts::UpdateComposite {
update: external::cpi::accounts::Update {
authority: ctx.accounts.authority.to_account_info(),
my_account: cpi_my_account.to_account_info(),
},
},
);
external::cpi::update_composite(cpi_ctx, 42)?;
cpi_my_account.reload()?;
require_eq!(cpi_my_account.field, 42);
// Composite accounts but not an actual instruction
let cpi_ctx = CpiContext::new(
ctx.accounts.external_program.key(),
external::cpi::accounts::UpdateNonInstructionComposite {
non_instruction_update: external::cpi::accounts::NonInstructionUpdate {
authority: ctx.accounts.authority.to_account_info(),
my_account: cpi_my_account.to_account_info(),
program: ctx.accounts.external_program.to_account_info(),
},
},
);
external::cpi::update_non_instruction_composite(cpi_ctx, value)?;
cpi_my_account.reload()?;
require_eq!(cpi_my_account.field, value);
Ok(())
}
pub fn account_utils(_ctx: Context<Utils>) -> Result<()> {
use external::utils::Account;
// Empty
if Account::try_from_bytes(&[]).is_ok() {
return Err(ProgramError::Custom(0).into());
}
const DISC: &[u8] = external::accounts::MyAccount::DISCRIMINATOR;
// Correct discriminator but invalid data
if Account::try_from_bytes(DISC).is_ok() {
return Err(ProgramError::Custom(1).into());
};
// Correct discriminator and valid data
match Account::try_from_bytes(&[DISC, &[1, 0, 0, 0]].concat()) {
Ok(Account::MyAccount(my_account)) => require_eq!(my_account.field, 1),
Err(e) => return Err(e.into()),
}
Ok(())
}
pub fn event_utils(_ctx: Context<Utils>) -> Result<()> {
use external::utils::Event;
// Empty
if Event::try_from_bytes(&[]).is_ok() {
return Err(ProgramError::Custom(0).into());
}
const DISC: &[u8] = external::events::MyEvent::DISCRIMINATOR;
// Correct discriminator but invalid data
if Event::try_from_bytes(DISC).is_ok() {
return Err(ProgramError::Custom(1).into());
};
// Correct discriminator and valid data
match Event::try_from_bytes(&[DISC, &[1, 0, 0, 0]].concat()) {
Ok(Event::MyEvent(my_event)) => require_eq!(my_event.value, 1),
Err(e) => return Err(e.into()),
}
Ok(())
}
pub fn test_amm_v3_errors(_ctx: Context<Utils>) -> Result<()> {
let _error: amm_v3::ProgramError = amm_v3::ProgramError::LOK;
let _error2: amm_v3::errors::ProgramError = amm_v3::errors::ProgramError::NotApproved;
Ok(())
}
}
#[derive(Accounts)]
pub struct Cpi<'info> {
pub authority: Signer<'info>,
#[account(mut)]
pub cpi_my_account: Account<'info, external::accounts::MyAccount>,
pub external_program: Program<'info, External>,
}
#[derive(Accounts)]
pub struct Utils<'info> {
pub authority: Signer<'info>,
}