1
1
pub use crate :: errors:: GameErrorCode ;
2
2
pub use crate :: errors:: ProgramErrorCode ;
3
3
pub use crate :: state:: game_data:: GameData ;
4
- use anchor_lang:: { prelude:: * , system_program} ;
4
+ use anchor_lang:: { prelude:: * , system_program } ;
5
5
use anchor_spl:: {
6
- associated_token:: { self , AssociatedToken } , token_2022, token_interface:: { spl_token_2022:: instruction:: AuthorityType , Token2022 }
6
+ associated_token:: { self , AssociatedToken } ,
7
+ token_2022,
8
+ token_interface:: { spl_token_2022:: instruction:: AuthorityType , Token2022 } ,
7
9
} ;
8
- use solana_program:: program:: { invoke, invoke_signed} ;
9
- 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 } ;
10
12
11
13
pub fn mint_nft ( ctx : Context < MintNft > ) -> Result < ( ) > {
12
14
msg ! ( "Mint nft with meta data extension and additional meta data" ) ;
13
15
14
- let space = match ExtensionType :: try_calculate_account_len :: < Mint > ( & [ ExtensionType :: MetadataPointer ] ) {
16
+ let space = match
17
+ ExtensionType :: try_calculate_account_len :: < Mint > ( & [ ExtensionType :: MetadataPointer ] )
18
+ {
15
19
Ok ( space) => space,
16
- Err ( _) => return err ! ( ProgramErrorCode :: InvalidMintAccountSpace )
20
+ Err ( _) => {
21
+ return err ! ( ProgramErrorCode :: InvalidMintAccountSpace ) ;
22
+ }
17
23
} ;
18
24
19
- // This is the space required for the metadata account.
20
- // We put the meta data into the mint account at the end so we
21
- // don't need to create and additional account.
25
+ // This is the space required for the metadata account.
26
+ // We put the meta data into the mint account at the end so we
27
+ // don't need to create and additional account.
22
28
let meta_data_space = 250 ;
23
29
24
- let lamports_required = ( Rent :: get ( ) ?) . minimum_balance ( space + meta_data_space) ;
30
+ let lamports_required = Rent :: get ( ) ?. minimum_balance ( space + meta_data_space) ;
25
31
26
32
msg ! (
27
33
"Create Mint and metadata account size and cost: {} lamports: {}" ,
@@ -35,59 +41,52 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
35
41
system_program:: CreateAccount {
36
42
from : ctx. accounts . signer . to_account_info ( ) ,
37
43
to : ctx. accounts . mint . to_account_info ( ) ,
38
- } ,
44
+ }
39
45
) ,
40
46
lamports_required,
41
47
space as u64 ,
42
- & ctx. accounts . token_program . key ( ) ,
48
+ & ctx. accounts . token_program . key ( )
43
49
) ?;
44
50
45
51
// Assign the mint to the token program
46
52
system_program:: assign (
47
- CpiContext :: new (
48
- ctx. accounts . token_program . to_account_info ( ) ,
49
- system_program:: Assign {
50
- account_to_assign : ctx. accounts . mint . to_account_info ( ) ,
51
- } ,
52
- ) ,
53
- & token_2022:: ID ,
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
54
57
) ?;
55
58
56
59
// Initialize the metadata pointer (Need to do this before initializing the mint)
57
- let init_meta_data_pointer_ix =
58
- match spl_token_2022:: extension:: metadata_pointer:: instruction:: initialize (
59
- & Token2022 :: id ( ) ,
60
- & ctx. accounts . mint . key ( ) ,
61
- Some ( ctx. accounts . nft_authority . key ( ) ) ,
62
- Some ( ctx. accounts . mint . key ( ) ) ,
63
- ) {
60
+ let init_meta_data_pointer_ix = match
61
+ spl_token_2022:: extension:: metadata_pointer:: instruction:: initialize (
62
+ & Token2022 :: id ( ) ,
63
+ & ctx. accounts . mint . key ( ) ,
64
+ Some ( ctx. accounts . nft_authority . key ( ) ) ,
65
+ Some ( ctx. accounts . mint . key ( ) )
66
+ )
67
+ {
64
68
Ok ( ix) => ix,
65
- Err ( _) => return err ! ( ProgramErrorCode :: CantInitializeMetadataPointer )
69
+ Err ( _) => {
70
+ return err ! ( ProgramErrorCode :: CantInitializeMetadataPointer ) ;
71
+ }
66
72
} ;
67
-
73
+
68
74
invoke (
69
75
& init_meta_data_pointer_ix,
70
- & [
71
- ctx. accounts . mint . to_account_info ( ) ,
72
- ctx. accounts . nft_authority . to_account_info ( )
73
- ] ,
76
+ & [ ctx. accounts . mint . to_account_info ( ) , ctx. accounts . nft_authority . to_account_info ( ) ]
74
77
) ?;
75
-
78
+
76
79
// Initialize the mint cpi
77
80
let mint_cpi_ix = CpiContext :: new (
78
81
ctx. accounts . token_program . to_account_info ( ) ,
79
82
token_2022:: InitializeMint2 {
80
83
mint : ctx. accounts . mint . to_account_info ( ) ,
81
- } ,
84
+ }
82
85
) ;
83
86
84
- token_2022:: initialize_mint2 (
85
- mint_cpi_ix,
86
- 0 ,
87
- & ctx. accounts . nft_authority . key ( ) ,
88
- None ) . unwrap ( ) ;
89
-
90
- // We use a PDA as a mint authority for the metadata account because
87
+ token_2022:: initialize_mint2 ( mint_cpi_ix, 0 , & ctx. accounts . nft_authority . key ( ) , None ) . unwrap ( ) ;
88
+
89
+ // We use a PDA as a mint authority for the metadata account because
91
90
// we want to be able to update the NFT from the program.
92
91
let seeds = b"nft_authority" ;
93
92
let bump = ctx. bumps . nft_authority ;
@@ -96,22 +95,24 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
96
95
msg ! ( "Init metadata {0}" , ctx. accounts. nft_authority. to_account_info( ) . key) ;
97
96
98
97
// Init the metadata account
99
- let init_token_meta_data_ix =
100
- & spl_token_metadata_interface:: instruction:: initialize (
98
+ let init_token_meta_data_ix = & spl_token_metadata_interface:: instruction:: initialize (
101
99
& spl_token_2022:: id ( ) ,
102
100
ctx. accounts . mint . key ,
103
101
ctx. accounts . nft_authority . to_account_info ( ) . key ,
104
102
ctx. accounts . mint . key ,
105
103
ctx. accounts . nft_authority . to_account_info ( ) . key ,
106
104
"Beaver" . to_string ( ) ,
107
105
"BVA" . to_string ( ) ,
108
- "https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0" . to_string ( ) ,
106
+ "https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0" . to_string ( )
109
107
) ;
110
108
111
109
invoke_signed (
112
110
init_token_meta_data_ix,
113
- & [ ctx. accounts . mint . to_account_info ( ) . clone ( ) , ctx. accounts . nft_authority . to_account_info ( ) . clone ( ) ] ,
114
- signer,
111
+ & [
112
+ ctx. accounts . mint . to_account_info ( ) . clone ( ) ,
113
+ ctx. accounts . nft_authority . to_account_info ( ) . clone ( ) ,
114
+ ] ,
115
+ signer
115
116
) ?;
116
117
117
118
// Update the metadata account with an additional metadata field in this case the player level
@@ -121,7 +122,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
121
122
ctx. accounts . mint . key ,
122
123
ctx. accounts . nft_authority . to_account_info ( ) . key ,
123
124
spl_token_metadata_interface:: state:: Field :: Key ( "level" . to_string ( ) ) ,
124
- "1" . to_string ( ) ,
125
+ "1" . to_string ( )
125
126
) ,
126
127
& [
127
128
ctx. accounts . mint . to_account_info ( ) . clone ( ) ,
@@ -133,16 +134,17 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
133
134
// Create the associated token account
134
135
associated_token:: create (
135
136
CpiContext :: new (
136
- ctx. accounts . associated_token_program . to_account_info ( ) ,
137
- associated_token:: Create {
138
- payer : ctx. accounts . signer . to_account_info ( ) ,
139
- associated_token : ctx. accounts . token_account . to_account_info ( ) ,
140
- authority : ctx. accounts . signer . to_account_info ( ) ,
141
- mint : ctx. accounts . mint . to_account_info ( ) ,
142
- system_program : ctx. accounts . system_program . to_account_info ( ) ,
143
- token_program : ctx. accounts . token_program . to_account_info ( ) ,
144
- } ,
145
- ) ) ?;
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
+ ) ?;
146
148
147
149
// Mint one token to the associated token account of the player
148
150
token_2022:: mint_to (
@@ -155,7 +157,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
155
157
} ,
156
158
signer
157
159
) ,
158
- 1 ,
160
+ 1
159
161
) ?;
160
162
161
163
// Freeze the mint authority so no more tokens can be minted to make it an NFT
@@ -169,7 +171,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
169
171
signer
170
172
) ,
171
173
AuthorityType :: MintTokens ,
172
- None ,
174
+ None
173
175
) ?;
174
176
175
177
Ok ( ( ) )
@@ -188,16 +190,9 @@ pub struct MintNft<'info> {
188
190
pub mint : Signer < ' info > ,
189
191
pub rent : Sysvar < ' info , Rent > ,
190
192
pub associated_token_program : Program < ' info , AssociatedToken > ,
191
- #[ account(
192
- init_if_needed,
193
- seeds = [ b"nft_authority" . as_ref( ) ] ,
194
- bump,
195
- space = 8 ,
196
- payer = signer,
197
- ) ]
198
- pub nft_authority : Account < ' info , NftAuthority >
193
+ #[ account( init_if_needed, seeds = [ b"nft_authority" . as_ref( ) ] , bump, space = 8 , payer = signer) ]
194
+ pub nft_authority : Account < ' info , NftAuthority > ,
199
195
}
200
196
201
197
#[ account]
202
- pub struct NftAuthority {
203
- }
198
+ pub struct NftAuthority { }
0 commit comments