Skip to content

Commit

Permalink
Merge pull request #1728 from multiversx/chain-simulator-interactor
Browse files Browse the repository at this point in the history
chain simulator - integration with interactor
  • Loading branch information
andrei-marinica authored Sep 30, 2024
2 parents 33bdb49 + d4773f4 commit fca4517
Show file tree
Hide file tree
Showing 41 changed files with 341 additions and 114 deletions.
3 changes: 2 additions & 1 deletion contracts/examples/adder/interact/config.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
gateway = 'https://devnet-gateway.multiversx.com'
chain_type = 'simulator'
gateway_uri = 'http://localhost:8085'
25 changes: 16 additions & 9 deletions contracts/examples/adder/interact/src/basic_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod basic_interact_cli;
mod basic_interact_config;
mod basic_interact_state;

use core::str;

use adder::adder_proxy;
use basic_interact_config::Config;
use basic_interact_state::State;
Expand Down Expand Up @@ -61,22 +63,27 @@ struct AdderInteract {
impl AdderInteract {
async fn init() -> Self {
let config = Config::load_config();
let mut interactor = Interactor::new(config.gateway())
let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator())
.await
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;

let adder_owner_address =
interactor.register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap());
let adder_owner_address = interactor
.register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap())
.await;
// PASSWORD: "alice"
// InsertPassword::Plaintext("alice".to_string()) || InsertPassword::StandardInput
let wallet_address = interactor.register_wallet(
Wallet::from_keystore_secret(
"alice.json",
InsertPassword::Plaintext("alice".to_string()),
let wallet_address = interactor
.register_wallet(
Wallet::from_keystore_secret(
"alice.json",
InsertPassword::Plaintext("alice".to_string()),
)
.unwrap(),
)
.unwrap(),
);
.await;

interactor.proxy.generate_blocks(1).await.unwrap();

Self {
interactor,
Expand Down
24 changes: 20 additions & 4 deletions contracts/examples/adder/interact/src/basic_interact_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ use std::io::Read;
/// Config file
const CONFIG_FILE: &str = "config.toml";

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChainType {
Real,
Simulator,
}

/// Adder Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
gateway: String,
gateway_uri: String,
chain_type: ChainType,
}

impl Config {
Expand All @@ -19,8 +27,16 @@ impl Config {
toml::from_str(&content).unwrap()
}

// Returns the gateway
pub fn gateway(&self) -> &str {
&self.gateway
// Returns the gateway URI
pub fn gateway_uri(&self) -> &str {
&self.gateway_uri
}

// Returns if chain type is chain simulator
pub fn use_chain_simulator(&self) -> bool {
match self.chain_type {
ChainType::Real => false,
ChainType::Simulator => true,
}
}
}
3 changes: 2 additions & 1 deletion contracts/examples/multisig/interact/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
gateway = 'https://devnet-gateway.multiversx.com'
chain_type = 'real'
gateway_uri = 'https://devnet-gateway.multiversx.com'
quorum = 2
wegld_address = "erd1qqqqqqqqqqqqqpgqqkwzsxkjc83vlfex9dmznwm7tjvxlqqkpauqx0n782"
10 changes: 5 additions & 5 deletions contracts/examples/multisig/interact/src/multisig_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() {
env_logger::init();

let mut multisig_interact = MultisigInteract::init().await;
multisig_interact.register_wallets();
multisig_interact.register_wallets().await;

let cli = multisig_interact_cli::InteractCli::parse();
match &cli.command {
Expand Down Expand Up @@ -86,11 +86,11 @@ struct MultisigInteract {
impl MultisigInteract {
async fn init() -> Self {
let config = Config::load_config();
let mut interactor = Interactor::new(&config.gateway)
let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator())
.await
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;
let wallet_address = interactor.register_wallet(test_wallets::mike());
let wallet_address = interactor.register_wallet(test_wallets::mike()).await;
let multisig_code = BytesValue::interpret_from(
"mxsc:../output/multisig.mxsc.json",
&InterpreterContext::default(),
Expand All @@ -106,13 +106,13 @@ impl MultisigInteract {
}
}

fn register_wallets(&mut self) {
async fn register_wallets(&mut self) {
let carol = test_wallets::carol();
let dan = test_wallets::dan();
let eve = test_wallets::eve();

for wallet in &[carol, dan, eve] {
self.interactor.register_wallet(*wallet);
self.interactor.register_wallet(*wallet).await;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ use std::io::Read;
/// Config file
const CONFIG_FILE: &str = "config.toml";

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChainType {
Real,
Simulator,
}

/// Multisig Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
pub gateway: String,
pub gateway_uri: String,
pub chain_type: ChainType,
pub quorum: usize,
pub wegld_address: Bech32Address,
}
Expand All @@ -21,4 +29,17 @@ impl Config {
file.read_to_string(&mut content).unwrap();
toml::from_str(&content).unwrap()
}

// Returns the gateway URI
pub fn gateway_uri(&self) -> &str {
&self.gateway_uri
}

// Returns if chain type is chain simulator
pub fn use_chain_simulator(&self) -> bool {
match self.chain_type {
ChainType::Real => false,
ChainType::Simulator => true,
}
}
}
3 changes: 2 additions & 1 deletion contracts/feature-tests/basic-features/interact/config.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
gateway = 'https://devnet-gateway.multiversx.com'
chain_type = 'real'
gateway_uri = 'https://devnet-gateway.multiversx.com'
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ struct BasicFeaturesInteract {
impl BasicFeaturesInteract {
async fn init() -> Self {
let config = Config::load_config();
let mut interactor = Interactor::new(config.gateway())
let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator())
.await
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;
let wallet_address = interactor.register_wallet(test_wallets::mike());
let wallet_address = interactor.register_wallet(test_wallets::mike()).await;
let code_expr = BytesValue::interpret_from(
"mxsc:../output/basic-features-storage-bytes.mxsc.json",
&InterpreterContext::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ use std::io::Read;
/// Config file
const CONFIG_FILE: &str = "config.toml";

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChainType {
Real,
Simulator,
}

/// Adder Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
gateway: String,
gateway_uri: String,
chain_type: ChainType,
}

impl Config {
Expand All @@ -19,8 +27,16 @@ impl Config {
toml::from_str(&content).unwrap()
}

// Returns the gateway
pub fn gateway(&self) -> &str {
&self.gateway
// Returns the gateway URI
pub fn gateway_uri(&self) -> &str {
&self.gateway_uri
}

// Returns if chain type is chain simulator
pub fn use_chain_simulator(&self) -> bool {
match self.chain_type {
ChainType::Real => false,
ChainType::Simulator => true,
}
}
}
9 changes: 5 additions & 4 deletions contracts/feature-tests/composability/interact/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
gateway = 'https://testnet-gateway.multiversx.com'
call_type = "LegacyAsync" # Sync / LegacyAsync / TransferExecute
chain_type = 'real'
gateway_uri = 'https://testnet-gateway.multiversx.com'
call_type = "LegacyAsync" # Sync / LegacyAsync / TransferExecute
# token_id = "CMPT-4e9332"
token_id = "EGLD"
token_nonce = 0 # is 0 if fungible
amount = '50000000000000000'
token_nonce = 0 # is 0 if fungible
amount = '50000000000000000'
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ use crate::forwarder_queue_proxy::QueuedCallType;
/// Config file
const CONFIG_FILE: &str = "config.toml";

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChainType {
Real,
Simulator,
}

/// Multisig Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
gateway: String,
gateway_uri: String,
chain_type: ChainType,
call_type: String,
token_id: String,
token_nonce: u64,
Expand All @@ -27,9 +35,17 @@ impl Config {
toml::from_str(&content).unwrap()
}

// Returns the gateway
pub fn gateway(&self) -> &str {
&self.gateway
// Returns the gateway URI
pub fn gateway_uri(&self) -> &str {
&self.gateway_uri
}

// Returns if chain type is chain simulator
pub fn use_chain_simulator(&self) -> bool {
match self.chain_type {
ChainType::Real => false,
ChainType::Simulator => true,
}
}

pub fn call_type(&self) -> QueuedCallType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub struct ComposabilityInteract {
impl ComposabilityInteract {
pub async fn init() -> Self {
let config = Config::load_config();
let mut interactor = Interactor::new(config.gateway())
let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator())
.await
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;
let wallet_address = interactor.register_wallet(test_wallets::judy());
let wallet_address = interactor.register_wallet(test_wallets::judy()).await;
let forw_queue_code = BytesValue::interpret_from(
"mxsc:../forwarder-queue/output/forwarder-queue.mxsc.json",
&InterpreterContext::default(),
Expand Down
10 changes: 9 additions & 1 deletion framework/meta/src/cli/cli_args_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,19 @@ pub struct InstallWasmOptArgs {}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct AccountArgs {
/// Provide the target API you want the real data to come from
/// Provide the target API you want the data to come from
#[arg(long = "api")]
#[clap(global = true)]
pub api: Option<String>,

/// Provide if the API is a chain simulator or not
#[arg(
long = "chain-simulator",
default_value = "false",
verbatim_doc_comment
)]
pub chain_simulator: Option<bool>,

/// Provide the address you want to retrieve data from
#[arg(long = "address", verbatim_doc_comment)]
pub address: String,
Expand Down
8 changes: 7 additions & 1 deletion framework/meta/src/cmd/retrieve_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ use crate::cli::AccountArgs;
/// Interprets arguments and call the account tool from `multiversx_sc_snippets`.
pub async fn retrieve_address(args: &AccountArgs) {
let api_string = args.api.clone().expect("API needs to be specified");
account_tool::print_account_as_scenario_set_state(api_string, args.address.to_string()).await;
let use_chain_simulator = args.chain_simulator.unwrap_or_default();
account_tool::print_account_as_scenario_set_state(
api_string,
use_chain_simulator,
args.address.to_string(),
)
.await;
}
41 changes: 26 additions & 15 deletions framework/snippets/src/account_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use std::collections::{BTreeMap, HashMap};
/// then formats it as a scenario set state step.
pub async fn print_account_as_scenario_set_state(
api_string: String,
use_chain_simulator: bool,
address_bech32_string: String,
) {
let api = GatewayProxy::new(api_string);
let api = GatewayProxy::new(api_string, use_chain_simulator);
let address = Bech32Address::from_bech32_string(address_bech32_string);
let set_state = retrieve_account_as_scenario_set_state(&api, &address).await;
let scenario = build_scenario(set_state);
Expand All @@ -40,20 +41,30 @@ pub async fn retrieve_account_as_scenario_set_state(
let sdk_address = Address::from_bech32_string(address.to_bech32_str()).unwrap();
let sdk_account = api.get_account(&sdk_address).await.unwrap();

let account_esdt = api
.get_account_esdt_tokens(&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)
.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)
.await
.unwrap_or_else(|err| panic!("failed to retrieve storage for address {address}: {err}"));
let (account_esdt, account_esdt_roles, account_storage) = if api.chain_simulator {
(HashMap::new(), HashMap::new(), HashMap::new())
} else {
let account_esdt = api
.get_account_esdt_tokens(&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)
.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)
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve storage for address {address}: {err}")
});

(account_esdt, account_esdt_roles, account_storage)
};

let account_state = set_account(
sdk_account,
Expand Down
Loading

0 comments on commit fca4517

Please sign in to comment.