-
Notifications
You must be signed in to change notification settings - Fork 31
/
create_spl.rs
62 lines (51 loc) · 1.77 KB
/
create_spl.rs
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
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
instruction::Instruction,
program_pack::Pack,
signer::{keypair::Keypair, Signer},
transaction::Transaction,
};
use spl_token::state::Mint;
#[derive(serde::Deserialize)]
struct Env {
rpc_url: url::Url,
signer_keypair: String,
mint_keypair: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let env = envy::from_env::<Env>()?;
let signer_wallet = Keypair::from_base58_string(&env.signer_keypair);
let mint_account = Keypair::from_base58_string(&env.mint_keypair);
let client = RpcClient::new(env.rpc_url.to_string());
let decimals = 9;
let minimum_balance_for_rent_exemption =
client.get_minimum_balance_for_rent_exemption(Mint::LEN)?;
let create_account_instruction: Instruction = solana_sdk::system_instruction::create_account(
&signer_wallet.pubkey(),
&mint_account.pubkey(),
minimum_balance_for_rent_exemption,
Mint::LEN as u64,
&spl_token::ID,
);
let initialize_mint_instruction: Instruction = spl_token::instruction::initialize_mint(
&spl_token::ID,
&mint_account.pubkey(),
&signer_wallet.pubkey(),
None,
decimals,
)?;
let recent_blockhash = client.get_latest_blockhash()?;
let transaction: Transaction = Transaction::new_signed_with_payer(
&[create_account_instruction, initialize_mint_instruction],
Some(&signer_wallet.pubkey()),
&[&mint_account, &signer_wallet],
recent_blockhash,
);
client.send_and_confirm_transaction_with_spinner(&transaction)?;
println!(
"SPL Token mint account with {} decimals created successfully:\n{}",
decimals,
mint_account.pubkey().to_string()
);
Ok(())
}