11pub use crate :: errors:: GameErrorCode ;
22pub use crate :: errors:: ProgramErrorCode ;
33pub use crate :: state:: game_data:: GameData ;
4- use anchor_lang:: { prelude:: * , system_program } ;
4+ use anchor_lang:: { prelude:: * , system_program} ;
55use anchor_spl:: {
6- associated_token:: { self , AssociatedToken } ,
6+ associated_token:: { self , AssociatedToken } ,
77 token_2022,
8- token_interface:: { spl_token_2022:: instruction:: AuthorityType , Token2022 } ,
8+ token_interface:: { spl_token_2022:: instruction:: AuthorityType , Token2022 } ,
99} ;
10- use solana_program:: program:: { invoke, invoke_signed } ;
11- use spl_token_2022:: { extension:: ExtensionType , state:: Mint } ;
10+ use solana_program:: program:: { invoke, invoke_signed} ;
11+ use spl_token_2022:: { extension:: ExtensionType , state:: Mint } ;
1212
1313pub fn mint_nft ( ctx : Context < MintNft > ) -> Result < ( ) > {
1414 msg ! ( "Mint nft with meta data extension and additional meta data" ) ;
1515
16- let space = match
17- ExtensionType :: try_calculate_account_len :: < Mint > ( & [ ExtensionType :: MetadataPointer ] )
18- {
19- Ok ( space) => space,
20- Err ( _) => {
21- return err ! ( ProgramErrorCode :: InvalidMintAccountSpace ) ;
22- }
23- } ;
16+ let space =
17+ match ExtensionType :: try_calculate_account_len :: < Mint > ( & [ ExtensionType :: MetadataPointer ] ) {
18+ Ok ( space) => space,
19+ Err ( _) => {
20+ return err ! ( ProgramErrorCode :: InvalidMintAccountSpace ) ;
21+ }
22+ } ;
2423
2524 // This is the space required for the metadata account.
2625 // We put the meta data into the mint account at the end so we
@@ -41,47 +40,52 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
4140 system_program:: CreateAccount {
4241 from : ctx. accounts . signer . to_account_info ( ) ,
4342 to : ctx. accounts . mint . to_account_info ( ) ,
44- }
43+ } ,
4544 ) ,
4645 lamports_required,
4746 space as u64 ,
48- & ctx. accounts . token_program . key ( )
47+ & ctx. accounts . token_program . key ( ) ,
4948 ) ?;
5049
5150 // Assign the mint to the token program
5251 system_program:: assign (
53- CpiContext :: new ( ctx. accounts . token_program . to_account_info ( ) , system_program:: Assign {
54- account_to_assign : ctx. accounts . mint . to_account_info ( ) ,
55- } ) ,
56- & token_2022:: ID
52+ CpiContext :: new (
53+ ctx. accounts . token_program . to_account_info ( ) ,
54+ system_program:: Assign {
55+ account_to_assign : ctx. accounts . mint . to_account_info ( ) ,
56+ } ,
57+ ) ,
58+ & token_2022:: ID ,
5759 ) ?;
5860
5961 // Initialize the metadata pointer (Need to do this before initializing the mint)
60- let init_meta_data_pointer_ix = match
61- spl_token_2022:: extension:: metadata_pointer:: instruction:: initialize (
62+ let init_meta_data_pointer_ix =
63+ match spl_token_2022:: extension:: metadata_pointer:: instruction:: initialize (
6264 & Token2022 :: id ( ) ,
6365 & ctx. accounts . mint . key ( ) ,
6466 Some ( ctx. accounts . nft_authority . key ( ) ) ,
65- Some ( ctx. accounts . mint . key ( ) )
66- )
67- {
68- Ok ( ix) => ix,
69- Err ( _) => {
70- return err ! ( ProgramErrorCode :: CantInitializeMetadataPointer ) ;
71- }
72- } ;
67+ Some ( ctx. accounts . mint . key ( ) ) ,
68+ ) {
69+ Ok ( ix) => ix,
70+ Err ( _) => {
71+ return err ! ( ProgramErrorCode :: CantInitializeMetadataPointer ) ;
72+ }
73+ } ;
7374
7475 invoke (
7576 & init_meta_data_pointer_ix,
76- & [ ctx. accounts . mint . to_account_info ( ) , ctx. accounts . nft_authority . to_account_info ( ) ]
77+ & [
78+ ctx. accounts . mint . to_account_info ( ) ,
79+ ctx. accounts . nft_authority . to_account_info ( ) ,
80+ ] ,
7781 ) ?;
7882
7983 // Initialize the mint cpi
8084 let mint_cpi_ix = CpiContext :: new (
8185 ctx. accounts . token_program . to_account_info ( ) ,
8286 token_2022:: InitializeMint2 {
8387 mint : ctx. accounts . mint . to_account_info ( ) ,
84- }
88+ } ,
8589 ) ;
8690
8791 token_2022:: initialize_mint2 ( mint_cpi_ix, 0 , & ctx. accounts . nft_authority . key ( ) , None ) . unwrap ( ) ;
@@ -92,7 +96,10 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
9296 let bump = ctx. bumps . nft_authority ;
9397 let signer: & [ & [ & [ u8 ] ] ] = & [ & [ seeds, & [ bump] ] ] ;
9498
95- msg ! ( "Init metadata {0}" , ctx. accounts. nft_authority. to_account_info( ) . key) ;
99+ msg ! (
100+ "Init metadata {0}" ,
101+ ctx. accounts. nft_authority. to_account_info( ) . key
102+ ) ;
96103
97104 // Init the metadata account
98105 let init_token_meta_data_ix = & spl_token_metadata_interface:: instruction:: initialize (
@@ -103,7 +110,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
103110 ctx. accounts . nft_authority . to_account_info ( ) . key ,
104111 "Beaver" . to_string ( ) ,
105112 "BVA" . to_string ( ) ,
106- "https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0" . to_string ( )
113+ "https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0" . to_string ( ) ,
107114 ) ;
108115
109116 invoke_signed (
@@ -112,7 +119,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
112119 ctx. accounts . mint . to_account_info ( ) . clone ( ) ,
113120 ctx. accounts . nft_authority . to_account_info ( ) . clone ( ) ,
114121 ] ,
115- signer
122+ signer,
116123 ) ?;
117124
118125 // Update the metadata account with an additional metadata field in this case the player level
@@ -122,29 +129,27 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
122129 ctx. accounts . mint . key ,
123130 ctx. accounts . nft_authority . to_account_info ( ) . key ,
124131 spl_token_metadata_interface:: state:: Field :: Key ( "level" . to_string ( ) ) ,
125- "1" . to_string ( )
132+ "1" . to_string ( ) ,
126133 ) ,
127134 & [
128135 ctx. accounts . mint . to_account_info ( ) . clone ( ) ,
129136 ctx. accounts . nft_authority . to_account_info ( ) . clone ( ) ,
130137 ] ,
131- signer
138+ signer,
132139 ) ?;
133140
134141 // Create the associated token account
135- associated_token:: create (
136- CpiContext :: new (
137- ctx. accounts . associated_token_program . to_account_info ( ) ,
138- associated_token:: Create {
139- payer : ctx. accounts . signer . to_account_info ( ) ,
140- associated_token : ctx. accounts . token_account . to_account_info ( ) ,
141- authority : ctx. accounts . signer . to_account_info ( ) ,
142- mint : ctx. accounts . mint . to_account_info ( ) ,
143- system_program : ctx. accounts . system_program . to_account_info ( ) ,
144- token_program : ctx. accounts . token_program . to_account_info ( ) ,
145- }
146- )
147- ) ?;
142+ associated_token:: create ( CpiContext :: new (
143+ ctx. accounts . associated_token_program . to_account_info ( ) ,
144+ associated_token:: Create {
145+ payer : ctx. accounts . signer . to_account_info ( ) ,
146+ associated_token : ctx. accounts . token_account . to_account_info ( ) ,
147+ authority : ctx. accounts . signer . to_account_info ( ) ,
148+ mint : ctx. accounts . mint . to_account_info ( ) ,
149+ system_program : ctx. accounts . system_program . to_account_info ( ) ,
150+ token_program : ctx. accounts . token_program . to_account_info ( ) ,
151+ } ,
152+ ) ) ?;
148153
149154 // Mint one token to the associated token account of the player
150155 token_2022:: mint_to (
@@ -155,9 +160,9 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
155160 to : ctx. accounts . token_account . to_account_info ( ) ,
156161 authority : ctx. accounts . nft_authority . to_account_info ( ) ,
157162 } ,
158- signer
163+ signer,
159164 ) ,
160- 1
165+ 1 ,
161166 ) ?;
162167
163168 // Freeze the mint authority so no more tokens can be minted to make it an NFT
@@ -168,10 +173,10 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
168173 current_authority : ctx. accounts . nft_authority . to_account_info ( ) ,
169174 account_or_mint : ctx. accounts . mint . to_account_info ( ) ,
170175 } ,
171- signer
176+ signer,
172177 ) ,
173178 AuthorityType :: MintTokens ,
174- None
179+ None ,
175180 ) ?;
176181
177182 Ok ( ( ) )
0 commit comments