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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/*
*
* 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 hedera_proto::services;
use hedera_proto::services::schedule_service_client::ScheduleServiceClient;
use time::OffsetDateTime;
use tonic::transport::Channel;
use super::schedulable_transaction_body::SchedulableTransactionBody;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
AccountId,
BoxGrpcFuture,
Error,
Key,
Transaction,
ValidateChecksums,
};
/// Create a new schedule entity (or simply, schedule) in the network's action queue.
///
/// Upon `SUCCESS`, the receipt contains the `ScheduleId` of the created schedule. A schedule
/// entity includes a `scheduled_transaction_body` to be executed.
///
/// When the schedule has collected enough signing keys to satisfy the schedule's signing
/// requirements, the schedule can be executed.
///
pub type ScheduleCreateTransaction = Transaction<ScheduleCreateTransactionData>;
#[derive(Default, Debug, Clone)]
pub struct ScheduleCreateTransactionData {
scheduled_transaction: Option<SchedulableTransactionBody>,
schedule_memo: Option<String>,
admin_key: Option<Key>,
payer_account_id: Option<AccountId>,
expiration_time: Option<OffsetDateTime>,
wait_for_expiry: bool,
}
impl ScheduleCreateTransaction {
// note(sr): not sure what the right way to go about this is?
// pub fn get_scheduled_transaction(&self) -> Option<&SchedulableTransactionBody> {
// self.data().scheduled_transaction.as_ref()
// }
/// Sets the scheduled transaction.
///
/// # Panics
/// panics if the transaction is not schedulable, a transaction can be non-schedulable due to:
/// - being a transaction kind that's non-schedulable, IE, `EthereumTransaction`, or
/// - being a chunked transaction with multiple chunks.
pub fn scheduled_transaction<D>(&mut self, transaction: Transaction<D>) -> &mut Self
where
D: TransactionExecute,
{
let body = transaction.into_body();
// this gets infered right but `foo.into().try_into()` looks really really weird.
let data: AnyTransactionData = body.data.into();
self.data_mut().scheduled_transaction = Some(SchedulableTransactionBody {
max_transaction_fee: body.max_transaction_fee,
transaction_memo: body.transaction_memo,
data: Box::new(data.try_into().unwrap()),
});
self
}
/// Returns the timestamp for when the transaction should be evaluated for execution and then expire.
#[must_use]
pub fn get_expiration_time(&self) -> Option<OffsetDateTime> {
self.data().expiration_time
}
/// Sets the timestamp for when the transaction should be evaluated for execution and then expire.
pub fn expiration_time(&mut self, time: OffsetDateTime) -> &mut Self {
self.data_mut().expiration_time = Some(time);
self
}
/// Returns `true` if the transaction will be evaluated at `expiration_time` instead
/// of when all the required signatures are received, `false` otherwise.
#[must_use]
pub fn get_wait_for_expiry(&self) -> bool {
self.data().wait_for_expiry
}
/// Sets if the transaction will be evaluated for execution at `expiration_time` instead
/// of when all required signatures are received.
pub fn wait_for_expiry(&mut self, wait: bool) -> &mut Self {
self.data_mut().wait_for_expiry = wait;
self
}
/// Returns the id of the account to be charged the service fee for the scheduled transaction at
/// the consensus time it executes (if ever).
#[must_use]
pub fn get_payer_account_id(&self) -> Option<AccountId> {
self.data().payer_account_id
}
/// Sets the id of the account to be charged the service fee for the scheduled transaction at
/// the consensus time that it executes (if ever).
pub fn payer_account_id(&mut self, id: AccountId) -> &mut Self {
self.data_mut().payer_account_id = Some(id);
self
}
/// Returns the memo for the schedule entity.
#[must_use]
pub fn get_schedule_memo(&self) -> Option<&str> {
self.data().schedule_memo.as_deref()
}
/// Sets the memo for the schedule entity.
pub fn schedule_memo(&mut self, memo: impl Into<String>) -> &mut Self {
self.data_mut().schedule_memo = Some(memo.into());
self
}
/// Returns the Hedera key which can be used to sign a `ScheduleDelete` and remove the schedule.
#[must_use]
pub fn get_admin_key(&self) -> Option<&Key> {
self.data().admin_key.as_ref()
}
/// Sets the Hedera key which can be used to sign a `ScheduleDelete` and remove the schedule.
pub fn admin_key(&mut self, key: impl Into<Key>) -> &mut Self {
self.data_mut().admin_key = Some(key.into());
self
}
}
impl TransactionData for ScheduleCreateTransactionData {}
impl TransactionExecute for ScheduleCreateTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async { ScheduleServiceClient::new(channel).create_schedule(request).await })
}
}
impl ValidateChecksums for ScheduleCreateTransactionData {
fn validate_checksums(&self, ledger_id: &crate::ledger_id::RefLedgerId) -> Result<(), Error> {
self.payer_account_id.validate_checksums(ledger_id)
}
}
impl ToTransactionDataProtobuf for ScheduleCreateTransactionData {
// not really anything I can do about this
#[allow(clippy::too_many_lines)]
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
let body = self.scheduled_transaction.as_ref().map(|scheduled| {
let data = scheduled.data.to_schedulable_transaction_data_protobuf();
services::SchedulableTransactionBody {
data: Some(data),
memo: scheduled.transaction_memo.clone(),
// FIXME: does not use the client to default the max transaction fee
transaction_fee: scheduled
.max_transaction_fee
.unwrap_or_else(|| scheduled.data.default_max_transaction_fee())
.to_tinybars() as u64,
}
});
let payer_account_id = self.payer_account_id.to_protobuf();
let admin_key = self.admin_key.to_protobuf();
let expiration_time = self.expiration_time.map(Into::into);
services::transaction_body::Data::ScheduleCreate(services::ScheduleCreateTransactionBody {
scheduled_transaction_body: body,
memo: self.schedule_memo.clone().unwrap_or_default(),
admin_key,
payer_account_id,
expiration_time,
wait_for_expiry: self.wait_for_expiry,
})
}
}
impl From<ScheduleCreateTransactionData> for AnyTransactionData {
fn from(transaction: ScheduleCreateTransactionData) -> Self {
Self::ScheduleCreate(transaction)
}
}
impl FromProtobuf<services::ScheduleCreateTransactionBody> for ScheduleCreateTransactionData {
fn from_protobuf(pb: services::ScheduleCreateTransactionBody) -> crate::Result<Self> {
Ok(Self {
scheduled_transaction: Option::from_protobuf(pb.scheduled_transaction_body)?,
schedule_memo: Some(pb.memo),
admin_key: Option::from_protobuf(pb.admin_key)?,
payer_account_id: Option::from_protobuf(pb.payer_account_id)?,
expiration_time: pb.expiration_time.map(Into::into),
wait_for_expiry: pb.wait_for_expiry,
})
}
}
#[cfg(test)]
mod tests {
use expect_test::expect;
use hedera_proto::services;
use time::OffsetDateTime;
use super::ScheduleCreateTransactionData;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::transaction::test_helpers::{
check_body,
transaction_body,
unused_private_key,
VALID_START,
};
use crate::transaction::ToSchedulableTransactionDataProtobuf;
use crate::{
AccountId,
AnyTransaction,
Hbar,
PublicKey,
ScheduleCreateTransaction,
TransferTransaction,
};
fn scheduled_transaction() -> TransferTransaction {
let mut tx = TransferTransaction::new();
tx.hbar_transfer("0.0.555".parse().unwrap(), -Hbar::new(10))
.hbar_transfer("0.0.666".parse().unwrap(), Hbar::new(10));
tx
}
fn admin_key() -> PublicKey {
unused_private_key().public_key()
}
const PAYER_ACCOUNT_ID: AccountId = AccountId::new(0, 0, 222);
const SCHEDULE_MEMO: &str = "hi";
const EXPIRATION_TIME: OffsetDateTime = VALID_START;
fn make_transaction() -> ScheduleCreateTransaction {
let mut tx = ScheduleCreateTransaction::new_for_tests();
tx.scheduled_transaction(scheduled_transaction())
.admin_key(admin_key())
.payer_account_id(PAYER_ACCOUNT_ID)
.schedule_memo(SCHEDULE_MEMO)
.expiration_time(EXPIRATION_TIME)
.freeze()
.unwrap();
tx
}
#[test]
fn serialize() {
let tx = make_transaction();
let tx = transaction_body(tx);
let tx = check_body(tx);
expect![[r#"
ScheduleCreate(
ScheduleCreateTransactionBody {
scheduled_transaction_body: Some(
SchedulableTransactionBody {
transaction_fee: 200000000,
memo: "",
data: Some(
CryptoTransfer(
CryptoTransferTransactionBody {
transfers: Some(
TransferList {
account_amounts: [
AccountAmount {
account_id: Some(
AccountId {
shard_num: 0,
realm_num: 0,
account: Some(
AccountNum(
555,
),
),
},
),
amount: -1000000000,
is_approval: false,
},
AccountAmount {
account_id: Some(
AccountId {
shard_num: 0,
realm_num: 0,
account: Some(
AccountNum(
666,
),
),
},
),
amount: 1000000000,
is_approval: false,
},
],
},
),
token_transfers: [],
},
),
),
},
),
memo: "hi",
admin_key: Some(
Key {
key: Some(
Ed25519(
[
224,
200,
236,
39,
88,
165,
135,
159,
250,
194,
38,
161,
60,
12,
81,
107,
121,
158,
114,
227,
81,
65,
160,
221,
130,
143,
148,
211,
121,
136,
164,
183,
],
),
),
},
),
payer_account_id: Some(
AccountId {
shard_num: 0,
realm_num: 0,
account: Some(
AccountNum(
222,
),
),
},
),
expiration_time: Some(
Timestamp {
seconds: 1554158542,
nanos: 0,
},
),
wait_for_expiry: false,
},
)
"#]]
.assert_debug_eq(&tx)
}
#[test]
fn to_from_bytes() {
let tx = make_transaction();
let tx2 = AnyTransaction::from_bytes(&tx.to_bytes().unwrap()).unwrap();
let tx = transaction_body(tx);
let tx2 = transaction_body(tx2);
assert_eq!(tx, tx2);
}
#[test]
fn from_proto_body() {
let tx = services::ScheduleCreateTransactionBody {
scheduled_transaction_body: Some(services::SchedulableTransactionBody {
transaction_fee: Hbar::new(2).to_tinybars() as _,
memo: String::new(),
data: Some(
scheduled_transaction().data().to_schedulable_transaction_data_protobuf(),
),
}),
memo: SCHEDULE_MEMO.to_owned(),
admin_key: Some(admin_key().to_protobuf()),
payer_account_id: Some(PAYER_ACCOUNT_ID.to_protobuf()),
expiration_time: Some(EXPIRATION_TIME.to_protobuf()),
wait_for_expiry: false,
};
let tx = ScheduleCreateTransactionData::from_protobuf(tx).unwrap();
expect![[r#"
SchedulableTransactionBody {
data: Transfer(
TransferTransactionData {
transfers: [
Transfer {
account_id: "0.0.555",
amount: -1000000000,
is_approval: false,
},
Transfer {
account_id: "0.0.666",
amount: 1000000000,
is_approval: false,
},
],
token_transfers: [],
},
),
max_transaction_fee: Some(
"2 ℏ",
),
transaction_memo: "",
}
"#]]
.assert_debug_eq(&tx.scheduled_transaction.unwrap());
assert_eq!(tx.schedule_memo.as_deref(), Some(SCHEDULE_MEMO));
assert_eq!(tx.admin_key, Some(admin_key().into()));
assert_eq!(tx.payer_account_id, Some(PAYER_ACCOUNT_ID));
assert_eq!(tx.expiration_time, Some(EXPIRATION_TIME));
assert_eq!(tx.wait_for_expiry, false);
}
mod get_set {
use super::*;
#[test]
fn admin_key() {
let mut tx = ScheduleCreateTransaction::new();
tx.admin_key(super::admin_key());
assert_eq!(tx.get_admin_key(), Some(&super::admin_key().into()));
}
#[test]
#[should_panic]
fn admin_key_frozen_panics() {
make_transaction().admin_key(super::admin_key());
}
#[test]
fn payer_account_id() {
let mut tx = ScheduleCreateTransaction::new();
tx.payer_account_id(PAYER_ACCOUNT_ID);
assert_eq!(tx.get_payer_account_id(), Some(PAYER_ACCOUNT_ID));
}
#[test]
#[should_panic]
fn payer_account_id_frozen_panics() {
make_transaction().payer_account_id(PAYER_ACCOUNT_ID);
}
#[test]
fn expiration_time() {
let mut tx = ScheduleCreateTransaction::new();
tx.expiration_time(EXPIRATION_TIME);
assert_eq!(tx.get_expiration_time(), Some(EXPIRATION_TIME));
}
#[test]
#[should_panic]
fn expiration_time_frozen_panics() {
make_transaction().expiration_time(EXPIRATION_TIME);
}
#[test]
fn wait_for_expiry() {
let mut tx = ScheduleCreateTransaction::new();
tx.wait_for_expiry(true);
assert_eq!(tx.get_wait_for_expiry(), true);
}
#[test]
#[should_panic]
fn wait_for_expiry_frozen_panics() {
make_transaction().wait_for_expiry(true);
}
}
}