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

chain simulator - integration with interactor #1728

Merged
merged 13 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
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 = 'real'
gateway_uri = 'http://localhost:8085'
23 changes: 14 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,25 @@ 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;

Self {
interactor,
Expand Down
21 changes: 17 additions & 4 deletions contracts/examples/adder/interact/src/basic_interact_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const CONFIG_FILE: &str = "config.toml";
/// Adder Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
gateway: String,
gateway_uri: String,
chain_type: String,
}

impl Config {
Expand All @@ -19,8 +20,20 @@ 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.as_str() {
"simulator" => true,
"real" => false,
_ => panic!(
"Invalid chain type: {}. Expected 'simulator' or 'real'.",
self.chain_type
),
}
}
}
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 @@ -8,7 +8,8 @@ const CONFIG_FILE: &str = "config.toml";
/// Multisig Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
pub gateway: String,
pub gateway_uri: String,
pub chain_type: String,
pub quorum: usize,
pub wegld_address: Bech32Address,
}
Expand All @@ -21,4 +22,21 @@ 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.as_str() {
"simulator" => true,
"real" => false,
_ => panic!(
"Invalid chain type: {}. Expected 'simulator' or 'real'.",
self.chain_type
),
}
}
}
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 @@ -7,7 +7,8 @@ const CONFIG_FILE: &str = "config.toml";
/// Adder Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
gateway: String,
gateway_uri: String,
chain_type: String,
}

impl Config {
Expand All @@ -19,8 +20,20 @@ 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 true if chain type is chain simulator
pub fn use_chain_simulator(&self) -> bool {
match self.chain_type.as_str() {
"simulator" => true,
"real" => false,
_ => panic!(
"Invalid chain type: {}. Expected 'simulator' or 'real'.",
self.chain_type
),
}
}
}
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 @@ -11,7 +11,8 @@ const CONFIG_FILE: &str = "config.toml";
/// Multisig Interact configuration
#[derive(Debug, Deserialize)]
pub struct Config {
gateway: String,
gateway_uri: String,
chain_type: String,
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
call_type: String,
token_id: String,
token_nonce: u64,
Expand All @@ -27,9 +28,21 @@ 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.as_str() {
"simulator" => true,
"real" => false,
_ => panic!(
"Invalid chain type: {}. Expected 'simulator' or 'real'.",
self.chain_type
),
}
}

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
11 changes: 8 additions & 3 deletions framework/snippets/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub struct Interactor {
}

impl Interactor {
pub async fn new(gateway_url: &str) -> Self {
let proxy = GatewayProxy::new(gateway_url.to_string());
pub async fn new(gateway_uri: &str, use_chain_simulator: bool) -> Self {
let proxy = GatewayProxy::new(gateway_uri.to_string(), use_chain_simulator);
let network_config = proxy.get_network_config().await.unwrap();
Self {
proxy,
Expand All @@ -46,8 +46,13 @@ impl Interactor {
}
}

pub fn register_wallet(&mut self, wallet: Wallet) -> Address {
pub async fn register_wallet(&mut self, wallet: Wallet) -> Address {
let address = erdrs_address_to_h256(wallet.address());
self.proxy
.send_user_funds(&Bech32Address::from(&address).to_bech32_string())
.await
.unwrap();

self.sender_map.insert(
address.clone(),
Sender {
Expand Down
Loading
Loading