Skip to content

Commit

Permalink
Merge pull request #816 from multiversx/simple-lock-break-farm-into-i…
Browse files Browse the repository at this point in the history
…nitial-tokens
  • Loading branch information
CostinCarabas authored Feb 28, 2024
2 parents cad0c11 + 31779fa commit 0271a91
Show file tree
Hide file tree
Showing 21 changed files with 849 additions and 64 deletions.
103 changes: 99 additions & 4 deletions dex/farm/tests/farm_setup/single_user_farm_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(deprecated)]

use common_structs::FarmTokenAttributes;
use multiversx_sc::codec::multi_types::OptionalValue;
use multiversx_sc::codec::multi_types::{MultiValue3, OptionalValue};
use multiversx_sc::storage::mappers::StorageTokenWrapper;
use multiversx_sc::types::{Address, EsdtLocalRole, ManagedAddress, MultiValueEncoded};
use multiversx_sc_scenario::whitebox_legacy::{TxContextStack, TxTokenTransfer};
Expand All @@ -17,9 +17,12 @@ use farm::exit_penalty::ExitPenaltyModule;
use farm::*;
use farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule;
use farm_token::FarmTokenModule;
use pair::pair_actions::add_liq::AddLiquidityModule as _;
use pair::{config::ConfigModule as OtherConfigModule, Pair};
use pausable::{PausableModule, State};

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

pub const WEGLD_TOKEN_ID: &[u8] = b"WEGLD-abcdef";
pub const MEX_TOKEN_ID: &[u8] = b"MEX-abcdef"; // reward token ID
Expand All @@ -36,25 +39,74 @@ pub const USER_REWARDS_ENERGY_CONST: u64 = 3;
pub const USER_REWARDS_FARM_CONST: u64 = 2;
pub const MIN_ENERGY_AMOUNT_FOR_BOOSTED_YIELDS: u64 = 1;
pub const MIN_FARM_AMOUNT_FOR_BOOSTED_YIELDS: u64 = 1;
pub const LOCKED_TOKEN_ID: &[u8] = b"XMEX-123456";
pub const LOCKED_LP_TOKEN_ID: &[u8] = b"LKLP-123456";
pub const FARM_PROXY_TOKEN_ID: &[u8] = b"PROXY-123456";

pub struct SingleUserFarmSetup<FarmObjBuilder>
pub struct SingleUserFarmSetup<FarmObjBuilder, PairObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj<DebugApi>,
{
pub blockchain_wrapper: BlockchainStateWrapper,
pub owner_address: Address,
pub user_address: Address,
pub farm_wrapper: ContractObjWrapper<farm::ContractObj<DebugApi>, FarmObjBuilder>,
pub pair_wrapper: ContractObjWrapper<pair::ContractObj<DebugApi>, PairObjBuilder>,
}

impl<FarmObjBuilder> SingleUserFarmSetup<FarmObjBuilder>
impl<FarmObjBuilder, PairObjBuilder> SingleUserFarmSetup<FarmObjBuilder, PairObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj<DebugApi>,
{
pub fn new(farm_builder: FarmObjBuilder) -> Self {
pub fn new(farm_builder: FarmObjBuilder, pair_builder: PairObjBuilder) -> Self {
let rust_zero = rust_biguint!(0u64);
let mut blockchain_wrapper = BlockchainStateWrapper::new();
let owner_addr = blockchain_wrapper.create_user_account(&rust_zero);

let pair_wrapper = blockchain_wrapper.create_sc_account(
&rust_zero,
Some(&owner_addr),
pair_builder,
PAIR_WASM_PATH,
);

// init pair contract
blockchain_wrapper
.execute_tx(&owner_addr, &pair_wrapper, &rust_zero, |sc| {
let first_token_id = managed_token_id!(WEGLD_TOKEN_ID);
let second_token_id = managed_token_id!(MEX_TOKEN_ID);
let router_address = managed_address!(&owner_addr);
let router_owner_address = managed_address!(&owner_addr);
let total_fee_percent = 300u64;
let special_fee_percent = 50u64;

sc.init(
first_token_id,
second_token_id,
router_address,
router_owner_address,
total_fee_percent,
special_fee_percent,
ManagedAddress::<DebugApi>::zero(),
MultiValueEncoded::<DebugApi, ManagedAddress<DebugApi>>::new(),
);

let lp_token_id = managed_token_id!(LP_TOKEN_ID);
sc.lp_token_identifier().set(&lp_token_id);

sc.state().set(State::Active);
})
.assert_ok();

let lp_token_roles = [EsdtLocalRole::Mint, EsdtLocalRole::Burn];
blockchain_wrapper.set_esdt_local_roles(
pair_wrapper.address_ref(),
LP_TOKEN_ID,
&lp_token_roles[..],
);

let farm_wrapper = blockchain_wrapper.create_sc_account(
&rust_zero,
Some(&owner_addr),
Expand Down Expand Up @@ -142,9 +194,52 @@ where
owner_address: owner_addr,
user_address: user_addr,
farm_wrapper,
pair_wrapper,
}
}

#[allow(clippy::too_many_arguments)]
pub fn add_liquidity(
&mut self,
first_token_amount: u64,
first_token_min: u64,
second_token_amount: u64,
second_token_min: u64,
) {
let payments = vec![
TxTokenTransfer {
token_identifier: WEGLD_TOKEN_ID.to_vec(),
nonce: 0,
value: rust_biguint!(first_token_amount),
},
TxTokenTransfer {
token_identifier: LOCKED_TOKEN_ID.to_vec(),
nonce: 0,
value: rust_biguint!(second_token_amount),
},
];

self.blockchain_wrapper
.execute_esdt_multi_transfer(&self.user_address, &self.pair_wrapper, &payments, |sc| {
let MultiValue3 { 0: payments } = sc.add_liquidity(
managed_biguint!(first_token_min),
managed_biguint!(second_token_min),
);

assert_eq!(payments.0.token_identifier, managed_token_id!(LP_TOKEN_ID));

assert_eq!(
payments.1.token_identifier,
managed_token_id!(WEGLD_TOKEN_ID)
);
assert_eq!(
payments.2.token_identifier,
managed_token_id!(LOCKED_TOKEN_ID)
);
})
.assert_ok();
}

pub fn enter_farm(
&mut self,
farm_in_amount: u64,
Expand Down
Loading

0 comments on commit 0271a91

Please sign in to comment.