|
| 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, AccountInfoQuery, AccountUpdateTransaction, Client, Hbar, Key, KeyList, PrivateKey, ScheduleInfoQuery, ScheduleSignTransaction, TransferTransaction |
| 24 | +}; |
| 25 | +use time::{Duration, OffsetDateTime}; |
| 26 | +use tokio::time::sleep; |
| 27 | + |
| 28 | +#[derive(Parser, Debug)] |
| 29 | +struct Args { |
| 30 | + #[clap(long, env)] |
| 31 | + operator_account_id: AccountId, |
| 32 | + |
| 33 | + #[clap(long, env)] |
| 34 | + operator_key: PrivateKey, |
| 35 | + |
| 36 | + #[clap(long, env, default_value = "testnet")] |
| 37 | + hedera_network: String, |
| 38 | +} |
| 39 | + |
| 40 | +#[tokio::main] |
| 41 | +async fn main() -> anyhow::Result<()> { |
| 42 | + let _ = dotenvy::dotenv(); |
| 43 | + |
| 44 | + let args = Args::parse(); |
| 45 | + |
| 46 | + /* |
| 47 | + * Step 0: Create and configure the client |
| 48 | + */ |
| 49 | + let client = Client::for_name(&args.hedera_network)?; |
| 50 | + client.set_operator(args.operator_account_id, args.operator_key); |
| 51 | + |
| 52 | + /* |
| 53 | + * Step 1: Create key pairs |
| 54 | + */ |
| 55 | + let key1 = PrivateKey::generate_ed25519(); |
| 56 | + let key2 = PrivateKey::generate_ed25519(); |
| 57 | + |
| 58 | + println!("Creating Key List... (w/ threshold, 2 of 2 keys generated above is required to modify the account)"); |
| 59 | + |
| 60 | + let threshold_key = KeyList { |
| 61 | + keys: vec![key1.public_key().into(), key2.public_key().into()], |
| 62 | + threshold: Some(2), |
| 63 | + }; |
| 64 | + |
| 65 | + println!("Created key list: {threshold_key:?}"); |
| 66 | + |
| 67 | + /* |
| 68 | + * Step 2: Create the account |
| 69 | + */ |
| 70 | + println!("Creating account with threshold key..."); |
| 71 | + let alice_id = AccountCreateTransaction::new() |
| 72 | + .key(Key::KeyList(threshold_key)) |
| 73 | + .initial_balance(Hbar::new(2)) |
| 74 | + .execute(&client) |
| 75 | + .await? |
| 76 | + .get_receipt(&client) |
| 77 | + .await? |
| 78 | + .account_id |
| 79 | + .unwrap(); |
| 80 | + |
| 81 | + println!("Created account with id: {alice_id}"); |
| 82 | + |
| 83 | + /* |
| 84 | + * Step 3: |
| 85 | + * Schedule a transfer transaction of 1 hbar from the newly created account to the operator account. |
| 86 | + * The transaction will be scheduled with expirationTime = 24 hours from now and waitForExpiry = false. |
| 87 | + */ |
| 88 | + println!("Creating new scheduled transaction with 1 day expiry..."); |
| 89 | + let mut transfer = TransferTransaction::new(); |
| 90 | + transfer |
| 91 | + .hbar_transfer(alice_id, Hbar::new(-1)) |
| 92 | + .hbar_transfer(args.operator_account_id, Hbar::new(1)); |
| 93 | + |
| 94 | + let schedule_id = transfer |
| 95 | + .schedule() |
| 96 | + .wait_for_expiry(false) |
| 97 | + .expiration_time(OffsetDateTime::now_utc() + Duration::seconds(86400)) |
| 98 | + .execute(&client) |
| 99 | + .await? |
| 100 | + .get_receipt(&client) |
| 101 | + .await? |
| 102 | + .schedule_id |
| 103 | + .unwrap(); |
| 104 | + |
| 105 | + /* |
| 106 | + * Step 4: Sign the transaction with one key and verify the transaction is not executed |
| 107 | + */ |
| 108 | + println!("Signing transaction with key 1..."); |
| 109 | + _ = ScheduleSignTransaction::new() |
| 110 | + .schedule_id(schedule_id) |
| 111 | + .freeze_with(&client)? |
| 112 | + .sign(key1.clone()) |
| 113 | + .execute(&client) |
| 114 | + .await? |
| 115 | + .get_receipt(&client) |
| 116 | + .await?; |
| 117 | + |
| 118 | + let info = ScheduleInfoQuery::new() |
| 119 | + .schedule_id(schedule_id) |
| 120 | + .execute(&client) |
| 121 | + .await?; |
| 122 | + |
| 123 | + println!( |
| 124 | + "Scheduled transaction is not executed yet. Executed at: {:?}", |
| 125 | + info.executed_at |
| 126 | + ); |
| 127 | + |
| 128 | + /* |
| 129 | + * Step 5: Sign the transaction with the second key and verify the transaction is executed |
| 130 | + */ |
| 131 | + |
| 132 | + let account_balance = AccountInfoQuery::new() |
| 133 | + .account_id(alice_id) |
| 134 | + .execute(&client) |
| 135 | + .await? |
| 136 | + .balance; |
| 137 | + |
| 138 | + println!("Alice's account balance before scheduled transaction: {account_balance}"); |
| 139 | + |
| 140 | + println!("Signing transaction with key 2..."); |
| 141 | + _ = ScheduleSignTransaction::new() |
| 142 | + .schedule_id(schedule_id) |
| 143 | + .freeze_with(&client)? |
| 144 | + .sign(key2.clone()) |
| 145 | + .execute(&client) |
| 146 | + .await? |
| 147 | + .get_receipt(&client) |
| 148 | + .await?; |
| 149 | + |
| 150 | + let account_balance = AccountInfoQuery::new() |
| 151 | + .account_id(alice_id) |
| 152 | + .execute(&client) |
| 153 | + .await? |
| 154 | + .balance; |
| 155 | + |
| 156 | + println!("Alice's account balance after scheduled transaction: {account_balance}"); |
| 157 | + |
| 158 | + let info = ScheduleInfoQuery::new() |
| 159 | + .schedule_id(schedule_id) |
| 160 | + .execute(&client) |
| 161 | + .await?; |
| 162 | + |
| 163 | + println!("Scheduled transaction executed at: {:?}", info.executed_at); |
| 164 | + |
| 165 | + /* |
| 166 | + * Step 6: |
| 167 | + * Schedule another transfer transaction of 1 Hbar from the account to the operator account |
| 168 | + * with an expirationTime of 10 seconds in the future and waitForExpiry=true. |
| 169 | + */ |
| 170 | + println!("Creating new scheduled transaction with 10 second expiry..."); |
| 171 | + let mut transfer = TransferTransaction::new(); |
| 172 | + transfer |
| 173 | + .hbar_transfer(alice_id, Hbar::new(-1)) |
| 174 | + .hbar_transfer(args.operator_account_id, Hbar::new(1)); |
| 175 | + |
| 176 | + let schedule_id = transfer |
| 177 | + .schedule() |
| 178 | + .wait_for_expiry(true) |
| 179 | + .expiration_time(OffsetDateTime::now_utc() + Duration::seconds(10)) |
| 180 | + .execute(&client) |
| 181 | + .await? |
| 182 | + .get_receipt(&client) |
| 183 | + .await? |
| 184 | + .schedule_id |
| 185 | + .unwrap(); |
| 186 | + |
| 187 | + /* |
| 188 | + * Step 7: |
| 189 | + * Sign the transaction with one key and verify the transaction is not executed |
| 190 | + */ |
| 191 | + println!("Signing scheduled transaction with key 1..."); |
| 192 | + _ = ScheduleSignTransaction::new() |
| 193 | + .schedule_id(schedule_id) |
| 194 | + .freeze_with(&client)? |
| 195 | + .sign(key1.clone()) |
| 196 | + .execute(&client) |
| 197 | + .await? |
| 198 | + .get_receipt(&client) |
| 199 | + .await?; |
| 200 | + |
| 201 | + let info = ScheduleInfoQuery::new() |
| 202 | + .schedule_id(schedule_id) |
| 203 | + .execute(&client) |
| 204 | + .await?; |
| 205 | + |
| 206 | + println!( |
| 207 | + "Scheduled transaction is not executed yet. Executed at: {:?}", |
| 208 | + info.executed_at |
| 209 | + ); |
| 210 | + |
| 211 | + /* |
| 212 | + * Step 8: |
| 213 | + * Update the account's key to be only the one key |
| 214 | + * that has already signed the scheduled transfer. |
| 215 | + */ |
| 216 | + println!("Updating account key to only key 1..."); |
| 217 | + _ = AccountUpdateTransaction::new() |
| 218 | + .account_id(alice_id) |
| 219 | + .key(key1.public_key()) |
| 220 | + .freeze_with(&client)? |
| 221 | + .sign(key1) |
| 222 | + .sign(key2) |
| 223 | + .execute(&client) |
| 224 | + .await? |
| 225 | + .get_receipt(&client) |
| 226 | + .await?; |
| 227 | + |
| 228 | + /* |
| 229 | + * Step 9: |
| 230 | + * Verify that the transfer successfully executes roughly at the time of its expiration. |
| 231 | + */ |
| 232 | + let account_balance = AccountInfoQuery::new() |
| 233 | + .account_id(alice_id) |
| 234 | + .execute(&client) |
| 235 | + .await? |
| 236 | + .balance; |
| 237 | + |
| 238 | + println!("Alice's account balance before scheduled transfer: {account_balance}"); |
| 239 | + |
| 240 | + sleep(std::time::Duration::from_millis(10_000)).await; |
| 241 | + |
| 242 | + let account_balance = AccountInfoQuery::new() |
| 243 | + .account_id(alice_id) |
| 244 | + .execute(&client) |
| 245 | + .await? |
| 246 | + .balance; |
| 247 | + |
| 248 | + println!("Alice's account balance after scheduled transfer: {account_balance}"); |
| 249 | + |
| 250 | + println!("Successfully executed scheduled transfer"); |
| 251 | + |
| 252 | + Ok(()) |
| 253 | +} |
0 commit comments