use std::mem;
use async_trait::async_trait;
use multiversx_sc::codec::TopEncodeMulti;
use multiversx_sc_scenario::scenario_model::{ScCallStep, ScDeployStep, TypedScCall, TypedScDeploy};
use novax_data::Address;
use crate::base::deploy::DeployExecutor;
use crate::base::transaction::TransactionExecutor;
use crate::error::executor::ExecutorError;
use crate::utils::transaction::data::{SendableTransaction, SendableTransactionConvertible};
pub type DummyTransactionExecutor = DummyExecutor<ScCallStep>;
pub type DummyDeployExecutor = DummyExecutor<ScDeployStep>;
pub struct DummyExecutor<Tx: SendableTransactionConvertible> {
pub tx: Tx,
pub caller: Option<Address>
}
impl<Tx: SendableTransactionConvertible> DummyExecutor<Tx> {
pub fn get_transaction_details(&self) -> SendableTransaction {
self.tx.to_sendable_transaction()
}
}
impl<Tx: SendableTransactionConvertible + Default> DummyExecutor<Tx> {
pub fn new(caller: &Option<Address>) -> DummyExecutor<Tx> {
DummyExecutor {
tx: Tx::default(),
caller: caller.clone()
}
}
}
#[async_trait]
impl TransactionExecutor for DummyExecutor<ScCallStep> {
async fn sc_call<OriginalResult: Send>(&mut self, sc_call_step: &mut TypedScCall<OriginalResult>) -> Result<(), ExecutorError> {
let mut owned_sc_call_step = mem::replace(sc_call_step, ScCallStep::new().into());
if let Some(caller) = &self.caller {
owned_sc_call_step = owned_sc_call_step.from(&multiversx_sc::types::Address::from(caller.to_bytes()));
}
self.tx = owned_sc_call_step.sc_call_step;
Ok(())
}
async fn should_skip_deserialization(&self) -> bool {
true
}
}
#[async_trait]
impl DeployExecutor for DummyExecutor<ScDeployStep> {
async fn sc_deploy<OriginalResult>(&mut self, sc_deploy_step: &mut TypedScDeploy<OriginalResult>) -> Result<(), ExecutorError>
where
OriginalResult: TopEncodeMulti + Send + Sync,
{
let mut owned_sc_deploy_step = mem::replace(sc_deploy_step, ScDeployStep::new().into());
if let Some(caller) = &self.caller {
owned_sc_deploy_step = owned_sc_deploy_step.from(&multiversx_sc::types::Address::from(caller.to_bytes()));
}
self.tx = owned_sc_deploy_step.sc_deploy_step;
Ok(())
}
async fn should_skip_deserialization(&self) -> bool {
true
}
}