Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactor unified syntax refactor #1532

Merged
merged 3 commits into from
Apr 3, 2024
Merged
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
5 changes: 5 additions & 0 deletions framework/snippets/src/interactor_scenario.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod interactor_sc_call;
mod interactor_sc_deploy;
mod interactor_sc_extra;
mod interactor_transfer;
mod interactor_vm_query;
17 changes: 17 additions & 0 deletions framework/snippets/src/interactor_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![allow(unused_imports)] // TEMP

mod interactor_exec_call;
mod interactor_exec_deploy;
mod interactor_exec_env;
mod interactor_exec_step;
mod interactor_exec_transf;
mod interactor_prepare_async;
mod interactor_query_call;
mod interactor_query_env;
mod interactor_query_step;

pub use interactor_exec_env::InteractorExecEnv;
pub use interactor_exec_step::InteractorExecStep;
pub use interactor_prepare_async::InteractorPrepareAsync;
pub use interactor_query_env::InteractorQueryEnv;
pub use interactor_query_step::InteractorQueryStep;
76 changes: 76 additions & 0 deletions framework/snippets/src/interactor_tx/interactor_exec_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use multiversx_sc_scenario::{
api::StaticApi,
multiversx_sc::{
tuple_util::NestedTupleFlatten,
types::{
FunctionCall, RHListExec, Tx, TxBaseWithEnv, TxFromSpecified, TxGas, TxPayment,
TxToSpecified,
},
},
scenario::tx_to_step::TxToStep,
scenario_model::{ScCallStep, TxResponse},
ScenarioTxEnvData,
};

use crate::Interactor;

use super::{InteractorExecEnv, InteractorExecStep, InteractorPrepareAsync};

impl<'w, From, To, Payment, Gas, RH> InteractorPrepareAsync
for Tx<InteractorExecEnv<'w>, From, To, Payment, Gas, FunctionCall<StaticApi>, RH>
where
From: TxFromSpecified<InteractorExecEnv<'w>>,
To: TxToSpecified<InteractorExecEnv<'w>>,
Payment: TxPayment<InteractorExecEnv<'w>>,
Gas: TxGas<InteractorExecEnv<'w>>,
RH: RHListExec<TxResponse, InteractorExecEnv<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
type Exec = InteractorExecStep<'w, ScCallStep, RH>;

fn prepare_async(self) -> Self::Exec {
InteractorExecStep {
step_wrapper: self.tx_to_step(),
}
}
}

impl<'w, RH> InteractorExecStep<'w, ScCallStep, RH>
where
RH: RHListExec<TxResponse, InteractorExecEnv<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
pub async fn run(mut self) -> <RH::ListReturns as NestedTupleFlatten>::Unpacked {
self.step_wrapper
.env
.world
.sc_call(&mut self.step_wrapper.step)
.await;
self.step_wrapper.process_result()
}
}

impl Interactor {
pub async fn chain_call<From, To, Payment, Gas, RH, F>(&mut self, f: F) -> &mut Self
where
From: TxFromSpecified<ScenarioTxEnvData>,
To: TxToSpecified<ScenarioTxEnvData>,
Payment: TxPayment<ScenarioTxEnvData>,
Gas: TxGas<ScenarioTxEnvData>,
RH: RHListExec<TxResponse, ScenarioTxEnvData, ListReturns = ()>,
F: FnOnce(
TxBaseWithEnv<ScenarioTxEnvData>,
)
-> Tx<ScenarioTxEnvData, From, To, Payment, Gas, FunctionCall<StaticApi>, RH>,
{
let env = self.new_env_data();
let tx_base = TxBaseWithEnv::new_with_env(env);
let tx = f(tx_base);

let mut step_wrapper = tx.tx_to_step();
self.sc_call(&mut step_wrapper.step).await;
step_wrapper.process_result();

self
}
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,50 @@
use std::path::PathBuf;

use multiversx_sc_scenario::{
api::StaticApi,
multiversx_sc::{
tuple_util::NestedTupleFlatten,
types::{
AnnotatedValue, Code, DeployCall, FunctionCall, ManagedAddress, ManagedBuffer,
RHListExec, Tx, TxBaseWithEnv, TxCodeSource, TxCodeSourceSpecified, TxCodeValue, TxEnv,
TxFromSpecified, TxGas, TxPayment, TxToSpecified,
Code, DeployCall, RHListExec, Tx, TxBaseWithEnv, TxCodeValue, TxFromSpecified, TxGas,
TxPayment,
},
},
scenario::tx_to_step::{StepWrapper, TxToStep},
scenario_model::{AddressValue, BytesValue, ScCallStep, ScDeployStep, TxResponse},
ScenarioEnvExec, ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun, ScenarioWorld,
scenario::tx_to_step::TxToStep,
scenario_model::{ScDeployStep, TxResponse},
ScenarioTxEnvData,
};

use crate::{Interactor, InteractorPrepareAsync};

use super::InteractorEnvExec;
use crate::Interactor;

pub struct InteractorDeployStep<'w, RH>
where
RH: RHListExec<TxResponse, InteractorEnvExec<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
step_wrapper: StepWrapper<InteractorEnvExec<'w>, ScDeployStep, RH>,
}
use super::{InteractorExecEnv, InteractorExecStep, InteractorPrepareAsync};

