Skip to content

Commit 77ee7fd

Browse files
authored
feat(HIP-646/657/765): add metadata examples and tests
1 parent 61067f0 commit 77ee7fd

File tree

11 files changed

+482
-159
lines changed

11 files changed

+482
-159
lines changed

.github/workflows/rust-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
repo-token: ${{ secrets.GITHUB_TOKEN }}
5858

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

6262
- name: "Create env file"
6363
run: |

examples/nft_update_metadata.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
AccountId, Client, PrivateKey, TokenCreateTransaction, TokenInfoQuery, TokenType, TokenUpdateTransaction
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+
let admin_key = PrivateKey::generate_ed25519();
50+
51+
// Initial metadata
52+
let metadata: Vec<u8> = vec![1];
53+
// New metadata
54+
let new_metadata: Vec<u8> = vec![1, 2];
55+
56+
let token_create_receipt = TokenCreateTransaction::new()
57+
.name("ffff")
58+
.symbol("F")
59+
.token_type(TokenType::FungibleCommon)
60+
.decimals(3)
61+
.initial_supply(1000000)
62+
.treasury_account_id(client.get_operator_account_id().unwrap())
63+
.expiration_time(OffsetDateTime::now_utc() + Duration::minutes(5))
64+
.admin_key(admin_key.public_key())
65+
.metadata(metadata)
66+
.freeze_with(&client)?
67+
.sign(admin_key.clone())
68+
.execute(&client)
69+
.await?
70+
.get_receipt(&client)
71+
.await?;
72+
73+
// Get token id
74+
let token_id = token_create_receipt.token_id.unwrap();
75+
println!("Created a mutable token: {token_id:?}");
76+
77+
let token_info = TokenInfoQuery::new()
78+
.token_id(token_id)
79+
.execute(&client)
80+
.await?;
81+
82+
println!(
83+
"Immutable token's metadata after creation: {:?}",
84+
token_info.metadata
85+
);
86+
87+
let token_update_receipt = TokenUpdateTransaction::new()
88+
.token_id(token_id)
89+
.metadata(new_metadata)
90+
.freeze_with(&client)?
91+
.sign(admin_key)
92+
.execute(&client)
93+
.await?
94+
.get_receipt(&client)
95+
.await?;
96+
97+
println!(
98+
"Status of token update transaction: {:?}",
99+
token_update_receipt.status
100+
);
101+
102+
let token_nft_info = TokenInfoQuery::new()
103+
.token_id(token_id)
104+
.execute(&client)
105+
.await?;
106+
107+
println!(
108+
"Immutable token's metadata after update: {:?}",
109+
token_nft_info.metadata
110+
);
111+
112+
Ok(())
113+
}

0 commit comments

Comments
 (0)