This repository was archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcreate_file.rs
More file actions
59 lines (47 loc) · 1.88 KB
/
create_file.rs
File metadata and controls
59 lines (47 loc) · 1.88 KB
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
use failure::{format_err, Error};
use hedera::{Client, SecretKey, Status};
use std::{env, thread::sleep, time::Duration};
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), Error> {
pretty_env_logger::try_init()?;
// Operator is the account that sends the transaction to the network
// This account is charged for the transaction fee
let operator = "0:0:2".parse()?;
let client = Client::builder("testnet.hedera.com:50003")
.node("0:0:3".parse()?)
.operator(operator, || env::var("OPERATOR_SECRET"))
.build()?;
let operator_secret : String = env::var("OPERATOR_SECRET")?;
let secret = SecretKey::from_str(&operator_secret)?;
let public = secret.public();
// init some file contents
let file_contents_string = String::from("Hedera Hashgraph is great");
let file_contents_bytes = file_contents_string.into_bytes();
// Create a file
let id = client
.create_file()
.expires_in(Duration::from_secs(2_592_000))
.key(public)
.contents(file_contents_bytes)
.memo("[hedera-sdk-rust][example] create_file")
.sign(&env::var("OPERATOR_SECRET")?.parse()?) // sign as the owner of the file
.execute_async()
.await?;
println!("creating file; transaction = {}", id);
// If we got here we know we passed pre-check
// Depending on your requirements that may be enough for some kinds of transactions
sleep(Duration::from_secs(2));
// Get the receipt and check the status to prove it was successful
let mut tx = client.transaction(id).receipt();
let receipt = tx.get_async().await?;
if receipt.status != Status::Success {
Err(format_err!(
"transaction has a non-successful status: {:?}",
receipt.status
))?;
}
let file = receipt.file_id.unwrap();
println!("file ID = {}", file);
Ok(())
}