-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate_collection.rs
More file actions
279 lines (247 loc) · 8.75 KB
/
Copy pathcreate_collection.rs
File metadata and controls
279 lines (247 loc) · 8.75 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! Create a verified collection
use mpl_token_metadata::{
accounts::{MasterEdition, Metadata},
instructions::{
CreateMasterEditionV3Cpi, CreateMasterEditionV3CpiAccounts,
CreateMasterEditionV3InstructionArgs, CreateMetadataAccountV3Cpi,
CreateMetadataAccountV3CpiAccounts, CreateMetadataAccountV3InstructionArgs,
},
types::DataV2,
};
use crate::{
cpi::Cpi,
state::{COLLECTION_NAME, COLLECTION_PREFIX, COLLECTION_URI, META_SYMBOL},
};
use {
bonfida_utils::{
checks::{check_account_key, check_account_owner, check_signer},
BorshSize, InstructionsAccount,
},
borsh::{BorshDeserialize, BorshSerialize},
mpl_token_metadata::types::Creator,
solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program::{invoke, invoke_signed},
program_error::ProgramError,
program_pack::Pack,
pubkey::Pubkey,
system_program, sysvar,
},
spl_associated_token_account::instruction::create_associated_token_account,
spl_token::{
instruction::{initialize_mint, mint_to},
state::Mint,
},
};
#[derive(BorshDeserialize, BorshSerialize, BorshSize)]
pub struct Params {}
#[derive(InstructionsAccount)]
pub struct Accounts<'a, T> {
/// The mint of the collection
#[cons(writable)]
pub collection_mint: &'a T,
#[cons(writable)]
pub edition: &'a T,
/// The metadata account
#[cons(writable)]
pub metadata_account: &'a T,
/// The central state account
pub central_state: &'a T,
#[cons(writable)]
/// Token account of the central state to hold the master edition
pub central_state_nft_ata: &'a T,
/// The fee payer account
#[cons(writable, signer)]
pub fee_payer: &'a T,
/// The SPL token program account
pub spl_token_program: &'a T,
/// The metadata program account
pub metadata_program: &'a T,
/// The system program account
pub system_program: &'a T,
/// The SPL name service program account
pub spl_name_service_program: &'a T,
pub ata_program: &'a T,
/// Rent sysvar account
pub rent_account: &'a T,
}
impl<'a, 'b: 'a> Accounts<'a, AccountInfo<'b>> {
pub fn parse(
accounts: &'a [AccountInfo<'b>],
_program_id: &Pubkey,
) -> Result<Self, ProgramError> {
let accounts_iter = &mut accounts.iter();
let accounts = Accounts {
collection_mint: next_account_info(accounts_iter)?,
edition: next_account_info(accounts_iter)?,
metadata_account: next_account_info(accounts_iter)?,
central_state: next_account_info(accounts_iter)?,
central_state_nft_ata: next_account_info(accounts_iter)?,
fee_payer: next_account_info(accounts_iter)?,
spl_token_program: next_account_info(accounts_iter)?,
metadata_program: next_account_info(accounts_iter)?,
system_program: next_account_info(accounts_iter)?,
spl_name_service_program: next_account_info(accounts_iter)?,
ata_program: next_account_info(accounts_iter)?,
rent_account: next_account_info(accounts_iter)?,
};
// Check keys
check_account_key(accounts.central_state, &crate::central_state::KEY)?;
check_account_key(accounts.spl_token_program, &spl_token::ID)?;
check_account_key(accounts.metadata_program, &mpl_token_metadata::ID)?;
check_account_key(accounts.system_program, &system_program::ID)?;
check_account_key(accounts.spl_name_service_program, &spl_name_service::ID)?;
check_account_key(accounts.ata_program, &spl_associated_token_account::ID)?;
check_account_key(accounts.rent_account, &sysvar::rent::ID)?;
// Check owners
check_account_owner(accounts.collection_mint, &system_program::ID)?;
check_account_owner(accounts.edition, &system_program::ID)?;
check_account_owner(accounts.metadata_account, &system_program::ID)?;
check_account_owner(accounts.central_state_nft_ata, &system_program::ID)?;
// Check signer
check_signer(accounts.fee_payer)?;
Ok(accounts)
}
}
pub fn process(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let accounts = Accounts::parse(accounts, program_id)?;
let (collection_mint, collection_mint_nonce) =
Pubkey::find_program_address(&[COLLECTION_PREFIX, &program_id.to_bytes()], program_id);
check_account_key(accounts.collection_mint, &collection_mint)?;
let (metadata_key, _) = Metadata::find_pda(&collection_mint);
check_account_key(accounts.metadata_account, &metadata_key)?;
let (edition_key, _) = MasterEdition::find_pda(&collection_mint);
check_account_key(accounts.edition, &edition_key)?;
// Create mint account
msg!("+ Creating mint");
let seeds: &[&[u8]] = &[
COLLECTION_PREFIX,
&program_id.to_bytes(),
&[collection_mint_nonce],
];
Cpi::create_account(
&spl_token::ID,
accounts.system_program,
accounts.fee_payer,
&accounts.collection_mint.clone(),
seeds,
Mint::LEN,
)?;
msg!("+ Initialize mint");
// Initialize mint
let ix = initialize_mint(
&spl_token::ID,
&collection_mint,
&crate::central_state::KEY,
Some(&crate::central_state::KEY),
0,
)?;
invoke_signed(
&ix,
&[
accounts.spl_token_program.clone(),
accounts.collection_mint.clone(),
accounts.rent_account.clone(),
],
&[seeds],
)?;
// Create central state ATA
msg!("+ Creating central state ATA");
let ix = create_associated_token_account(
accounts.fee_payer.key,
&crate::central_state::KEY,
&collection_mint,
&spl_token::ID,
);
invoke(
&ix,
&[
accounts.ata_program.clone(),
accounts.fee_payer.clone(),
accounts.central_state_nft_ata.clone(),
accounts.central_state.clone(),
accounts.collection_mint.clone(),
accounts.system_program.clone(),
accounts.spl_token_program.clone(),
accounts.rent_account.clone(),
],
)?;
// Mint NFT
// (because the master edition ix requires mint supply === 1)
msg!("+ Minting NFT");
let seeds: &[&[u8]] = &[&program_id.to_bytes(), &[crate::central_state::NONCE]];
let ix = mint_to(
&spl_token::ID,
&collection_mint,
accounts.central_state_nft_ata.key,
&crate::central_state::KEY,
&[],
1,
)?;
invoke_signed(
&ix,
&[
accounts.spl_token_program.clone(),
accounts.collection_mint.clone(),
accounts.central_state_nft_ata.clone(),
accounts.central_state.clone(),
],
&[seeds],
)?;
// Create collection
msg!("+ Creating collection");
let central_creator = Creator {
address: crate::central_state::KEY,
verified: true,
share: 100,
};
CreateMetadataAccountV3Cpi::new(
accounts.metadata_program,
CreateMetadataAccountV3CpiAccounts {
metadata: accounts.metadata_account,
mint: accounts.collection_mint,
mint_authority: accounts.central_state,
payer: accounts.fee_payer,
update_authority: (accounts.central_state, true),
system_program: accounts.system_program,
rent: Some(accounts.rent_account),
},
CreateMetadataAccountV3InstructionArgs {
data: DataV2 {
name: COLLECTION_NAME.to_string(),
uri: COLLECTION_URI.to_string(),
symbol: META_SYMBOL.to_string(),
seller_fee_basis_points: 0,
creators: Some(vec![central_creator]),
uses: None,
collection: None,
},
is_mutable: true,
collection_details: None,
},
)
.invoke_signed(&[seeds])?;
// Create master edition
msg!("+ Creating master edition");
CreateMasterEditionV3Cpi::new(
accounts.metadata_program,
CreateMasterEditionV3CpiAccounts {
edition: accounts.edition,
mint: accounts.collection_mint,
update_authority: accounts.central_state,
token_program: accounts.spl_token_program,
system_program: accounts.system_program,
rent: Some(accounts.rent_account),
mint_authority: accounts.central_state,
metadata: accounts.metadata_account,
payer: accounts.fee_payer,
},
CreateMasterEditionV3InstructionArgs {
max_supply: Some(0),
},
)
.invoke_signed(&[seeds])?;
Ok(())
}