Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated price discovery #991

Open
wants to merge 16 commits into
base: rc/v3.0.2
Choose a base branch
from
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

248 changes: 8 additions & 240 deletions dex/fuzz/src/fuzz_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ pub mod fuzz_data_tests {
use farm::exit_penalty::ExitPenaltyModule;
use farm::*;
use farm_token::FarmTokenModule;
use multiversx_sc::codec::Empty;
use multiversx_sc::types::{Address, BigUint, EsdtLocalRole};
use multiversx_sc_scenario::{
managed_address, managed_biguint, managed_token_id, managed_token_id_wrapped, rust_biguint,
whitebox_legacy::*, DebugApi,
managed_address, managed_biguint, managed_token_id, rust_biguint, whitebox_legacy::*,
DebugApi,
};
use pair::*;
use pausable::{PausableModule, State};
use price_discovery::redeem_token::*;
use price_discovery::*;
use simple_lock::locked_token::LockedTokenModule;
use simple_lock::SimpleLock;

use rand::prelude::StdRng;
use rand::SeedableRng;
use std::cell::Cell;
Expand All @@ -31,7 +25,6 @@ pub mod fuzz_data_tests {

pub const FARM_WASM_PATH: &str = "farm/output/farm.wasm";
pub const PAIR_WASM_PATH: &str = "pair/output/pair.wasm";
pub const PD_WASM_PATH: &str = "../output/price-discovery.wasm";

pub const WEGLD_TOKEN_ID: &[u8] = b"WEGLD-abcdef";
pub const MEX_TOKEN_ID: &[u8] = b"MEX-abcdef";
Expand All @@ -50,25 +43,8 @@ pub mod fuzz_data_tests {
pub const DIVISION_SAFETY_CONSTANT: u64 = 1_000_000_000_000;

// Price discovery constants
pub const DISC_LAUNCHED_TOKEN_ID: &[u8] = b"SOCOOLWOW-123456";
pub const DISC_ACCEPTED_TOKEN_ID: &[u8] = b"USDC-123456";
pub const DISC_REDEEM_TOKEN_ID: &[u8] = b"GIBREWARDS-123456";
pub const LOCKED_TOKEN_ID: &[u8] = b"LOCKED-abcdef";
pub const DISC_LAUNCHED_TOKEN_REDEEM_NONCE: u64 = 1;
pub const DISC_ACCEPTED_TOKEN_REDEEM_NONCE: u64 = 2;
pub const USER_TOTAL_DISC_TOKENS: u64 = 1_000_000_000;
pub const DISC_LAUNCHED_TOKENS: u64 = 5_000_000_000;
pub const DISC_USER_LAUNCH_TOKENS: u64 = 100_000_000;
pub const TOTAL_FEE_PERCENT: u64 = 300;
pub const SPECIAL_FEE_PERCENT: u64 = 50;
pub const MIN_PENALTY_PERCENTAGE: u64 = 1_000_000_000_000; // 10%
pub const MAX_PENALTY_PERCENTAGE: u64 = 5_000_000_000_000; // 50%
pub const FIXED_PENALTY_PERCENTAGE: u64 = 2_500_000_000_000; // 25%
pub const START_BLOCK: u64 = 1;
pub const NO_LIMIT_PHASE_DURATION_BLOCKS: u64 = 150;
pub const LINEAR_PENALTY_PHASE_DURATION_BLOCKS: u64 = 50;
pub const FIXED_PENALTY_PHASE_DURATION_BLOCKS: u64 = 25;
pub const UNLOCK_EPOCH: u64 = 20;

#[derive(Clone, TopEncode)]
pub struct FuzzDexExecutorInitArgs {
Expand All @@ -81,9 +57,6 @@ pub mod fuzz_data_tests {
pub exit_farm_prob: u64,
pub claim_rewards_prob: u64,
pub compound_rewards_prob: u64,
pub price_discovery_deposit_prob: u64,
pub price_discovery_withdraw_prob: u64,
pub price_discovery_redeem_prob: u64,
pub block_nonce_increase: u64,
pub compound_rewards_max_value: u64,
pub token_deposit_max_value: u64,
Expand All @@ -93,9 +66,6 @@ pub mod fuzz_data_tests {
pub enter_farm_max_value: u64,
pub exit_farm_max_value: u64,
pub claim_rewards_max_value: u64,
pub price_discovery_deposit_max_value: u64,
pub price_discovery_withdraw_max_value: u64,
pub price_discovery_redeem_max_value: u64,
}

impl FuzzDexExecutorInitArgs {
Expand All @@ -110,9 +80,6 @@ pub mod fuzz_data_tests {
exit_farm_prob: 10,
claim_rewards_prob: 15,
compound_rewards_prob: 10,
price_discovery_deposit_prob: 30,
price_discovery_withdraw_prob: 15,
price_discovery_redeem_prob: 30,
block_nonce_increase: 1,
compound_rewards_max_value: 1000000u64,
token_deposit_max_value: 50000000u64,
Expand All @@ -122,18 +89,14 @@ pub mod fuzz_data_tests {
enter_farm_max_value: 100000000u64,
exit_farm_max_value: 1000000u64,
claim_rewards_max_value: 1000000u64,
price_discovery_deposit_max_value: 1000000u64,
price_discovery_withdraw_max_value: 1000000u64,
price_discovery_redeem_max_value: 1000000u64,
}
}
}

pub struct FuzzerData<PairObjBuilder, FarmObjBuilder, PriceDiscObjBuilder>
pub struct FuzzerData<PairObjBuilder, FarmObjBuilder>
where
PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj<DebugApi>,
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
PriceDiscObjBuilder: 'static + Copy + Fn() -> price_discovery::ContractObj<DebugApi>,
{
pub rng: StdRng,
pub fuzz_args: FuzzDexExecutorInitArgs,
Expand All @@ -142,33 +105,25 @@ pub mod fuzz_data_tests {
pub users: Vec<User>,
pub swap_pairs: Vec<PairSetup<PairObjBuilder>>,
pub farms: Vec<FarmSetup<FarmObjBuilder>>,
pub price_disc: PriceDiscSetup<PriceDiscObjBuilder>,
}

impl<PairObjBuilder, FarmObjBuilder, PriceDiscObjBuilder>
FuzzerData<PairObjBuilder, FarmObjBuilder, PriceDiscObjBuilder>
impl<PairObjBuilder, FarmObjBuilder> FuzzerData<PairObjBuilder, FarmObjBuilder>
where
PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj<DebugApi>,
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
PriceDiscObjBuilder: 'static + Copy + Fn() -> price_discovery::ContractObj<DebugApi>,
{
pub fn new(
seed: u64,
pair_builder: PairObjBuilder,
farm_builder: FarmObjBuilder,
price_discovery: PriceDiscObjBuilder,
) -> Self {
pub fn new(seed: u64, pair_builder: PairObjBuilder, farm_builder: FarmObjBuilder) -> Self {
let egld_amount = rust_biguint!(OWNER_EGLD_BALANCE);

let rng = StdRng::seed_from_u64(seed);
let fuzz_args = FuzzDexExecutorInitArgs::new();
let statistics = EventsStatistics::new();
let statistics = EventsStatistics::default();
let mut blockchain_wrapper = BlockchainStateWrapper::new();
let owner_addr = blockchain_wrapper.create_user_account(&egld_amount);

let mut users = vec![];

for i in 1..=fuzz_args.num_users {
for _ in 1..=fuzz_args.num_users {
let user_address = blockchain_wrapper.create_user_account(&egld_amount);
blockchain_wrapper.set_esdt_balance(
&user_address,
Expand All @@ -186,27 +141,8 @@ pub mod fuzz_data_tests {
&rust_biguint!(USER_TOTAL_BUSD_TOKENS),
);

// 2/3 chance for price discovery buy intention
// else sale intention
let mut price_discovery_buy = false;
if i % 3 == 0 {
blockchain_wrapper.set_esdt_balance(
&user_address,
DISC_LAUNCHED_TOKEN_ID,
&rust_biguint!(DISC_USER_LAUNCH_TOKENS),
);
} else {
blockchain_wrapper.set_esdt_balance(
&user_address,
DISC_ACCEPTED_TOKEN_ID,
&rust_biguint!(USER_TOTAL_DISC_TOKENS),
);
price_discovery_buy = true;
}

let user = User {
address: user_address,
price_discovery_buy,
};

users.push(user);
Expand Down Expand Up @@ -264,9 +200,6 @@ pub mod fuzz_data_tests {

let farms = vec![first_farm, second_farm, third_farm];

let price_disc =
setup_price_disc(&owner_addr, &mut blockchain_wrapper, price_discovery);

FuzzerData {
rng,
fuzz_args,
Expand All @@ -275,15 +208,13 @@ pub mod fuzz_data_tests {
users,
swap_pairs,
farms,
price_disc,
}
}
}

#[derive()]
pub struct User {
pub address: Address,
pub price_discovery_buy: bool,
}

#[derive()]
Expand Down Expand Up @@ -466,130 +397,7 @@ pub mod fuzz_data_tests {
}
}

pub struct PriceDiscSetup<PriceDiscObjBuilder>
where
PriceDiscObjBuilder: 'static + Copy + Fn() -> price_discovery::ContractObj<DebugApi>,
{
pub pd_wrapper:
ContractObjWrapper<price_discovery::ContractObj<DebugApi>, PriceDiscObjBuilder>,
}

pub fn setup_price_disc<PriceDiscObjBuilder>(
owner_addr: &Address,
blockchain_wrapper: &mut BlockchainStateWrapper,
pd_builder: PriceDiscObjBuilder,
) -> PriceDiscSetup<PriceDiscObjBuilder>
where
PriceDiscObjBuilder: 'static + Copy + Fn() -> price_discovery::ContractObj<DebugApi>,
{
let rust_zero = rust_biguint!(0u64);
let pd_wrapper = blockchain_wrapper.create_sc_account(
&rust_zero,
Some(owner_addr),
pd_builder,
PD_WASM_PATH,
);

// set user balances
blockchain_wrapper.set_esdt_balance(
owner_addr,
DISC_LAUNCHED_TOKEN_ID,
&rust_biguint!(DISC_LAUNCHED_TOKENS),
);

// set sc roles and initial minted SFTs (only needed for the purpose of SFT add quantity)
blockchain_wrapper.set_esdt_local_roles(
pd_wrapper.address_ref(),
DISC_REDEEM_TOKEN_ID,
&[
EsdtLocalRole::NftCreate,
EsdtLocalRole::NftBurn,
EsdtLocalRole::NftAddQuantity,
],
);
blockchain_wrapper.set_nft_balance(
pd_wrapper.address_ref(),
DISC_REDEEM_TOKEN_ID,
LAUNCHED_TOKEN_REDEEM_NONCE,
&rust_biguint!(1),
&Empty,
);
blockchain_wrapper.set_nft_balance(
pd_wrapper.address_ref(),
DISC_REDEEM_TOKEN_ID,
ACCEPTED_TOKEN_REDEEM_NONCE,
&rust_biguint!(1),
&Empty,
);

// init locking SC
let locking_sc_wrapper = blockchain_wrapper.create_sc_account(
&rust_zero,
Some(owner_addr),
simple_lock::contract_obj,
"lock wasm path",
);
blockchain_wrapper
.execute_tx(owner_addr, &locking_sc_wrapper, &rust_zero, |sc| {
sc.init();
sc.locked_token()
.set_token_id(managed_token_id!(LOCKED_TOKEN_ID));
})
.assert_ok();

blockchain_wrapper.set_esdt_local_roles(
locking_sc_wrapper.address_ref(),
LOCKED_TOKEN_ID,
&[
EsdtLocalRole::NftCreate,
EsdtLocalRole::NftAddQuantity,
EsdtLocalRole::NftBurn,
],
);

// init Price Discovery SC
blockchain_wrapper
.execute_tx(owner_addr, &pd_wrapper, &rust_zero, |sc| {
sc.init(
managed_token_id!(DISC_LAUNCHED_TOKEN_ID),
managed_token_id_wrapped!(DISC_ACCEPTED_TOKEN_ID),
18,
managed_biguint!(0),
START_BLOCK,
NO_LIMIT_PHASE_DURATION_BLOCKS,
LINEAR_PENALTY_PHASE_DURATION_BLOCKS,
FIXED_PENALTY_PHASE_DURATION_BLOCKS,
UNLOCK_EPOCH,
managed_biguint!(MIN_PENALTY_PERCENTAGE),
managed_biguint!(MAX_PENALTY_PERCENTAGE),
managed_biguint!(FIXED_PENALTY_PERCENTAGE),
managed_address!(locking_sc_wrapper.address_ref()),
);

sc.redeem_token()
.set_token_id(managed_token_id!(DISC_REDEEM_TOKEN_ID));
})
.assert_ok();

blockchain_wrapper.set_block_nonce(START_BLOCK);

blockchain_wrapper
.execute_esdt_transfer(
owner_addr,
&pd_wrapper,
DISC_LAUNCHED_TOKEN_ID,
0,
&rust_biguint!(DISC_LAUNCHED_TOKENS),
|sc| {
sc.deposit();
},
)
.assert_ok();

PriceDiscSetup { pd_wrapper }
}

#[derive(Clone, PartialEq)]
#[derive(Clone, PartialEq, Default)]
pub struct EventsStatistics {
pub swap_fixed_input_hits: u64,
pub swap_fixed_input_misses: u64,
Expand All @@ -616,46 +424,6 @@ pub mod fuzz_data_tests {

pub compound_rewards_hits: u64,
pub compound_rewards_misses: u64,

pub price_discovery_deposit_hits: u64,
pub price_discovery_deposit_misses: u64,

pub price_discovery_withdraw_hits: u64,
pub price_discovery_withdraw_misses: u64,

pub price_discovery_redeem_hits: u64,
pub price_discovery_redeem_misses: u64,
}

impl EventsStatistics {
pub fn new() -> EventsStatistics {
EventsStatistics {
swap_fixed_input_hits: 0,
swap_fixed_input_misses: 0,
swap_fixed_output_hits: 0,
swap_fixed_output_misses: 0,
add_liquidity_hits: 0,
add_liquidity_misses: 0,
remove_liquidity_hits: 0,
remove_liquidity_misses: 0,
enter_farm_hits: 0,
enter_farm_misses: 0,
exit_farm_hits: 0,
exit_farm_misses: 0,
exit_farm_with_rewards: 0,
claim_rewards_hits: 0,
claim_rewards_misses: 0,
claim_rewards_with_rewards: 0,
compound_rewards_hits: 0,
compound_rewards_misses: 0,
price_discovery_deposit_hits: 0,
price_discovery_deposit_misses: 0,
price_discovery_withdraw_hits: 0,
price_discovery_withdraw_misses: 0,
price_discovery_redeem_hits: 0,
price_discovery_redeem_misses: 0,
}
}
}

pub fn to_managed_biguint(value: RustBigUint) -> BigUint<DebugApi> {
Expand Down
Loading
Loading