-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathtest_add_price.rs
212 lines (188 loc) · 6.54 KB
/
test_add_price.rs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use {
crate::{
accounts::{
clear_account,
MappingAccount,
PermissionAccount,
PriceAccount,
ProductAccount,
PythAccount,
},
c_oracle_header::{
PC_VERSION,
PRICE_ACCOUNT_DEFAULT_MIN_PUB,
},
deserialize::load_checked,
error::OracleError,
instruction::{
AddPriceArgs,
CommandHeader,
OracleCommand,
},
processor::process_instruction,
tests::test_utils::AccountSetup,
},
bytemuck::bytes_of,
solana_program::{
program_error::ProgramError,
pubkey::Pubkey,
},
};
#[test]
fn test_add_price() {
let hdr_add_product = OracleCommand::AddProduct.into();
let mut hdr_add_price = AddPriceArgs {
header: OracleCommand::AddPrice.into(),
exponent: 1,
price_type: 1,
};
let instruction_data_add_product = bytes_of::<CommandHeader>(&hdr_add_product);
let mut instruction_data_add_price = bytes_of::<AddPriceArgs>(&hdr_add_price);
let program_id = Pubkey::new_unique();
let mut funding_setup = AccountSetup::new_funding();
let funding_account = funding_setup.as_account_info();
let mut mapping_setup = AccountSetup::new::<MappingAccount>(&program_id);
let mapping_account = mapping_setup.as_account_info();
MappingAccount::initialize(&mapping_account, PC_VERSION).unwrap();
let mut product_setup = AccountSetup::new::<ProductAccount>(&program_id);
let product_account = product_setup.as_account_info();
let mut price_setup = AccountSetup::new::<PriceAccount>(&program_id);
let price_account = price_setup.as_account_info();
let mut price_setup_2 = AccountSetup::new::<PriceAccount>(&program_id);
let price_account_2 = price_setup_2.as_account_info();
let mut permissions_setup = AccountSetup::new_permission(&program_id);
let permissions_account = permissions_setup.as_account_info();
{
let mut permissions_account_data =
PermissionAccount::initialize(&permissions_account, PC_VERSION).unwrap();
permissions_account_data.master_authority = *funding_account.key;
permissions_account_data.data_curation_authority = *funding_account.key;
permissions_account_data.security_authority = *funding_account.key;
}
assert!(process_instruction(
&program_id,
&[
funding_account.clone(),
mapping_account.clone(),
product_account.clone(),
permissions_account.clone(),
],
instruction_data_add_product
)
.is_ok());
process_instruction(
&program_id,
&[
funding_account.clone(),
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
],
instruction_data_add_price,
)
.unwrap();
{
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
let product_data = load_checked::<ProductAccount>(&product_account, PC_VERSION).unwrap();
assert_eq!(price_data.exponent, 1);
assert_eq!(price_data.price_type, 1);
assert_eq!(price_data.min_pub_, PRICE_ACCOUNT_DEFAULT_MIN_PUB);
assert!(price_data.product_account == *product_account.key);
assert!(price_data.next_price_account == Pubkey::default());
assert_eq!(price_data.feed_index, 1);
assert!(product_data.first_price_account == *price_account.key);
}
process_instruction(
&program_id,
&[
funding_account.clone(),
product_account.clone(),
price_account_2.clone(),
permissions_account.clone(),
],
instruction_data_add_price,
)
.unwrap();
{
let price_data_2 = load_checked::<PriceAccount>(&price_account_2, PC_VERSION).unwrap();
let product_data = load_checked::<ProductAccount>(&product_account, PC_VERSION).unwrap();
assert_eq!(price_data_2.exponent, 1);
assert_eq!(price_data_2.price_type, 1);
assert_eq!(price_data_2.min_pub_, PRICE_ACCOUNT_DEFAULT_MIN_PUB);
assert!(price_data_2.product_account == *product_account.key);
assert!(price_data_2.next_price_account == *price_account.key);
assert_eq!(price_data_2.feed_index, 2);
assert!(product_data.first_price_account == *price_account_2.key);
}
// Wrong number of accounts
assert_eq!(
process_instruction(
&program_id,
&[
funding_account.clone(),
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
permissions_account.clone(),
],
instruction_data_add_price
),
Err(OracleError::InvalidNumberOfAccounts.into())
);
// Price account is already initialized
assert_eq!(
process_instruction(
&program_id,
&[
funding_account.clone(),
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
],
instruction_data_add_price
),
Err(OracleError::InvalidFreshAccount.into())
);
clear_account(&price_account).unwrap();
// Wrong ptype
hdr_add_price = AddPriceArgs {
header: OracleCommand::AddPrice.into(),
exponent: 6,
price_type: 0,
};
instruction_data_add_price = bytes_of::<AddPriceArgs>(&hdr_add_price);
assert_eq!(
process_instruction(
&program_id,
&[
funding_account.clone(),
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
],
instruction_data_add_price
),
Err(ProgramError::InvalidArgument)
);
// Fresh product account
clear_account(&product_account).unwrap();
hdr_add_price = AddPriceArgs {
header: OracleCommand::AddPrice.into(),
exponent: 6,
price_type: 1,
};
instruction_data_add_price = bytes_of::<AddPriceArgs>(&hdr_add_price);
assert_eq!(
process_instruction(
&program_id,
&[
funding_account.clone(),
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
],
instruction_data_add_price
),
Err(OracleError::InvalidAccountHeader.into())
);
}