Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/account/account_create_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub struct AccountCreateTransactionData {

/// If true, the account declines receiving a staking reward. The default value is false.
decline_staking_reward: bool,

/// Hooks to add immediately after creating this account.
hooks: Vec<LambdaEvmHook>,
}

impl Default for AccountCreateTransactionData {
Expand All @@ -91,6 +94,7 @@ impl Default for AccountCreateTransactionData {
alias: None,
staked_id: None,
decline_staking_reward: false,
hooks: Vec::new(),
}
}
}
Expand Down Expand Up @@ -293,6 +297,20 @@ impl AccountCreateTransaction {
self.data_mut().decline_staking_reward = decline;
self
}

pub fn add_hook(&mut self, hook: LambdaEvmHook) -> &mut Self {
self.data_mut().hooks.push(hook);
self
}

pub fn set_hooks(&mut self, hooks: Vec<LambdaEvmHook>) -> &mut Self {
self.data_mut().hooks = hooks;
self
}

pub fn get_hooks(&self) -> &[LambdaEvmHook] {
&self.data().hooks
}
}

impl TransactionData for AccountCreateTransactionData {}
Expand Down Expand Up @@ -353,6 +371,11 @@ impl FromProtobuf<services::CryptoCreateTransactionBody> for AccountCreateTransa
alias,
staked_id: Option::from_protobuf(pb.staked_id)?,
decline_staking_reward: pb.decline_reward,
hooks: pb
.hook_creation_details
.into_iter()
.map(LambdaEvmHook::from_protobuf)
.collect::<Result<Vec<_>, _>>()?,
})
}
}
Expand Down Expand Up @@ -391,6 +414,7 @@ impl ToProtobuf for AccountCreateTransactionData {
alias: self.alias.map_or(vec![], |it| it.to_bytes().to_vec()),
decline_reward: self.decline_staking_reward,
staked_id,
hooks: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/account/account_update_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use time::{
};
use tonic::transport::Channel;

use crate::hooks::LambdaEvmHook;
use crate::ledger_id::RefLedgerId;
use crate::protobuf::{
FromProtobuf,
Expand Down Expand Up @@ -89,6 +90,10 @@ pub struct AccountUpdateTransactionData {

/// If true, the account declines receiving a staking reward. The default value is false.
decline_staking_reward: Option<bool>,

/// Hooks to add immediately after updating this account.
hooks: Vec<LambdaEvmHook>,
hook_ids_to_delete: Vec<i64>,
}

impl AccountUpdateTransaction {
Expand Down Expand Up @@ -271,6 +276,42 @@ impl AccountUpdateTransaction {
self.data_mut().decline_staking_reward = Some(decline);
self
}

/// Returns the hooks to be created.
#[must_use]
pub fn get_hooks_to_create(&self) -> &[LambdaEvmHook] {
&self.data().hooks
}

/// Adds a hook to be created.
pub fn add_hook(&mut self, hook: LambdaEvmHook) -> &mut Self {
self.data_mut().hooks.push(hook);
self
}

/// Sets the hooks to be created.
pub fn set_hooks(&mut self, hooks: Vec<LambdaEvmHook>) -> &mut Self {
self.data_mut().hooks = hooks;
self
}

/// Returns the hook IDs to be deleted.
#[must_use]
pub fn get_hooks_to_delete(&self) -> &[i64] {
&self.data().hook_ids_to_delete
}

/// Adds a hook ID to be deleted.
pub fn delete_hook(&mut self, hook_id: i64) -> &mut Self {
self.data_mut().hook_ids_to_delete.push(hook_id);
self
}

/// Sets the hook IDs to be deleted.
pub fn delete_hooks(&mut self, hook_ids: Vec<i64>) -> &mut Self {
self.data_mut().hook_ids_to_delete = hook_ids;
self
}
}

impl TransactionData for AccountUpdateTransactionData {}
Expand Down Expand Up @@ -339,6 +380,12 @@ impl FromProtobuf<services::CryptoUpdateTransactionBody> for AccountUpdateTransa
max_automatic_token_associations: pb.max_automatic_token_associations,
staked_id: Option::from_protobuf(pb.staked_id)?,
decline_staking_reward: pb.decline_reward,
hooks: pb
.hook_creation_details
.into_iter()
.map(LambdaEvmHook::from_protobuf)
.collect::<Result<Vec<_>, _>>()?,
hook_ids_to_delete: pb.hook_ids_to_delete,
})
}
}
Expand Down Expand Up @@ -382,6 +429,8 @@ impl ToProtobuf for AccountUpdateTransactionData {
receive_record_threshold_field: None,
receiver_sig_required_field: receiver_signature_required,
staked_id,
hooks: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
hook_ids_to_delete: self.hook_ids_to_delete,
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/contract/contract_create_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub struct ContractCreateTransactionData {
staked_id: Option<StakedId>,

decline_staking_reward: bool,

/// Hooks to add immediately after creating this contract.
hooks: Vec<LambdaEvmHook>,
}

impl Default for ContractCreateTransactionData {
Expand All @@ -74,6 +77,7 @@ impl Default for ContractCreateTransactionData {
auto_renew_account_id: None,
staked_id: None,
decline_staking_reward: false,
hooks: Vec::new(),
}
}
}
Expand Down Expand Up @@ -313,6 +317,11 @@ impl FromProtobuf<services::ContractCreateTransactionBody> for ContractCreateTra
auto_renew_account_id: Option::from_protobuf(pb.auto_renew_account_id)?,
staked_id: Option::from_protobuf(pb.staked_id)?,
decline_staking_reward: pb.decline_reward,
hooks: pb
.hook_creation_details
.into_iter()
.map(LambdaEvmHook::from_protobuf)
.collect::<Result<Vec<_>, _>>()?,
})
}
}
Expand Down Expand Up @@ -372,6 +381,7 @@ impl ToProtobuf for ContractCreateTransactionData {
decline_reward: self.decline_staking_reward,
initcode_source,
staked_id,
hooks: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/contract/contract_update_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use time::{
};
use tonic::transport::Channel;

use crate::hooks::LambdaEvmHook;
use crate::ledger_id::RefLedgerId;
use crate::protobuf::FromProtobuf;
use crate::staked_id::StakedId;
Expand Down Expand Up @@ -55,6 +56,10 @@ pub struct ContractUpdateTransactionData {
staked_id: Option<StakedId>,

decline_staking_reward: Option<bool>,

/// Hooks to add immediately after updating this contract.
hooks: Vec<LambdaEvmHook>,
hook_ids_to_delete: Vec<i64>,
}

impl ContractUpdateTransaction {
Expand Down Expand Up @@ -195,6 +200,42 @@ impl ContractUpdateTransaction {
self.data_mut().decline_staking_reward = Some(decline);
self
}

/// Returns the hooks to be created.
#[must_use]
pub fn get_hooks_to_create(&self) -> &[LambdaEvmHook] {
&self.data().hooks
}

/// Adds a hook to be created.
pub fn add_hook(&mut self, hook: LambdaEvmHook) -> &mut Self {
self.data_mut().hooks.push(hook);
self
}

/// Sets the hooks to be created.
pub fn set_hooks(&mut self, hooks: Vec<LambdaEvmHook>) -> &mut Self {
self.data_mut().hooks = hooks;
self
}

/// Returns the hook IDs to be deleted.
#[must_use]
pub fn get_hooks_to_delete(&self) -> &[i64] {
&self.data().hook_ids_to_delete
}

/// Adds a hook ID to be deleted.
pub fn delete_hook(&mut self, hook_id: i64) -> &mut Self {
self.data_mut().hook_ids_to_delete.push(hook_id);
self
}

/// Sets the hook IDs to be deleted.
pub fn delete_hooks(&mut self, hook_ids: Vec<i64>) -> &mut Self {
self.data_mut().hook_ids_to_delete = hook_ids;
self
}
}

impl TransactionData for ContractUpdateTransactionData {}
Expand Down Expand Up @@ -255,6 +296,12 @@ impl FromProtobuf<services::ContractUpdateTransactionBody> for ContractUpdateTra
proxy_account_id: Option::from_protobuf(pb.proxy_account_id)?,
staked_id: Option::from_protobuf(pb.staked_id)?,
decline_staking_reward: pb.decline_reward,
hooks: pb
.hook_creation_details
.into_iter()
.map(LambdaEvmHook::from_protobuf)
.collect::<Result<Vec<_>, _>>()?,
hook_ids_to_delete: pb.hook_ids_to_delete,
})
}
}
Expand Down Expand Up @@ -301,6 +348,8 @@ impl ToProtobuf for ContractUpdateTransactionData {
staked_id,
file_id: None,
memo_field,
hook_creation_details: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
hook_ids_to_delete: self.hook_ids_to_delete.clone(),
}
}
}
Expand Down
71 changes: 71 additions & 0 deletions src/hooks/evm_hook_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::protobuf::services;
use crate::{
FromProtobuf,
ToProtobuf,
};

