1+ use anchor_lang:: prelude:: * ;
2+ use anchor_spl:: {
3+ associated_token:: AssociatedToken ,
4+ metadata:: Metadata ,
5+ token:: {
6+ mint_to,
7+ Mint ,
8+ MintTo ,
9+ Token ,
10+ TokenAccount ,
11+ }
12+ } ;
13+ use anchor_spl:: metadata:: mpl_token_metadata:: {
14+ instructions:: {
15+ CreateMasterEditionV3Cpi ,
16+ CreateMasterEditionV3CpiAccounts ,
17+ CreateMasterEditionV3InstructionArgs ,
18+ CreateMetadataAccountV3Cpi ,
19+ CreateMetadataAccountV3CpiAccounts ,
20+ CreateMetadataAccountV3InstructionArgs
21+ } ,
22+ types:: {
23+ CollectionDetails ,
24+ Creator ,
25+ DataV2
26+ }
27+ } ;
28+
29+ #[ derive( Accounts ) ]
30+ pub struct CreateCollection < ' info > {
31+ #[ account( mut ) ]
32+ user : Signer < ' info > ,
33+ #[ account(
34+ init,
35+ payer = user,
36+ mint:: decimals = 0 ,
37+ mint:: authority = mint_authority,
38+ mint:: freeze_authority = mint_authority,
39+ ) ]
40+ mint : Account < ' info , Mint > ,
41+ #[ account(
42+ seeds = [ b"authority" ] ,
43+ bump,
44+ ) ]
45+ /// CHECK: This account is not initialized and is being used for signing purposes only
46+ pub mint_authority : UncheckedAccount < ' info > ,
47+ #[ account( mut ) ]
48+ /// CHECK: This account will be initialized by the metaplex program
49+ metadata : UncheckedAccount < ' info > ,
50+ #[ account( mut ) ]
51+ /// CHECK: This account will be initialized by the metaplex program
52+ master_edition : UncheckedAccount < ' info > ,
53+ #[ account(
54+ init,
55+ payer = user,
56+ associated_token:: mint = mint,
57+ associated_token:: authority = user
58+ ) ]
59+ destination : Account < ' info , TokenAccount > ,
60+ system_program : Program < ' info , System > ,
61+ token_program : Program < ' info , Token > ,
62+ associated_token_program : Program < ' info , AssociatedToken > ,
63+ token_metadata_program : Program < ' info , Metadata > ,
64+ }
65+
66+ impl < ' info > CreateCollection < ' info > {
67+ pub fn create_collection ( & mut self , bumps : & CreateCollectionBumps ) -> Result < ( ) > {
68+
69+ let metadata = & self . metadata . to_account_info ( ) ;
70+ let master_edition = & self . master_edition . to_account_info ( ) ;
71+ let mint = & self . mint . to_account_info ( ) ;
72+ let authority = & self . mint_authority . to_account_info ( ) ;
73+ let payer = & self . user . to_account_info ( ) ;
74+ let system_program = & self . system_program . to_account_info ( ) ;
75+ let spl_token_program = & self . token_program . to_account_info ( ) ;
76+ let spl_metadata_program = & self . token_metadata_program . to_account_info ( ) ;
77+
78+ let seeds = & [
79+ & b"authority" [ ..] ,
80+ & [ bumps. mint_authority ]
81+ ] ;
82+ let signer_seeds = & [ & seeds[ ..] ] ;
83+
84+ let cpi_program = self . token_program . to_account_info ( ) ;
85+ let cpi_accounts = MintTo {
86+ mint : self . mint . to_account_info ( ) ,
87+ to : self . destination . to_account_info ( ) ,
88+ authority : self . mint_authority . to_account_info ( ) ,
89+ } ;
90+ let cpi_ctx = CpiContext :: new_with_signer ( cpi_program, cpi_accounts, signer_seeds) ;
91+ mint_to ( cpi_ctx, 1 ) ?;
92+ msg ! ( "Collection NFT minted!" ) ;
93+
94+ let creator = vec ! [
95+ Creator {
96+ address: self . mint_authority. key( ) . clone( ) ,
97+ verified: true ,
98+ share: 100 ,
99+ } ,
100+ ] ;
101+
102+ let metadata_account = CreateMetadataAccountV3Cpi :: new (
103+ spl_metadata_program,
104+ CreateMetadataAccountV3CpiAccounts {
105+ metadata,
106+ mint,
107+ mint_authority : authority,
108+ payer,
109+ update_authority : ( authority, true ) ,
110+ system_program,
111+ rent : None ,
112+ } ,
113+ CreateMetadataAccountV3InstructionArgs {
114+ data : DataV2 {
115+ name : "DummyCollection" . to_owned ( ) ,
116+ symbol : "DC" . to_owned ( ) ,
117+ uri : "" . to_owned ( ) ,
118+ seller_fee_basis_points : 0 ,
119+ creators : Some ( creator) ,
120+ collection : None ,
121+ uses : None ,
122+ } ,
123+ is_mutable : true ,
124+ collection_details : Some (
125+ CollectionDetails :: V1 {
126+ size : 0
127+ }
128+ )
129+ }
130+ ) ;
131+ metadata_account. invoke_signed ( signer_seeds) ?;
132+ msg ! ( "Metadata Account created!" ) ;
133+
134+ let master_edition_account = CreateMasterEditionV3Cpi :: new (
135+ spl_metadata_program,
136+ CreateMasterEditionV3CpiAccounts {
137+ edition : master_edition,
138+ update_authority : authority,
139+ mint_authority : authority,
140+ mint,
141+ payer,
142+ metadata,
143+ token_program : spl_token_program,
144+ system_program,
145+ rent : None ,
146+ } ,
147+ CreateMasterEditionV3InstructionArgs {
148+ max_supply : Some ( 0 ) ,
149+ }
150+ ) ;
151+ master_edition_account. invoke_signed ( signer_seeds) ?;
152+ msg ! ( "Master Edition Account created" ) ;
153+
154+ Ok ( ( ) )
155+ }
156+ }
0 commit comments