From 7e28fa20a2c963cf1bc2632f8b1a3d9cda3ee916 Mon Sep 17 00:00:00 2001 From: Petru-Vlad Ionescu Date: Wed, 21 Aug 2024 19:30:01 +0300 Subject: [PATCH 1/6] Add interactor tests --- .../src/potlock_interactor_main.rs | 1124 +++++++++++++++-- 1 file changed, 1035 insertions(+), 89 deletions(-) diff --git a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs index 85eba287..b63a7b2d 100644 --- a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs +++ b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs @@ -5,13 +5,26 @@ mod proxy; use multiversx_sc_snippets::imports::*; use multiversx_sc_snippets::sdk; use serde::{Deserialize, Serialize}; +use std::char::MAX; +use std::result; use std::{ io::{Read, Write}, path::Path, }; -const GATEWAY: &str = sdk::gateway::DEVNET_GATEWAY; +const GATEWAY: &str = sdk::gateway::TESTNET_GATEWAY; const STATE_FILE: &str = "state.toml"; +const TOKEN_ID: &str = "VLD-070dac"; +const SECOND_TOKEN_ID: &str = "SCND-620d29"; +const THIRD_TOKEN_ID: &str = "RAND-e3641c"; +const INVALID_TOKEN_ID: &str = "123"; +const FEE_AMOUNT: u64 = 1; +const DONATION_AMOUNT: u64 = 10; +const OWNER_ADDR: &str = "erd1r6f7nfpyzul2tef7gne5h6nx9xqnyt5gehwltlxymnqkztjjzvuqdhderc"; +const SECOND_USER_ADDR: &str = "erd1tjusdv806tuwzllgesljglm7y9jef38wdylkvp85v7a46z9x23us0z5xtr"; +const THIRD_USER_ADDR: &str = "erd1pu4r9rxgn8f7a7gwjchtxjz6y4u3ha7fy93w6r3fjeq26jaqkqjs4ly8fd"; +const MAX_PERCENTAGE: u64 = 10_000; +const BIG_ID: u32 = 1000u32; #[tokio::main] async fn main() { @@ -23,28 +36,28 @@ async fn main() { let mut interact = ContractInteract::new().await; match cmd.as_str() { "deploy" => interact.deploy().await, - "upgrade" => interact.upgrade().await, - "changeFeeForPots" => interact.change_fee_for_pots().await, - "acceptPot" => interact.accept_pot().await, - "removePot" => interact.remove_pot().await, - "acceptApplication" => interact.accept_application().await, - "removeApplication" => interact.remove_application().await, - "rejectDonation" => interact.reject_donation().await, - "distributePotToProjects" => interact.distribute_pot_to_projects().await, - "addPot" => interact.add_pot().await, - "applyForPot" => interact.apply_for_pot().await, - "donateToPot" => interact.donate_to_pot().await, - "donateToProject" => interact.donate_to_project().await, - "getFeeTokenIdentifier" => interact.fee_token_identifier().await, - "getFeeAmount" => interact.fee_amount().await, - "getPotlocks" => interact.potlocks().await, - "getProjects" => interact.projects().await, - "potDonations" => interact.pot_donations().await, - "projectDonations" => interact.project_donations().await, - "isAdmin" => interact.is_admin().await, - "addAdmin" => interact.add_admin().await, - "removeAdmin" => interact.remove_admin().await, - "getAdmins" => interact.admins().await, + // "upgrade" => interact.upgrade().await, + // "changeFeeForPots" => interact.change_fee_for_pots().await, + // "acceptPot" => interact.accept_pot().await, + // "removePot" => interact.remove_pot().await, + // "acceptApplication" => interact.accept_application().await, + // "removeApplication" => interact.remove_application().await, + // "rejectDonation" => interact.reject_donation().await, + // "distributePotToProjects" => interact.distribute_pot_to_projects().await, + // "addPot" => interact.add_pot().await, + // "applyForPot" => interact.apply_for_pot().await, + // "donateToPot" => interact.donate_to_pot().await, + // "donateToProject" => interact.donate_to_project().await, + // "getFeeTokenIdentifier" => interact.fee_token_identifier().await, + // "getFeeAmount" => interact.fee_amount().await, + // "getPotlocks" => interact.potlocks().await, + // "getProjects" => interact.projects().await, + // "potDonations" => interact.pot_donations().await, + // "projectDonations" => interact.project_donations().await, + // "isAdmin" => interact.is_admin().await, + // "addAdmin" => interact.add_admin().await, + // "removeAdmin" => interact.remove_admin().await, + // "getAdmins" => interact.admins().await, _ => panic!("unknown command: {}", &cmd), } } @@ -91,7 +104,9 @@ impl Drop for State { struct ContractInteract { interactor: Interactor, - wallet_address: Address, + owner_address: Address, + second_address: Address, + third_address: Address, contract_code: BytesValue, state: State, } @@ -99,7 +114,12 @@ struct ContractInteract { impl ContractInteract { async fn new() -> Self { let mut interactor = Interactor::new(GATEWAY).await; - let wallet_address = interactor.register_wallet(test_wallets::alice()); + let owner_address = interactor + .register_wallet(Wallet::from_pem_file("wallet1.pem").expect("wallet 1 not found")); + let second_address = interactor + .register_wallet(Wallet::from_pem_file("wallet2.pem").expect("wallet 2 not found")); + let third_address = interactor + .register_wallet(Wallet::from_pem_file("wallet3.pem").expect("wallet 3 not found")); let contract_code = BytesValue::interpret_from( "mxsc:../output/potlock.mxsc.json", @@ -108,20 +128,22 @@ impl ContractInteract { ContractInteract { interactor, - wallet_address, + owner_address, + second_address, + third_address, contract_code, state: State::load_state(), } } async fn deploy(&mut self) { - let admins = MultiValueVec::from(vec![bech32::decode("")]); + let admins = MultiValueVec::from(vec![bech32::decode(THIRD_USER_ADDR)]); let new_address = self .interactor .tx() - .from(&self.wallet_address) - .gas(30_000_000u64) + .from(&self.owner_address) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .init(admins) .code(&self.contract_code) @@ -142,8 +164,8 @@ impl ContractInteract { .interactor .tx() .to(self.state.current_address()) - .from(&self.wallet_address) - .gas(30_000_000u64) + .from(&self.owner_address) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .upgrade() .code(&self.contract_code) @@ -156,16 +178,16 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn change_fee_for_pots(&mut self) { - let token_identifier = TokenIdentifier::from_esdt_bytes(&b""[..]); - let fee = BigUint::::from(0u128); + async fn change_fee_for_pots(&mut self, caller: &Bech32Address, token_id: &str, fee: u128) { + let token_identifier = TokenIdentifier::from_esdt_bytes(token_id); + let fee = BigUint::::from(fee); let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .change_fee_for_pots(token_identifier, fee) .returns(ReturnsResultUnmanaged) @@ -176,15 +198,36 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn accept_pot(&mut self) { - let potlock_id = 0u32; + async fn change_fee_for_pots_fail( + &mut self, + caller: &Bech32Address, + token_id: &str, + fee: u128, + expected_result: ExpectError<'_>, + ) { + let token_identifier = TokenIdentifier::from_esdt_bytes(token_id); + let fee = BigUint::::from(fee); + + self.interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .change_fee_for_pots(token_identifier, fee) + .returns(expected_result) + .prepare_async() + .run() + .await; + } + async fn accept_pot(&mut self, caller: &Bech32Address, potlock_id: u32) { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .accept_pot(potlock_id) .returns(ReturnsResultUnmanaged) @@ -195,15 +238,35 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn remove_pot(&mut self) { - let potlock_id = 0u32; + async fn accept_pot_fail( + &mut self, + caller: &Bech32Address, + potlock_id: u32, + expected_result: ExpectError<'_>, + ) { + let response = self + .interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .accept_pot(potlock_id) + .returns(expected_result) + .prepare_async() + .run() + .await; + + println!("Result: {response:?}"); + } + async fn remove_pot(&mut self, caller: &Bech32Address, potlock_id: u32) { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .remove_pot(potlock_id) .returns(ReturnsResultUnmanaged) @@ -214,15 +277,35 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn accept_application(&mut self) { - let project_id = 0u32; + async fn remove_pot_fail( + &mut self, + caller: &Bech32Address, + potlock_id: u32, + expected_result: ExpectError<'_>, + ) { + let response = self + .interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .remove_pot(potlock_id) + .returns(expected_result) + .prepare_async() + .run() + .await; + + println!("Result: {response:?}"); + } + async fn accept_application(&mut self, caller: &Bech32Address, project_id: u32) { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .accept_application(project_id) .returns(ReturnsResultUnmanaged) @@ -233,15 +316,35 @@ impl ContractInteract { println!("Result: {response:?}"); } + async fn accept_application_fail( + &mut self, + caller: &Bech32Address, + project_id: u32, + expected_result: ExpectError<'_>, + ) { + let response = self + .interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .accept_application(project_id) + .returns(expected_result) + .prepare_async() + .run() + .await; + } + async fn remove_application(&mut self) { let project_id = 0u32; let response = self .interactor .tx() - .from(&self.wallet_address) + .from(&self.owner_address) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .remove_application(project_id) .returns(ReturnsResultUnmanaged) @@ -259,9 +362,9 @@ impl ContractInteract { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(&self.owner_address) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .reject_donation(potlock_id, user) .returns(ReturnsResultUnmanaged) @@ -272,17 +375,18 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn distribute_pot_to_projects(&mut self) { - let potlock_id = 0u32; - let project_percentages = - MultiValueVec::from(vec![MultiValue2::::from((0u32, 0u64))]); - + async fn distribute_pot_to_projects( + &mut self, + caller: &Bech32Address, + potlock_id: u32, + project_percentages: MultiValueVec>, + ) { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .distribute_pot_to_projects(potlock_id, project_percentages) .returns(ReturnsResultUnmanaged) @@ -293,10 +397,33 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn add_pot(&mut self) { - let token_id = String::new(); + async fn distribute_pot_to_projects_fail( + &mut self, + caller: &Bech32Address, + potlock_id: u32, + project_percentages: MultiValueVec>, + expected_result: ExpectError<'_>, + ) { + let response = self + .interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .distribute_pot_to_projects(potlock_id, project_percentages) + .returns(expected_result) + .prepare_async() + .run() + .await; + + println!("Result: {response:?}"); + } + + async fn add_pot(&mut self, caller: &Bech32Address, token_id: &str, fee: u128) { + let token_id = token_id.to_string(); let token_nonce = 0u64; - let token_amount = BigUint::::from(0u128); + let token_amount = BigUint::::from(fee); let name = ManagedBuffer::new_from_bytes(&b""[..]); let description = ManagedBuffer::new_from_bytes(&b""[..]); @@ -304,9 +431,9 @@ impl ContractInteract { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .add_pot(name, description) .payment(( @@ -322,17 +449,51 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn apply_for_pot(&mut self) { - let potlock_id = 0u32; + async fn add_pot_fail( + &mut self, + caller: &Bech32Address, + token_id: &str, + fee: u128, + expected_result: ExpectError<'_>, + ) { + let token_id = token_id.to_string(); + let token_nonce = 0u64; + let token_amount = BigUint::::from(fee); + + let name = ManagedBuffer::new_from_bytes(&b""[..]); + let description = ManagedBuffer::new_from_bytes(&b""[..]); + + let response = self + .interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .add_pot(name, description) + .payment(( + TokenIdentifier::from(token_id.as_str()), + token_nonce, + token_amount, + )) + .returns(expected_result) + .prepare_async() + .run() + .await; + + println!("Result: {response:?}"); + } + + async fn apply_for_pot(&mut self, caller: &Bech32Address, potlock_id: u32) { let project_name = ManagedBuffer::new_from_bytes(&b""[..]); let description = ManagedBuffer::new_from_bytes(&b""[..]); let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .apply_for_pot(potlock_id, project_name, description) .returns(ReturnsResultUnmanaged) @@ -343,19 +504,23 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn donate_to_pot(&mut self) { - let token_id = String::new(); + async fn donate_to_pot( + &mut self, + caller: &Bech32Address, + potlock_id: u32, + token_id: &str, + amount: u128, + ) { + let token_id = token_id.to_string(); let token_nonce = 0u64; - let token_amount = BigUint::::from(0u128); - - let potlock_id = 0u32; + let token_amount = BigUint::::from(amount); let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .donate_to_pot(potlock_id) .payment(( @@ -371,19 +536,56 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn donate_to_project(&mut self) { - let token_id = String::new(); + async fn donate_to_pot_fail( + &mut self, + caller: &Bech32Address, + potlock_id: u32, + token_id: &str, + amount: u128, + expected_result: ExpectError<'_>, + ) { + let token_id = token_id.to_string(); let token_nonce = 0u64; - let token_amount = BigUint::::from(0u128); + let token_amount = BigUint::::from(amount); - let project_id = 0u32; + let response = self + .interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .donate_to_pot(potlock_id) + .payment(( + TokenIdentifier::from(token_id.as_str()), + token_nonce, + token_amount, + )) + .returns(expected_result) + .prepare_async() + .run() + .await; + + println!("Result: {response:?}"); + } + + async fn donate_to_project( + &mut self, + caller: &Bech32Address, + project_id: u32, + token_id: &str, + amount: u128, + ) { + let token_id = token_id.to_string(); + let token_nonce = 0u64; + let token_amount = BigUint::::from(amount); let response = self .interactor .tx() - .from(&self.wallet_address) + .from(caller) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .donate_to_project(project_id) .payment(( @@ -399,7 +601,40 @@ impl ContractInteract { println!("Result: {response:?}"); } - async fn fee_token_identifier(&mut self) { + async fn donate_to_project_fail( + &mut self, + caller: &Bech32Address, + project_id: u32, + token_id: &str, + amount: u128, + expected_result: ExpectError<'_>, + ) { + let token_id = token_id.to_string(); + let token_nonce = 0u64; + let token_amount = BigUint::::from(amount); + + let response = self + .interactor + .tx() + .from(caller) + .to(self.state.current_address()) + .gas(70_000_000u64) + .typed(proxy::PotlockProxy) + .donate_to_project(project_id) + .payment(( + TokenIdentifier::from(token_id.as_str()), + token_nonce, + token_amount, + )) + .returns(expected_result) + .prepare_async() + .run() + .await; + + println!("Result: {response:?}"); + } + + async fn fee_token_identifier(&mut self) -> String { let result_value = self .interactor .query() @@ -411,10 +646,10 @@ impl ContractInteract { .run() .await; - println!("Result: {result_value:?}"); + result_value.to_string() } - async fn fee_amount(&mut self) { + async fn fee_amount(&mut self) -> RustBigUint { let result_value = self .interactor .query() @@ -426,7 +661,7 @@ impl ContractInteract { .run() .await; - println!("Result: {result_value:?}"); + result_value } async fn potlocks(&mut self) { @@ -516,9 +751,9 @@ impl ContractInteract { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(&self.owner_address) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .add_admin(address) .returns(ReturnsResultUnmanaged) @@ -535,9 +770,9 @@ impl ContractInteract { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(&self.owner_address) .to(self.state.current_address()) - .gas(30_000_000u64) + .gas(70_000_000u64) .typed(proxy::PotlockProxy) .remove_admin(address) .returns(ReturnsResultUnmanaged) @@ -563,3 +798,714 @@ impl ContractInteract { println!("Result: {result_value:?}"); } } + +#[tokio::test] +async fn test_deploy_and_config() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; +} + +#[tokio::test] +async fn test_add_pot() { + let mut interact = ContractInteract::new().await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; +} + +#[tokio::test] +async fn test_accept_pot() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; +} + +#[tokio::test] +async fn test_remove_pot() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .remove_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; +} + +#[tokio::test] +async fn test_donate_to_pot() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .donate_to_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ) + .await; +} + +#[tokio::test] +async fn test_accept_application() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .apply_for_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .accept_application( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; +} + +#[tokio::test] +async fn test_donate_to_project() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .apply_for_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .accept_application( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .donate_to_project( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ) + .await; +} + +#[tokio::test] +async fn test_distribute_pot_to_projects() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .donate_to_pot( + &Bech32Address::from_bech32_string(THIRD_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ) + .await; + + interact + .apply_for_pot( + &Bech32Address::from_bech32_string(THIRD_USER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .accept_application( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + let project_percentages = MultiValueVec::from(vec![MultiValue2::from((1u32, MAX_PERCENTAGE))]); + + interact + .distribute_pot_to_projects( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + project_percentages, + ) + .await; +} + +#[tokio::test] +async fn test_donate_to_pot_twice_with_same_token() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .donate_to_pot( + &Bech32Address::from_bech32_string(THIRD_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ) + .await; + + interact + .donate_to_pot( + &Bech32Address::from_bech32_string(THIRD_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + (DONATION_AMOUNT + 1).into(), + ) + .await; +} + +#[tokio::test] +async fn test_multiple_change_fee_for_pots() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + let fee = interact.fee_amount().await; + let token_id = interact.fee_token_identifier().await; + + assert_eq!(fee, FEE_AMOUNT.into()); + assert_eq!(token_id, TOKEN_ID.to_string()); + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + SECOND_TOKEN_ID, + (FEE_AMOUNT + 1).into(), + ) + .await; + + let fee = interact.fee_amount().await; + let token_id = interact.fee_token_identifier().await; + + assert_eq!(fee, FEE_AMOUNT.into()); + assert_eq!(token_id, TOKEN_ID.to_string()); +} + +#[tokio::test] +async fn test_change_fee_for_pots_non_admin() { + let mut interact = ContractInteract::new().await; + + interact + .change_fee_for_pots_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + INVALID_TOKEN_ID, + FEE_AMOUNT.into(), + ExpectError(4, "Endpoint can only be called by admins"), + ) + .await; +} + +#[tokio::test] +async fn test_accept_pot_non_admin() { + let mut interact = ContractInteract::new().await; + + interact + .accept_pot_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + ExpectError(4, "Endpoint can only be called by admins"), + ) + .await; +} + +#[tokio::test] +async fn test_remove_pot_non_admin() { + let mut interact = ContractInteract::new().await; + + interact + .remove_pot_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + ExpectError(4, "Endpoint can only be called by admins"), + ) + .await; +} + +#[tokio::test] +async fn test_accept_application_non_admin() { + let mut interact = ContractInteract::new().await; + + interact + .accept_application_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + ExpectError(4, "Endpoint can only be called by admins"), + ) + .await; +} + +#[tokio::test] +async fn test_distribute_pot_to_projects_non_admin() { + let mut interact = ContractInteract::new().await; + + let project_percentages = MultiValueVec::from(vec![MultiValue2::from((1u32, MAX_PERCENTAGE))]); + + interact + .distribute_pot_to_projects_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + project_percentages, + ExpectError(4, "Endpoint can only be called by admins"), + ) + .await; +} + +#[tokio::test] +async fn test_distribute_pot_to_projects_more_than_max_percent() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .donate_to_pot( + &Bech32Address::from_bech32_string(THIRD_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ) + .await; + + interact + .apply_for_pot( + &Bech32Address::from_bech32_string(THIRD_USER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .accept_application( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + let project_percentages = + MultiValueVec::from(vec![MultiValue2::from((1u32, MAX_PERCENTAGE + 1))]); + + interact + .distribute_pot_to_projects_fail( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + project_percentages, + ExpectError(4, "Total percentages more than 100%"), + ) + .await; +} + +#[tokio::test] +async fn test_donate_to_project_with_different_token() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .apply_for_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .accept_application( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .donate_to_project( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + SECOND_TOKEN_ID, + DONATION_AMOUNT.into(), + ) + .await; + + interact + .donate_to_project_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ExpectError(4, "Already made a payment with a different TokenID"), + ) + .await; +} + +#[tokio::test] +async fn test_donate_to_project_inactive_project() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .accept_pot( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .apply_for_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + ) + .await; + + interact + .donate_to_project_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ExpectError(4, "Project is not active!"), + ) + .await; +} + +#[tokio::test] +async fn test_donate_to_pot_inactive_pot() { + let mut interact = ContractInteract::new().await; + + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .add_pot( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + + interact + .donate_to_pot_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + 1u32, + TOKEN_ID, + DONATION_AMOUNT.into(), + ExpectError(4, "Pot is not active!"), + ) + .await; +} + +#[tokio::test] +async fn test_add_pot_wrong_payment() { + let mut interact = ContractInteract::new().await; + + interact + .add_pot_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + SECOND_TOKEN_ID, + FEE_AMOUNT.into(), + ExpectError(4, "Wrong token identifier for creating a pot!"), + ) + .await; + + interact + .add_pot_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + TOKEN_ID, + (FEE_AMOUNT + 1).into(), + ExpectError(4, "Wrong fee amount for creating a pot"), + ) + .await; +} + +#[tokio::test] +async fn test_accept_pot_non_existent() { + let mut interact = ContractInteract::new().await; + + interact + .accept_pot_fail( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + BIG_ID, + ExpectError(4, "Potlock doesn't exist!"), + ) + .await; +} + +#[tokio::test] +async fn test_remove_pot_non_existent() { + let mut interact = ContractInteract::new().await; + + interact + .remove_pot_fail( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + BIG_ID, + ExpectError(4, "Potlock doesn't exist!"), + ) + .await; +} + +#[tokio::test] +async fn test_donate_to_pot_non_existent() { + let mut interact = ContractInteract::new().await; + + interact + .donate_to_pot_fail( + &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), + BIG_ID, + TOKEN_ID, + DONATION_AMOUNT.into(), + ExpectError(4, "Potlock doesn't exist!"), + ) + .await; +} From eadba85c94ddcd21b6c4b60dd7ddfdd6ad0c72b0 Mon Sep 17 00:00:00 2001 From: Petru-Vlad Ionescu Date: Fri, 6 Sep 2024 11:36:34 +0300 Subject: [PATCH 2/6] Add interactor tests --- .../src/potlock_interactor_main.rs | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs index b63a7b2d..26dec0d2 100644 --- a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs +++ b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs @@ -1,5 +1,7 @@ #![allow(non_snake_case)] - +#[allow(unused_imports)] +#[allow(dead_code)] +#[allow(unused_variables)] mod proxy; use multiversx_sc_snippets::imports::*; @@ -16,13 +18,12 @@ const GATEWAY: &str = sdk::gateway::TESTNET_GATEWAY; const STATE_FILE: &str = "state.toml"; const TOKEN_ID: &str = "VLD-070dac"; const SECOND_TOKEN_ID: &str = "SCND-620d29"; -const THIRD_TOKEN_ID: &str = "RAND-e3641c"; const INVALID_TOKEN_ID: &str = "123"; const FEE_AMOUNT: u64 = 1; const DONATION_AMOUNT: u64 = 10; -const OWNER_ADDR: &str = "erd1r6f7nfpyzul2tef7gne5h6nx9xqnyt5gehwltlxymnqkztjjzvuqdhderc"; -const SECOND_USER_ADDR: &str = "erd1tjusdv806tuwzllgesljglm7y9jef38wdylkvp85v7a46z9x23us0z5xtr"; -const THIRD_USER_ADDR: &str = "erd1pu4r9rxgn8f7a7gwjchtxjz6y4u3ha7fy93w6r3fjeq26jaqkqjs4ly8fd"; +const OWNER_ADDR: &str = "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"; +const SECOND_USER_ADDR: &str = "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"; +const THIRD_USER_ADDR: &str = "erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"; const MAX_PERCENTAGE: u64 = 10_000; const BIG_ID: u32 = 1000u32; @@ -114,12 +115,9 @@ struct ContractInteract { impl ContractInteract { async fn new() -> Self { let mut interactor = Interactor::new(GATEWAY).await; - let owner_address = interactor - .register_wallet(Wallet::from_pem_file("wallet1.pem").expect("wallet 1 not found")); - let second_address = interactor - .register_wallet(Wallet::from_pem_file("wallet2.pem").expect("wallet 2 not found")); - let third_address = interactor - .register_wallet(Wallet::from_pem_file("wallet3.pem").expect("wallet 3 not found")); + let owner_address = interactor.register_wallet(test_wallets::alice()); + let second_address = interactor.register_wallet(test_wallets::bob()); + let third_address = interactor.register_wallet(test_wallets::carol()); let contract_code = BytesValue::interpret_from( "mxsc:../output/potlock.mxsc.json", @@ -818,6 +816,16 @@ async fn test_deploy_and_config() { async fn test_add_pot() { let mut interact = ContractInteract::new().await; + interact.deploy().await; + + interact + .change_fee_for_pots( + &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), + TOKEN_ID, + FEE_AMOUNT.into(), + ) + .await; + interact .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), From f804a7d085edc62f362ebc12518dac95a623d927 Mon Sep 17 00:00:00 2001 From: Petru-Vlad Ionescu Date: Fri, 6 Sep 2024 11:56:06 +0300 Subject: [PATCH 3/6] Solve warnings --- .../potlock/interact-rs/src/potlock_interactor_main.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs index 26dec0d2..81d8faad 100644 --- a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs +++ b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs @@ -1,7 +1,8 @@ #![allow(non_snake_case)] -#[allow(unused_imports)] -#[allow(dead_code)] -#[allow(unused_variables)] +#![allow(unused_imports)] +#![allow(dead_code)] +#![allow(unused_variables)] + mod proxy; use multiversx_sc_snippets::imports::*; From 28addacc05ce84cb41129178fa53d27e949ee5c7 Mon Sep 17 00:00:00 2001 From: Petru-Vlad Ionescu Date: Fri, 6 Sep 2024 14:41:15 +0300 Subject: [PATCH 4/6] Execute interactor tests only on demand --- .../src/potlock_interactor_main.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs index 81d8faad..7788b245 100644 --- a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs +++ b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs @@ -799,6 +799,7 @@ impl ContractInteract { } #[tokio::test] +#[ignore = "run on demand"] async fn test_deploy_and_config() { let mut interact = ContractInteract::new().await; @@ -814,6 +815,7 @@ async fn test_deploy_and_config() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_add_pot() { let mut interact = ContractInteract::new().await; @@ -837,6 +839,7 @@ async fn test_add_pot() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_accept_pot() { let mut interact = ContractInteract::new().await; @@ -867,6 +870,7 @@ async fn test_accept_pot() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_remove_pot() { let mut interact = ContractInteract::new().await; @@ -897,6 +901,7 @@ async fn test_remove_pot() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_donate_to_pot() { let mut interact = ContractInteract::new().await; @@ -936,6 +941,7 @@ async fn test_donate_to_pot() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_accept_application() { let mut interact = ContractInteract::new().await; @@ -980,6 +986,7 @@ async fn test_accept_application() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_donate_to_project() { let mut interact = ContractInteract::new().await; @@ -1033,6 +1040,7 @@ async fn test_donate_to_project() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_distribute_pot_to_projects() { let mut interact = ContractInteract::new().await; @@ -1096,6 +1104,7 @@ async fn test_distribute_pot_to_projects() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_donate_to_pot_twice_with_same_token() { let mut interact = ContractInteract::new().await; @@ -1144,6 +1153,7 @@ async fn test_donate_to_pot_twice_with_same_token() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_multiple_change_fee_for_pots() { let mut interact = ContractInteract::new().await; @@ -1179,6 +1189,7 @@ async fn test_multiple_change_fee_for_pots() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_change_fee_for_pots_non_admin() { let mut interact = ContractInteract::new().await; @@ -1193,6 +1204,7 @@ async fn test_change_fee_for_pots_non_admin() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_accept_pot_non_admin() { let mut interact = ContractInteract::new().await; @@ -1206,6 +1218,7 @@ async fn test_accept_pot_non_admin() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_remove_pot_non_admin() { let mut interact = ContractInteract::new().await; @@ -1219,6 +1232,7 @@ async fn test_remove_pot_non_admin() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_accept_application_non_admin() { let mut interact = ContractInteract::new().await; @@ -1232,6 +1246,7 @@ async fn test_accept_application_non_admin() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_distribute_pot_to_projects_non_admin() { let mut interact = ContractInteract::new().await; @@ -1248,6 +1263,7 @@ async fn test_distribute_pot_to_projects_non_admin() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_distribute_pot_to_projects_more_than_max_percent() { let mut interact = ContractInteract::new().await; @@ -1313,6 +1329,7 @@ async fn test_distribute_pot_to_projects_more_than_max_percent() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_donate_to_project_with_different_token() { let mut interact = ContractInteract::new().await; @@ -1376,6 +1393,7 @@ async fn test_donate_to_project_with_different_token() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_donate_to_project_inactive_project() { let mut interact = ContractInteract::new().await; @@ -1423,6 +1441,7 @@ async fn test_donate_to_project_inactive_project() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_donate_to_pot_inactive_pot() { let mut interact = ContractInteract::new().await; @@ -1456,6 +1475,7 @@ async fn test_donate_to_pot_inactive_pot() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_add_pot_wrong_payment() { let mut interact = ContractInteract::new().await; @@ -1479,6 +1499,7 @@ async fn test_add_pot_wrong_payment() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_accept_pot_non_existent() { let mut interact = ContractInteract::new().await; @@ -1492,6 +1513,7 @@ async fn test_accept_pot_non_existent() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_remove_pot_non_existent() { let mut interact = ContractInteract::new().await; @@ -1505,6 +1527,7 @@ async fn test_remove_pot_non_existent() { } #[tokio::test] +#[ignore = "run on demand"] async fn test_donate_to_pot_non_existent() { let mut interact = ContractInteract::new().await; From e416e524ca5cc9ddbd10fbbaafcf6a6b7003ab24 Mon Sep 17 00:00:00 2001 From: Petru-Vlad Ionescu Date: Fri, 6 Sep 2024 15:07:42 +0300 Subject: [PATCH 5/6] Cleanse tests --- .../src/potlock_interactor_main.rs | 122 +++++------------- 1 file changed, 35 insertions(+), 87 deletions(-) diff --git a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs index 7788b245..bc453acf 100644 --- a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs +++ b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs @@ -20,7 +20,7 @@ const STATE_FILE: &str = "state.toml"; const TOKEN_ID: &str = "VLD-070dac"; const SECOND_TOKEN_ID: &str = "SCND-620d29"; const INVALID_TOKEN_ID: &str = "123"; -const FEE_AMOUNT: u64 = 1; +const FEE_AMOUNT: u128 = 1; const DONATION_AMOUNT: u64 = 10; const OWNER_ADDR: &str = "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"; const SECOND_USER_ADDR: &str = "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"; @@ -38,28 +38,6 @@ async fn main() { let mut interact = ContractInteract::new().await; match cmd.as_str() { "deploy" => interact.deploy().await, - // "upgrade" => interact.upgrade().await, - // "changeFeeForPots" => interact.change_fee_for_pots().await, - // "acceptPot" => interact.accept_pot().await, - // "removePot" => interact.remove_pot().await, - // "acceptApplication" => interact.accept_application().await, - // "removeApplication" => interact.remove_application().await, - // "rejectDonation" => interact.reject_donation().await, - // "distributePotToProjects" => interact.distribute_pot_to_projects().await, - // "addPot" => interact.add_pot().await, - // "applyForPot" => interact.apply_for_pot().await, - // "donateToPot" => interact.donate_to_pot().await, - // "donateToProject" => interact.donate_to_project().await, - // "getFeeTokenIdentifier" => interact.fee_token_identifier().await, - // "getFeeAmount" => interact.fee_amount().await, - // "getPotlocks" => interact.potlocks().await, - // "getProjects" => interact.projects().await, - // "potDonations" => interact.pot_donations().await, - // "projectDonations" => interact.project_donations().await, - // "isAdmin" => interact.is_admin().await, - // "addAdmin" => interact.add_admin().await, - // "removeAdmin" => interact.remove_admin().await, - // "getAdmins" => interact.admins().await, _ => panic!("unknown command: {}", &cmd), } } @@ -420,7 +398,6 @@ impl ContractInteract { } async fn add_pot(&mut self, caller: &Bech32Address, token_id: &str, fee: u128) { - let token_id = token_id.to_string(); let token_nonce = 0u64; let token_amount = BigUint::::from(fee); @@ -435,11 +412,7 @@ impl ContractInteract { .gas(70_000_000u64) .typed(proxy::PotlockProxy) .add_pot(name, description) - .payment(( - TokenIdentifier::from(token_id.as_str()), - token_nonce, - token_amount, - )) + .payment((TokenIdentifier::from(token_id), token_nonce, token_amount)) .returns(ReturnsResultUnmanaged) .prepare_async() .run() @@ -455,7 +428,6 @@ impl ContractInteract { fee: u128, expected_result: ExpectError<'_>, ) { - let token_id = token_id.to_string(); let token_nonce = 0u64; let token_amount = BigUint::::from(fee); @@ -470,11 +442,7 @@ impl ContractInteract { .gas(70_000_000u64) .typed(proxy::PotlockProxy) .add_pot(name, description) - .payment(( - TokenIdentifier::from(token_id.as_str()), - token_nonce, - token_amount, - )) + .payment((TokenIdentifier::from(token_id), token_nonce, token_amount)) .returns(expected_result) .prepare_async() .run() @@ -510,7 +478,6 @@ impl ContractInteract { token_id: &str, amount: u128, ) { - let token_id = token_id.to_string(); let token_nonce = 0u64; let token_amount = BigUint::::from(amount); @@ -522,11 +489,7 @@ impl ContractInteract { .gas(70_000_000u64) .typed(proxy::PotlockProxy) .donate_to_pot(potlock_id) - .payment(( - TokenIdentifier::from(token_id.as_str()), - token_nonce, - token_amount, - )) + .payment((TokenIdentifier::from(token_id), token_nonce, token_amount)) .returns(ReturnsResultUnmanaged) .prepare_async() .run() @@ -543,7 +506,6 @@ impl ContractInteract { amount: u128, expected_result: ExpectError<'_>, ) { - let token_id = token_id.to_string(); let token_nonce = 0u64; let token_amount = BigUint::::from(amount); @@ -555,11 +517,7 @@ impl ContractInteract { .gas(70_000_000u64) .typed(proxy::PotlockProxy) .donate_to_pot(potlock_id) - .payment(( - TokenIdentifier::from(token_id.as_str()), - token_nonce, - token_amount, - )) + .payment((TokenIdentifier::from(token_id), token_nonce, token_amount)) .returns(expected_result) .prepare_async() .run() @@ -575,7 +533,6 @@ impl ContractInteract { token_id: &str, amount: u128, ) { - let token_id = token_id.to_string(); let token_nonce = 0u64; let token_amount = BigUint::::from(amount); @@ -587,11 +544,7 @@ impl ContractInteract { .gas(70_000_000u64) .typed(proxy::PotlockProxy) .donate_to_project(project_id) - .payment(( - TokenIdentifier::from(token_id.as_str()), - token_nonce, - token_amount, - )) + .payment((TokenIdentifier::from(token_id), token_nonce, token_amount)) .returns(ReturnsResultUnmanaged) .prepare_async() .run() @@ -608,7 +561,6 @@ impl ContractInteract { amount: u128, expected_result: ExpectError<'_>, ) { - let token_id = token_id.to_string(); let token_nonce = 0u64; let token_amount = BigUint::::from(amount); @@ -620,11 +572,7 @@ impl ContractInteract { .gas(70_000_000u64) .typed(proxy::PotlockProxy) .donate_to_project(project_id) - .payment(( - TokenIdentifier::from(token_id.as_str()), - token_nonce, - token_amount, - )) + .payment((TokenIdentifier::from(token_id), token_nonce, token_amount)) .returns(expected_result) .prepare_async() .run() @@ -809,7 +757,7 @@ async fn test_deploy_and_config() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; } @@ -825,7 +773,7 @@ async fn test_add_pot() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -833,7 +781,7 @@ async fn test_add_pot() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; } @@ -849,7 +797,7 @@ async fn test_accept_pot() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -857,7 +805,7 @@ async fn test_accept_pot() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -880,7 +828,7 @@ async fn test_remove_pot() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -888,7 +836,7 @@ async fn test_remove_pot() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -911,7 +859,7 @@ async fn test_donate_to_pot() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -919,7 +867,7 @@ async fn test_donate_to_pot() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -951,7 +899,7 @@ async fn test_accept_application() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -959,7 +907,7 @@ async fn test_accept_application() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -996,7 +944,7 @@ async fn test_donate_to_project() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1004,7 +952,7 @@ async fn test_donate_to_project() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1050,7 +998,7 @@ async fn test_distribute_pot_to_projects() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1058,7 +1006,7 @@ async fn test_distribute_pot_to_projects() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1114,7 +1062,7 @@ async fn test_donate_to_pot_twice_with_same_token() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1122,7 +1070,7 @@ async fn test_donate_to_pot_twice_with_same_token() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1163,7 +1111,7 @@ async fn test_multiple_change_fee_for_pots() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1197,7 +1145,7 @@ async fn test_change_fee_for_pots_non_admin() { .change_fee_for_pots_fail( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), INVALID_TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ExpectError(4, "Endpoint can only be called by admins"), ) .await; @@ -1273,7 +1221,7 @@ async fn test_distribute_pot_to_projects_more_than_max_percent() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1281,7 +1229,7 @@ async fn test_distribute_pot_to_projects_more_than_max_percent() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1339,7 +1287,7 @@ async fn test_donate_to_project_with_different_token() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1347,7 +1295,7 @@ async fn test_donate_to_project_with_different_token() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1403,7 +1351,7 @@ async fn test_donate_to_project_inactive_project() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1411,7 +1359,7 @@ async fn test_donate_to_project_inactive_project() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1451,7 +1399,7 @@ async fn test_donate_to_pot_inactive_pot() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1459,7 +1407,7 @@ async fn test_donate_to_pot_inactive_pot() { .add_pot( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ) .await; @@ -1483,7 +1431,7 @@ async fn test_add_pot_wrong_payment() { .add_pot_fail( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), SECOND_TOKEN_ID, - FEE_AMOUNT.into(), + FEE_AMOUNT, ExpectError(4, "Wrong token identifier for creating a pot!"), ) .await; From 82238528e978fc5400b4d9547c60fb85cbc446c8 Mon Sep 17 00:00:00 2001 From: Petru-Vlad Ionescu Date: Fri, 6 Sep 2024 15:09:41 +0300 Subject: [PATCH 6/6] Cleanse tests --- contracts/potlock/interact-rs/src/potlock_interactor_main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs index bc453acf..e3a9f14c 100644 --- a/contracts/potlock/interact-rs/src/potlock_interactor_main.rs +++ b/contracts/potlock/interact-rs/src/potlock_interactor_main.rs @@ -1125,7 +1125,7 @@ async fn test_multiple_change_fee_for_pots() { .change_fee_for_pots( &Bech32Address::from_bech32_string(OWNER_ADDR.to_string()), SECOND_TOKEN_ID, - (FEE_AMOUNT + 1).into(), + FEE_AMOUNT + 1, ) .await; @@ -1440,7 +1440,7 @@ async fn test_add_pot_wrong_payment() { .add_pot_fail( &Bech32Address::from_bech32_string(SECOND_USER_ADDR.to_string()), TOKEN_ID, - (FEE_AMOUNT + 1).into(), + FEE_AMOUNT + 1, ExpectError(4, "Wrong fee amount for creating a pot"), ) .await;