impl<'w, From, Payment, Gas, CodeValue, RH> InteractorPrepareAsync
for Tx<
InteractorEnvExec<'w>,
InteractorExecEnv<'w>,
From,
(),
Payment,
Gas,
DeployCall<InteractorEnvExec<'w>, Code<CodeValue>>,
DeployCall<InteractorExecEnv<'w>, Code<CodeValue>>,
RH,
>
where
From: TxFromSpecified<InteractorEnvExec<'w>>,
Payment: TxPayment<InteractorEnvExec<'w>>,
Gas: TxGas<InteractorEnvExec<'w>>,
CodeValue: TxCodeValue<InteractorEnvExec<'w>>,
RH: RHListExec<TxResponse, InteractorEnvExec<'w>>,
From: TxFromSpecified<InteractorExecEnv<'w>>,
Payment: TxPayment<InteractorExecEnv<'w>>,
Gas: TxGas<InteractorExecEnv<'w>>,
CodeValue: TxCodeValue<InteractorExecEnv<'w>>,
RH: RHListExec<TxResponse, InteractorExecEnv<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
type Exec = InteractorDeployStep<'w, RH>;
type Exec = InteractorExecStep<'w, ScDeployStep, RH>;

fn prepare_async(self) -> Self::Exec {
InteractorDeployStep {
InteractorExecStep {
step_wrapper: self.tx_to_step(),
}
}
}

impl<'w, RH> InteractorDeployStep<'w, RH>
impl<'w, RH> InteractorExecStep<'w, ScDeployStep, RH>
where
RH: RHListExec<TxResponse, InteractorEnvExec<'w>>,
RH: RHListExec<TxResponse, InteractorExecEnv<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
pub async fn run(mut self) -> <RH::ListReturns as NestedTupleFlatten>::Unpacked {
Expand Down Expand Up @@ -94,7 +82,7 @@ impl Interactor {
let tx = f(tx_base);

let mut step_wrapper = tx.tx_to_step();
self.sc_deploy(&mut step_wrapper.step);
self.sc_deploy(&mut step_wrapper.step).await;
step_wrapper.process_result();

self
Expand Down Expand Up @@ -128,7 +116,7 @@ impl Interactor {
let tx = f(tx_base);

let mut step_wrapper = tx.tx_to_step();
self.sc_deploy(&mut step_wrapper.step);
self.sc_deploy(&mut step_wrapper.step).await;
step_wrapper.process_result()
}
}
46 changes: 46 additions & 0 deletions framework/snippets/src/interactor_tx/interactor_exec_env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use multiversx_sc_scenario::{
api::StaticApi,
multiversx_sc::types::{ManagedAddress, ManagedBuffer, Tx, TxBaseWithEnv, TxEnv},
scenario_model::TxExpect,
ScenarioTxEnv, ScenarioTxEnvData,
};

use crate::Interactor;

impl Interactor {
pub fn tx(&mut self) -> TxBaseWithEnv<InteractorExecEnv<'_>> {
let data = self.new_env_data();
let env = InteractorExecEnv { world: self, data };
Tx::new_with_env(env)
}
}

/// Environment for executing transactions.
pub struct InteractorExecEnv<'w> {
pub world: &'w mut Interactor,
pub data: ScenarioTxEnvData,
}

impl<'w> TxEnv for InteractorExecEnv<'w> {
type Api = StaticApi;

type RHExpect = TxExpect;

fn resolve_sender_address(&self) -> ManagedAddress<Self::Api> {
panic!("Explicit sender address expected")
}

fn default_gas_annotation(&self) -> ManagedBuffer<Self::Api> {
self.data.default_gas_annotation()
}

fn default_gas_value(&self) -> u64 {
self.data.default_gas_value()
}
}

