forked from solana-developers/program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_token.rs
More file actions
112 lines (101 loc) · 3.51 KB
/
create_token.rs
File metadata and controls
112 lines (101 loc) · 3.51 KB
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
use steel_api::prelude::*;
use spl_token::state::Mint;
use solana_program::msg;
use solana_program::program_pack::Pack;
use steel::*;
pub fn process_create_token(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
//Parse Args
let args = CreateToken::try_from_bytes(data)?;
let name = String::from_utf8(args.data.name.to_vec()).expect("Invalid UTF-8");
let symbol = String::from_utf8(args.data.symbol.to_vec()).expect("Invalid UTF-8");
let uri = String::from_utf8(args.data.uri.to_vec()).expect("Invalid UTF-8");
let decimals = args.data.decimals;
msg!("Parsed Arguments");
// Load accounts.
let [signer_info, mint_info,metadata_info, system_program, token_program, metadata_program, rent_sysvar] =
accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
msg!("Loaded Accounts");
//Validation
signer_info.is_signer()?;
mint_info.is_signer()?.is_empty()?.is_writable()?;
metadata_info.is_empty()?.is_writable()?
.has_seeds(
&[METADATA, mpl_token_metadata::ID.as_ref(), mint_info.key.as_ref()],
&mpl_token_metadata::ID,
)?
;
system_program.is_program(&system_program::ID)?;
token_program.is_program(&spl_token::ID)?;
metadata_program.is_program(&mpl_token_metadata::ID)?;
rent_sysvar.is_sysvar(&sysvar::rent::ID)?;
msg!("Accounts validated");
// First, create an account for the Mint.
//
msg!("Creating mint account...");
msg!("Mint: {}", mint_info.key);
solana_program::program::invoke(
&solana_program::system_instruction::create_account(
signer_info.key,
mint_info.key,
(solana_program::rent::Rent::get()?).minimum_balance(Mint::LEN),
Mint::LEN as u64,
token_program.key,
),
&[
mint_info.clone(),
signer_info.clone(),
system_program.clone(),
token_program.clone(),
],
)?;
// Second, Initialize account as Mint Account
//
msg!("Initializing mint account...");
solana_program::program::invoke(
&spl_token::instruction::initialize_mint(
token_program.key,
mint_info.key,
signer_info.key,
Some(signer_info.key),
decimals, // 9 Decimals for the default SPL Token standard
)?,
&[
mint_info.clone(),
signer_info.clone(),
token_program.clone(),
rent_sysvar.clone(),
],
)?;
// Lastly, create the account for that Mint's metadata
//
msg!("Creating metadata account...");
msg!("Metadata account address: {}", metadata_info.key);
mpl_token_metadata::instructions::CreateMetadataAccountV3Cpi {
__program: metadata_program,
metadata: metadata_info,
mint: mint_info,
mint_authority: signer_info,
payer: signer_info,
update_authority: (signer_info, true),
system_program,
rent: Some(rent_sysvar),
__args: mpl_token_metadata::instructions::CreateMetadataAccountV3InstructionArgs {
data: mpl_token_metadata::types::DataV2 {
name: name,
symbol: symbol,
uri: uri,
seller_fee_basis_points: 0,
creators: None,
collection: None,
uses: None,
},
is_mutable: true,
collection_details: None,
},
}
.invoke()?;
msg!("Token mint created successfully.");
Ok(())
}