-
Notifications
You must be signed in to change notification settings - Fork 39
/
mint_nft.rs
122 lines (103 loc) · 4.12 KB
/
mint_nft.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
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
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! In this example we will mint some NFTs.
//!
//! Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by
//! running the `./how_tos/accounts_and_addresses/create_account.rs` example!
//!
//! Rename `.env.example` to `.env` first, then run the command:
//! ```sh
//! cargo run --release --all-features --example mint_nft
//! ```
use iota_sdk::{
types::block::output::{
feature::{Irc27Metadata, IssuerFeature, SenderFeature},
unlock_condition::AddressUnlockCondition,
NftId, NftOutputBuilder,
},
wallet::{MintNftParams, Result},
Wallet,
};
// The owner address of the first NFT we'll mint
const NFT1_OWNER_ADDRESS: &str = "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu";
// The metadata of the first minted NFT
const NFT1_METADATA: &str = "some NFT metadata";
// The tag of the first minted NFT
const NFT1_TAG: &str = "some NFT tag";
// The base coin amount we sent with the second NFT
const NFT2_AMOUNT: u64 = 1_000_000;
#[tokio::main]
async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();
for var in ["WALLET_DB_PATH", "STRONGHOLD_PASSWORD", "EXPLORER_URL"] {
std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example"));
}
// Create the wallet
let wallet = Wallet::builder()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
// Get the account we generated with `create_account`
let account = wallet.get_account("Alice").await?;
// Ensure the account is synced after minting.
account.sync(None).await?;
// We send from the first address in the account.
let sender_address = *account.addresses().await?[0].address();
// Set the stronghold password
wallet
.set_stronghold_password(std::env::var("STRONGHOLD_PASSWORD").unwrap())
.await?;
let metadata = Irc27Metadata::new(
"video/mp4",
"https://ipfs.io/ipfs/QmPoYcVm9fx47YXNTkhpMEYSxCD3Bqh7PJYr7eo5YjLgiT"
.parse()
.unwrap(),
"Shimmer OG NFT",
)
.with_description("The original Shimmer NFT");
let nft_params = [MintNftParams::new()
.try_with_address(NFT1_OWNER_ADDRESS)?
.try_with_sender(sender_address)?
.with_metadata(NFT1_METADATA.as_bytes().to_vec())
.with_tag(NFT1_TAG.as_bytes().to_vec())
.try_with_issuer(sender_address)?
.with_immutable_metadata(metadata.to_bytes())];
let transaction = account.mint_nfts(nft_params, None).await?;
println!("Transaction sent: {}", transaction.transaction_id);
// Wait for transaction to get included
let block_id = account
.retry_transaction_until_included(&transaction.transaction_id, None, None)
.await?;
println!(
"Block included: {}/block/{}",
std::env::var("EXPLORER_URL").unwrap(),
block_id
);
println!("Minted NFT 1");
// Build an NFT manually by using the `NftOutputBuilder`
let token_supply = account.client().get_token_supply().await?;
let outputs = [
// address of the owner of the NFT
NftOutputBuilder::new_with_amount(NFT2_AMOUNT, NftId::null())
.add_unlock_condition(AddressUnlockCondition::new(sender_address))
.add_feature(SenderFeature::new(sender_address))
.add_immutable_feature(IssuerFeature::new(sender_address))
.finish_output(token_supply)?,
];
let transaction = account.send_outputs(outputs, None).await?;
println!("Transaction sent: {}", transaction.transaction_id);
// Wait for transaction to get included
let block_id = account
.retry_transaction_until_included(&transaction.transaction_id, None, None)
.await?;
println!(
"Block included: {}/block/{}",
std::env::var("EXPLORER_URL").unwrap(),
block_id
);
println!("Minted NFT 2");
// Ensure the account is synced after minting.
account.sync(None).await?;
Ok(())
}