Skip to content

Commit

Permalink
Merge pull request #1502 from multiversx/rh-refactor
Browse files Browse the repository at this point in the history
Unified syntax - result handler refactor
  • Loading branch information
andrei-marinica authored Mar 25, 2024
2 parents b0aedac + da7e01b commit 8c28032
Show file tree
Hide file tree
Showing 29 changed files with 211 additions and 512 deletions.
99 changes: 18 additions & 81 deletions framework/base/src/types/interaction/tx_call_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,16 @@ use crate::{

use super::{
contract_call_exec::decode_result, Code, ConsNoRet, ConsRet, DeployCall, FromSource,
OriginalResultMarker, RHList, RHListItem, Tx, TxCodeValue, TxEmptyResultHandler, TxEnv,
TxFromSourceValue, TxGas, TxPaymentEgldOnly, TxResultHandler, TxScEnv,
OriginalResultMarker, RHList, RHListExec, RHListItem, Tx, TxCodeValue, TxEmptyResultHandler,
TxEnv, TxFromSourceValue, TxGas, TxPaymentEgldOnly, TxResultHandler, TxScEnv,
};

pub trait RHListItemDeploy<Env, Original>: RHListItem<Env, Original>
pub struct DeployRawResult<Api>
where
Env: TxEnv,
{
fn item_deploy_result(
self,
new_address: &ManagedAddress<Env::Api>,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::Returns;
}

pub trait RHListDeploy<Env>: RHList<Env>
where
Env: TxEnv,
{
fn list_deploy_result(
self,
new_address: &ManagedAddress<Env::Api>,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns;
}

impl<Env> RHListDeploy<Env> for ()
where
Env: TxEnv,
{
fn list_deploy_result(
self,
_new_address: &ManagedAddress<Env::Api>,
_raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
}
}

impl<Env, O> RHListDeploy<Env> for OriginalResultMarker<O>
where
Env: TxEnv,
{
fn list_deploy_result(
self,
_new_address: &ManagedAddress<Env::Api>,
_raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
}
}

impl<Env, Head, Tail> RHListDeploy<Env> for ConsRet<Env, Head, Tail>
where
Env: TxEnv,
Head: RHListItemDeploy<Env, Tail::OriginalResult>,
Tail: RHListDeploy<Env>,
{
fn list_deploy_result(
self,
new_address: &ManagedAddress<Env::Api>,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
let head_result = self.head.item_deploy_result(new_address, raw_results);
let tail_result = self.tail.list_deploy_result(new_address, raw_results);
(head_result, tail_result)
}
}

impl<Env, Head, Tail> RHListDeploy<Env> for ConsNoRet<Env, Head, Tail>
where
Env: TxEnv,
Head: RHListItemDeploy<Env, Tail::OriginalResult, Returns = ()>,
Tail: RHListDeploy<Env>,
Api: CallTypeApi,
{
fn list_deploy_result(
self,
new_address: &ManagedAddress<Env::Api>,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
self.head.item_deploy_result(new_address, raw_results);
self.tail.list_deploy_result(new_address, raw_results)
}
pub new_address: ManagedAddress<Api>,
pub raw_results: ManagedVec<Api, ManagedBuffer<Api>>,
}

impl<Api, Payment, Gas, CodeValue, RH>
Expand Down Expand Up @@ -165,14 +94,18 @@ where
Payment: TxPaymentEgldOnly<TxScEnv<Api>>,
Gas: TxGas<TxScEnv<Api>>,
CodeValue: TxCodeValue<TxScEnv<Api>>,
RH: RHListDeploy<TxScEnv<Api>>,
RH: RHListExec<DeployRawResult<Api>, TxScEnv<Api>>,
RH::ListReturns: NestedTupleFlatten,
{
/// Synchronously deploys a contract.
pub fn sync_call(self) -> <RH::ListReturns as NestedTupleFlatten>::Unpacked {
let (new_address, raw_results, result_handler) = self.execute_deploy_raw();

let tuple_result = result_handler.list_deploy_result(&new_address, &raw_results);
let deploy_raw_result = DeployRawResult {
new_address,
raw_results,
};
let tuple_result = result_handler.list_process_result(&deploy_raw_result);
tuple_result.flatten_unpack()
}
}
Expand All @@ -192,14 +125,18 @@ where
Payment: TxPaymentEgldOnly<TxScEnv<Api>>,
Gas: TxGas<TxScEnv<Api>>,
FromSourceValue: TxFromSourceValue<TxScEnv<Api>>,
RH: RHListDeploy<TxScEnv<Api>>,
RH: RHListExec<DeployRawResult<Api>, TxScEnv<Api>>,
RH::ListReturns: NestedTupleFlatten,
{
/// Synchronously deploys a contract from source.
pub fn sync_call(self) -> <RH::ListReturns as NestedTupleFlatten>::Unpacked {
let (new_address, raw_results, result_handler) = self.execute_deploy_from_source_raw();

let tuple_result = result_handler.list_deploy_result(&new_address, &raw_results);
let deploy_raw_result = DeployRawResult {
new_address,
raw_results,
};
let tuple_result = result_handler.list_process_result(&deploy_raw_result);
tuple_result.flatten_unpack()
}
}
Expand Down
82 changes: 7 additions & 75 deletions framework/base/src/types/interaction/tx_call_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,13 @@ use crate::{

use super::{
contract_call_exec::decode_result, BackTransfers, ConsNoRet, ConsRet, OriginalResultMarker,
RHList, RHListItem, Tx, TxDataFunctionCall, TxEnv, TxGas, TxPayment, TxScEnv, TxToSpecified,
RHList, RHListExec, RHListItem, Tx, TxDataFunctionCall, TxEnv, TxGas, TxPayment, TxScEnv,
TxToSpecified,
};

pub trait RHListItemSync<Env, Original>: RHListItem<Env, Original>
pub struct SyncCallRawResult<Api>(pub ManagedVec<Api, ManagedBuffer<Api>>)
where
Env: TxEnv,
{
fn item_sync_call_result(
self,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::Returns;
}

pub trait RHListSync<Env>: RHList<Env>
where
Env: TxEnv,
{
fn list_sync_call_result(
self,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns;
}

impl<Env> RHListSync<Env> for ()
where
Env: TxEnv,
{
fn list_sync_call_result(
self,
_raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
}
}

impl<Env, O> RHListSync<Env> for OriginalResultMarker<O>
where
Env: TxEnv,
{
fn list_sync_call_result(
self,
_raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
}
}

impl<Env, Head, Tail> RHListSync<Env> for ConsRet<Env, Head, Tail>
where
Env: TxEnv,
Head: RHListItemSync<Env, Tail::OriginalResult>,
Tail: RHListSync<Env>,
{
fn list_sync_call_result(
self,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
let head_result = self.head.item_sync_call_result(raw_results);
let tail_result = self.tail.list_sync_call_result(raw_results);
(head_result, tail_result)
}
}

impl<Env, Head, Tail> RHListSync<Env> for ConsNoRet<Env, Head, Tail>
where
Env: TxEnv,
Head: RHListItemSync<Env, Tail::OriginalResult, Returns = ()>,
Tail: RHListSync<Env>,
{
fn list_sync_call_result(
self,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::ListReturns {
self.head.item_sync_call_result(raw_results);
self.tail.list_sync_call_result(raw_results)
}
}
Api: CallTypeApi;

impl<Api, To, Payment, Gas, FC, RH> Tx<TxScEnv<Api>, (), To, Payment, Gas, FC, RH>
where
Expand All @@ -92,7 +24,7 @@ where
Payment: TxPayment<TxScEnv<Api>>,
Gas: TxGas<TxScEnv<Api>>,
FC: TxDataFunctionCall<TxScEnv<Api>>,
RH: RHListSync<TxScEnv<Api>>,
RH: RHListExec<SyncCallRawResult<Api>, TxScEnv<Api>>,
RH::ListReturns: NestedTupleFlatten,
{
fn execute_sync_call_raw(self) -> (ManagedVec<Api, ManagedBuffer<Api>>, RH) {
Expand Down Expand Up @@ -121,8 +53,8 @@ where

pub fn sync_call(self) -> <RH::ListReturns as NestedTupleFlatten>::Unpacked {
let (raw_result, result_handler) = self.execute_sync_call_raw();

let tuple_result = result_handler.list_sync_call_result(&raw_result);
let sync_raw_result = SyncCallRawResult(raw_result);
let tuple_result = result_handler.list_process_result(&sync_raw_result);
tuple_result.flatten_unpack()
}
}
Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/types/interaction/tx_rh_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod returns_new_address;
mod returns_raw;
mod returns_similar;
mod tx_rh_list_cons;
mod tx_rh_list_exec;
mod tx_rh_list_item;
mod with_result_exact;
mod with_result_new_address;
Expand All @@ -16,6 +17,7 @@ pub use returns_new_address::*;
pub use returns_raw::*;
pub use returns_similar::*;
pub use tx_rh_list_cons::*;
pub use tx_rh_list_exec::*;
pub use tx_rh_list_item::*;
pub use with_result_new_address::*;
pub use with_result_similar::*;
27 changes: 4 additions & 23 deletions framework/base/src/types/interaction/tx_rh_list/returns_bt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::{
contract_base::BlockchainWrapper,
types::{
interaction::tx_call_deploy::RHListItemDeploy, BackTransfers, ManagedAddress,
ManagedBuffer, ManagedVec, RHListItemSync, TxEnv,
},
types::{BackTransfers, ManagedAddress, ManagedBuffer, ManagedVec, TxEnv},
};

use super::RHListItem;
use super::{RHListItem, RHListItemExec};

pub struct ReturnsBackTransfers;

Expand All @@ -17,27 +14,11 @@ where
type Returns = BackTransfers<Env::Api>;
}

impl<Env, Original> RHListItemSync<Env, Original> for ReturnsBackTransfers
impl<RawResult, Env, Original> RHListItemExec<RawResult, Env, Original> for ReturnsBackTransfers
where
Env: TxEnv,
{
fn item_sync_call_result(
self,
_raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::Returns {
BlockchainWrapper::<Env::Api>::new().get_back_transfers()
}
}

impl<Env, Original> RHListItemDeploy<Env, Original> for ReturnsBackTransfers
where
Env: TxEnv,
{
fn item_deploy_result(
self,
_new_address: &ManagedAddress<Env::Api>,
_raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::Returns {
fn item_process_result(self, _raw_result: &RawResult) -> Self::Returns {
BlockchainWrapper::<Env::Api>::new().get_back_transfers()
}
}
17 changes: 7 additions & 10 deletions framework/base/src/types/interaction/tx_rh_list/returns_exact.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use multiversx_sc_codec::TopDecodeMulti;

use crate::types::{
interaction::contract_call_exec::decode_result, ManagedBuffer, ManagedVec, RHListItemSync,
TxEnv,
use crate::{
proxy_imports::SyncCallRawResult,
types::{interaction::contract_call_exec::decode_result, ManagedBuffer, ManagedVec, TxEnv},
};

use super::RHListItem;
use super::{RHListItem, RHListItemExec};

pub struct ReturnsExact;

Expand All @@ -16,15 +16,12 @@ where
type Returns = Original;
}

impl<Env, Original> RHListItemSync<Env, Original> for ReturnsExact
impl<Env, Original> RHListItemExec<SyncCallRawResult<Env::Api>, Env, Original> for ReturnsExact
where
Env: TxEnv,
Original: TopDecodeMulti,
{
fn item_sync_call_result(
self,
raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::Returns {
decode_result::<Env::Api, Original>(raw_results.clone())
fn item_process_result(self, raw_result: &SyncCallRawResult<Env::Api>) -> Original {
decode_result::<Env::Api, Original>(raw_result.0.clone())
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::types::{
interaction::tx_call_deploy::RHListItemDeploy, ManagedAddress, ManagedBuffer, ManagedVec, TxEnv,
};
use crate::types::{DeployRawResult, ManagedAddress, ManagedBuffer, ManagedVec, TxEnv};

use super::RHListItem;
use super::{RHListItem, RHListItemExec};

pub struct ReturnsNewAddress;

Expand All @@ -13,15 +11,11 @@ where
type Returns = ManagedAddress<Env::Api>;
}

impl<Env, Original> RHListItemDeploy<Env, Original> for ReturnsNewAddress
impl<Env, Original> RHListItemExec<DeployRawResult<Env::Api>, Env, Original> for ReturnsNewAddress
where
Env: TxEnv,
{
fn item_deploy_result(
self,
new_address: &ManagedAddress<Env::Api>,
_raw_results: &ManagedVec<Env::Api, ManagedBuffer<Env::Api>>,
) -> Self::Returns {
new_address.clone()
fn item_process_result(self, raw_result: &DeployRawResult<Env::Api>) -> Self::Returns {
raw_result.new_address.clone()
}
}
Loading

0 comments on commit 8c28032

Please sign in to comment.