/// An EVM hook call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EvmHookCall {
/// The call data for the EVM hook.
pub call_data: Option<Vec<u8>>,
}

impl EvmHookCall {
/// Create a new `EvmHookCall`.
pub fn new(call_data: Option<Vec<u8>>) -> Self {
Self { call_data }
}

pub fn set_call_data(&mut self, call_data: Vec<u8>) -> &mut Self {
self.call_data = Some(call_data);
self
}
}

impl ToProtobuf for EvmHookCall {
type Protobuf = services::EvmHookCall;

fn to_protobuf(&self) -> Self::Protobuf {
services::EvmHookCall { call_data: self.call_data.clone().unwrap_or_default() }
}
}

impl FromProtobuf<services::EvmHookCall> for EvmHookCall {
fn from_protobuf(pb: services::EvmHookCall) -> crate::Result<Self> {
Ok(Self { call_data: if pb.call_data.is_empty() { None } else { Some(pb.call_data) } })
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_evm_hook_call_creation() {
let call_data = vec![1, 2, 3, 4, 5];
let hook_call = EvmHookCall::with_call_data(call_data.clone());

assert_eq!(hook_call.call_data, Some(call_data));
}

#[test]
fn test_evm_hook_call_setters() {
let mut hook_call = EvmHookCall::new(None);
let call_data = vec![6, 7, 8, 9, 10];

hook_call.set_call_data(call_data.clone());
assert_eq!(hook_call.call_data, Some(call_data));
}

#[test]
fn test_evm_hook_call_protobuf_roundtrip() {
let call_data = vec![11, 12, 13, 14, 15];
let original = EvmHookCall::with_call_data(call_data);

let protobuf = original.to_protobuf();
let reconstructed = EvmHookCall::from_protobuf(protobuf).unwrap();

assert_eq!(original, reconstructed);
}
}
Loading
Loading