diff --git a/Cargo.lock b/Cargo.lock index 9823cd2820..ad7b63aba3 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -2948,6 +2948,18 @@ dependencies = [ "multiversx-sc-scenario", ] +[[package]] +name = "ping-pong-egld-interact" +version = "0.0.0" +dependencies = [ + "clap", + "multiversx-sc-snippets", + "ping-pong-egld", + "serde", + "tokio", + "toml", +] + [[package]] name = "ping-pong-egld-meta" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index 826bef3c0f..a1547c7726 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -103,6 +103,7 @@ members = [ "contracts/examples/proxy-pause/meta", "contracts/examples/ping-pong-egld", "contracts/examples/ping-pong-egld/meta", + "contracts/examples/ping-pong-egld/interactor", "contracts/examples/ping-pong-egld/dapp", "contracts/examples/rewards-distribution", "contracts/examples/rewards-distribution/meta", diff --git a/contracts/examples/ping-pong-egld/interactor/.gitignore b/contracts/examples/ping-pong-egld/interactor/.gitignore new file mode 100644 index 0000000000..ac5c74ec15 --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/.gitignore @@ -0,0 +1,9 @@ +# Pem files are used for interactions, but shouldn't be committed +*.pem +*.json + +# Temporary storage of deployed contract address, so we can preserve the context between executions. +state.toml + +# Trace file of interactor tooling +interactor_trace.scen.json diff --git a/contracts/examples/ping-pong-egld/interactor/Cargo.toml b/contracts/examples/ping-pong-egld/interactor/Cargo.toml new file mode 100644 index 0000000000..a232d0317d --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "ping-pong-egld-interact" +version = "0.0.0" +authors = ["Ovidiu Stinga "] +edition = "2021" +publish = false + +[[bin]] +name = "ping-pong-egld-interact" +path = "src/interact_main.rs" + +[lib] +path = "src/interact.rs" + +[dependencies] +clap = { version = "4.4.7", features = ["derive"] } +serde = { version = "1.0", features = ["derive"] } +toml = "0.8.6" +tokio = { version = "1.24" } + +[dependencies.ping-pong-egld] +path = ".." + +[dependencies.multiversx-sc-snippets] +version = "0.53.2" +path = "../../../../framework/snippets" + +[features] +chain-simulator-tests = [] diff --git a/contracts/examples/ping-pong-egld/interactor/config.toml b/contracts/examples/ping-pong-egld/interactor/config.toml new file mode 100644 index 0000000000..aca748706a --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/config.toml @@ -0,0 +1,5 @@ +chain_type = 'simulator' +gateway_uri = 'http://localhost:8085' + +# chain_type = 'real' +# gateway_uri = 'https://devnet-gateway.multiversx.com' diff --git a/contracts/examples/ping-pong-egld/interactor/src/interact.rs b/contracts/examples/ping-pong-egld/interactor/src/interact.rs new file mode 100644 index 0000000000..2206564a11 --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/src/interact.rs @@ -0,0 +1,372 @@ +mod interact_cli; +mod interact_config; +mod interact_state; + +use crate::interact_state::State; +use clap::Parser; +pub use interact_config::Config; +use ping_pong_egld::proxy_ping_pong_egld::{self, ContractState, UserStatus}; + +use multiversx_sc_snippets::imports::*; + +const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json"; + +const PING_PONG_CODE: MxscPath = MxscPath::new("../output/ping-pong-egld.mxsc.json"); + +pub async fn ping_pong_egld_cli() { + env_logger::init(); + + let config = Config::load_config(); + + let mut interact = PingPongEgldInteract::init(config).await; + + let cli = interact_cli::InteractCli::parse(); + match &cli.command { + Some(interact_cli::InteractCliCommand::Deploy(args)) => { + interact + .deploy( + args.ping_amount.clone(), + args.duration_in_seconds, + args.opt_activation_timestamp, + OptionalValue::from(args.max_funds.clone()), + ) + .await; + }, + Some(interact_cli::InteractCliCommand::Upgrade(args)) => { + interact + .upgrade( + args.ping_amount.clone(), + args.duration_in_seconds, + args.opt_activation_timestamp, + OptionalValue::from(args.max_funds.clone()), + ) + .await + }, + Some(interact_cli::InteractCliCommand::Ping(args)) => { + let sender = interact.ping_pong_owner_address.clone(); + interact + .ping(args.cost.unwrap_or_default(), None, &sender) + .await + }, + Some(interact_cli::InteractCliCommand::Pong) => { + let sender = interact.ping_pong_owner_address.clone(); + interact.pong(None, &sender).await; + }, + Some(interact_cli::InteractCliCommand::PongAll) => { + let sender = interact.ping_pong_owner_address.clone(); + interact.pong_all(None, &sender).await; + }, + Some(interact_cli::InteractCliCommand::GetUserAddresses) => { + let user_addresses = interact.get_user_addresses().await; + println!("User addresses: "); + for address in user_addresses { + print!("{address} "); + } + }, + Some(interact_cli::InteractCliCommand::GetContractState) => { + let contract_state = interact.get_contract_state().await; + println!("Contract state: ping_amount -> {:#?} | deadline -> {:#?} | activation_timestamp -> {:#?} | max_funds -> {:#?} | pong_all_last_user -> {:#?}", + contract_state.ping_amount, + contract_state.deadline, + contract_state.activation_timestamp, + contract_state.max_funds, + contract_state.pong_all_last_user); + }, + Some(interact_cli::InteractCliCommand::GetPingAmount) => { + let ping_amount = interact.get_ping_amount().await; + println!("Ping amount: {}", ping_amount); + }, + Some(interact_cli::InteractCliCommand::GetDeadline) => { + let deadline = interact.get_deadline().await; + println!("Deadline: {}", deadline); + }, + Some(interact_cli::InteractCliCommand::GetActivationTimestamp) => { + let activation_timestamp = interact.get_activation_timestamp().await; + println!("Activation timestamp: {}", activation_timestamp); + }, + Some(interact_cli::InteractCliCommand::GetMaxFunds) => { + let max_funds = interact.get_max_funds().await; + match max_funds { + Some(funds) => println!("Max funds: {}", funds), + None => println!("Max funds: none"), + } + }, + Some(interact_cli::InteractCliCommand::GetUserStatus(args)) => { + let user_status = interact.get_user_status(args.id).await; + match user_status { + UserStatus::New => println!("User status: unknown"), + UserStatus::Registered => println!("User status: `ping`-ed"), + UserStatus::Withdrawn => println!("User status: `pong`-ed"), + } + }, + Some(interact_cli::InteractCliCommand::PongAllLastUser) => { + let pong_all_last_user = interact.pong_all_last_user().await; + println!("Pong all last user: {pong_all_last_user}"); + }, + None => {}, + } +} + +pub struct PingPongEgldInteract { + pub interactor: Interactor, + pub ping_pong_owner_address: Bech32Address, + pub wallet_address: Bech32Address, + pub state: State, +} + +impl PingPongEgldInteract { + pub async fn init(config: Config) -> Self { + let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) + .await + .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) + .await; + + let ping_pong_owner_address = interactor.register_wallet(test_wallets::eve()).await; + let wallet_address = interactor.register_wallet(test_wallets::mallory()).await; + + // generate blocks until ESDTSystemSCAddress is enabled + interactor.generate_blocks_until_epoch(1).await.unwrap(); + + Self { + interactor, + ping_pong_owner_address: ping_pong_owner_address.into(), + wallet_address: wallet_address.into(), + state: State::load_state(), + } + } + + pub async fn set_state(&mut self) { + println!("wallet address: {}", self.wallet_address); + self.interactor + .retrieve_account(&self.ping_pong_owner_address) + .await; + self.interactor.retrieve_account(&self.wallet_address).await; + } + + pub async fn deploy( + &mut self, + ping_amount: RustBigUint, + duration_in_seconds: u64, + opt_activation_timestamp: Option, + max_funds: OptionalValue, + ) -> (u64, String) { + self.set_state().await; + + let (new_address, status, message) = self + .interactor + .tx() + .from(&self.ping_pong_owner_address) + .gas(30_000_000u64) + .typed(proxy_ping_pong_egld::PingPongProxy) + .init( + ping_amount, + duration_in_seconds, + opt_activation_timestamp, + max_funds, + ) + .code(PING_PONG_CODE) + .returns(ReturnsNewBech32Address) + .returns(ReturnsStatus) + .returns(ReturnsMessage) + .run() + .await; + + println!("new address: {new_address}"); + self.state.set_ping_pong_egld_address(new_address); + + (status, message) + } + + pub async fn upgrade( + &mut self, + ping_amount: RustBigUint, + duration_in_seconds: u64, + opt_activation_timestamp: Option, + max_funds: OptionalValue, + ) { + let response = self + .interactor + .tx() + .to(self.state.current_ping_pong_egld_address()) + .from(&self.wallet_address) + .gas(30_000_000u64) + .typed(proxy_ping_pong_egld::PingPongProxy) + .upgrade( + ping_amount, + duration_in_seconds, + opt_activation_timestamp, + max_funds, + ) + .code(PING_PONG_CODE) + .returns(ReturnsNewAddress) + .run() + .await; + + println!("Result: {response:?}"); + } + + pub async fn ping(&mut self, egld_amount: u64, message: Option<&str>, sender: &Bech32Address) { + let _data: IgnoreValue = IgnoreValue; + + let response = self + .interactor + .tx() + .from(sender) + .to(self.state.current_ping_pong_egld_address()) + .gas(30_000_000u64) + .typed(proxy_ping_pong_egld::PingPongProxy) + .ping(_data) + .egld(egld_amount) + .returns(ReturnsHandledOrError::new()) + .run() + .await; + + match response { + Ok(_) => println!("Ping successful!"), + Err(err) => { + println!("Ping failed with message: {}", err.message); + assert_eq!(message.unwrap_or_default(), err.message); + }, + } + } + + pub async fn pong(&mut self, message: Option<&str>, sender: &Bech32Address) { + let response = self + .interactor + .tx() + .from(sender) + .to(self.state.current_ping_pong_egld_address()) + .gas(30_000_000u64) + .typed(proxy_ping_pong_egld::PingPongProxy) + .pong() + .returns(ReturnsHandledOrError::new()) + .run() + .await; + + match response { + Ok(_) => println!("Pong successful!"), + Err(err) => { + println!("Pong failed with message: {}", err.message); + assert_eq!(message.unwrap_or_default(), err.message); + }, + } + } + + pub async fn pong_all(&mut self, message: Option, sender: &Bech32Address) { + let response = self + .interactor + .tx() + .from(sender) + .to(self.state.current_ping_pong_egld_address()) + .gas(30_000_000u64) + .typed(proxy_ping_pong_egld::PingPongProxy) + .pong_all() + .returns(ReturnsHandledOrError::new()) + .run() + .await; + + match response { + Ok(_) => println!("Pong All successful!"), + Err(err) => { + println!("Pong All failed with message: {}", err.message); + assert_eq!(message.unwrap_or_default(), err.message); + }, + } + } + + pub async fn get_user_addresses(&mut self) -> Vec { + let response = self + .interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .get_user_addresses() + .returns(ReturnsResult) + .run() + .await; + + let mut response_vec: Vec = Vec::new(); + for r in response.to_vec().into_vec() { + response_vec.push(r.as_managed_buffer().to_string()); + } + + response_vec + } + + pub async fn get_contract_state(&mut self) -> ContractState { + self.interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .get_contract_state() + .returns(ReturnsResult) + .run() + .await + } + + pub async fn get_ping_amount(&mut self) -> RustBigUint { + self.interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .ping_amount() + .returns(ReturnsResultUnmanaged) + .run() + .await + } + + pub async fn get_deadline(&mut self) -> u64 { + self.interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .deadline() + .returns(ReturnsResultUnmanaged) + .run() + .await + } + + pub async fn get_activation_timestamp(&mut self) -> u64 { + self.interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .activation_timestamp() + .returns(ReturnsResultUnmanaged) + .run() + .await + } + + pub async fn get_max_funds(&mut self) -> Option { + self.interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .max_funds() + .returns(ReturnsResultUnmanaged) + .run() + .await + } + + pub async fn get_user_status(&mut self, user_id: usize) -> UserStatus { + self.interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .user_status(user_id) + .returns(ReturnsResultUnmanaged) + .run() + .await + } + + pub async fn pong_all_last_user(&mut self) -> usize { + self.interactor + .query() + .to(self.state.current_ping_pong_egld_address()) + .typed(proxy_ping_pong_egld::PingPongProxy) + .pong_all_last_user() + .returns(ReturnsResultUnmanaged) + .run() + .await + } +} diff --git a/contracts/examples/ping-pong-egld/interactor/src/interact_cli.rs b/contracts/examples/ping-pong-egld/interactor/src/interact_cli.rs new file mode 100644 index 0000000000..700f6949f5 --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/src/interact_cli.rs @@ -0,0 +1,81 @@ +use clap::{Args, Parser, Subcommand}; +use multiversx_sc_snippets::imports::RustBigUint; + +/// Ping Pong Interact CLI +#[derive(Default, PartialEq, Eq, Debug, Parser)] +#[command(version, about)] +#[command(propagate_version = true)] +pub struct InteractCli { + #[command(subcommand)] + pub command: Option, +} + +/// Ping Pong Interact CLI Commands +#[derive(Clone, PartialEq, Eq, Debug, Subcommand)] +pub enum InteractCliCommand { + #[command(name = "deploy", about = "Deploy contract.")] + Deploy(DeployArgs), + #[command(name = "upgrade", about = "Upgrade contract.")] + Upgrade(DeployArgs), + #[command( + name = "ping", + about = "User sends some EGLD to be locked in the contract for a period of time." + )] + Ping(PingArgs), + #[command(name = "pong", about = "User can take back funds from the contract.")] + Pong, + #[command(name = "pong-all", about = "Send back funds to all users who pinged.")] + PongAll, + #[command( + name = "user-addresses", + about = "Lists the addresses of all users that have `ping`-ed in the order they have `ping`-ed." + )] + GetUserAddresses, + #[command(name = "contract-state", about = "Returns the current contract state.")] + GetContractState, + #[command(name = "ping-amount", about = "Returns the ping amount.")] + GetPingAmount, + #[command(name = "deadline", about = "Return deadline.")] + GetDeadline, + #[command( + name = "activation-timestamp", + about = "Block timestamp of the block where the contract got activated. If not specified in the constructor it is the the deploy block timestamp." + )] + GetActivationTimestamp, + #[command(name = "max-funds", about = "Optional funding cap.")] + GetMaxFunds, + #[command(name = "user-status", about = "State of user funds.")] + GetUserStatus(UserStatusArgs), + #[command( + name = "pong-all-last-user", + about = "`pongAll` status, the last user to be processed. 0 if never called `pongAll` or `pongAll` completed." + )] + PongAllLastUser, +} + +#[derive(Default, Clone, PartialEq, Eq, Debug, Args)] +pub struct DeployArgs { + #[arg(short = 'p', long = "ping-amount")] + pub ping_amount: RustBigUint, + + #[arg(short = 'd', long = "duration-in-seconds")] + pub duration_in_seconds: u64, + + #[arg(short = 'a', long = "activation-timestamp")] + pub opt_activation_timestamp: Option, + + #[arg(short = 'm', long = "max-funds")] + pub max_funds: Option, +} + +#[derive(Default, Clone, PartialEq, Eq, Debug, Args)] +pub struct PingArgs { + #[arg(short = 'c', long = "cost", default_value = "50000000000000000")] + pub cost: Option, +} + +#[derive(Default, Clone, PartialEq, Eq, Debug, Args)] +pub struct UserStatusArgs { + #[arg(short = 'i')] + pub id: usize, +} diff --git a/contracts/examples/ping-pong-egld/interactor/src/interact_config.rs b/contracts/examples/ping-pong-egld/interactor/src/interact_config.rs new file mode 100644 index 0000000000..013e16714f --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/src/interact_config.rs @@ -0,0 +1,49 @@ +use serde::Deserialize; +use std::io::Read; + +/// Config file +const CONFIG_FILE: &str = "config.toml"; + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ChainType { + Real, + Simulator, +} + +/// Ping Pong Interact configuration +#[derive(Debug, Deserialize)] +pub struct Config { + pub gateway_uri: String, + pub chain_type: ChainType, +} + +impl Config { + // Deserializes config from file + pub fn load_config() -> Self { + let mut file = std::fs::File::open(CONFIG_FILE).unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + toml::from_str(&content).unwrap() + } + + pub fn chain_simulator_config() -> Self { + Config { + gateway_uri: "http://localhost:8085".to_owned(), + chain_type: ChainType::Simulator, + } + } + + // 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, + } + } +} diff --git a/contracts/examples/ping-pong-egld/interactor/src/interact_main.rs b/contracts/examples/ping-pong-egld/interactor/src/interact_main.rs new file mode 100644 index 0000000000..1e090c46a4 --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/src/interact_main.rs @@ -0,0 +1,6 @@ +extern crate ping_pong_egld_interact; + +#[tokio::main] +pub async fn main() { + ping_pong_egld_interact::ping_pong_egld_cli().await; +} diff --git a/contracts/examples/ping-pong-egld/interactor/src/interact_state.rs b/contracts/examples/ping-pong-egld/interactor/src/interact_state.rs new file mode 100644 index 0000000000..38fb3d320c --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/src/interact_state.rs @@ -0,0 +1,50 @@ +use multiversx_sc_snippets::imports::*; +use serde::{Deserialize, Serialize}; +use std::{ + io::{Read, Write}, + path::Path, +}; + +/// State file +const STATE_FILE: &str = "state.toml"; + +/// Multisig Interact state +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct State { + ping_pong_egld_address: Option, +} + +impl State { + // Deserializes state from file + pub fn load_state() -> Self { + if Path::new(STATE_FILE).exists() { + let mut file = std::fs::File::open(STATE_FILE).unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + toml::from_str(&content).unwrap() + } else { + Self::default() + } + } + + /// Sets the ping pong address + pub fn set_ping_pong_egld_address(&mut self, address: Bech32Address) { + self.ping_pong_egld_address = Some(address); + } + + /// Returns the ping pong contract + pub fn current_ping_pong_egld_address(&self) -> &Bech32Address { + self.ping_pong_egld_address + .as_ref() + .expect("no known ping pong contract, deploy first") + } +} + +impl Drop for State { + // Serializes state to file + fn drop(&mut self) { + let mut file = std::fs::File::create(STATE_FILE).unwrap(); + file.write_all(toml::to_string(self).unwrap().as_bytes()) + .unwrap(); + } +} diff --git a/contracts/examples/ping-pong-egld/interactor/tests/interact_cs_test.rs b/contracts/examples/ping-pong-egld/interactor/tests/interact_cs_test.rs new file mode 100644 index 0000000000..f25bcfde12 --- /dev/null +++ b/contracts/examples/ping-pong-egld/interactor/tests/interact_cs_test.rs @@ -0,0 +1,138 @@ +use multiversx_sc_snippets::imports::RustBigUint; +use ping_pong_egld_interact::{Config, PingPongEgldInteract}; + +#[tokio::test] +#[cfg_attr(not(feature = "chain-simulator-tests"), ignore)] +async fn test_ping_pong_egld() { + let mut interact = PingPongEgldInteract::init(Config::chain_simulator_config()).await; + let wallet_address = interact.wallet_address.clone(); + let ping_pong_owner_address = interact.ping_pong_owner_address.clone(); + + let ping_amount = 1u64; + + // test_ping_unmatched_amount + let duration_in_seconds = 5u64; + let opt_activation_timestamp = 2u64; + let max_funds = 100_000u64; + + interact + .deploy( + ping_amount.into(), + duration_in_seconds, + Some(opt_activation_timestamp), + multiversx_sc_snippets::imports::OptionalValue::Some(max_funds.into()), + ) + .await; + + interact + .ping( + 0u64, + Some("the payment must match the fixed sum"), + &wallet_address, + ) + .await; + + // test_ping_inactive_contracts + let duration_in_seconds = 5u64; + let opt_activation_timestamp = 2_000_000_000u64; + let max_funds = 100_000u64; + + interact + .deploy( + ping_amount.into(), + duration_in_seconds, + Some(opt_activation_timestamp), + multiversx_sc_snippets::imports::OptionalValue::Some(max_funds.into()), + ) + .await; + + interact + .ping(1u64, Some("smart contract not active yet"), &wallet_address) + .await; + + // test_ping_passed_deadline + let duration_in_seconds = 5u64; + let opt_activation_timestamp = 2u64; + let max_funds = 100_000u64; + + interact + .deploy( + ping_amount.into(), + duration_in_seconds, + Some(opt_activation_timestamp), + multiversx_sc_snippets::imports::OptionalValue::Some(max_funds.into()), + ) + .await; + + interact + .ping(1u64, Some("deadline has passed"), &wallet_address) + .await; + + // test_ping_max_funds + let ping_amount = 10u64; + let duration_in_seconds = 30000u64; + let max_funds = 10u64; + + interact + .deploy( + ping_amount.into(), + duration_in_seconds, + None, + multiversx_sc_snippets::imports::OptionalValue::Some(max_funds.into()), + ) + .await; + + interact + .ping(10u64, Some("smart contract full"), &wallet_address) + .await; + + // test ping + let ping_amount = 1u64; + let duration_in_seconds = 20u64; + let max_funds = 100_000u64; + + interact + .deploy( + ping_amount.into(), + duration_in_seconds, + None, + multiversx_sc_snippets::imports::OptionalValue::Some(max_funds.into()), + ) + .await; + + interact.ping(1u64, None, &wallet_address).await; + interact + .ping(1u64, Some("can only ping once"), &wallet_address) + .await; + + assert_eq!(interact.get_ping_amount().await, RustBigUint::from(1u64)); + + interact + .pong(Some("can't withdraw before deadline"), &wallet_address) + .await; + + interact.pong(None, &wallet_address).await; + + // test_pong_all + let ping_amount = 1u64; + let duration_in_seconds = 18u64; + let max_funds = 100_000u64; + + interact + .deploy( + ping_amount.into(), + duration_in_seconds, + None, + multiversx_sc_snippets::imports::OptionalValue::Some(max_funds.into()), + ) + .await; + + interact.ping(1u64, None, &ping_pong_owner_address).await; + + interact.ping(1u64, None, &wallet_address).await; + + interact.pong_all(None, &ping_pong_owner_address).await; + interact + .pong(Some("already withdrawn"), &wallet_address) + .await; +} diff --git a/contracts/examples/ping-pong-egld/sc-config.toml b/contracts/examples/ping-pong-egld/sc-config.toml new file mode 100644 index 0000000000..2a4a6ee147 --- /dev/null +++ b/contracts/examples/ping-pong-egld/sc-config.toml @@ -0,0 +1,2 @@ +[[proxy]] +path = "src/ping_pong_egld_proxy.rs" diff --git a/contracts/examples/ping-pong-egld/src/ping_pong.rs b/contracts/examples/ping-pong-egld/src/ping_pong.rs index b2b7aaa6dd..abecb0dbcc 100644 --- a/contracts/examples/ping-pong-egld/src/ping_pong.rs +++ b/contracts/examples/ping-pong-egld/src/ping_pong.rs @@ -2,6 +2,7 @@ use multiversx_sc::imports::*; +pub mod proxy_ping_pong_egld; mod types; use types::{ContractState, UserStatus}; @@ -27,7 +28,7 @@ pub trait PingPong { /// Necessary configuration when deploying: /// `ping_amount` - the exact EGLD amount that needs to be sent when `ping`-ing. /// `duration_in_seconds` - how much time (in seconds) until contract expires. - /// `opt_activation_timestamp` - optionally specify the contract to only actvivate at a later date. + /// `opt_activation_timestamp` - optionally specify the contract to only activate at a later date. /// `max_funds` - optional funding cap, no more funds than this can be added to the contract. #[allow_multiple_var_args] #[init] @@ -245,7 +246,7 @@ pub trait PingPong { fn user_status(&self, user_id: usize) -> SingleValueMapper; /// Part of the `pongAll` status, the last user to be processed. - /// 0 if never called `pongAll` or `pongAll` completed.. + /// 0 if never called `pongAll` or `pongAll` completed. #[view(pongAllLastUser)] #[storage_mapper("pongAllLastUser")] fn pong_all_last_user(&self) -> SingleValueMapper; diff --git a/contracts/examples/ping-pong-egld/src/proxy_ping_pong_egld.rs b/contracts/examples/ping-pong-egld/src/proxy_ping_pong_egld.rs new file mode 100644 index 0000000000..ad8944411b --- /dev/null +++ b/contracts/examples/ping-pong-egld/src/proxy_ping_pong_egld.rs @@ -0,0 +1,263 @@ +// Code generated by the multiversx-sc proxy generator. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +#![allow(dead_code)] +#![allow(clippy::all)] + +use multiversx_sc::proxy_imports::*; + +pub struct PingPongProxy; + +impl TxProxyTrait for PingPongProxy +where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + type TxProxyMethods = PingPongProxyMethods; + + fn proxy_methods(self, tx: Tx) -> Self::TxProxyMethods { + PingPongProxyMethods { wrapped_tx: tx } + } +} + +pub struct PingPongProxyMethods +where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + wrapped_tx: Tx, +} + +#[rustfmt::skip] +impl PingPongProxyMethods +where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + Gas: TxGas, +{ + /// Necessary configuration when deploying: + /// `ping_amount` - the exact EGLD amount that needs to be sent when `ping`-ing. + /// `duration_in_seconds` - how much time (in seconds) until contract expires. + /// `opt_activation_timestamp` - optionally specify the contract to only activate at a later date. + /// `max_funds` - optional funding cap, no more funds than this can be added to the contract. + pub fn init< + Arg0: ProxyArg>, + Arg1: ProxyArg, + Arg2: ProxyArg>, + Arg3: ProxyArg>>, + >( + self, + ping_amount: Arg0, + duration_in_seconds: Arg1, + opt_activation_timestamp: Arg2, + max_funds: Arg3, + ) -> TxTypedDeploy { + self.wrapped_tx + .payment(NotPayable) + .raw_deploy() + .argument(&ping_amount) + .argument(&duration_in_seconds) + .argument(&opt_activation_timestamp) + .argument(&max_funds) + .original_result() + } +} + +#[rustfmt::skip] +impl PingPongProxyMethods +where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + pub fn upgrade< + Arg0: ProxyArg>, + Arg1: ProxyArg, + Arg2: ProxyArg>, + Arg3: ProxyArg>>, + >( + self, + ping_amount: Arg0, + duration_in_seconds: Arg1, + opt_activation_timestamp: Arg2, + max_funds: Arg3, + ) -> TxTypedUpgrade { + self.wrapped_tx + .payment(NotPayable) + .raw_upgrade() + .argument(&ping_amount) + .argument(&duration_in_seconds) + .argument(&opt_activation_timestamp) + .argument(&max_funds) + .original_result() + } +} + +#[rustfmt::skip] +impl PingPongProxyMethods +where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + /// User sends some EGLD to be locked in the contract for a period of time. + /// Optional `_data` argument is ignored. + pub fn ping< + Arg0: ProxyArg, + >( + self, + _data: Arg0, + ) -> TxTypedCall { + self.wrapped_tx + .raw_call("ping") + .argument(&_data) + .original_result() + } + + /// User can take back funds from the contract. + /// Can only be called after expiration. + pub fn pong( + self, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("pong") + .original_result() + } + + /// Send back funds to all users who pinged. + /// Returns + /// - `completed` if everything finished + /// - `interrupted` if run out of gas midway. + /// Can only be called after expiration. + pub fn pong_all( + self, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("pongAll") + .original_result() + } + + /// Lists the addresses of all users that have `ping`-ed, + /// in the order they have `ping`-ed + pub fn get_user_addresses( + self, + ) -> TxTypedCall>> { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getUserAddresses") + .original_result() + } + + /// Returns the current contract state as a struct + /// for faster fetching from external parties + pub fn get_contract_state( + self, + ) -> TxTypedCall> { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getContractState") + .original_result() + } + + pub fn ping_amount( + self, + ) -> TxTypedCall> { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getPingAmount") + .original_result() + } + + pub fn deadline( + self, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getDeadline") + .original_result() + } + + /// Block timestamp of the block where the contract got activated. + /// If not specified in the constructor it is the the deploy block timestamp. + pub fn activation_timestamp( + self, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getActivationTimestamp") + .original_result() + } + + /// Optional funding cap. + pub fn max_funds( + self, + ) -> TxTypedCall>> { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getMaxFunds") + .original_result() + } + + /// State of user funds. + /// 0 - user unknown, never `ping`-ed + /// 1 - `ping`-ed + /// 2 - `pong`-ed + pub fn user_status< + Arg0: ProxyArg, + >( + self, + user_id: Arg0, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getUserStatus") + .argument(&user_id) + .original_result() + } + + /// Part of the `pongAll` status, the last user to be processed. + /// 0 if never called `pongAll` or `pongAll` completed. + pub fn pong_all_last_user( + self, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("pongAllLastUser") + .original_result() + } +} + +#[type_abi] +#[derive(TopEncode, TopDecode, Default)] +pub struct ContractState +where + Api: ManagedTypeApi, +{ + pub ping_amount: BigUint, + pub deadline: u64, + pub activation_timestamp: u64, + pub max_funds: Option>, + pub pong_all_last_user: usize, +} + +#[type_abi] +#[derive(TopEncode, TopDecode, PartialEq, Eq, Clone, Copy)] +pub enum UserStatus { + New, + Registered, + Withdrawn, +} diff --git a/contracts/examples/ping-pong-egld/wasm/Cargo.lock b/contracts/examples/ping-pong-egld/wasm/Cargo.lock index 28555d04bb..207d5b3d9b 100755 --- a/contracts/examples/ping-pong-egld/wasm/Cargo.lock +++ b/contracts/examples/ping-pong-egld/wasm/Cargo.lock @@ -38,12 +38,21 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" +[[package]] +name = "multiversx-chain-core" +version = "0.10.0" +dependencies = [ + "bitflags", + "multiversx-sc-codec", +] + [[package]] name = "multiversx-sc" version = "0.53.2" dependencies = [ "bitflags", "hex-literal", + "multiversx-chain-core", "multiversx-sc-codec", "multiversx-sc-derive", "num-traits", diff --git a/framework/meta-lib/src/tools/panic_report.rs b/framework/meta-lib/src/tools/panic_report.rs index 1be0f7cc56..84ad687447 100644 --- a/framework/meta-lib/src/tools/panic_report.rs +++ b/framework/meta-lib/src/tools/panic_report.rs @@ -30,7 +30,6 @@ impl PanicReport { } if is_panic_without_message_triggered(data_section) { - println!("here"); return Self::WithoutMessage; } diff --git a/framework/snippets-base/src/interactor.rs b/framework/snippets-base/src/interactor.rs index d49ab2cbe8..94da772847 100644 --- a/framework/snippets-base/src/interactor.rs +++ b/framework/snippets-base/src/interactor.rs @@ -55,6 +55,7 @@ where let address = wallet.to_address(); self.send_user_funds(&address).await.unwrap(); + self.generate_blocks(1).await.unwrap(); self.sender_map.insert( address.clone(), Sender { diff --git a/sdk/core/src/data/transaction.rs b/sdk/core/src/data/transaction.rs index 46783cfaea..450057a7b7 100644 --- a/sdk/core/src/data/transaction.rs +++ b/sdk/core/src/data/transaction.rs @@ -134,6 +134,7 @@ pub struct ApiSmartContractResult { pub value: u64, pub receiver: SdkAddress, pub sender: SdkAddress, + #[serde(default)] pub data: String, pub prev_tx_hash: String, pub original_tx_hash: String,