|
| 1 | +/* |
| 2 | + * |
| 3 | + * Hedera Rust SDK |
| 4 | + * |
| 5 | + * Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +use clap::Parser; |
| 22 | +use hedera::{ |
| 23 | + AccountCreateTransaction, AccountId, Client, Hbar, NftId, PrivateKey, TokenCreateTransaction, TokenInfoQuery, TokenMintTransaction, TokenNftInfoQuery, TokenType, TokenUpdateNftsTransaction, TransferTransaction |
| 24 | +}; |
| 25 | +use time::{Duration, OffsetDateTime}; |
| 26 | + |
| 27 | +#[derive(Parser, Debug)] |
| 28 | +struct Args { |
| 29 | + #[clap(long, env)] |
| 30 | + operator_account_id: AccountId, |
| 31 | + |
| 32 | + #[clap(long, env)] |
| 33 | + operator_key: PrivateKey, |
| 34 | + |
| 35 | + #[clap(long, env, default_value = "testnet")] |
| 36 | + hedera_network: String, |
| 37 | +} |
| 38 | + |
| 39 | +#[tokio::main] |
| 40 | +async fn main() -> anyhow::Result<()> { |
| 41 | + let _ = dotenvy::dotenv(); |
| 42 | + |
| 43 | + let args = Args::parse(); |
| 44 | + |
| 45 | + let client = Client::for_name(&args.hedera_network)?; |
| 46 | + |
| 47 | + client.set_operator(args.operator_account_id, args.operator_key.clone()); |
| 48 | + |
| 49 | + // Generate a supply key |
| 50 | + let supply_key = PrivateKey::generate_ed25519(); |
| 51 | + // Generate a metadata key |
| 52 | + let metadata_key = PrivateKey::generate_ed25519(); |
| 53 | + // Initial metadata |
| 54 | + let metadata: Vec<u8> = vec![1]; |
| 55 | + // New metadata |
| 56 | + let new_metadata: Vec<u8> = vec![1, 2]; |
| 57 | + |
| 58 | + let token_create_receipt = TokenCreateTransaction::new() |
| 59 | + .name("ffff") |
| 60 | + .symbol("F") |
| 61 | + .token_type(TokenType::NonFungibleUnique) |
| 62 | + .treasury_account_id(client.get_operator_account_id().unwrap()) |
| 63 | + .supply_key(client.get_operator_public_key().unwrap()) |
| 64 | + .metadata_key(metadata_key.public_key()) |
| 65 | + .expiration_time(OffsetDateTime::now_utc() + Duration::minutes(5)) |
| 66 | + .sign(args.operator_key.clone()) |
| 67 | + .execute(&client) |
| 68 | + .await? |
| 69 | + .get_receipt(&client) |
| 70 | + .await?; |
| 71 | + |
| 72 | + let token_id = token_create_receipt.token_id.unwrap(); |
| 73 | + |
| 74 | + println!("Token id: {token_id:?}"); |
| 75 | + |
| 76 | + let token_info = TokenInfoQuery::new() |
| 77 | + .token_id(token_id) |
| 78 | + .execute(&client) |
| 79 | + .await?; |
| 80 | + |
| 81 | + println!("Token metadata key: {:?}", token_info.metadata_key); |
| 82 | + |
| 83 | + // Mint the token |
| 84 | + let token_mint_receipt = TokenMintTransaction::new() |
| 85 | + .token_id(token_id) |
| 86 | + .metadata([metadata]) |
| 87 | + .freeze_with(&client)? |
| 88 | + .sign(supply_key) |
| 89 | + .execute(&client) |
| 90 | + .await? |
| 91 | + .get_receipt(&client) |
| 92 | + .await?; |
| 93 | + |
| 94 | + println!( |
| 95 | + "Status of token mint transaction: {:?}", |
| 96 | + token_create_receipt.status |
| 97 | + ); |
| 98 | + |
| 99 | + let nft_serial = *token_mint_receipt.serials.first().unwrap() as u64; |
| 100 | + |
| 101 | + let nft_id = NftId { |
| 102 | + token_id, |
| 103 | + serial: nft_serial, |
| 104 | + }; |
| 105 | + |
| 106 | + let token_nfts_info = TokenNftInfoQuery::new() |
| 107 | + .nft_id(nft_id) |
| 108 | + .execute(&client) |
| 109 | + .await?; |
| 110 | + |
| 111 | + println!("Set token NFT metadata: {:?}", token_nfts_info.metadata); |
| 112 | + |
| 113 | + let account_id = AccountCreateTransaction::new() |
| 114 | + .key(client.get_operator_public_key().unwrap()) |
| 115 | + .max_automatic_token_associations(10) |
| 116 | + .initial_balance(Hbar::new(100)) |
| 117 | + .freeze_with(&client)? |
| 118 | + .sign(args.operator_key.clone()) |
| 119 | + .execute(&client) |
| 120 | + .await? |
| 121 | + .get_receipt(&client) |
| 122 | + .await? |
| 123 | + .account_id |
| 124 | + .unwrap(); |
| 125 | + |
| 126 | + println!("New Account id: {account_id:?}"); |
| 127 | + |
| 128 | + let transfer_nft_tx = TransferTransaction::new() |
| 129 | + .nft_transfer( |
| 130 | + nft_id, |
| 131 | + client.get_operator_account_id().unwrap(), |
| 132 | + account_id, |
| 133 | + ) |
| 134 | + .freeze_with(&client)? |
| 135 | + .sign(args.operator_key.clone()) |
| 136 | + .execute(&client) |
| 137 | + .await?; |
| 138 | + |
| 139 | + let transfer_nft_response = transfer_nft_tx.get_receipt(&client).await?; |
| 140 | + |
| 141 | + println!( |
| 142 | + "Status of transfer NFT transaction: {:?}", |
| 143 | + transfer_nft_response.status |
| 144 | + ); |
| 145 | + |
| 146 | + let token_update_nfts_receipt = TokenUpdateNftsTransaction::new() |
| 147 | + .token_id(token_id) |
| 148 | + .serials(vec![nft_serial as i64]) |
| 149 | + .metadata(new_metadata) |
| 150 | + .freeze_with(&client)? |
| 151 | + .sign(metadata_key) |
| 152 | + .execute(&client) |
| 153 | + .await? |
| 154 | + .get_receipt(&client) |
| 155 | + .await?; |
| 156 | + |
| 157 | + println!( |
| 158 | + "Status of token update NFT transaction: {:?}", |
| 159 | + token_update_nfts_receipt.status |
| 160 | + ); |
| 161 | + |
| 162 | + let token_nft_info = TokenNftInfoQuery::new() |
| 163 | + .nft_id(nft_id) |
| 164 | + .execute(&client) |
| 165 | + .await?; |
| 166 | + |
| 167 | + println!("Updated token NFT metadata: {:?}", token_nft_info.metadata); |
| 168 | + |
| 169 | + Ok(()) |
| 170 | +} |
0 commit comments