Skip to content

Commit

Permalink
ping pong interactor - generate one transaction when wallet is regist…
Browse files Browse the repository at this point in the history
…ered
  • Loading branch information
BiancaIalangi committed Oct 24, 2024
1 parent c22d9fc commit 64cc229
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 51 deletions.
2 changes: 1 addition & 1 deletion contracts/examples/ping-pong-egld/interactor/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Pem files are used for interactions, but shouldn't be committed
# *.pem
*.pem
*.json

# Temporary storage of deployed contract address, so we can preserve the context between executions.
Expand Down

This file was deleted.

39 changes: 18 additions & 21 deletions contracts/examples/ping-pong-egld/interactor/src/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,19 @@ pub async fn ping_pong_egld_cli() {
.await
},
Some(interact_cli::InteractCliCommand::Ping(args)) => {
let sender = interact.ping_pong_owner_address.clone();
interact
.ping(args.cost.unwrap_or_default(), None, None)
.ping(args.cost.unwrap_or_default(), None, &sender)
.await
},
Some(interact_cli::InteractCliCommand::Pong) => interact.pong(None, None).await,
Some(interact_cli::InteractCliCommand::PongAll) => interact.pong_all(None, None).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: ");
Expand Down Expand Up @@ -114,12 +121,8 @@ impl PingPongEgldInteract {
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;

let ping_pong_owner_address = interactor
.register_wallet(Wallet::from_pem_file("ping-pong-owner.pem").unwrap())
.await;
let wallet_address = interactor
.register_wallet(Wallet::from_pem_file("wallet.pem").unwrap())
.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();
Expand All @@ -133,7 +136,6 @@ impl PingPongEgldInteract {
}

pub async fn set_state(&mut self) {
println!("ping pong owner address: {}", self.ping_pong_owner_address);
println!("wallet address: {}", self.wallet_address);
self.interactor
.retrieve_account(&self.ping_pong_owner_address)
Expand Down Expand Up @@ -203,18 +205,13 @@ impl PingPongEgldInteract {
println!("Result: {response:?}");
}

pub async fn ping(
&mut self,
egld_amount: u64,
message: Option<&str>,
sender: Option<&Bech32Address>,
) {
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.unwrap_or(&self.wallet_address))
.from(sender)
.to(self.state.current_ping_pong_egld_address())
.gas(30_000_000u64)
.typed(proxy_ping_pong_egld::PingPongProxy)
Expand All @@ -233,11 +230,11 @@ impl PingPongEgldInteract {
}
}

pub async fn pong(&mut self, message: Option<&str>, sender: Option<&Bech32Address>) {
pub async fn pong(&mut self, message: Option<&str>, sender: &Bech32Address) {
let response = self
.interactor
.tx()
.from(sender.unwrap_or(&self.wallet_address))
.from(sender)
.to(self.state.current_ping_pong_egld_address())
.gas(30_000_000u64)
.typed(proxy_ping_pong_egld::PingPongProxy)
Expand All @@ -255,11 +252,11 @@ impl PingPongEgldInteract {
}
}

pub async fn pong_all(&mut self, message: Option<String>, sender: Option<&Bech32Address>) {
pub async fn pong_all(&mut self, message: Option<String>, sender: &Bech32Address) {
let response = self
.interactor
.tx()
.from(sender.unwrap_or(&self.wallet_address))
.from(sender)
.to(self.state.current_ping_pong_egld_address())
.gas(30_000_000u64)
.typed(proxy_ping_pong_egld::PingPongProxy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use ping_pong_egld_interact::{Config, PingPongEgldInteract};
#[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
Expand All @@ -22,7 +25,11 @@ async fn test_ping_pong_egld() {
.await;

interact
.ping(0u64, Some("the payment must match the fixed sum"), None)
.ping(
0u64,
Some("the payment must match the fixed sum"),
&wallet_address,
)
.await;

// test_ping_inactive_contracts
Expand All @@ -40,7 +47,7 @@ async fn test_ping_pong_egld() {
.await;

interact
.ping(1u64, Some("smart contract not active yet"), None)
.ping(1u64, Some("smart contract not active yet"), &wallet_address)
.await;

// test_ping_passed_deadline
Expand All @@ -57,7 +64,9 @@ async fn test_ping_pong_egld() {
)
.await;

interact.ping(1u64, Some("deadline has passed"), None).await;
interact
.ping(1u64, Some("deadline has passed"), &wallet_address)
.await;

// test_ping_max_funds
let ping_amount = 10u64;
Expand All @@ -74,7 +83,7 @@ async fn test_ping_pong_egld() {
.await;

interact
.ping(10u64, Some("smart contract full"), None)
.ping(10u64, Some("smart contract full"), &wallet_address)
.await;

// test ping
Expand All @@ -91,16 +100,18 @@ async fn test_ping_pong_egld() {
)
.await;

interact.ping(1u64, None, None).await;
interact.ping(1u64, Some("can only ping once"), None).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"), None)
.pong(Some("can't withdraw before deadline"), &wallet_address)
.await;

interact.pong(None, None).await;
interact.pong(None, &wallet_address).await;

// test_pong_all
let ping_amount = 1u64;
Expand All @@ -116,19 +127,12 @@ async fn test_ping_pong_egld() {
)
.await;

interact
.ping(1u64, None, Some(&interact.ping_pong_owner_address.clone()))
.await;
interact.ping(1u64, None, &ping_pong_owner_address).await;

interact
.ping(1u64, None, Some(&interact.wallet_address.clone()))
.await;
interact.ping(1u64, None, &wallet_address).await;

interact.pong_all(None, None).await;
interact.pong_all(None, &ping_pong_owner_address).await;
interact
.pong(
Some("already withdrawn"),
Some(&interact.wallet_address.clone()),
)
.pong(Some("already withdrawn"), &wallet_address)
.await;
}
5 changes: 0 additions & 5 deletions contracts/examples/ping-pong-egld/interactor/wallet.pem

This file was deleted.

1 change: 1 addition & 0 deletions framework/snippets-base/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 64cc229

Please sign in to comment.