-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate_mint.rs
More file actions
132 lines (111 loc) · 3.55 KB
/
Copy pathcreate_mint.rs
File metadata and controls
132 lines (111 loc) · 3.55 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Create the NFT mint
use crate::{cpi::Cpi, state::MINT_PREFIX};
use {
bonfida_utils::{
checks::{check_account_key, check_account_owner, check_signer},
BorshSize, InstructionsAccount,
},
borsh::{BorshDeserialize, BorshSerialize},
solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program::invoke_signed,
program_error::ProgramError,
program_pack::Pack,
pubkey::Pubkey,
system_program, sysvar,
},
spl_token::{instruction::initialize_mint, state::Mint},
};
#[derive(BorshDeserialize, BorshSerialize, BorshSize)]
pub struct Params {}
#[derive(InstructionsAccount)]
pub struct Accounts<'a, T> {
/// The mint of the NFT
#[cons(writable)]
pub mint: &'a T,
/// The domain name account
#[cons(writable)]
pub name_account: &'a T,
/// The central state account
pub central_state: &'a T,
/// The SPL token program account
pub spl_token_program: &'a T,
/// The system program account
pub system_program: &'a T,
/// Rent sysvar account
pub rent_account: &'a T,
/// Fee payer account
#[cons(writable, signer)]
pub fee_payer: &'a T,
}
impl<'a, 'b: 'a> Accounts<'a, AccountInfo<'b>> {
pub fn parse(
accounts: &'a [AccountInfo<'b>],
_program_id: &Pubkey,
) -> Result<Self, ProgramError> {
let accounts_iter = &mut accounts.iter();
let accounts = Accounts {
mint: next_account_info(accounts_iter)?,
name_account: next_account_info(accounts_iter)?,
central_state: next_account_info(accounts_iter)?,
spl_token_program: next_account_info(accounts_iter)?,
system_program: next_account_info(accounts_iter)?,
rent_account: next_account_info(accounts_iter)?,
fee_payer: next_account_info(accounts_iter)?,
};
// Check keys
check_account_key(accounts.central_state, &crate::central_state::KEY)?;
check_account_key(accounts.spl_token_program, &spl_token::ID)?;
check_account_key(accounts.system_program, &system_program::ID)?;
check_account_key(accounts.rent_account, &sysvar::rent::ID)?;
// Check owners
check_account_owner(accounts.mint, &system_program::ID)?;
check_account_owner(accounts.name_account, &spl_name_service::ID)?;
// Check signer
check_signer(accounts.fee_payer)?;
Ok(accounts)
}
}
pub fn process(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let accounts = Accounts::parse(accounts, program_id)?;
let (mint, mint_nonce) = Pubkey::find_program_address(
&[MINT_PREFIX, &accounts.name_account.key.to_bytes()],
program_id,
);
check_account_key(accounts.mint, &mint)?;
msg!("+ Creating mint");
// Create mint account
let seeds: &[&[u8]] = &[
MINT_PREFIX,
&accounts.name_account.key.to_bytes(),
&[mint_nonce],
];
Cpi::create_account(
&spl_token::ID,
accounts.system_program,
accounts.fee_payer,
accounts.mint,
seeds,
Mint::LEN,
)?;
// Initialize mint
let ix = initialize_mint(
&spl_token::ID,
&mint,
&crate::central_state::KEY,
Some(&crate::central_state::KEY),
0,
)?;
invoke_signed(
&ix,
&[
accounts.spl_token_program.clone(),
accounts.mint.clone(),
accounts.rent_account.clone(),
],
&[seeds],
)?;
Ok(())
}