Skip to content

Commit

Permalink
interactor made generic on gateway proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 3, 2024
1 parent 3e29d77 commit a8f382a
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 60 deletions.
13 changes: 7 additions & 6 deletions framework/snippets/src/account_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use multiversx_sc_scenario::{
imports::Bech32Address,
scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep, Step},
};
use multiversx_sdk::gateway::GatewayAsyncService;
use multiversx_sdk::gateway::{
GetAccountEsdtRolesRequest, GetAccountEsdtTokensRequest, GetAccountRequest,
GetAccountStorageRequest,
Expand Down Expand Up @@ -37,34 +38,34 @@ fn build_scenario(set_state: SetStateStep) -> Scenario {
}
}

pub async fn retrieve_account_as_scenario_set_state(
api: &GatewayHttpProxy,
pub async fn retrieve_account_as_scenario_set_state<GatewayProxy: GatewayAsyncService>(
api: &GatewayProxy,
use_chain_simulator: bool,
address: &Bech32Address,
) -> SetStateStep {
let sdk_address = SdkAddress::from_bech32_string(address.to_bech32_str()).unwrap();
let sdk_account = api
.http_request(GetAccountRequest::new(&sdk_address))
.request(GetAccountRequest::new(&sdk_address))
.await
.unwrap();

let (account_esdt, account_esdt_roles, account_storage) = if use_chain_simulator {
(HashMap::new(), HashMap::new(), HashMap::new())
} else {
let account_esdt = api
.http_request(GetAccountEsdtTokensRequest::new(&sdk_address))
.request(GetAccountEsdtTokensRequest::new(&sdk_address))
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve ESDT tokens for address {address}: {err}")
});
let account_esdt_roles = api
.http_request(GetAccountEsdtRolesRequest::new(&sdk_address))
.request(GetAccountEsdtRolesRequest::new(&sdk_address))
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve ESDT roles for address {address}: {err}")
});
let account_storage = api
.http_request(GetAccountStorageRequest::new(&sdk_address))
.request(GetAccountStorageRequest::new(&sdk_address))
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve storage for address {address}: {err}")
Expand Down
25 changes: 20 additions & 5 deletions framework/snippets/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use multiversx_sc_scenario::{
mandos_system::{run_list::ScenarioRunnerList, run_trace::ScenarioTraceFile},
multiversx_sc::types::Address,
};
use multiversx_sdk::gateway::{GatewayAsyncService, NetworkConfigRequest};
use multiversx_sdk_http::GatewayHttpProxy;
use std::{
collections::HashMap,
Expand All @@ -15,8 +16,11 @@ use crate::{account_tool::retrieve_account_as_scenario_set_state, Sender};

pub const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json";

pub struct Interactor {
pub proxy: GatewayHttpProxy,
pub struct InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub proxy: GatewayProxy,
pub use_chain_simulator: bool,
pub network_config: NetworkConfig,
pub sender_map: HashMap<Address, Sender>,
Expand All @@ -28,10 +32,16 @@ pub struct Interactor {
pub current_dir: PathBuf,
}

impl Interactor {
pub type HttpInteractor = InteractorBase<GatewayHttpProxy>;

/// Backwards compatibility.
pub type Interactor = HttpInteractor;

impl HttpInteractor {
/// Not yet changed for backwards compatibility.
pub async fn new(gateway_uri: &str, use_chain_simulator: bool) -> Self {
let proxy = GatewayHttpProxy::new(gateway_uri.to_string());
let network_config = proxy.get_network_config().await.unwrap();
let proxy: GatewayHttpProxy = GatewayHttpProxy::new(gateway_uri.to_string());
let network_config = proxy.request(NetworkConfigRequest).await.unwrap();
Self {
proxy,
use_chain_simulator,
Expand All @@ -43,7 +53,12 @@ impl Interactor {
current_dir: PathBuf::default(),
}
}
}

impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn register_wallet(&mut self, wallet: Wallet) -> Address {
let wallet_address = wallet.address();

Expand Down
7 changes: 5 additions & 2 deletions framework/snippets/src/interactor_chain_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use multiversx_sdk::{
},
};

use crate::Interactor;
use crate::InteractorBase;

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn send_user_funds(&self, receiver: &SdkAddress) -> Result<String, Error> {
if !self.use_chain_simulator {
return Ok(String::from("no-simulator"));
Expand Down
18 changes: 14 additions & 4 deletions framework/snippets/src/interactor_scenario/interactor_sc_call.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use crate::{network_response, Interactor};
use crate::{network_response, InteractorBase};
use log::info;
use multiversx_sc_scenario::{
api::StaticApi,
scenario::ScenarioRunner,
scenario_model::{ScCallStep, SetStateStep, TxCall},
};
use multiversx_sdk::retrieve_tx_on_network;
use multiversx_sdk::{
gateway::{GatewayAsyncService, SendTxRequest},
retrieve_tx_on_network,
};
use multiversx_sdk_http::core::{data::transaction::Transaction, utils::base64_encode};

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn sc_call<S>(&mut self, mut sc_call_step: S)
where
S: AsMut<ScCallStep>,
Expand Down Expand Up @@ -41,7 +47,11 @@ impl Interactor {
let mut transaction = self.tx_call_to_blockchain_tx(&sc_call_step.tx);
self.set_nonce_and_sign_tx(sender_address, &mut transaction)
.await;
let tx_hash = self.proxy.send_transaction(&transaction).await.unwrap();
let tx_hash = self
.proxy
.request(SendTxRequest(&transaction))
.await
.unwrap();
println!("sc call tx hash: {tx_hash}");
info!("sc call tx hash: {}", tx_hash);

Expand Down
14 changes: 10 additions & 4 deletions framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use crate::{network_response, Interactor};
use crate::{network_response, InteractorBase};
use log::info;
use multiversx_sc_scenario::{
imports::{Address, Bech32Address},
mandos_system::ScenarioRunner,
scenario_model::{ScDeployStep, SetStateStep},
};
use multiversx_sdk::retrieve_tx_on_network;
use multiversx_sdk::{
gateway::{GatewayAsyncService, SendTxRequest},
retrieve_tx_on_network,
};
use multiversx_sdk_http::core::{data::transaction::Transaction, utils::base64_encode};

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub(crate) fn sc_deploy_to_blockchain_tx(&self, sc_deploy_step: &ScDeployStep) -> Transaction {
Transaction {
nonce: 0,
Expand All @@ -34,7 +40,7 @@ impl Interactor {
.await;
let tx_hash = self
.proxy
.send_transaction(&transaction)
.request(SendTxRequest(&transaction))
.await
.expect("error sending tx (possible API failure)");
println!("sc deploy tx hash: {tx_hash}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(deprecated)]

use crate::Interactor;
use crate::InteractorBase;
use multiversx_sc_scenario::{
api::StaticApi,
multiversx_sc::{
Expand All @@ -13,8 +13,12 @@ use multiversx_sc_scenario::{
TypedScDeploy, TypedScQuery,
},
};
use multiversx_sdk::gateway::GatewayAsyncService;

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
#[deprecated(
since = "0.49.0",
note = "Please use the unified transaction syntax instead."
Expand Down
18 changes: 14 additions & 4 deletions framework/snippets/src/interactor_scenario/interactor_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use crate::Interactor;
use crate::InteractorBase;
use log::info;
use multiversx_sc_scenario::{scenario::ScenarioRunner, scenario_model::TransferStep};
use multiversx_sdk::retrieve_tx_on_network;
use multiversx_sdk::{
gateway::{GatewayAsyncService, SendTxRequest},
retrieve_tx_on_network,
};

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn transfer(&mut self, transfer_step: TransferStep) -> String {
self.pre_runners.run_transfer_step(&transfer_step);

let sender_address = &transfer_step.tx.from.value;
let mut transaction = self.tx_call_to_blockchain_tx(&transfer_step.tx.to_tx_call());
self.set_nonce_and_sign_tx(sender_address, &mut transaction)
.await;
let tx_hash = self.proxy.send_transaction(&transaction).await.unwrap();
let tx_hash = self
.proxy
.request(SendTxRequest(&transaction))
.await
.unwrap();
self.generate_blocks_until_tx_processed(&tx_hash)
.await
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#![allow(deprecated)]

use crate::Interactor;
use crate::InteractorBase;
use log::info;
use multiversx_sc_scenario::{
api::StaticApi,
mandos_system::ScenarioRunner,
multiversx_sc::{abi::TypeAbiFrom, codec::TopDecodeMulti, types::ContractCall},
scenario_model::{ScQueryStep, TxResponse},
};
use multiversx_sdk::gateway::{GatewayAsyncService, VMQueryRequest};
use multiversx_sdk_http::core::{data::vm::VMQueryInput, utils::base64_decode};

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn sc_query<S>(&mut self, mut step: S) -> &mut Self
where
S: AsMut<ScQueryStep>,
Expand All @@ -33,7 +37,7 @@ impl Interactor {
};
let result = self
.proxy
.execute_vmquery(&req)
.request(VMQueryRequest(&req))
.await
.expect("error executing VM query");

Expand Down
10 changes: 7 additions & 3 deletions framework/snippets/src/interactor_sender.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::sdk::{data::transaction::Transaction, wallet::Wallet};
use log::debug;
use multiversx_sc_scenario::multiversx_sc::types::Address;
use multiversx_sdk::gateway::{GatewayAsyncService, GetAccountRequest};

use crate::Interactor;
use crate::InteractorBase;

/// A user account that can sign transactions (a pem is present).
pub struct Sender {
Expand All @@ -11,11 +12,14 @@ pub struct Sender {
pub current_nonce: Option<u64>,
}

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn recall_nonce(&self, address: &Address) -> u64 {
let account = self
.proxy
.get_account(&address.clone().into())
.request(GetAccountRequest::new(&address.clone().into()))
.await
.expect("failed to retrieve account nonce");
account.nonce
Expand Down
8 changes: 6 additions & 2 deletions framework/snippets/src/interactor_tx/interactor_exec_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use multiversx_sc_scenario::{
scenario_model::{ScCallStep, TxResponse},
ScenarioTxEnvData,
};
use multiversx_sdk::gateway::GatewayAsyncService;

use crate::Interactor;
use crate::InteractorBase;

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

Expand Down Expand Up @@ -50,7 +51,10 @@ where
}
}

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn chain_call<From, To, Payment, Gas, RH, F>(&mut self, f: F) -> &mut Self
where
From: TxFromSpecified<ScenarioTxEnvData>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use multiversx_sc_scenario::{
scenario_model::{ScDeployStep, TxResponse},
ScenarioTxEnvData,
};
use multiversx_sdk::gateway::GatewayAsyncService;

use crate::Interactor;
use crate::InteractorBase;

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

Expand Down Expand Up @@ -57,7 +58,10 @@ where
}
}

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn chain_deploy<From, Payment, Gas, CodeValue, RH, F>(&mut self, f: F) -> &mut Self
where
From: TxFromSpecified<ScenarioTxEnvData>,
Expand Down
8 changes: 5 additions & 3 deletions framework/snippets/src/interactor_tx/interactor_exec_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use multiversx_sc_scenario::{
scenario_model::TxExpect,
ScenarioTxEnv, ScenarioTxEnvData,
};
use multiversx_sdk::gateway::GatewayAsyncService;
use multiversx_sdk_http::GatewayHttpProxy;

use crate::Interactor;
use crate::InteractorBase;

impl Interactor {
impl InteractorBase<GatewayHttpProxy> {
pub fn tx(&mut self) -> TxBaseWithEnv<InteractorEnvExec<'_>> {
let data = self.new_env_data();
let env = InteractorEnvExec { world: self, data };
Expand All @@ -19,7 +21,7 @@ impl Interactor {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use multiversx_sc_scenario::{
ScenarioTxEnvData,
};

use crate::Interactor;
use crate::InteractorBase;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use multiversx_sc_scenario::{imports::InterpreterContext, ScenarioTxEnvData};
use multiversx_sdk::gateway::GatewayAsyncService;

use crate::Interactor;
use crate::InteractorBase;

impl Interactor {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub(crate) fn new_env_data(&self) -> ScenarioTxEnvData {
ScenarioTxEnvData {
interpreter_context: InterpreterContext::new().with_dir(self.current_dir.clone()),
Expand Down
Loading

0 comments on commit a8f382a

Please sign in to comment.