Skip to content

Commit

Permalink
test setup + fix wrong percentage usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Jan 16, 2024
1 parent a942ef5 commit 1563288
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
9 changes: 6 additions & 3 deletions contracts/fair-launch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions contracts/fair-launch/src/initial_launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 11 additions & 8 deletions contracts/fair-launch/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]

use common::Percentage;
use initial_launch::InitialLaunchBlocks;

use crate::{common::MAX_FEE_PERCENTAGE, initial_launch::InitialLaunchInfo};
Expand All @@ -20,30 +21,32 @@ 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,
"Invalid launch duration blocks"
);
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"
);

Expand Down
10 changes: 10 additions & 0 deletions contracts/fair-launch/tests/fair_launch_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![allow(deprecated)]

mod tests_common;

use tests_common::*;

#[test]
fn init_test() {
let _ = FairLaunchSetup::new(fair_launch::contract_obj);
}
70 changes: 70 additions & 0 deletions contracts/fair-launch/tests/tests_common.rs
Original file line number Diff line number Diff line change
@@ -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";

Check warning on line 11 in contracts/fair-launch/tests/tests_common.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

static `TOKEN_ID` is never used

Check warning on line 11 in contracts/fair-launch/tests/tests_common.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

static `TOKEN_ID` is never used

Check warning on line 11 in contracts/fair-launch/tests/tests_common.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/fair-launch/tests/tests_common.rs#L11

warning: static `TOKEN_ID` is never used --> contracts/fair-launch/tests/tests_common.rs:11:12 | 11 | pub static TOKEN_ID: &[u8] = b"MYTOKEN-123456"; | ^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
Raw output
contracts/fair-launch/tests/tests_common.rs:11:12:w:warning: static `TOKEN_ID` is never used
  --> contracts/fair-launch/tests/tests_common.rs:11:12
   |
11 | pub static TOKEN_ID: &[u8] = b"MYTOKEN-123456";
   |            ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default


__END__
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<FairLaunchObjBuilder>
where
FairLaunchObjBuilder: 'static + Copy + Fn() -> fair_launch::ContractObj<DebugApi>,
{
pub b_mock: BlockchainStateWrapper,
pub owner_address: Address,
pub first_user_address: Address,
pub second_user_address: Address,
pub fl_wrapper: ContractObjWrapper<fair_launch::ContractObj<DebugApi>, FairLaunchObjBuilder>,
}

impl<FairLaunchObjBuilder> FairLaunchSetup<FairLaunchObjBuilder>
where
FairLaunchObjBuilder: 'static + Copy + Fn() -> fair_launch::ContractObj<DebugApi>,
{
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,
}
}
}

0 comments on commit 1563288

Please sign in to comment.