Last active
January 10, 2022 09:06
-
-
Save jozanza/577a21d2e94d595e80031ce0a5269614 to your computer and use it in GitHub Desktop.
Mint a Solana NFT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use metaplex_token_metadata::{ | |
instruction::create_metadata_accounts, | |
state::{Metadata, PREFIX}, | |
}; | |
use solana_client::rpc_client::RpcClient; | |
use solana_program::{ | |
borsh::try_from_slice_unchecked, program_pack::Pack, system_instruction::create_account, | |
}; | |
use solana_sdk::{ | |
pubkey::Pubkey, | |
signature::{read_keypair_file, Keypair}, | |
signer::Signer, | |
transaction::Transaction, | |
}; | |
use spl_associated_token_account::{create_associated_token_account, get_associated_token_address}; | |
use spl_token::{ | |
instruction::{initialize_mint, mint_to}, | |
state::Mint, | |
}; | |
use yaml_rust::YamlLoader; | |
// UPDATE THESE | |
static TOKEN_NAME: &str = "Token name"; | |
static TOKEN_SYMBOL: &str = "TOKEN"; | |
static TOKEN_JSON_URI: &str = "https://..."; | |
fn main() { | |
let home_dir = std::env::var("HOME").unwrap(); | |
let solana_config_yaml_path = format!("{}/{}", home_dir, ".config/solana/cli/config.yml"); | |
let mut solana_config_yaml_source = String::new(); | |
let _res = std::io::Read::read_to_string( | |
&mut std::fs::File::open(solana_config_yaml_path).unwrap(), | |
&mut solana_config_yaml_source, | |
); | |
let solana_config_yaml = YamlLoader::load_from_str(&solana_config_yaml_source).unwrap(); | |
let doc = &solana_config_yaml[0]; | |
let rpc_url = doc["json_rpc_url"].as_str().unwrap(); | |
let keypair_path = solana_config_yaml[0]["keypair_path"].as_str().unwrap(); | |
let payer = read_keypair_file(keypair_path).unwrap(); | |
let payer_pubkey = payer.pubkey(); | |
println!("- update_authority: {:?}", payer_pubkey); | |
let name = TOKEN_NAME.to_string(); | |
let symbol = TOKEN_SYMBOL.to_string(); | |
let uri = TOKEN_JSON_URI.to_string(); | |
let mutable = false; | |
let mint_keypair = Keypair::new(); | |
let mint_pubkey = mint_keypair.pubkey(); | |
println!("- mint_pubkey: {:?}", mint_pubkey); | |
let token_program_pubkey = spl_token::id(); | |
let metadata_program_pubkey = metaplex_token_metadata::id(); | |
let metadata_seeds = &[ | |
PREFIX.as_bytes(), | |
metadata_program_pubkey.as_ref(), | |
mint_pubkey.as_ref(), | |
]; | |
let (metadata_pubkey, _) = | |
Pubkey::find_program_address(metadata_seeds, &metadata_program_pubkey); | |
println!("- metadata_pubkey: {:?}", metadata_pubkey); | |
let client = RpcClient::new(rpc_url.to_string()); | |
let create_account_instruction = create_account( | |
&payer_pubkey, | |
&mint_pubkey, | |
client | |
.get_minimum_balance_for_rent_exemption(Mint::LEN) | |
.unwrap(), | |
Mint::LEN as u64, | |
&token_program_pubkey, | |
); | |
let initialize_mint_instruction = initialize_mint( | |
&token_program_pubkey, | |
&mint_pubkey, | |
&payer_pubkey, | |
Some(&payer_pubkey), | |
0, | |
) | |
.unwrap(); | |
let create_metadata_instruction = create_metadata_accounts( | |
metadata_program_pubkey, | |
metadata_pubkey, | |
mint_pubkey, | |
payer_pubkey, | |
payer_pubkey, | |
payer_pubkey, | |
name, | |
symbol, | |
uri, | |
None, | |
0, | |
true, | |
mutable, | |
); | |
let create_ata_instruction = | |
create_associated_token_account(&payer_pubkey, &payer_pubkey, &mint_pubkey); | |
let ata_pubkey = get_associated_token_address(&payer_pubkey, &mint_pubkey); | |
println!("- ata_pubkey: {:?}", ata_pubkey); | |
let mint_token_instruction = mint_to( | |
&token_program_pubkey, | |
&mint_pubkey, | |
&ata_pubkey, | |
&payer_pubkey, | |
&[&payer_pubkey], | |
1, | |
) | |
.unwrap(); | |
let instructions = &[ | |
create_account_instruction, | |
initialize_mint_instruction, | |
create_metadata_instruction, | |
create_ata_instruction, | |
mint_token_instruction, | |
]; | |
let mut transaction = Transaction::new_with_payer(instructions, Some(&payer_pubkey)); | |
transaction.sign( | |
&[&payer, &mint_keypair], | |
client.get_latest_blockhash().unwrap(), | |
); | |
println!("Sending tx..."); | |
let sig = client.send_and_confirm_transaction(&transaction).unwrap(); | |
println!("Tx confirmed! {}", sig); | |
println!("Getting metadata account"); | |
let account = client.get_account(&metadata_pubkey).unwrap(); | |
let metadata: Metadata = try_from_slice_unchecked(&account.data).unwrap(); | |
println!("{:?}", metadata); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment