Skip to content

Commit

Permalink
feat(HIP-646/657/765): add metadata examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyLB authored Apr 25, 2024
1 parent 61067f0 commit 77ee7fd
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Start the local node
run: npx @hashgraph/hedera-local start -d --network local --network-tag=0.48.0-alpha.12
run: npx @hashgraph/hedera-local start -d --network local --balance=100000

- name: "Create env file"
run: |
Expand Down
170 changes: 170 additions & 0 deletions examples/nft_update_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* ‌
* Hedera Rust SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

use clap::Parser;
use hedera::{
AccountCreateTransaction, AccountId, Client, Hbar, NftId, PrivateKey, TokenCreateTransaction, TokenInfoQuery, TokenMintTransaction, TokenNftInfoQuery, TokenType, TokenUpdateNftsTransaction, TransferTransaction
};
use time::{Duration, OffsetDateTime};

#[derive(Parser, Debug)]
struct Args {
#[clap(long, env)]
operator_account_id: AccountId,

#[clap(long, env)]
operator_key: PrivateKey,

#[clap(long, env, default_value = "testnet")]
hedera_network: String,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = dotenvy::dotenv();

let args = Args::parse();

let client = Client::for_name(&args.hedera_network)?;

client.set_operator(args.operator_account_id, args.operator_key.clone());

// Generate a supply key
let supply_key = PrivateKey::generate_ed25519();
// Generate a metadata key
let metadata_key = PrivateKey::generate_ed25519();
// Initial metadata
let metadata: Vec<u8> = vec![1];
// New metadata
let new_metadata: Vec<u8> = vec![1, 2];

let token_create_receipt = TokenCreateTransaction::new()
.name("ffff")
.symbol("F")
.token_type(TokenType::NonFungibleUnique)
.treasury_account_id(client.get_operator_account_id().unwrap())
.supply_key(client.get_operator_public_key().unwrap())
.metadata_key(metadata_key.public_key())
.expiration_time(OffsetDateTime::now_utc() + Duration::minutes(5))
.sign(args.operator_key.clone())
.execute(&client)
.await?
.get_receipt(&client)
.await?;

let token_id = token_create_receipt.token_id.unwrap();

println!("Token id: {token_id:?}");

let token_info = TokenInfoQuery::new()
.token_id(token_id)
.execute(&client)
.await?;

println!("Token metadata key: {:?}", token_info.metadata_key);

// Mint the token
let token_mint_receipt = TokenMintTransaction::new()
.token_id(token_id)
.metadata([metadata])
.freeze_with(&client)?
.sign(supply_key)
.execute(&client)
.await?
.get_receipt(&client)
.await?;

println!(
"Status of token mint transaction: {:?}",
token_create_receipt.status
);

let nft_serial = *token_mint_receipt.serials.first().unwrap() as u64;

let nft_id = NftId {
token_id,
serial: nft_serial,
};

let token_nfts_info = TokenNftInfoQuery::new()
.nft_id(nft_id)
.execute(&client)
.await?;

println!("Set token NFT metadata: {:?}", token_nfts_info.metadata);

let account_id = AccountCreateTransaction::new()
.key(client.get_operator_public_key().unwrap())
.max_automatic_token_associations(10)
.initial_balance(Hbar::new(100))
.freeze_with(&client)?
.sign(args.operator_key.clone())
.execute(&client)
.await?
.get_receipt(&client)
.await?
.account_id
.unwrap();

println!("New Account id: {account_id:?}");

let transfer_nft_tx = TransferTransaction::new()
.nft_transfer(
nft_id,
client.get_operator_account_id().unwrap(),
account_id,
)
.freeze_with(&client)?
.sign(args.operator_key.clone())
.execute(&client)
.await?;

let transfer_nft_response = transfer_nft_tx.get_receipt(&client).await?;

println!(
"Status of transfer NFT transaction: {:?}",
transfer_nft_response.status
);

let token_update_nfts_receipt = TokenUpdateNftsTransaction::new()
.token_id(token_id)
.serials(vec![nft_serial as i64])
.metadata(new_metadata)
.freeze_with(&client)?
.sign(metadata_key)
.execute(&client)
.await?
.get_receipt(&client)
.await?;

println!(
"Status of token update NFT transaction: {:?}",
token_update_nfts_receipt.status
);

let token_nft_info = TokenNftInfoQuery::new()
.nft_id(nft_id)
.execute(&client)
.await?;

println!("Updated token NFT metadata: {:?}", token_nft_info.metadata);

Ok(())
}
113 changes: 113 additions & 0 deletions examples/token_update_metadata_with_admin_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* ‌
* Hedera Rust SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

use clap::Parser;
use hedera::{
AccountId, Client, PrivateKey, TokenCreateTransaction, TokenInfoQuery, TokenType, TokenUpdateTransaction
};
use time::{Duration, OffsetDateTime};

#[derive(Parser, Debug)]
struct Args {
#[clap(long, env)]
operator_account_id: AccountId,

#[clap(long, env)]
operator_key: PrivateKey,

#[clap(long, env, default_value = "testnet")]
hedera_network: String,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = dotenvy::dotenv();

let args = Args::parse();

let client = Client::for_name(&args.hedera_network)?;

client.set_operator(args.operator_account_id, args.operator_key.clone());

let admin_key = PrivateKey::generate_ed25519();

// Initial metadata
let metadata: Vec<u8> = vec![1];
// New metadata
let new_metadata: Vec<u8> = vec![1, 2];

let token_create_receipt = TokenCreateTransaction::new()
.name("ffff")
.symbol("F")
.token_type(TokenType::FungibleCommon)
.decimals(3)
.initial_supply(1000000)
.treasury_account_id(client.get_operator_account_id().unwrap())
.expiration_time(OffsetDateTime::now_utc() + Duration::minutes(5))
.admin_key(admin_key.public_key())
.metadata(metadata)
.freeze_with(&client)?
.sign(admin_key.clone())
.execute(&client)
.await?
.get_receipt(&client)
.await?;

// Get token id
let token_id = token_create_receipt.token_id.unwrap();
println!("Created a mutable token: {token_id:?}");

let token_info = TokenInfoQuery::new()
.token_id(token_id)
.execute(&client)
.await?;

println!(
"Immutable token's metadata after creation: {:?}",
token_info.metadata
);

let token_update_receipt = TokenUpdateTransaction::new()
.token_id(token_id)
.metadata(new_metadata)
.freeze_with(&client)?
.sign(admin_key)
.execute(&client)
.await?
.get_receipt(&client)
.await?;

println!(
"Status of token update transaction: {:?}",
token_update_receipt.status
);

let token_nft_info = TokenInfoQuery::new()
.token_id(token_id)
.execute(&client)
.await?;

println!(
"Immutable token's metadata after update: {:?}",
token_nft_info.metadata
);

Ok(())
}
Loading

0 comments on commit 77ee7fd

Please sign in to comment.