Skip to content

Commit

Permalink
chain-sim - prototype of integration with adder interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi committed Aug 12, 2024
1 parent d1f19c0 commit 2bbca77
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion contracts/examples/adder/interact/config.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gateway = 'https://devnet-gateway.multiversx.com'
gateway = 'http://localhost:8085'
36 changes: 19 additions & 17 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 @@ -63,10 +65,22 @@ impl AdderInteract {
.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(test_wallets::alice());

let wallet_address = interactor.register_wallet(test_wallets::mike());

interactor
.proxy
.send_user_funds(&Bech32Address::from(&adder_owner_address).to_bech32_string())
.await;

interactor
.proxy
.send_user_funds(&Bech32Address::from(&wallet_address).to_bech32_string())
.await;

interactor.proxy.generate_blocks(20).await;

Self {
interactor,
adder_owner_address: adder_owner_address.into(),
Expand All @@ -75,20 +89,10 @@ impl AdderInteract {
}
}

async fn set_state(&mut self) {
println!("wallet address: {}", self.wallet_address);
self.interactor
.retrieve_account(&self.adder_owner_address)
.await;
self.interactor.retrieve_account(&self.wallet_address).await;
}

async fn deploy(&mut self) {
// warning: multi deploy not yet fully supported
// only works with last deployed address

self.set_state().await;

let new_address = self
.interactor
.tx()
Expand All @@ -104,7 +108,7 @@ impl AdderInteract {
.await;

println!("new address: {new_address}");
self.state.set_adder_address(new_address);
self.state.set_adder_address(new_address.clone());
}

async fn multi_deploy(&mut self, count: usize) {
Expand All @@ -113,7 +117,6 @@ impl AdderInteract {
return;
}

self.set_state().await;
println!("deploying {count} contracts...");

let mut buffer = self.interactor.homogenous_call_buffer();
Expand Down Expand Up @@ -141,7 +144,6 @@ impl AdderInteract {
}

async fn multi_add(&mut self, value: u32, count: usize) {
self.set_state().await;
println!("calling contract {count} times...");

let mut buffer = self.interactor.homogenous_call_buffer();
Expand Down Expand Up @@ -205,7 +207,7 @@ impl AdderInteract {
let response = self
.interactor
.tx()
.from(&self.wallet_address)
.from(&self.adder_owner_address)
.to(self.state.current_adder_address())
.gas(3_000_000)
.typed(adder_proxy::AdderProxy)
Expand Down Expand Up @@ -241,5 +243,5 @@ async fn test() {
basic_interact.deploy().await;
basic_interact.add(1u32).await;

basic_interact.upgrade(7u32).await;
basic_interact.upgrade(5u32).await;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl Interactor {
{
let sc_call_step = sc_call_step.as_mut();
let tx_hash = self.launch_sc_call(sc_call_step).await;
// self.proxy.generate_blocks(20).await;
let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;

sc_call_step.save_response(network_response::parse_tx_response(tx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Interactor {
{
let sc_deploy_step = sc_deploy_step.as_mut();
let tx_hash = self.launch_sc_deploy(sc_deploy_step).await;
// self.proxy.generate_blocks(1).await;
let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;

let addr = sc_deploy_step.tx.from.clone();
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ mod gateway_network;
mod gateway_proxy;
mod gateway_tx;
mod gateway_tx_retrieve;
mod gateway_chain_simulator;

pub use gateway_proxy::GatewayProxy;

pub const MAINNET_GATEWAY: &str = "https://gateway.multiversx.com";
pub const TESTNET_GATEWAY: &str = "https://testnet-gateway.multiversx.com";
pub const DEVNET_GATEWAY: &str = "https://devnet-gateway.multiversx.com";
pub const SIMULATOR_GATEWAY: &str = "http://localhost:8085";

// MetachainShardId will be used to identify a shard ID as metachain
pub const METACHAIN_SHARD_ID: u32 = 0xFFFFFFFF;
32 changes: 32 additions & 0 deletions sdk/core/src/gateway/gateway_chain_simulator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::collections::HashMap;

use super::GatewayProxy;

const SEND_USER_FUNDS_ENDPOINT: &str = "transaction/send-user-funds";
const GENERATE_BLOCKS_FUNDS_ENDPOINT: &str = "simulator/generate-blocks";
const ACCOUNT_DATA: &str = "address/";

impl GatewayProxy {
pub async fn send_user_funds(&self, receiver: &String) {
let mut r = HashMap::new();
r.insert("receiver", receiver);
let endpoint_funds = self.get_endpoint(SEND_USER_FUNDS_ENDPOINT);
let _ = self.client.post(endpoint_funds).json(&r).send().await;
}

pub async fn generate_blocks(&self, number_blocks: u64) {
let url_gen_blocks: String =
format!("{}/{}", GENERATE_BLOCKS_FUNDS_ENDPOINT, number_blocks);
let endpoint_blocks = self.get_endpoint(&url_gen_blocks);
let _ = self.client.post(endpoint_blocks).send().await;
}

pub async fn set_state_chain_sim(&self, address: String) {
let endpoint_funds = self.get_endpoint(&format!("{}{}", ACCOUNT_DATA, address));
let data = self.client.get(endpoint_funds).send().await;
match data {
Ok(d) => println!("{:?}", d.text().await),
Err(_) => todo!(),
}
}
}

0 comments on commit 2bbca77

Please sign in to comment.