Skip to content

Commit

Permalink
Use IRC metadata in examples (#1180)
Browse files Browse the repository at this point in the history
* Use metadata in examples

* Update sdk/examples/how_tos/nft_collection/01_mint_collection_nft.rs

Co-authored-by: Thoralf-M <[email protected]>

* fix and add more

* Update sdk/examples/how_tos/native_tokens/create.rs

Co-authored-by: Thoralf-M <[email protected]>

* fmt

* changelog

* impls

---------

Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
Alexandcoats and Thoralf-M authored Sep 8, 2023
1 parent b78019e commit c3c0cf8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Output::kind_str()` method;
- `ConflictReason` display implementation with an explanation of the conflict;
- `TokenScheme` methods `is_simple` and `as_simple`;
- `Irc27Metadata` and `Irc30Metadata` helpers;

### Changed

Expand Down
6 changes: 3 additions & 3 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ required-features = ["wallet", "storage", "stronghold"]
[[example]]
name = "mint_collection_nft"
path = "examples/how_tos/nft_collection/01_mint_collection_nft.rs"
required-features = ["wallet", "stronghold"]
required-features = ["wallet", "stronghold", "irc_27"]

# Native Tokens Examples

[[example]]
name = "create_native_token"
path = "examples/how_tos/native_tokens/create.rs"
required-features = ["rocksdb", "stronghold"]
required-features = ["rocksdb", "stronghold", "irc_30"]

[[example]]
name = "destroy_foundry"
Expand Down Expand Up @@ -788,7 +788,7 @@ required-features = ["wallet", "storage"]
[[example]]
name = "mint_nft"
path = "examples/how_tos/nfts/mint_nft.rs"
required-features = ["wallet", "stronghold"]
required-features = ["wallet", "stronghold", "irc_27"]

[[example]]
name = "send_nft"
Expand Down
6 changes: 5 additions & 1 deletion sdk/examples/how_tos/native_tokens/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! ```
use iota_sdk::{
types::block::output::feature::Irc30Metadata,
wallet::{CreateNativeTokenParams, Result},
Wallet, U256,
};
Expand Down Expand Up @@ -59,13 +60,16 @@ async fn main() -> Result<()> {
println!("Account synced");
}

let metadata =
Irc30Metadata::new("My Native Token", "MNT", 10).with_description("A native token to test the iota-sdk.");

println!("Preparing transaction to create native token...");

let params = CreateNativeTokenParams {
alias_id: None,
circulating_supply: U256::from(CIRCULATING_SUPPLY),
maximum_supply: U256::from(MAXIMUM_SUPPLY),
foundry_metadata: None,
foundry_metadata: Some(metadata.to_bytes()),
};

let transaction = account.create_native_token(params, None).await?;
Expand Down
33 changes: 16 additions & 17 deletions sdk/examples/how_tos/nft_collection/01_mint_collection_nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use iota_sdk::{
types::block::{
address::{Bech32Address, NftAddress},
output::NftId,
output::{feature::Irc27Metadata, NftId},
payload::transaction::TransactionId,
},
wallet::{Account, MintNftParams, Result},
Expand Down Expand Up @@ -60,7 +60,7 @@ async fn main() -> Result<()> {
let nft_mint_params = (0..NFT_COLLECTION_SIZE)
.map(|index| {
MintNftParams::new()
.with_immutable_metadata(get_immutable_metadata(index, issuer_nft_id).as_bytes().to_vec())
.with_immutable_metadata(get_immutable_metadata(index).to_bytes())
// The NFT address from the NFT we minted in mint_issuer_nft example
.with_issuer(issuer)
})
Expand Down Expand Up @@ -89,21 +89,20 @@ async fn main() -> Result<()> {
Ok(())
}

fn get_immutable_metadata(index: usize, issuer_nft_id: NftId) -> String {
// Note: we use `serde_json::from_str` to remove all unnecessary whitespace
serde_json::from_str::<serde_json::Value>(&format!(
r#"{{
"standard":"IRC27",
"version":"v1.0",
"type":"video/mp4",
"uri":"ipfs://wrongcVm9fx47YXNTkhpMEYSxCD3Bqh7PJYr7eo5Ywrong",
"name":"Shimmer OG NFT #{index}",
"description":"The Shimmer OG NFT was handed out 1337 times by the IOTA Foundation to celebrate the official launch of the Shimmer Network.",
"issuerName":"IOTA Foundation",
"collectionId":"{issuer_nft_id}",
"collectionName":"Shimmer OG"
}}"#
)).unwrap().to_string()
fn get_immutable_metadata(index: usize) -> Irc27Metadata {
Irc27Metadata::new(
"video/mp4",
"https://ipfs.io/ipfs/QmPoYcVm9fx47YXNTkhpMEYSxCD3Bqh7PJYr7eo5YjLgiT"
.parse()
.unwrap(),
format!("Shimmer OG NFT #{index}"),
)
.with_description(
"The Shimmer OG NFT was handed out 1337 times by the IOTA Foundation \
to celebrate the official launch of the Shimmer Network.",
)
.with_issuer_name("IOTA Foundation")
.with_collection_name("Shimmer OG")
}

async fn wait_for_inclusion(transaction_id: &TransactionId, account: &Account) -> Result<()> {
Expand Down
15 changes: 11 additions & 4 deletions sdk/examples/how_tos/nfts/mint_nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use iota_sdk::{
types::block::output::{
feature::{IssuerFeature, SenderFeature},
feature::{Irc27Metadata, IssuerFeature, SenderFeature},
unlock_condition::AddressUnlockCondition,
NftId, NftOutputBuilder,
},
Expand All @@ -25,8 +25,6 @@ use iota_sdk::{
const NFT1_OWNER_ADDRESS: &str = "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu";
// The metadata of the first minted NFT
const NFT1_METADATA: &str = "some NFT metadata";
// The immutable metadata of the first minted NFT
const NFT1_IMMUTABLE_METADATA: &str = "some NFT immutable 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
Expand Down Expand Up @@ -57,13 +55,22 @@ async fn main() -> Result<()> {
.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(NFT1_IMMUTABLE_METADATA.as_bytes().to_vec())];
.with_immutable_metadata(metadata.to_bytes())];

let transaction = account.mint_nfts(nft_params, None).await?;
println!("Transaction sent: {}", transaction.transaction_id);
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/types/block/output/feature/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub(crate) mod irc_27 {
}

pub fn to_bytes(&self) -> Vec<u8> {
// Unwrap: Safe because this struct is known to be valid
serde_json::to_string(self).unwrap().into_bytes()
}
}
Expand All @@ -212,6 +213,12 @@ pub(crate) mod irc_27 {
}
}

impl From<Irc27Metadata> for Vec<u8> {
fn from(value: Irc27Metadata) -> Self {
value.to_bytes()
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Getters, PartialEq, Eq)]
#[getset(get = "pub")]
pub struct Attribute {
Expand Down Expand Up @@ -365,6 +372,7 @@ pub(crate) mod irc_30 {
}

pub fn to_bytes(&self) -> Vec<u8> {
// Unwrap: Safe because this struct is known to be valid
serde_json::to_string(self).unwrap().into_bytes()
}
}
Expand All @@ -376,6 +384,12 @@ pub(crate) mod irc_30 {
}
}

impl From<Irc30Metadata> for Vec<u8> {
fn from(value: Irc30Metadata) -> Self {
value.to_bytes()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit c3c0cf8

Please sign in to comment.