From 44b1c480940db40a37cac9c0b9df462607728c3b Mon Sep 17 00:00:00 2001 From: Ricky Saechao <76449893+RickyLB@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:29:25 -0800 Subject: [PATCH] Add `exchange_rates` to TransactionReceipt --- src/exchange_rates.rs | 24 ++++++++ .../transaction_record/serialize.txt | 27 ++++++++- .../transaction_record/serialize2.txt | 27 ++++++++- src/transaction_receipt.rs | 59 +++++++++++++++++-- 4 files changed, 130 insertions(+), 7 deletions(-) diff --git a/src/exchange_rates.rs b/src/exchange_rates.rs index 8a6c52c36..0b6d6d43d 100644 --- a/src/exchange_rates.rs +++ b/src/exchange_rates.rs @@ -22,6 +22,7 @@ use hedera_proto::services; use time::OffsetDateTime; use crate::protobuf::FromProtobuf; +use crate::ToProtobuf; /// The current and next exchange rates between [`Hbar`](crate::HbarUnit::Hbar) and USD-cents. #[derive(Debug, Clone)] @@ -52,6 +53,17 @@ impl FromProtobuf for ExchangeRates { } } +impl ToProtobuf for ExchangeRates { + type Protobuf = services::ExchangeRateSet; + + fn to_protobuf(&self) -> Self::Protobuf { + services::ExchangeRateSet { + current_rate: Some(self.current_rate.to_protobuf()), + next_rate: Some(self.next_rate.to_protobuf()), + } + } +} + /// Denotes a conversion between Hbars and cents (USD). #[derive(Debug, Clone)] pub struct ExchangeRate { @@ -82,6 +94,18 @@ impl FromProtobuf for ExchangeRate { } } +impl ToProtobuf for ExchangeRate { + type Protobuf = services::ExchangeRate; + + fn to_protobuf(&self) -> Self::Protobuf { + services::ExchangeRate { + hbar_equiv: self.hbars as i32, + cent_equiv: self.cents as i32, + expiration_time: Some(self.expiration_time.into()), + } + } +} + #[cfg(test)] mod tests { use expect_test::expect; diff --git a/src/snapshots/transaction_record/serialize.txt b/src/snapshots/transaction_record/serialize.txt index 9b0d7c867..1dbf2a579 100644 --- a/src/snapshots/transaction_record/serialize.txt +++ b/src/snapshots/transaction_record/serialize.txt @@ -31,7 +31,32 @@ TransactionRecord { ), }, ), - exchange_rate: None, + exchange_rate: Some( + ExchangeRateSet { + current_rate: Some( + ExchangeRate { + hbar_equiv: 100, + cent_equiv: 100, + expiration_time: Some( + TimestampSeconds { + seconds: 1554158542, + }, + ), + }, + ), + next_rate: Some( + ExchangeRate { + hbar_equiv: 200, + cent_equiv: 200, + expiration_time: Some( + TimestampSeconds { + seconds: 1554158542, + }, + ), + }, + ), + }, + ), topic_id: Some( TopicId { shard_num: 9, diff --git a/src/snapshots/transaction_record/serialize2.txt b/src/snapshots/transaction_record/serialize2.txt index 62ff2567f..6c47c797c 100644 --- a/src/snapshots/transaction_record/serialize2.txt +++ b/src/snapshots/transaction_record/serialize2.txt @@ -31,7 +31,32 @@ TransactionRecord { ), }, ), - exchange_rate: None, + exchange_rate: Some( + ExchangeRateSet { + current_rate: Some( + ExchangeRate { + hbar_equiv: 100, + cent_equiv: 100, + expiration_time: Some( + TimestampSeconds { + seconds: 1554158542, + }, + ), + }, + ), + next_rate: Some( + ExchangeRate { + hbar_equiv: 200, + cent_equiv: 200, + expiration_time: Some( + TimestampSeconds { + seconds: 1554158542, + }, + ), + }, + ), + }, + ), topic_id: Some( TopicId { shard_num: 9, diff --git a/src/transaction_receipt.rs b/src/transaction_receipt.rs index 184fd16bd..a03597cc6 100644 --- a/src/transaction_receipt.rs +++ b/src/transaction_receipt.rs @@ -29,6 +29,7 @@ use crate::{ AccountId, ContractId, Error, + ExchangeRates, FileId, FromProtobuf, ScheduleId, @@ -60,8 +61,9 @@ pub struct TransactionReceipt { /// In the receipt for a `ContractCreateTransaction`, the id of the newly created contract. pub contract_id: Option, - // The exchange rates in effect when the transaction reached consensus. - // TODO: pub exchange_rate: ExchangeRate, + /// The exchange rates in effect when the transaction reached consensus. + pub exchange_rates: Option, + /// In the receipt for a `TopicCreateTransaction`, the id of the newly created topic. pub topic_id: Option, @@ -156,6 +158,7 @@ impl TransactionReceipt { let topic_id = Option::from_protobuf(receipt.topic_id)?; let token_id = Option::from_protobuf(receipt.token_id)?; let schedule_id = Option::from_protobuf(receipt.schedule_id)?; + let exchange_rates = Option::from_protobuf(receipt.exchange_rate)?; let scheduled_transaction_id = Option::from_protobuf(receipt.scheduled_transaction_id)?; @@ -163,6 +166,7 @@ impl TransactionReceipt { status, total_supply: receipt.new_total_supply, serials: receipt.serial_numbers, + exchange_rates, topic_running_hash_version: receipt.topic_running_hash_version, topic_sequence_number: receipt.topic_sequence_number, topic_running_hash: receipt @@ -227,7 +231,7 @@ impl ToProtobuf for TransactionReceipt { account_id: self.account_id.to_protobuf(), file_id: self.file_id.to_protobuf(), contract_id: self.contract_id.to_protobuf(), - exchange_rate: None, + exchange_rate: self.exchange_rates.to_protobuf(), topic_id: self.topic_id.to_protobuf(), topic_sequence_number: self.topic_sequence_number, topic_running_hash: self.topic_running_hash.clone().unwrap_or_default(), @@ -245,12 +249,18 @@ impl ToProtobuf for TransactionReceipt { #[cfg(test)] mod tests { use expect_test::expect; + use time::OffsetDateTime; use crate::protobuf::ToProtobuf; - use crate::transaction::test_helpers::TEST_TX_ID; + use crate::transaction::test_helpers::{ + TEST_TX_ID, + VALID_START, + }; use crate::{ AccountId, ContractId, + ExchangeRate, + ExchangeRates, FileId, ScheduleId, Status, @@ -259,6 +269,8 @@ mod tests { TransactionReceipt, }; + const EXPIRATION_TIME: OffsetDateTime = VALID_START; + // needed in `transaction_record`. pub(crate) fn make_receipt() -> TransactionReceipt { TransactionReceipt { @@ -266,6 +278,18 @@ mod tests { status: Status::ScheduleAlreadyDeleted, account_id: Some(AccountId::new(1, 2, 3)), file_id: Some(FileId::new(4, 5, 6)), + exchange_rates: Some(ExchangeRates { + current_rate: ExchangeRate { + hbars: 100, + cents: 100, + expiration_time: EXPIRATION_TIME, + }, + next_rate: ExchangeRate { + hbars: 200, + cents: 200, + expiration_time: EXPIRATION_TIME, + }, + }), contract_id: Some(ContractId::new(3, 2, 1)), topic_id: Some(TopicId::new(9, 8, 7)), topic_sequence_number: 3, @@ -316,7 +340,32 @@ mod tests { ), }, ), - exchange_rate: None, + exchange_rate: Some( + ExchangeRateSet { + current_rate: Some( + ExchangeRate { + hbar_equiv: 100, + cent_equiv: 100, + expiration_time: Some( + TimestampSeconds { + seconds: 1554158542, + }, + ), + }, + ), + next_rate: Some( + ExchangeRate { + hbar_equiv: 200, + cent_equiv: 200, + expiration_time: Some( + TimestampSeconds { + seconds: 1554158542, + }, + ), + }, + ), + }, + ), topic_id: Some( TopicId { shard_num: 9,