Skip to content

Commit

Permalink
Add exchange_rates to TransactionReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyLB authored Dec 6, 2024
1 parent ab816ca commit 44b1c48
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/exchange_rates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -52,6 +53,17 @@ impl FromProtobuf<services::ExchangeRateSet> 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 {
Expand Down Expand Up @@ -82,6 +94,18 @@ impl FromProtobuf<services::ExchangeRate> 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;
Expand Down
27 changes: 26 additions & 1 deletion src/snapshots/transaction_record/serialize.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion src/snapshots/transaction_record/serialize2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
59 changes: 54 additions & 5 deletions src/transaction_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::{
AccountId,
ContractId,
Error,
ExchangeRates,
FileId,
FromProtobuf,
ScheduleId,
Expand Down Expand Up @@ -60,8 +61,9 @@ pub struct TransactionReceipt {
/// In the receipt for a `ContractCreateTransaction`, the id of the newly created contract.
pub contract_id: Option<ContractId>,

// 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<ExchangeRates>,

/// In the receipt for a `TopicCreateTransaction`, the id of the newly created topic.
pub topic_id: Option<TopicId>,

Expand Down Expand Up @@ -156,13 +158,15 @@ 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)?;

Ok(Self {
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
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -259,13 +269,27 @@ mod tests {
TransactionReceipt,
};

const EXPIRATION_TIME: OffsetDateTime = VALID_START;

// needed in `transaction_record`.
pub(crate) fn make_receipt() -> TransactionReceipt {
TransactionReceipt {
transaction_id: None,
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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 44b1c48

Please sign in to comment.