impl<'w> ScenarioTxEnv for InteractorExecEnv<'w> {
fn env_data(&self) -> &ScenarioTxEnvData {
&self.data
}
}
15 changes: 15 additions & 0 deletions framework/snippets/src/interactor_tx/interactor_exec_step.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use multiversx_sc_scenario::{
multiversx_sc::{tuple_util::NestedTupleFlatten, types::RHListExec},
scenario::tx_to_step::StepWrapper,
scenario_model::TxResponse,
};

use super::InteractorExecEnv;

pub struct InteractorExecStep<'w, Step, RH>
where
RH: RHListExec<TxResponse, InteractorExecEnv<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
pub(crate) step_wrapper: StepWrapper<InteractorExecEnv<'w>, Step, RH>,
}
34 changes: 34 additions & 0 deletions framework/snippets/src/interactor_tx/interactor_exec_transf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use multiversx_sc_scenario::{
multiversx_sc::types::{Tx, TxFromSpecified, TxGas, TxPayment, TxToSpecified},
scenario::tx_to_step::TxToStep,
scenario_model::TransferStep,
};

use super::{InteractorExecEnv, InteractorExecStep, InteractorPrepareAsync};

impl<'w, From, To, Payment, Gas> InteractorPrepareAsync
for Tx<InteractorExecEnv<'w>, From, To, Payment, Gas, (), ()>
where
From: TxFromSpecified<InteractorExecEnv<'w>>,
To: TxToSpecified<InteractorExecEnv<'w>>,
Payment: TxPayment<InteractorExecEnv<'w>>,
Gas: TxGas<InteractorExecEnv<'w>>,
{
type Exec = InteractorExecStep<'w, TransferStep, ()>;

fn prepare_async(self) -> Self::Exec {
InteractorExecStep {
step_wrapper: self.tx_to_step(),
}
}
}

impl<'w> InteractorExecStep<'w, TransferStep, ()> {
pub async fn run(self) {
self.step_wrapper
.env
.world
.transfer(self.step_wrapper.step)
.await;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use std::path::PathBuf;

use multiversx_sc_scenario::{
api::StaticApi,
multiversx_sc::types::{AnnotatedValue, ManagedAddress, TxBaseWithEnv, TxEnv},
scenario_model::TxResponse,
ScenarioTxEnvData, ScenarioWorld,
};
use multiversx_sc_scenario::ScenarioTxEnvData;

use crate::Interactor;

Expand Down
65 changes: 65 additions & 0 deletions framework/snippets/src/interactor_tx/interactor_query_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use multiversx_sc_scenario::{
api::StaticApi,
multiversx_sc::{
tuple_util::NestedTupleFlatten,
types::{FunctionCall, RHListExec, Tx, TxBaseWithEnv, TxToSpecified},
},
scenario::tx_to_step::TxToQueryStep,
scenario_model::TxResponse,
ScenarioTxEnvData,
};

use crate::Interactor;

use super::{InteractorPrepareAsync, InteractorQueryEnv, InteractorQueryStep};

impl<'w, To, RH> InteractorPrepareAsync
for Tx<InteractorQueryEnv<'w>, (), To, (), (), FunctionCall<StaticApi>, RH>
where
To: TxToSpecified<InteractorQueryEnv<'w>>,
RH: RHListExec<TxResponse, InteractorQueryEnv<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
type Exec = InteractorQueryStep<'w, RH>;

fn prepare_async(self) -> Self::Exec {
InteractorQueryStep {
step_wrapper: self.tx_to_query_step(),
}
}
}

impl<'w, RH> InteractorQueryStep<'w, RH>
where
RH: RHListExec<TxResponse, InteractorQueryEnv<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
pub async fn run(mut self) -> <RH::ListReturns as NestedTupleFlatten>::Unpacked {
self.step_wrapper
.env
.world
.sc_query(&mut self.step_wrapper.step)
.await;
self.step_wrapper.process_result()
}
}

impl Interactor {
pub async fn chain_query<To, RH, F>(&mut self, f: F) -> &mut Self
where
To: TxToSpecified<ScenarioTxEnvData>,
RH: RHListExec<TxResponse, ScenarioTxEnvData, ListReturns = ()>,
F: FnOnce(
TxBaseWithEnv<ScenarioTxEnvData>,
) -> Tx<ScenarioTxEnvData, (), To, (), (), FunctionCall<StaticApi>, RH>,
{
let env = self.new_env_data();
let tx_base = TxBaseWithEnv::new_with_env(env);
let tx = f(tx_base);

let mut step_wrapper = tx.tx_to_query_step();
self.sc_query(&mut step_wrapper.step).await;
step_wrapper.process_result();
self
}
}
Loading
Loading