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 made generic on gateway proxy #1801

Merged
merged 4 commits into from
Oct 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
20 changes: 14 additions & 6 deletions framework/snippets/src/account_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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,
};
use multiversx_sdk_http::GatewayHttpProxy;
use std::collections::{BTreeMap, HashMap};

Expand Down Expand Up @@ -33,31 +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.get_account(&sdk_address).await.unwrap();
let sdk_account = api
.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
.get_account_esdt_tokens(&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
.get_account_esdt_roles(&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
.get_account_storage_keys(&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
19 changes: 15 additions & 4 deletions framework/snippets/src/interactor_scenario/interactor_sc_call.rs
Original file line number Diff line number Diff line change
@@ -1,13 +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::{
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 All @@ -17,7 +24,7 @@ impl Interactor {
self.generate_blocks_until_tx_processed(&tx_hash)
.await
.unwrap();
let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;
let tx = retrieve_tx_on_network(&self.proxy, tx_hash.clone()).await;

sc_call_step.save_response(network_response::parse_tx_response(tx));

Expand All @@ -40,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
15 changes: 11 additions & 4 deletions framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +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::{
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 @@ -33,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 All @@ -51,7 +58,7 @@ impl Interactor {
self.generate_blocks_until_tx_processed(&tx_hash)
.await
.unwrap();
let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;
let tx = retrieve_tx_on_network(&self.proxy, tx_hash.clone()).await;

let addr = sc_deploy_step.tx.from.clone();
let nonce = tx.nonce;
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
19 changes: 15 additions & 4 deletions framework/snippets/src/interactor_scenario/interactor_transfer.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
use crate::Interactor;
use crate::InteractorBase;
use log::info;
use multiversx_sc_scenario::{scenario::ScenarioRunner, scenario_model::TransferStep};
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();

println!("transfer tx hash: {tx_hash}");
info!("transfer tx hash: {}", tx_hash);

self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;
retrieve_tx_on_network(&self.proxy, tx_hash.clone()).await;

self.post_runners.run_transfer_step(&transfer_step);

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
Loading
Loading