From 15632886f71710182d680face74a2929038a8319 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Tue, 16 Jan 2024 10:36:30 +0200 Subject: [PATCH] test setup + fix wrong percentage usage --- contracts/fair-launch/Cargo.toml | 9 ++- contracts/fair-launch/src/initial_launch.rs | 6 +- contracts/fair-launch/src/lib.rs | 19 ++--- .../fair-launch/tests/fair_launch_test.rs | 10 +++ contracts/fair-launch/tests/tests_common.rs | 70 +++++++++++++++++++ 5 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 contracts/fair-launch/tests/fair_launch_test.rs create mode 100644 contracts/fair-launch/tests/tests_common.rs diff --git a/contracts/fair-launch/Cargo.toml b/contracts/fair-launch/Cargo.toml index aecfa2da..553c1a94 100644 --- a/contracts/fair-launch/Cargo.toml +++ b/contracts/fair-launch/Cargo.toml @@ -10,9 +10,12 @@ path = "src/lib.rs" [dependencies.multiversx-sc] version = "0.46.0" - -[dev-dependencies] -num-bigint = "0.4.2" +features = ["esdt-token-payment-legacy-decode"] [dev-dependencies.multiversx-sc-scenario] version = "0.46.0" + +[dev-dependencies] +num-bigint = "0.4.2" +num-traits = "0.2" +hex = "0.4" diff --git a/contracts/fair-launch/src/initial_launch.rs b/contracts/fair-launch/src/initial_launch.rs index 81a314fa..7a0f81a5 100644 --- a/contracts/fair-launch/src/initial_launch.rs +++ b/contracts/fair-launch/src/initial_launch.rs @@ -156,12 +156,12 @@ pub trait InitialLaunchModule: let blocks_passed_in_penalty_phase = current_block - initial_launch_blocks.start; let blocks_diff = initial_launch_blocks.end - initial_launch_blocks.start; - let percentage_diff = fee_percentage_end - fee_percentage_start; + let percentage_diff = fee_percentage_start - fee_percentage_end; - let penalty_percentage_increase = + let penalty_percentage_decrease = percentage_diff as u64 * blocks_passed_in_penalty_phase / (blocks_diff - 1); - fee_percentage_start + penalty_percentage_increase as u32 + fee_percentage_start - penalty_percentage_decrease as u32 } fn require_not_initial_launch(&self) { diff --git a/contracts/fair-launch/src/lib.rs b/contracts/fair-launch/src/lib.rs index b1554d12..36cb9554 100644 --- a/contracts/fair-launch/src/lib.rs +++ b/contracts/fair-launch/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] +use common::Percentage; use initial_launch::InitialLaunchBlocks; use crate::{common::MAX_FEE_PERCENTAGE, initial_launch::InitialLaunchInfo}; @@ -20,16 +21,18 @@ pub trait FairLaunch: + token_info::TokenInfoModule + transfer::TransferModule { + /// Percentages have to be between 0 and 10_000 + /// Start percentage >= End percentage #[init] fn init( &self, initial_launch_duration_blocks: u64, account_buy_limit: BigUint, tx_buy_limit: BigUint, - buy_fee_percentage_start: u32, - buy_fee_percentage_end: u32, - sell_fee_percentage_start: u32, - sell_fee_percentage_end: u32, + buy_fee_percentage_start: Percentage, + buy_fee_percentage_end: Percentage, + sell_fee_percentage_start: Percentage, + sell_fee_percentage_end: Percentage, ) { require!( initial_launch_duration_blocks > 0, @@ -37,13 +40,13 @@ pub trait FairLaunch: ); require!(tx_buy_limit <= account_buy_limit, "Invalid limits"); require!( - buy_fee_percentage_start < buy_fee_percentage_end - && buy_fee_percentage_end <= MAX_FEE_PERCENTAGE, + buy_fee_percentage_start >= buy_fee_percentage_end + && buy_fee_percentage_start <= MAX_FEE_PERCENTAGE, "Invalid percentage" ); require!( - sell_fee_percentage_start < sell_fee_percentage_end - && sell_fee_percentage_end <= MAX_FEE_PERCENTAGE, + sell_fee_percentage_start >= sell_fee_percentage_end + && sell_fee_percentage_start <= MAX_FEE_PERCENTAGE, "Invalid percentage" ); diff --git a/contracts/fair-launch/tests/fair_launch_test.rs b/contracts/fair-launch/tests/fair_launch_test.rs new file mode 100644 index 00000000..98638e07 --- /dev/null +++ b/contracts/fair-launch/tests/fair_launch_test.rs @@ -0,0 +1,10 @@ +#![allow(deprecated)] + +mod tests_common; + +use tests_common::*; + +#[test] +fn init_test() { + let _ = FairLaunchSetup::new(fair_launch::contract_obj); +} diff --git a/contracts/fair-launch/tests/tests_common.rs b/contracts/fair-launch/tests/tests_common.rs new file mode 100644 index 00000000..016f1c57 --- /dev/null +++ b/contracts/fair-launch/tests/tests_common.rs @@ -0,0 +1,70 @@ +#![allow(deprecated)] + +use fair_launch::{common::Percentage, FairLaunch}; +use multiversx_sc::types::Address; +use multiversx_sc_scenario::{ + managed_biguint, rust_biguint, + testing_framework::{BlockchainStateWrapper, ContractObjWrapper}, + DebugApi, +}; + +pub static TOKEN_ID: &[u8] = b"MYTOKEN-123456"; +pub const LAUNCH_DURATION_BLOCKS: u64 = 100; +pub const ACCOUNT_BUY_LIMIT: u64 = 2_000; +pub const TX_BUY_LIMIT: u64 = 1_000; +pub const BUY_FEE_PERCENTAGE_START: Percentage = 9_000; +pub const BUY_FEE_PERCENTAGE_END: Percentage = 1_000; +pub const SELL_FEE_PERCENTAGE_START: Percentage = 10_000; +pub const SELL_FEE_PERCENTAGE_END: Percentage = 5_000; + +pub struct FairLaunchSetup +where + FairLaunchObjBuilder: 'static + Copy + Fn() -> fair_launch::ContractObj, +{ + pub b_mock: BlockchainStateWrapper, + pub owner_address: Address, + pub first_user_address: Address, + pub second_user_address: Address, + pub fl_wrapper: ContractObjWrapper, FairLaunchObjBuilder>, +} + +impl FairLaunchSetup +where + FairLaunchObjBuilder: 'static + Copy + Fn() -> fair_launch::ContractObj, +{ + pub fn new(fl_builder: FairLaunchObjBuilder) -> Self { + let rust_zero = rust_biguint!(0u64); + let mut b_mock = BlockchainStateWrapper::new(); + let first_user_address = b_mock.create_user_account(&rust_zero); + let second_user_address = b_mock.create_user_account(&rust_zero); + let owner_address = b_mock.create_user_account(&rust_zero); + + let fl_wrapper = b_mock.create_sc_account( + &rust_zero, + Some(&owner_address), + fl_builder, + "some wasm path", + ); + b_mock + .execute_tx(&owner_address, &fl_wrapper, &rust_zero, |sc| { + sc.init( + LAUNCH_DURATION_BLOCKS, + managed_biguint!(ACCOUNT_BUY_LIMIT), + managed_biguint!(TX_BUY_LIMIT), + BUY_FEE_PERCENTAGE_START, + BUY_FEE_PERCENTAGE_END, + SELL_FEE_PERCENTAGE_START, + SELL_FEE_PERCENTAGE_END, + ); + }) + .assert_ok(); + + Self { + b_mock, + owner_address, + first_user_address, + second_user_address, + fl_wrapper, + } + } +}