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::query::{
AnyQueryData,
QueryExecute,
ToQueryProtobuf,
};
use crate::{
AccountId,
ContractFunctionResult,
ContractId,
Error,
LedgerId,
Query,
ToProtobuf,
};
pub type ContractCallQuery = Query<ContractCallQueryData>;
#[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 ContractCallQueryData {
pub contract_id: Option<ContractId>,
pub gas: u64,
#[cfg_attr(feature = "ffi", serde(with = "serde_with::As::<serde_with::base64::Base64>"))]
pub function_parameters: Vec<u8>,
pub sender_account_id: Option<AccountId>,
}
impl ContractCallQuery {
pub fn contract_id(&mut self, contract_id: ContractId) -> &mut Self {
self.data.contract_id = Some(contract_id);
self
}
pub fn gas(&mut self, gas: u64) -> &mut Self {
self.data.gas = gas;
self
}
pub fn function_parameters(&mut self, data: Vec<u8>) -> &mut Self {
self.data.function_parameters = data;
self
}
pub fn sender_account_id(&mut self, sender_account_id: AccountId) -> &mut Self {
self.data.sender_account_id = Some(sender_account_id);
self
}
}
impl From<ContractCallQueryData> for AnyQueryData {
#[inline]
fn from(data: ContractCallQueryData) -> Self {
Self::ContractCall(data)
}
}
impl ToQueryProtobuf for ContractCallQueryData {
fn to_query_protobuf(&self, header: services::QueryHeader) -> services::Query {
let contract_id = self.contract_id.to_protobuf();
let sender_id = self.sender_account_id.to_protobuf();
services::Query {
query: Some(services::query::Query::ContractCallLocal(
#[allow(deprecated)]
services::ContractCallLocalQuery {
contract_id,
gas: self.gas as i64,
function_parameters: self.function_parameters.clone(),
max_result_size: 0,
header: Some(header),
sender_id,
},
)),
}
}
}
#[async_trait]
impl QueryExecute for ContractCallQueryData {
type Response = ContractFunctionResult;
fn validate_checksums_for_ledger_id(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.contract_id.validate_checksum_for_ledger_id(ledger_id)?;
self.sender_account_id.validate_checksum_for_ledger_id(ledger_id)
}
async fn execute(
&self,
channel: Channel,
request: services::Query,
) -> Result<tonic::Response<services::Response>, tonic::Status> {
SmartContractServiceClient::new(channel).contract_call_local_method(request).await
}
}