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
use anchor_client::{solana_sdk::pubkey::Pubkey, Client};
use anyhow::Result;
use mpl_token_metadata::{
instruction::{create_master_edition_v3, create_metadata_accounts_v2},
pda::find_collection_authority_account,
state::Creator,
};
use spl_associated_token_account::{create_associated_token_account, get_associated_token_address};
use spl_token::{
instruction::{initialize_mint, mint_to},
ID as TOKEN_PROGRAM_ID,
};
use crate::{
candy_machine::CANDY_MACHINE_ID,
common::*,
config::ConfigData,
pdas::{find_collection_pda, find_master_edition_pda, find_metadata_pda},
};
pub fn create_and_set_collection(
client: Client,
candy_pubkey: Pubkey,
cache: &mut Cache,
config_data: &ConfigData,
) -> Result<(Signature, Pubkey)> {
let program = client.program(CANDY_MACHINE_ID);
let payer = program.payer();
let collection_mint = Keypair::new();
let collection_item: &mut CacheItem = match cache.items.get_mut("-1") {
Some(item) => item,
None => {
return Err(anyhow!("Trying to create and set collection when collection item info isn't in cache! This shouldn't happen!"));
}
};
let min_rent = program
.rpc()
.get_minimum_balance_for_rent_exemption(MINT_LAYOUT as usize)?;
let create_mint_account_ix = system_instruction::create_account(
&payer,
&collection_mint.pubkey(),
min_rent,
MINT_LAYOUT,
&TOKEN_PROGRAM_ID,
);
let init_mint_ix = initialize_mint(
&TOKEN_PROGRAM_ID,
&collection_mint.pubkey(),
&payer,
Some(&payer),
0,
)?;
let ata_pubkey = get_associated_token_address(&payer, &collection_mint.pubkey());
let create_assoc_account_ix =
create_associated_token_account(&payer, &payer, &collection_mint.pubkey());
let mint_to_ix = mint_to(
&TOKEN_PROGRAM_ID,
&collection_mint.pubkey(),
&ata_pubkey,
&payer,
&[],
1,
)?;
let creator = Creator {
address: payer,
verified: true,
share: 100,
};
let collection_metadata_pubkey = find_metadata_pda(&collection_mint.pubkey());
let create_metadata_account_ix = create_metadata_accounts_v2(
mpl_token_metadata::ID,
collection_metadata_pubkey,
collection_mint.pubkey(),
payer,
payer,
payer,
collection_item.name.clone(),
config_data.symbol.clone(),
collection_item.metadata_link.clone(),
Some(vec![creator]),
0,
true,
true,
None,
None,
);
let collection_edition_pubkey = find_master_edition_pda(&collection_mint.pubkey());
let create_master_edition_ix = create_master_edition_v3(
mpl_token_metadata::ID,
collection_edition_pubkey,
collection_mint.pubkey(),
payer,
payer,
collection_metadata_pubkey,
payer,
Some(0),
);
let collection_pda_pubkey = find_collection_pda(&candy_pubkey).0;
let collection_authority_record =
find_collection_authority_account(&collection_mint.pubkey(), &collection_pda_pubkey).0;
let builder = program
.request()
.instruction(create_mint_account_ix)
.instruction(init_mint_ix)
.instruction(create_assoc_account_ix)
.instruction(mint_to_ix)
.signer(&collection_mint)
.instruction(create_metadata_account_ix)
.instruction(create_master_edition_ix)
.accounts(nft_accounts::SetCollection {
candy_machine: candy_pubkey,
authority: payer,
collection_pda: collection_pda_pubkey,
payer,
system_program: system_program::id(),
rent: sysvar::rent::ID,
metadata: collection_metadata_pubkey,
mint: collection_mint.pubkey(),
edition: collection_edition_pubkey,
collection_authority_record,
token_metadata_program: mpl_token_metadata::ID,
})
.args(nft_instruction::SetCollection);
let sig = builder.send()?;
collection_item.on_chain = true;
cache.program.collection_mint = collection_mint.pubkey().to_string();
cache.sync_file()?;
Ok((sig, collection_mint.pubkey()))
}