use async_trait::async_trait;
use hedera_proto::services;
use hedera_proto::services::smart_contract_service_client::SmartContractServiceClient;
use tonic::transport::Channel;
use crate::entity_id::AutoValidateChecksum;
use crate::transaction::{
AnyTransactionData,
ToTransactionDataProtobuf,
TransactionExecute,
};
use crate::{
AccountId,
ContractId,
Error,
Hbar,
LedgerId,
ToProtobuf,
Transaction,
};
pub type ContractExecuteTransaction = Transaction<ContractExecuteTransactionData>;
#[cfg_attr(feature = "ffi", serde_with::skip_serializing_none)]
#[derive(Default, Debug, Clone)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase"))]
pub struct ContractExecuteTransactionData {
contract_id: Option<ContractId>,
gas: u64,
payable_amount: Hbar,
function_parameters: Vec<u8>,
}
impl ContractExecuteTransaction {
pub fn contract_id(&mut self, contract_id: ContractId) -> &mut Self {
self.body.data.contract_id = Some(contract_id);
self
}
pub fn gas(&mut self, gas: u64) -> &mut Self {
self.body.data.gas = gas;
self
}
pub fn payable_amount(&mut self, amount: Hbar) -> &mut Self {
self.body.data.payable_amount = amount;
self
}
pub fn function_parameters(&mut self, data: Vec<u8>) -> &mut Self {
self.body.data.function_parameters = data;
self
}
}
#[async_trait]
impl TransactionExecute for ContractExecuteTransactionData {
fn validate_checksums_for_ledger_id(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.contract_id.validate_checksum_for_ledger_id(ledger_id)?;
Ok(())
}
async fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> Result<tonic::Response<services::TransactionResponse>, tonic::Status> {
SmartContractServiceClient::new(channel).contract_call_method(request).await
}
}
impl ToTransactionDataProtobuf for ContractExecuteTransactionData {
fn to_transaction_data_protobuf(
&self,
_node_account_id: AccountId,
_transaction_id: &crate::TransactionId,
) -> services::transaction_body::Data {
let contract_id = self.contract_id.to_protobuf();
services::transaction_body::Data::ContractCall(
#[allow(deprecated)]
services::ContractCallTransactionBody {
gas: self.gas as i64,
amount: self.payable_amount.to_tinybars(),
contract_id,
function_parameters: self.function_parameters.clone(),
},
)
}
}
impl From<ContractExecuteTransactionData> for AnyTransactionData {
fn from(transaction: ContractExecuteTransactionData) -> Self {
Self::ContractExecute(transaction)
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ffi")]
mod ffi {
use assert_matches::assert_matches;
use crate::transaction::{
AnyTransaction,
AnyTransactionData,
};
use crate::{
ContractExecuteTransaction,
ContractId,
Hbar,
};
const CONTRACT_EXECUTE_TRANSACTION_JSON: &str = r#"{
"$type": "contractExecute",
"contractId": "0.0.1001",
"gas": 1000,
"payableAmount": 10,
"functionParameters": [
72,
101,
108,
108,
111,
44,
32,
119,
111,
114,
108,
100,
33
]
}"#;
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = ContractExecuteTransaction::new();
transaction
.contract_id(ContractId::from(1001))
.gas(1000)
.payable_amount(Hbar::from_tinybars(10))
.function_parameters("Hello, world!".into());
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, CONTRACT_EXECUTE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction =
serde_json::from_str(CONTRACT_EXECUTE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.body.data, AnyTransactionData::ContractExecute(transaction) => transaction);
assert_eq!(data.contract_id.unwrap(), ContractId::from(1001));
assert_eq!(data.gas, 1000);
assert_eq!(data.payable_amount.to_tinybars(), 10);
let bytes: Vec<u8> = "Hello, world!".into();
assert_eq!(data.function_parameters, bytes);
Ok(())
}
}
}