forked from regolith-labs/ore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.rs
206 lines (194 loc) · 6.48 KB
/
initialize.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
use std::mem::size_of;
use ore_api::{
consts::*,
instruction::*,
loaders::*,
state::{Bus, Config, Treasury},
};
use ore_utils::spl::create_ata;
use solana_program::{
account_info::AccountInfo,
entrypoint::ProgramResult,
program_error::ProgramError,
program_pack::Pack,
system_program, {self, sysvar},
};
use spl_token::state::Mint;
use crate::utils::{create_pda, AccountDeserialize, Discriminator};
/// Initialize sets up the ORE program to begin mining.
pub fn process_initialize<'a, 'info>(
accounts: &'a [AccountInfo<'info>],
data: &[u8],
) -> ProgramResult {
// Parse args.
let args = InitializeArgs::try_from_bytes(data)?;
// Load accounts.
let [signer, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, config_info, metadata_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program, metadata_program, rent_sysvar] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_uninitialized_pda(bus_0_info, &[BUS, &[0]], args.bus_0_bump, &ore_api::id())?;
load_uninitialized_pda(bus_1_info, &[BUS, &[1]], args.bus_1_bump, &ore_api::id())?;
load_uninitialized_pda(bus_2_info, &[BUS, &[2]], args.bus_2_bump, &ore_api::id())?;
load_uninitialized_pda(bus_3_info, &[BUS, &[3]], args.bus_3_bump, &ore_api::id())?;
load_uninitialized_pda(bus_4_info, &[BUS, &[4]], args.bus_4_bump, &ore_api::id())?;
load_uninitialized_pda(bus_5_info, &[BUS, &[5]], args.bus_5_bump, &ore_api::id())?;
load_uninitialized_pda(bus_6_info, &[BUS, &[6]], args.bus_6_bump, &ore_api::id())?;
load_uninitialized_pda(bus_7_info, &[BUS, &[7]], args.bus_7_bump, &ore_api::id())?;
load_uninitialized_pda(config_info, &[CONFIG], args.config_bump, &ore_api::id())?;
load_uninitialized_pda(
metadata_info,
&[
METADATA,
mpl_token_metadata::ID.as_ref(),
MINT_ADDRESS.as_ref(),
],
args.metadata_bump,
&mpl_token_metadata::ID,
)?;
load_uninitialized_pda(
mint_info,
&[MINT, MINT_NOISE.as_slice()],
args.mint_bump,
&ore_api::id(),
)?;
load_uninitialized_pda(
treasury_info,
&[TREASURY],
args.treasury_bump,
&ore_api::id(),
)?;
load_system_account(treasury_tokens_info, true)?;
load_program(system_program, system_program::id())?;
load_program(token_program, spl_token::id())?;
load_program(associated_token_program, spl_associated_token_account::id())?;
load_program(metadata_program, mpl_token_metadata::ID)?;
load_sysvar(rent_sysvar, sysvar::rent::id())?;
// Check signer.
if signer.key.ne(&INITIALIZER_ADDRESS) {
return Err(ProgramError::MissingRequiredSignature);
}
// Initialize bus accounts.
let bus_infos = [
bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info,
bus_7_info,
];
let bus_bumps = [
args.bus_0_bump,
args.bus_1_bump,
args.bus_2_bump,
args.bus_3_bump,
args.bus_4_bump,
args.bus_5_bump,
args.bus_6_bump,
args.bus_7_bump,
];
for i in 0..BUS_COUNT {
create_pda(
bus_infos[i],
&ore_api::id(),
8 + size_of::<Bus>(),
&[BUS, &[i as u8], &[bus_bumps[i]]],
system_program,
signer,
)?;
let mut bus_data = bus_infos[i].try_borrow_mut_data()?;
bus_data[0] = Bus::discriminator() as u8;
let bus = Bus::try_from_bytes_mut(&mut bus_data)?;
bus.id = i as u64;
bus.rewards = 0;
bus.theoretical_rewards = 0;
bus.top_balance = 0;
}
// Initialize config.
create_pda(
config_info,
&ore_api::id(),
8 + size_of::<Config>(),
&[CONFIG, &[args.config_bump]],
system_program,
signer,
)?;
let mut config_data = config_info.data.borrow_mut();
config_data[0] = Config::discriminator() as u8;
let config = Config::try_from_bytes_mut(&mut config_data)?;
config.base_reward_rate = INITIAL_BASE_REWARD_RATE;
config.last_reset_at = 0;
config.min_difficulty = INITIAL_MIN_DIFFICULTY as u64;
config.top_balance = 0;
// Initialize treasury.
create_pda(
treasury_info,
&ore_api::id(),
8 + size_of::<Treasury>(),
&[TREASURY, &[args.treasury_bump]],
system_program,
signer,
)?;
let mut treasury_data = treasury_info.data.borrow_mut();
treasury_data[0] = Treasury::discriminator() as u8;
drop(treasury_data);
// Initialize mint.
create_pda(
mint_info,
&spl_token::id(),
Mint::LEN,
&[MINT, MINT_NOISE.as_slice(), &[args.mint_bump]],
system_program,
signer,
)?;
solana_program::program::invoke_signed(
&spl_token::instruction::initialize_mint(
&spl_token::id(),
mint_info.key,
treasury_info.key,
None,
TOKEN_DECIMALS,
)?,
&[
token_program.clone(),
mint_info.clone(),
treasury_info.clone(),
rent_sysvar.clone(),
],
&[&[MINT, MINT_NOISE.as_slice(), &[args.mint_bump]]],
)?;
// Initialize mint metadata.
mpl_token_metadata::instructions::CreateMetadataAccountV3Cpi {
__program: metadata_program,
metadata: metadata_info,
mint: mint_info,
mint_authority: treasury_info,
payer: signer,
update_authority: (signer, true),
system_program,
rent: Some(rent_sysvar),
__args: mpl_token_metadata::instructions::CreateMetadataAccountV3InstructionArgs {
data: mpl_token_metadata::types::DataV2 {
name: METADATA_NAME.to_string(),
symbol: METADATA_SYMBOL.to_string(),
uri: METADATA_URI.to_string(),
seller_fee_basis_points: 0,
creators: None,
collection: None,
uses: None,
},
is_mutable: true,
collection_details: None,
},
}
.invoke_signed(&[&[TREASURY, &[args.treasury_bump]]])?;
// Initialize treasury token account.
create_ata(
signer,
treasury_info,
treasury_tokens_info,
mint_info,
system_program,
token_program,
associated_token_program,
)?;
Ok(())
}