11pub use crate :: constants:: TOKEN_METADATA_EXTENSION_SPACE ;
22pub use crate :: errors:: GameErrorCode ;
3- pub use crate :: errors:: ProgramErrorCode ;
43pub use crate :: state:: game_data:: GameData ;
5- use anchor_lang:: { prelude:: * , system_program } ;
4+ use anchor_lang:: solana_program:: program:: { invoke, invoke_signed} ;
5+ use anchor_lang:: { prelude:: * , system_program} ;
66use anchor_spl:: {
7- associated_token:: { self , AssociatedToken } ,
7+ associated_token:: { self , AssociatedToken } ,
88 token_2022,
9- token_interface:: { spl_token_2022:: instruction:: AuthorityType , Token2022 } ,
9+ token_2022_extensions:: spl_token_metadata_interface,
10+ token_interface:: {
11+ spl_token_2022:: { self , extension:: ExtensionType , instruction:: AuthorityType , state:: Mint } ,
12+ Token2022 ,
13+ } ,
1014} ;
11- use solana_program:: program:: { invoke, invoke_signed } ;
12- use spl_token_2022:: { extension:: ExtensionType , state:: Mint } ;
1315
1416pub fn handle_mint_nft ( context : Context < MintNft > ) -> Result < ( ) > {
1517 msg ! ( "Mint nft with meta data extension and additional meta data" ) ;
1618
17- let space = match
18- ExtensionType :: try_calculate_account_len :: < Mint > ( & [ ExtensionType :: MetadataPointer ] )
19- {
20- Ok ( space) => space,
21- Err ( _) => {
22- return err ! ( ProgramErrorCode :: InvalidMintAccountSpace ) ;
23- }
24- } ;
19+ let space =
20+ match ExtensionType :: try_calculate_account_len :: < Mint > ( & [ ExtensionType :: MetadataPointer ] ) {
21+ Ok ( space) => space,
22+ Err ( _) => {
23+ return err ! ( GameErrorCode :: InvalidMintAccountSpace ) ;
24+ }
25+ } ;
2526
2627 // Space required for the inline SPL Token Metadata extension TLV. The
2728 // metadata lives on the mint account itself (not a separate account)
@@ -42,58 +43,67 @@ pub fn handle_mint_nft(context: Context<MintNft>) -> Result<()> {
4243 system_program:: CreateAccount {
4344 from : context. accounts . signer . to_account_info ( ) ,
4445 to : context. accounts . mint . to_account_info ( ) ,
45- }
46+ } ,
4647 ) ,
4748 lamports_required,
4849 space as u64 ,
49- & context. accounts . token_program . key ( )
50+ & context. accounts . token_program . key ( ) ,
5051 ) ?;
5152
5253 // Assign the mint to the token program
5354 system_program:: assign (
54- CpiContext :: new ( context. accounts . token_program . key ( ) , system_program:: Assign {
55- account_to_assign : context. accounts . mint . to_account_info ( ) ,
56- } ) ,
57- & token_2022:: ID
55+ CpiContext :: new (
56+ context. accounts . token_program . key ( ) ,
57+ system_program:: Assign {
58+ account_to_assign : context. accounts . mint . to_account_info ( ) ,
59+ } ,
60+ ) ,
61+ & token_2022:: ID ,
5862 ) ?;
5963
6064 // Initialize the metadata pointer (Need to do this before initializing the mint)
61- let init_meta_data_pointer_ix = match
62- spl_token_2022:: extension:: metadata_pointer:: instruction:: initialize (
65+ let init_meta_data_pointer_ix =
66+ match spl_token_2022:: extension:: metadata_pointer:: instruction:: initialize (
6367 & Token2022 :: id ( ) ,
6468 & context. accounts . mint . key ( ) ,
6569 Some ( context. accounts . nft_authority . key ( ) ) ,
66- Some ( context. accounts . mint . key ( ) )
67- )
68- {
69- Ok ( ix) => ix,
70- Err ( _) => {
71- return err ! ( ProgramErrorCode :: CantInitializeMetadataPointer ) ;
72- }
73- } ;
70+ Some ( context. accounts . mint . key ( ) ) ,
71+ ) {
72+ Ok ( ix) => ix,
73+ Err ( _) => {
74+ return err ! ( GameErrorCode :: CantInitializeMetadataPointer ) ;
75+ }
76+ } ;
7477
7578 invoke (
7679 & init_meta_data_pointer_ix,
77- & [ context. accounts . mint . to_account_info ( ) , context. accounts . nft_authority . to_account_info ( ) ]
80+ & [
81+ context. accounts . mint . to_account_info ( ) ,
82+ context. accounts . nft_authority . to_account_info ( ) ,
83+ ] ,
7884 ) ?;
7985
8086 // Initialize the mint cpi
8187 let mint_cpi_ix = CpiContext :: new (
8288 context. accounts . token_program . key ( ) ,
8389 token_2022:: InitializeMint2 {
8490 mint : context. accounts . mint . to_account_info ( ) ,
85- }
91+ } ,
8692 ) ;
8793
88- token_2022:: initialize_mint2 ( mint_cpi_ix, 0 , & context. accounts . nft_authority . key ( ) , None ) . unwrap ( ) ;
94+ token_2022:: initialize_mint2 ( mint_cpi_ix, 0 , & context. accounts . nft_authority . key ( ) , None )
95+ . unwrap ( ) ;
8996
9097 // We use a PDA as a mint authority for the metadata account because
9198 // we want to be able to update the NFT from the program.
9299 let seeds = b"nft_authority" ;
93100 let bump = context. bumps . nft_authority ;
94101 let signer: & [ & [ & [ u8 ] ] ] = & [ & [ seeds, & [ bump] ] ] ;
95102
96- msg ! ( "Init metadata {0}" , context. accounts. nft_authority. to_account_info( ) . key) ;
103+ msg ! (
104+ "Init metadata {0}" ,
105+ context. accounts. nft_authority. to_account_info( ) . key
106+ ) ;
97107
98108 // Init the metadata account
99109 let init_token_meta_data_ix = & spl_token_metadata_interface:: instruction:: initialize (
@@ -104,7 +114,7 @@ pub fn handle_mint_nft(context: Context<MintNft>) -> Result<()> {
104114 context. accounts . nft_authority . to_account_info ( ) . key ,
105115 "Beaver" . to_string ( ) ,
106116 "BVA" . to_string ( ) ,
107- "https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0" . to_string ( )
117+ "https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0" . to_string ( ) ,
108118 ) ;
109119
110120 invoke_signed (
@@ -113,7 +123,7 @@ pub fn handle_mint_nft(context: Context<MintNft>) -> Result<()> {
113123 context. accounts . mint . to_account_info ( ) . clone ( ) ,
114124 context. accounts . nft_authority . to_account_info ( ) . clone ( ) ,
115125 ] ,
116- signer
126+ signer,
117127 ) ?;
118128
119129 // Update the metadata account with an additional metadata field in this case the player level
@@ -123,29 +133,27 @@ pub fn handle_mint_nft(context: Context<MintNft>) -> Result<()> {
123133 context. accounts . mint . key ,
124134 context. accounts . nft_authority . to_account_info ( ) . key ,
125135 spl_token_metadata_interface:: state:: Field :: Key ( "level" . to_string ( ) ) ,
126- "1" . to_string ( )
136+ "1" . to_string ( ) ,
127137 ) ,
128138 & [
129139 context. accounts . mint . to_account_info ( ) . clone ( ) ,
130140 context. accounts . nft_authority . to_account_info ( ) . clone ( ) ,
131141 ] ,
132- signer
142+ signer,
133143 ) ?;
134144
135145 // Create the associated token account
136- associated_token:: create (
137- CpiContext :: new (
138- context. accounts . associated_token_program . key ( ) ,
139- associated_token:: Create {
140- payer : context. accounts . signer . to_account_info ( ) ,
141- associated_token : context. accounts . token_account . to_account_info ( ) ,
142- authority : context. accounts . signer . to_account_info ( ) ,
143- mint : context. accounts . mint . to_account_info ( ) ,
144- system_program : context. accounts . system_program . to_account_info ( ) ,
145- token_program : context. accounts . token_program . to_account_info ( ) ,
146- }
147- )
148- ) ?;
146+ associated_token:: create ( CpiContext :: new (
147+ context. accounts . associated_token_program . key ( ) ,
148+ associated_token:: Create {
149+ payer : context. accounts . signer . to_account_info ( ) ,
150+ associated_token : context. accounts . token_account . to_account_info ( ) ,
151+ authority : context. accounts . signer . to_account_info ( ) ,
152+ mint : context. accounts . mint . to_account_info ( ) ,
153+ system_program : context. accounts . system_program . to_account_info ( ) ,
154+ token_program : context. accounts . token_program . to_account_info ( ) ,
155+ } ,
156+ ) ) ?;
149157
150158 // Mint one token to the associated token account of the player
151159 token_2022:: mint_to (
@@ -156,9 +164,9 @@ pub fn handle_mint_nft(context: Context<MintNft>) -> Result<()> {
156164 to : context. accounts . token_account . to_account_info ( ) ,
157165 authority : context. accounts . nft_authority . to_account_info ( ) ,
158166 } ,
159- signer
167+ signer,
160168 ) ,
161- 1
169+ 1 ,
162170 ) ?;
163171
164172 // Freeze the mint authority so no more tokens can be minted to make it an NFT
@@ -169,10 +177,10 @@ pub fn handle_mint_nft(context: Context<MintNft>) -> Result<()> {
169177 current_authority : context. accounts . nft_authority . to_account_info ( ) ,
170178 account_or_mint : context. accounts . mint . to_account_info ( ) ,
171179 } ,
172- signer
180+ signer,
173181 ) ,
174182 AuthorityType :: MintTokens ,
175- None
183+ None ,
176184 ) ?;
177185
178186 Ok ( ( ) )
@@ -186,7 +194,7 @@ pub struct MintNft<'info> {
186194 pub token_program : Program < ' info , Token2022 > ,
187195 /// CHECK: We will create this one for the user
188196 #[ account( mut ) ]
189- pub token_account : AccountInfo < ' info > ,
197+ pub token_account : UncheckedAccount < ' info > ,
190198 #[ account( mut ) ]
191199 pub mint : Signer < ' info > ,
192200 pub rent : Sysvar < ' info , Rent > ,
0 commit comments