diff --git a/pallets/ceres-liquidity-locker/benchmarking/src/lib.rs b/pallets/ceres-liquidity-locker/benchmarking/src/lib.rs index ed0529ec2e..09f3123ea0 100644 --- a/pallets/ceres-liquidity-locker/benchmarking/src/lib.rs +++ b/pallets/ceres-liquidity-locker/benchmarking/src/lib.rs @@ -1,8 +1,6 @@ //! Ceres liquidity locker module benchmarking. #![cfg_attr(not(feature = "std"), no_std)] -// TODO #167: fix clippy warnings -#![allow(clippy::all)] use ceres_liquidity_locker::AccountIdOf; use codec::Decode; @@ -85,21 +83,11 @@ fn setup_benchmark_assets_only() -> Result<(), &'static str> { None, ); - TradingPair::::register( - owner_origin.clone(), - DEX.into(), - XOR.into(), - ceres_asset_id.into(), - ) - .unwrap(); - - Assets::::mint_to(&XOR.into(), &owner.clone(), &owner.clone(), balance!(50000))?; - Assets::::mint_to( - &ceres_asset_id.into(), - &owner.clone(), - &owner.clone(), - balance!(50000), - )?; + TradingPair::::register(owner_origin, DEX.into(), XOR.into(), ceres_asset_id.into()) + .unwrap(); + + Assets::::mint_to(&XOR.into(), &owner, &owner, balance!(50000))?; + Assets::::mint_to(&ceres_asset_id.into(), &owner, &owner, balance!(50000))?; Ok(()) } @@ -107,8 +95,7 @@ fn setup_benchmark_assets_only() -> Result<(), &'static str> { fn setup_benchmark() -> Result<(), &'static str> { let owner = alice::(); frame_system::Pallet::::inc_providers(&owner); - let owner_origin: ::RuntimeOrigin = - RawOrigin::Signed(owner.clone()).into(); + let owner_origin: ::RuntimeOrigin = RawOrigin::Signed(owner).into(); let ceres_asset_id = common::AssetId32::from_bytes(hex!( "008bcfd2387d3fc453333557eecb0efe59fcba128769b2feefdd306e98e66440" )); @@ -123,7 +110,7 @@ fn setup_benchmark() -> Result<(), &'static str> { )?; XYKPool::::deposit_liquidity( - owner_origin.clone(), + owner_origin, DEX.into(), XOR.into(), ceres_asset_id.into(), diff --git a/pallets/ceres-liquidity-locker/benchmarking/src/mock.rs b/pallets/ceres-liquidity-locker/benchmarking/src/mock.rs index 1bc6979aa0..82b13218b5 100644 --- a/pallets/ceres-liquidity-locker/benchmarking/src/mock.rs +++ b/pallets/ceres-liquidity-locker/benchmarking/src/mock.rs @@ -8,7 +8,6 @@ use currencies::BasicCurrencyAdapter; use frame_support::traits::{Everything, GenesisBuild}; use frame_support::{construct_runtime, parameter_types}; -use frame_system; use common::prelude::Balance; use frame_system::pallet_prelude::BlockNumberFor; @@ -42,7 +41,7 @@ parameter_types! { pub GetPswapDistributionAccountId: AccountId = AccountId32::from([3; 32]); pub const GetDefaultSubscriptionFrequency: BlockNumber = 10; pub const GetBurnUpdateFrequency: BlockNumber = 14400; - pub GetIncentiveAssetId: AssetId = common::PSWAP.into(); + pub GetIncentiveAssetId: AssetId = common::PSWAP; pub GetParliamentAccountId: AccountId = AccountId32::from([8; 32]); pub GetXykFee: Fixed = fixed!(0.003); pub const MinimumPeriod: u64 = 5; diff --git a/pallets/ceres-liquidity-locker/src/lib.rs b/pallets/ceres-liquidity-locker/src/lib.rs index 617828daea..571ed56332 100644 --- a/pallets/ceres-liquidity-locker/src/lib.rs +++ b/pallets/ceres-liquidity-locker/src/lib.rs @@ -1,6 +1,4 @@ #![cfg_attr(not(feature = "std"), no_std)] -// TODO #167: fix clippy warnings -#![allow(clippy::all)] #[cfg(test)] mod mock; @@ -229,10 +227,11 @@ pub mod pallet { let mut locked_pool_tokens = 0; for locks in lockups.iter() { - if locks.asset_a == asset_a && locks.asset_b == asset_b { - if current_timestamp < locks.unlocking_timestamp { - locked_pool_tokens = locked_pool_tokens + locks.pool_tokens; - } + if locks.asset_a == asset_a + && locks.asset_b == asset_b + && current_timestamp < locks.unlocking_timestamp + { + locked_pool_tokens += locks.pool_tokens; } } @@ -257,7 +256,7 @@ pub mod pallet { } else { // Transfer CERES fee amount Assets::::transfer_from( - &T::CeresAssetId::get().into(), + &T::CeresAssetId::get(), &user, &FeesOptionTwoAccount::::get(), FeesOptionTwoCeresAmount::::get(), @@ -274,8 +273,8 @@ pub mod pallet { )?; } - pool_tokens = T::XYKPool::balance_of_pool_provider(pool_account.clone(), user.clone()) - .unwrap_or(0); + pool_tokens = + T::XYKPool::balance_of_pool_provider(pool_account, user.clone()).unwrap_or(0); T::DemeterFarmingPlatform::update_pool_tokens( user.clone(), pool_tokens, @@ -367,7 +366,7 @@ pub mod pallet { withdrawing_amount: Balance, ) -> bool { // Get lock info of extrinsic caller - let lockups = >::get(&user); + let lockups = >::get(user); let current_timestamp = Timestamp::::get(); // Get pool account @@ -380,27 +379,23 @@ pub mod pallet { // Calculate number of pool tokens to be locked let pool_tokens = - T::XYKPool::balance_of_pool_provider(pool_account.clone(), user.clone()) - .unwrap_or(0); + T::XYKPool::balance_of_pool_provider(pool_account, user.clone()).unwrap_or(0); if pool_tokens == 0 { return false; } let mut locked_pool_tokens = 0; for locks in lockups.iter() { - if locks.asset_a == asset_a && locks.asset_b == asset_b { - if current_timestamp < locks.unlocking_timestamp { - locked_pool_tokens = locked_pool_tokens + locks.pool_tokens; - } + if locks.asset_a == asset_a + && locks.asset_b == asset_b + && current_timestamp < locks.unlocking_timestamp + { + locked_pool_tokens += locks.pool_tokens; } } - let unlocked_pool_tokens = pool_tokens.checked_sub(locked_pool_tokens).unwrap_or(0); + let unlocked_pool_tokens = pool_tokens.saturating_sub(locked_pool_tokens); - if withdrawing_amount > pool_tokens || unlocked_pool_tokens >= withdrawing_amount { - true - } else { - false - } + withdrawing_amount > pool_tokens || unlocked_pool_tokens >= withdrawing_amount } /// Pay Locker fees in LP tokens @@ -423,15 +418,14 @@ pub mod pallet { FeesOptionTwoAccount::::get() }; - let result = T::XYKPool::transfer_lp_tokens( + T::XYKPool::transfer_lp_tokens( pool_account, asset_a, asset_b, user, fee_account, pool_tokens, - ); - return result; + ) } } } diff --git a/pallets/ceres-liquidity-locker/src/mock.rs b/pallets/ceres-liquidity-locker/src/mock.rs index 0023bb64e2..919368278c 100644 --- a/pallets/ceres-liquidity-locker/src/mock.rs +++ b/pallets/ceres-liquidity-locker/src/mock.rs @@ -7,7 +7,7 @@ use currencies::BasicCurrencyAdapter; use frame_support::traits::{Everything, GenesisBuild, Hooks}; use frame_support::weights::Weight; use frame_support::{construct_runtime, parameter_types}; -use frame_system; + use hex_literal::hex; use orml_traits::parameter_type_with_key; use permissions::{Scope, MANAGE_DEX}; @@ -44,8 +44,8 @@ parameter_types! { pub const MaximumBlockWeight: Weight = Weight::from_parts(1024, 0); pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); - pub GetBaseAssetId: AssetId = common::AssetId32::from_bytes(hex!("0200000000000000000000000000000000000000000000000000000000000000").into()); - pub GetIncentiveAssetId: AssetId = common::AssetId32::from_bytes(hex!("0200050000000000000000000000000000000000000000000000000000000000").into()); + pub GetBaseAssetId: AssetId = common::AssetId32::from_bytes(hex!("0200000000000000000000000000000000000000000000000000000000000000")); + pub GetIncentiveAssetId: AssetId = common::AssetId32::from_bytes(hex!("0200050000000000000000000000000000000000000000000000000000000000")); pub const ExistentialDeposit: u128 = 0; pub GetPswapDistributionAccountId: AccountId = 3u128; pub const GetDefaultSubscriptionFrequency: BlockNumber = 10; @@ -289,14 +289,14 @@ impl Default for ExtBuilder { initial_dex_list: vec![( DEX_A_ID, DEXInfo { - base_asset_id: XOR.into(), - synthetic_base_asset_id: XST.into(), + base_asset_id: XOR, + synthetic_base_asset_id: XST, is_public: true, }, )], endowed_accounts: vec![ - (ALICE(), CERES_ASSET_ID.into(), balance!(2000)), - (BOB(), CERES_ASSET_ID.into(), balance!(1000)), + (ALICE(), CERES_ASSET_ID, balance!(2000)), + (BOB(), CERES_ASSET_ID, balance!(1000)), ], initial_permission_owners: vec![( MANAGE_DEX, diff --git a/pallets/ceres-liquidity-locker/src/tests.rs b/pallets/ceres-liquidity-locker/src/tests.rs index 87aa5b500d..423cf02f98 100644 --- a/pallets/ceres-liquidity-locker/src/tests.rs +++ b/pallets/ceres-liquidity-locker/src/tests.rs @@ -18,13 +18,13 @@ where { let mut ext = ExtBuilder::default().build(); let dex_id = DEX_A_ID; - let xor: AssetId = XOR.into(); - let ceres: AssetId = CERES_ASSET_ID.into(); + let xor: AssetId = XOR; + let ceres: AssetId = CERES_ASSET_ID; ext.execute_with(|| { assert_ok!(assets::Pallet::::register_asset_id( ALICE(), - XOR.into(), + XOR, AssetSymbol(b"XOR".to_vec()), AssetName(b"SORA".to_vec()), DEFAULT_BALANCE_PRECISION, @@ -36,7 +36,7 @@ where assert_ok!(assets::Pallet::::register_asset_id( ALICE(), - CERES_ASSET_ID.into(), + CERES_ASSET_ID, AssetSymbol(b"CERES".to_vec()), AssetName(b"Ceres".to_vec()), DEFAULT_BALANCE_PRECISION, @@ -48,23 +48,23 @@ where assert_ok!(trading_pair::Pallet::::register( RuntimeOrigin::signed(BOB()), - dex_id.clone(), - XOR.into(), - CERES_ASSET_ID.into() + dex_id, + XOR, + CERES_ASSET_ID )); assert_ok!(pool_xyk::Pallet::::initialize_pool( RuntimeOrigin::signed(BOB()), - dex_id.clone(), - XOR.into(), - CERES_ASSET_ID.into(), + dex_id, + XOR, + CERES_ASSET_ID, )); assert!( trading_pair::Pallet::::is_source_enabled_for_trading_pair( &dex_id, - &XOR.into(), - &CERES_ASSET_ID.into(), + &XOR, + &CERES_ASSET_ID, LiquiditySourceType::XYKPool, ) .expect("Failed to query trading pair status.") @@ -72,13 +72,13 @@ where let (_tpair, tech_acc_id) = pool_xyk::Pallet::::tech_account_from_dex_and_asset_pair( - dex_id.clone(), - XOR.into(), - CERES_ASSET_ID.into(), + dex_id, + XOR, + CERES_ASSET_ID, ) .unwrap(); - let fee_acc = tech_acc_id.clone().to_fee_account().unwrap(); + let fee_acc = tech_acc_id.to_fee_account().unwrap(); let repr: AccountId = technical::Pallet::::tech_account_id_to_account_id(&tech_acc_id).unwrap(); let fee_repr: AccountId = @@ -136,7 +136,7 @@ where assert_eq!( pool_xyk::Pallet::::properties(xor, ceres), - Some((repr.clone(), fee_repr.clone())) + Some((repr, fee_repr)) ); tests(dex_id); @@ -146,8 +146,8 @@ where #[test] fn lock_liquidity_ok_with_first_fee_option() { preset_initial(|dex_id| { - let base_asset: AssetId = XOR.into(); - let target_asset: AssetId = CERES_ASSET_ID.into(); + let base_asset: AssetId = XOR; + let target_asset: AssetId = CERES_ASSET_ID; // Deposit liquidity to XOR/CERES pair assert_ok!(pool_xyk::Pallet::::deposit_liquidity( @@ -173,7 +173,7 @@ fn lock_liquidity_ok_with_first_fee_option() { // Calculate number of pool tokens of user's account let pool_tokens: Balance = ::XYKPool::pool_providers( - pool_account.clone(), + pool_account, ALICE(), ) .expect("User is not pool provider"); @@ -201,7 +201,7 @@ fn lock_liquidity_ok_with_first_fee_option() { // Calculate number of user's pool tokens after locking let pool_tokens_after_locking = ::XYKPool::pool_providers( - pool_account.clone(), + pool_account, ALICE(), ) .expect("User is not pool provider"); @@ -213,7 +213,7 @@ fn lock_liquidity_ok_with_first_fee_option() { let fee_account: AccountId = ceres_liquidity_locker::FeesOptionOneAccount::::get(); let fee_account_pool_tokens_after_locking = ::XYKPool::pool_providers( - pool_account.clone(), + pool_account, fee_account, ) .expect("User is not pool provider"); @@ -223,7 +223,7 @@ fn lock_liquidity_ok_with_first_fee_option() { let target_asset_expected = ::XYKPool::account_pools( fee_account, - &base_asset, + base_asset, ); assert_eq!( target_asset_expected.get(&target_asset), @@ -235,8 +235,8 @@ fn lock_liquidity_ok_with_first_fee_option() { #[test] fn lock_liquidity_ok_with_second_fee_option() { preset_initial(|dex_id| { - let base_asset: AssetId = XOR.into(); - let target_asset: AssetId = CERES_ASSET_ID.into(); + let base_asset: AssetId = XOR; + let target_asset: AssetId = CERES_ASSET_ID; // Deposit liquidity to XOR/CERES pair assert_ok!(pool_xyk::Pallet::::deposit_liquidity( @@ -262,7 +262,7 @@ fn lock_liquidity_ok_with_second_fee_option() { // Calculate number of pool tokens of user's account let pool_tokens: Balance = ::XYKPool::pool_providers( - pool_account.clone(), + pool_account, ALICE(), ) .expect("User is not pool provider"); @@ -298,7 +298,7 @@ fn lock_liquidity_ok_with_second_fee_option() { // Calculate number of user's pool tokens after locking let pool_tokens_after_locking = ::XYKPool::pool_providers( - pool_account.clone(), + pool_account, ALICE(), ) .expect("User is not pool provider"); @@ -309,8 +309,8 @@ fn lock_liquidity_ok_with_second_fee_option() { // Calculate number of fee account pool tokens after locking let fee_account_pool_tokens_after_locking = ::XYKPool::pool_providers( - pool_account.clone(), - fee_account.clone(), + pool_account, + fee_account, ) .expect("User is not pool provider"); assert_eq!(fee_account_pool_tokens_after_locking, lp_fee); @@ -319,7 +319,7 @@ fn lock_liquidity_ok_with_second_fee_option() { let target_asset_expected = ::XYKPool::account_pools( fee_account, - &base_asset, + base_asset, ); assert_eq!( target_asset_expected.get(&target_asset), @@ -334,8 +334,8 @@ fn lock_liquidity_invalid_percentage() { assert_err!( ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get() + 1, balance!(1.1), true, @@ -351,8 +351,8 @@ fn lock_liquidity_invalid_unlocking_timestamp() { assert_err!( ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get(), balance!(0.8), true, @@ -368,8 +368,8 @@ fn lock_liquidity_pool_does_not_exist() { assert_err!( ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - DOT.into(), + XOR, + DOT, pallet_timestamp::Pallet::::get() + 1, balance!(0.5), true, @@ -385,8 +385,8 @@ fn lock_liquidity_user_is_not_pool_provider() { assert_err!( ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get() + 1, balance!(0.5), true @@ -402,8 +402,8 @@ fn lock_liquidity_insufficient_liquidity_to_lock() { assert_ok!(pool_xyk::Pallet::::deposit_liquidity( RuntimeOrigin::signed(ALICE()), dex_id, - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(360000), balance!(144000), balance!(360000), @@ -413,8 +413,8 @@ fn lock_liquidity_insufficient_liquidity_to_lock() { // Lock 50% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get() + 5, balance!(0.5), true @@ -423,8 +423,8 @@ fn lock_liquidity_insufficient_liquidity_to_lock() { // Lock 30% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get() + 5, balance!(0.3), true @@ -434,8 +434,8 @@ fn lock_liquidity_insufficient_liquidity_to_lock() { assert_err!( ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get() + 5, balance!(0.3), true @@ -481,8 +481,8 @@ fn should_remove_expired_lockups() { assert_ok!(pool_xyk::Pallet::::deposit_liquidity( RuntimeOrigin::signed(ALICE()), dex_id, - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(360000), balance!(144000), balance!(360000), @@ -492,8 +492,8 @@ fn should_remove_expired_lockups() { // Lock 50% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, current_timestamp + 5, balance!(0.5), true @@ -502,8 +502,8 @@ fn should_remove_expired_lockups() { // Lock 30% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, current_timestamp + 500, balance!(0.3), true @@ -512,8 +512,8 @@ fn should_remove_expired_lockups() { assert_ok!(pool_xyk::Pallet::::deposit_liquidity( RuntimeOrigin::signed(BOB()), dex_id, - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(360000), balance!(144000), balance!(360000), @@ -523,8 +523,8 @@ fn should_remove_expired_lockups() { // Lock 50% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(BOB()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, current_timestamp + 250, balance!(0.5), true @@ -533,8 +533,8 @@ fn should_remove_expired_lockups() { // Lock 30% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(BOB()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, current_timestamp + 20000, balance!(0.3), true @@ -563,14 +563,13 @@ fn should_remove_expired_lockups() { #[test] fn check_if_has_enough_unlocked_liquidity_pool_does_not_exist() { preset_initial(|_dex_id| { - assert_eq!( - ceres_liquidity_locker::Pallet::::check_if_has_enough_unlocked_liquidity( + assert!( + !ceres_liquidity_locker::Pallet::::check_if_has_enough_unlocked_liquidity( &ALICE(), - XOR.into(), - DOT.into(), + XOR, + DOT, balance!(0.3), - ), - false + ) ); }); } @@ -578,14 +577,13 @@ fn check_if_has_enough_unlocked_liquidity_pool_does_not_exist() { #[test] fn check_if_has_enough_unlocked_liquidity_user_is_not_pool_provider() { preset_initial(|_dex_id| { - assert_eq!( - ceres_liquidity_locker::Pallet::::check_if_has_enough_unlocked_liquidity( + assert!( + !ceres_liquidity_locker::Pallet::::check_if_has_enough_unlocked_liquidity( &ALICE(), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(0.3) - ), - false + ) ); }); } @@ -596,8 +594,8 @@ fn check_if_has_enough_unlocked_liquidity_true() { assert_ok!(pool_xyk::Pallet::::deposit_liquidity( RuntimeOrigin::signed(ALICE()), dex_id, - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(360), balance!(144), balance!(360), @@ -607,21 +605,20 @@ fn check_if_has_enough_unlocked_liquidity_true() { // Lock 50% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get() + 5, balance!(0.5), true )); - assert_eq!( + assert!( ceres_liquidity_locker::Pallet::::check_if_has_enough_unlocked_liquidity( &ALICE(), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(1) - ), - true + ) ); }); } @@ -632,8 +629,8 @@ fn check_if_has_enough_unlocked_liquidity_false() { assert_ok!(pool_xyk::Pallet::::deposit_liquidity( RuntimeOrigin::signed(ALICE()), dex_id, - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(360), balance!(144), balance!(360), @@ -643,21 +640,20 @@ fn check_if_has_enough_unlocked_liquidity_false() { // Lock 50% of LP tokens assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( RuntimeOrigin::signed(ALICE()), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, pallet_timestamp::Pallet::::get() + 5, balance!(1), true )); - assert_eq!( - ceres_liquidity_locker::Pallet::::check_if_has_enough_unlocked_liquidity( + assert!( + !ceres_liquidity_locker::Pallet::::check_if_has_enough_unlocked_liquidity( &ALICE(), - XOR.into(), - CERES_ASSET_ID.into(), + XOR, + CERES_ASSET_ID, balance!(10) - ), - false + ) ); }); } @@ -674,8 +670,8 @@ fn liquidity_locker_storage_migration_works() { ValueQuery, >; - let base_asset: AssetId = XOR.into(); - let target_asset: AssetId = CERES_ASSET_ID.into(); + let base_asset: AssetId = XOR; + let target_asset: AssetId = CERES_ASSET_ID; let mut alice_vec: Vec<(Balance, BlockNumber, AssetIdOf, AssetIdOf)> = Vec::new(); diff --git a/pallets/ceres-staking/src/benchmarking.rs b/pallets/ceres-staking/src/benchmarking.rs index b23d5c9c35..6f444db3d3 100644 --- a/pallets/ceres-staking/src/benchmarking.rs +++ b/pallets/ceres-staking/src/benchmarking.rs @@ -37,11 +37,11 @@ benchmarks! { let caller = alice::(); let amount = balance!(100); let asset_id = T::AssetId::from(CERES_ASSET_ID); - let asset_owner = Assets::::asset_owner(&asset_id).unwrap(); + let asset_owner = Assets::::asset_owner(asset_id).unwrap(); frame_system::Pallet::::inc_providers(&caller); let _ = Assets::::mint( - RawOrigin::Signed(asset_owner.clone()).into(), + RawOrigin::Signed(asset_owner).into(), CERES_ASSET_ID.into(), caller.clone(), balance!(101) @@ -55,11 +55,11 @@ benchmarks! { let caller = alice::(); let amount = balance!(100); let asset_id = T::AssetId::from(CERES_ASSET_ID); - let asset_owner = Assets::::asset_owner(&asset_id).unwrap(); + let asset_owner = Assets::::asset_owner(asset_id).unwrap(); frame_system::Pallet::::inc_providers(&caller); Assets::::mint( - RawOrigin::Signed(asset_owner.clone()).into(), + RawOrigin::Signed(asset_owner).into(), CERES_ASSET_ID.into(), caller.clone(), balance!(101) diff --git a/pallets/ceres-staking/src/lib.rs b/pallets/ceres-staking/src/lib.rs index db24e15b45..d0548159ea 100644 --- a/pallets/ceres-staking/src/lib.rs +++ b/pallets/ceres-staking/src/lib.rs @@ -1,6 +1,4 @@ #![cfg_attr(not(feature = "std"), no_std)] -// TODO #167: fix clippy warnings -#![allow(clippy::all)] pub mod weights; @@ -153,7 +151,7 @@ pub mod pallet { let mut staking_info = >::get(&source); // Set staking info - staking_info.deposited = staking_info.deposited + amount; + staking_info.deposited += amount; // Put updated staking info into storage >::insert(&source, staking_info); diff --git a/pallets/ceres-staking/src/mock.rs b/pallets/ceres-staking/src/mock.rs index 4d93cdca74..986db5b379 100644 --- a/pallets/ceres-staking/src/mock.rs +++ b/pallets/ceres-staking/src/mock.rs @@ -12,7 +12,7 @@ use currencies::BasicCurrencyAdapter; use frame_support::traits::{Everything, GenesisBuild, Hooks}; use frame_support::weights::Weight; use frame_support::{construct_runtime, parameter_types}; -use frame_system; + use frame_system::pallet_prelude::BlockNumberFor; use sp_core::H256; use sp_runtime::testing::Header; @@ -185,6 +185,7 @@ impl pallet_balances::Config for Runtime { type ReserveIdentifier = (); } +#[allow(clippy::type_complexity)] pub struct ExtBuilder { pub endowed_assets: Vec<( AssetId, diff --git a/pallets/ceres-staking/src/tests.rs b/pallets/ceres-staking/src/tests.rs index 9a07e0a00d..8300339b98 100644 --- a/pallets/ceres-staking/src/tests.rs +++ b/pallets/ceres-staking/src/tests.rs @@ -1,215 +1,201 @@ -mod tests { - use crate::mock::*; - use crate::{pallet, Error}; - use common::prelude::FixedWrapper; - use common::{balance, AssetInfoProvider, CERES_ASSET_ID}; - use frame_support::{assert_err, assert_ok, PalletId}; - use sp_runtime::traits::AccountIdConversion; +use crate::mock::*; +use crate::{pallet, Error}; +use common::prelude::FixedWrapper; +use common::{balance, AssetInfoProvider, CERES_ASSET_ID}; +use frame_support::{assert_err, assert_ok, PalletId}; +use sp_runtime::traits::AccountIdConversion; - #[test] - fn should_not_allow_deposit_to_full_staking_pool() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresStaking::deposit(RuntimeOrigin::signed(ALICE), balance!(7201)), - Error::::StakingPoolIsFull - ); - }); - } +#[test] +fn should_not_allow_deposit_to_full_staking_pool() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresStaking::deposit(RuntimeOrigin::signed(ALICE), balance!(7201)), + Error::::StakingPoolIsFull + ); + }); +} - #[test] - fn should_deposit_to_staking_pool() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - // Deposit 500 from Alice's account - assert_ok!(CeresStaking::deposit( - RuntimeOrigin::signed(ALICE), - balance!(500) - )); +#[test] +fn should_deposit_to_staking_pool() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + // Deposit 500 from Alice's account + assert_ok!(CeresStaking::deposit( + RuntimeOrigin::signed(ALICE), + balance!(500) + )); - // Get staking pool account id - let staking_pool = PalletId(*b"cerstake").into_account_truncating(); + // Get staking pool account id + let staking_pool = PalletId(*b"cerstake").into_account_truncating(); - // Check Alice's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(6800) - ); - // Check staking pool's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &staking_pool) - .expect("Failed to query free balance."), - balance!(500) - ); - // Check total deposited - assert_eq!(pallet::TotalDeposited::::get(), balance!(500)); - // Check Stakers map - let staking_info = pallet::Stakers::::get(&ALICE); - assert_eq!(staking_info.deposited, balance!(500)); + // Check Alice's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(6800) + ); + // Check staking pool's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &staking_pool) + .expect("Failed to query free balance."), + balance!(500) + ); + // Check total deposited + assert_eq!(pallet::TotalDeposited::::get(), balance!(500)); + // Check Stakers map + let staking_info = pallet::Stakers::::get(ALICE); + assert_eq!(staking_info.deposited, balance!(500)); - // Deposit 250 more from Alice's account - assert_ok!(CeresStaking::deposit( - RuntimeOrigin::signed(ALICE), - balance!(250) - )); - // Check Alice's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(6550) - ); - // Check staking pool's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &staking_pool) - .expect("Failed to query free balance."), - balance!(750) - ); - // Check total deposited - assert_eq!(pallet::TotalDeposited::::get(), balance!(750)); - // Check Stakers map - let staking_info = pallet::Stakers::::get(&ALICE); - assert_eq!(staking_info.deposited, balance!(750)); + // Deposit 250 more from Alice's account + assert_ok!(CeresStaking::deposit( + RuntimeOrigin::signed(ALICE), + balance!(250) + )); + // Check Alice's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(6550) + ); + // Check staking pool's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &staking_pool) + .expect("Failed to query free balance."), + balance!(750) + ); + // Check total deposited + assert_eq!(pallet::TotalDeposited::::get(), balance!(750)); + // Check Stakers map + let staking_info = pallet::Stakers::::get(ALICE); + assert_eq!(staking_info.deposited, balance!(750)); - // Deposit 50 from BOB's account - assert_ok!(CeresStaking::deposit( - RuntimeOrigin::signed(BOB), - balance!(50) - )); - // Check Bob's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &BOB).expect("Failed to query free balance."), - balance!(50) - ); - // Check staking pool's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &staking_pool) - .expect("Failed to query free balance."), - balance!(800) - ); - // Check total deposited - assert_eq!(pallet::TotalDeposited::::get(), balance!(800)); - // Check Stakers map for Alice - let staking_info_alice = pallet::Stakers::::get(&ALICE); - assert_eq!(staking_info_alice.deposited, balance!(750)); - // Check Stakers map for Bob - let staking_info_bob = pallet::Stakers::::get(&BOB); - assert_eq!(staking_info_bob.deposited, balance!(50)); - }); - } + // Deposit 50 from BOB's account + assert_ok!(CeresStaking::deposit( + RuntimeOrigin::signed(BOB), + balance!(50) + )); + // Check Bob's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &BOB).expect("Failed to query free balance."), + balance!(50) + ); + // Check staking pool's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &staking_pool) + .expect("Failed to query free balance."), + balance!(800) + ); + // Check total deposited + assert_eq!(pallet::TotalDeposited::::get(), balance!(800)); + // Check Stakers map for Alice + let staking_info_alice = pallet::Stakers::::get(ALICE); + assert_eq!(staking_info_alice.deposited, balance!(750)); + // Check Stakers map for Bob + let staking_info_bob = pallet::Stakers::::get(BOB); + assert_eq!(staking_info_bob.deposited, balance!(50)); + }); +} - #[test] - fn should_withdraw_from_staking_pool() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - // Deposit 1200 from Alice's account - assert_ok!(CeresStaking::deposit( - RuntimeOrigin::signed(ALICE), - balance!(1200) - )); - // Deposit 50 from Bob's account - assert_ok!(CeresStaking::deposit( - RuntimeOrigin::signed(BOB), - balance!(50) - )); +#[test] +fn should_withdraw_from_staking_pool() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + // Deposit 1200 from Alice's account + assert_ok!(CeresStaking::deposit( + RuntimeOrigin::signed(ALICE), + balance!(1200) + )); + // Deposit 50 from Bob's account + assert_ok!(CeresStaking::deposit( + RuntimeOrigin::signed(BOB), + balance!(50) + )); - // Add rewards to Alice - let mut staking_info = pallet::Stakers::::get(&ALICE); - staking_info.rewards = staking_info.rewards + balance!(11); - pallet::Stakers::::insert(&ALICE, staking_info); + // Add rewards to Alice + let mut staking_info = pallet::Stakers::::get(ALICE); + staking_info.rewards += balance!(11); + pallet::Stakers::::insert(ALICE, staking_info); - // Withdraw Alice's stake - assert_ok!(CeresStaking::withdraw(RuntimeOrigin::signed(ALICE))); - // Check Alice's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(7311) - ); - // Check total deposited - assert_eq!(pallet::TotalDeposited::::get(), balance!(50)); - // Check Stakers map - let staking_info_alice = pallet::Stakers::::get(&ALICE); - assert_eq!(staking_info_alice.deposited, balance!(0)); - assert_eq!(staking_info_alice.rewards, balance!(0)); - // Check staking pool's balance - let staking_pool = PalletId(*b"cerstake").into_account_truncating(); - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &staking_pool) - .expect("Failed to query free balance."), - balance!(39) - ); - }); - } + // Withdraw Alice's stake + assert_ok!(CeresStaking::withdraw(RuntimeOrigin::signed(ALICE))); + // Check Alice's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(7311) + ); + // Check total deposited + assert_eq!(pallet::TotalDeposited::::get(), balance!(50)); + // Check Stakers map + let staking_info_alice = pallet::Stakers::::get(ALICE); + assert_eq!(staking_info_alice.deposited, balance!(0)); + assert_eq!(staking_info_alice.rewards, balance!(0)); + // Check staking pool's balance + let staking_pool = PalletId(*b"cerstake").into_account_truncating(); + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &staking_pool) + .expect("Failed to query free balance."), + balance!(39) + ); + }); +} - #[test] - fn should_calculate_rewards_and_withdraw_from_staking_pool() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - // Deposit 500 from Alice's account - assert_ok!(CeresStaking::deposit( - RuntimeOrigin::signed(ALICE), - balance!(500) - )); - assert_ok!(CeresStaking::deposit( - RuntimeOrigin::signed(BOB), - balance!(50) - )); - run_to_block(14_440); - let diff = FixedWrapper::from(0.0001); - // Check remaining rewards - let remaining_rewards = pallet::RewardsRemaining::::get(); - assert_eq!( - (FixedWrapper::from(593.333333333) - FixedWrapper::from(remaining_rewards)) < diff, - true - ); - // Check Alice's staking rewards - let staking_info_alice = pallet::Stakers::::get(&ALICE); - assert_eq!( - (FixedWrapper::from(staking_info_alice.rewards) - FixedWrapper::from(6.0606060606)) - < diff, - true - ); - // Check Bob's staking rewards - let staking_info_bob = pallet::Stakers::::get(&BOB); - assert_eq!( - (FixedWrapper::from(staking_info_bob.rewards) - FixedWrapper::from(0.606060606)) - < diff, - true - ); - // Withdraw Alice's stake - assert_ok!(CeresStaking::withdraw(RuntimeOrigin::signed(ALICE))); - // Check Alice's balance after withdrawal - let alice_balance = Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."); - assert_eq!( - (FixedWrapper::from(7306.0606060606) - FixedWrapper::from(alice_balance)) < diff, - true - ); - }); - } +#[test] +fn should_calculate_rewards_and_withdraw_from_staking_pool() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + // Deposit 500 from Alice's account + assert_ok!(CeresStaking::deposit( + RuntimeOrigin::signed(ALICE), + balance!(500) + )); + assert_ok!(CeresStaking::deposit( + RuntimeOrigin::signed(BOB), + balance!(50) + )); + run_to_block(14_440); + let diff = FixedWrapper::from(0.0001); + // Check remaining rewards + let remaining_rewards = pallet::RewardsRemaining::::get(); + assert!((FixedWrapper::from(593.333333333) - FixedWrapper::from(remaining_rewards)) < diff); + // Check Alice's staking rewards + let staking_info_alice = pallet::Stakers::::get(ALICE); + assert!( + (FixedWrapper::from(staking_info_alice.rewards) - FixedWrapper::from(6.0606060606)) + < diff + ); + // Check Bob's staking rewards + let staking_info_bob = pallet::Stakers::::get(BOB); + assert!( + (FixedWrapper::from(staking_info_bob.rewards) - FixedWrapper::from(0.606060606)) < diff + ); + // Withdraw Alice's stake + assert_ok!(CeresStaking::withdraw(RuntimeOrigin::signed(ALICE))); + // Check Alice's balance after withdrawal + let alice_balance = + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."); + assert!((FixedWrapper::from(7306.0606060606) - FixedWrapper::from(alice_balance)) < diff); + }); +} - #[test] - fn change_rewards_remaining_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresStaking::change_rewards_remaining(RuntimeOrigin::signed(ALICE), balance!(100)), - Error::::Unauthorized - ); - }); - } +#[test] +fn change_rewards_remaining_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresStaking::change_rewards_remaining(RuntimeOrigin::signed(ALICE), balance!(100)), + Error::::Unauthorized + ); + }); +} - #[test] - fn change_rewards_remaining_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_ok!(CeresStaking::change_rewards_remaining( - RuntimeOrigin::signed(pallet::AuthorityAccount::::get()), - balance!(100) - )); +#[test] +fn change_rewards_remaining_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_ok!(CeresStaking::change_rewards_remaining( + RuntimeOrigin::signed(pallet::AuthorityAccount::::get()), + balance!(100) + )); - assert_eq!(pallet::RewardsRemaining::::get(), balance!(100)); - }); - } + assert_eq!(pallet::RewardsRemaining::::get(), balance!(100)); + }); } diff --git a/pallets/ceres-token-locker/src/benchmarking.rs b/pallets/ceres-token-locker/src/benchmarking.rs index 3271a16166..8346aeb7a8 100644 --- a/pallets/ceres-token-locker/src/benchmarking.rs +++ b/pallets/ceres-token-locker/src/benchmarking.rs @@ -36,14 +36,14 @@ benchmarks! { lock_tokens { let caller = alice::(); let asset_id = T::AssetId::from(CERES_ASSET_ID); - let asset_owner = Assets::::asset_owner(&asset_id).unwrap(); + let asset_owner = Assets::::asset_owner(asset_id).unwrap(); frame_system::Pallet::::inc_providers(&caller); let timestamp = Timestamp::::get() + 10u32.into(); let locked_tokens = balance!(2000); let token_balance = locked_tokens + balance!(100); Assets::::mint( - RawOrigin::Signed(asset_owner.clone()).into(), + RawOrigin::Signed(asset_owner).into(), CERES_ASSET_ID.into(), caller.clone(), token_balance @@ -63,14 +63,14 @@ benchmarks! { withdraw_tokens { let caller = alice::(); let asset_id = T::AssetId::from(CERES_ASSET_ID); - let asset_owner = Assets::::asset_owner(&asset_id).unwrap(); + let asset_owner = Assets::::asset_owner(asset_id).unwrap(); frame_system::Pallet::::inc_providers(&caller); let timestamp = Timestamp::::get() + 10u32.into(); let locked_tokens = balance!(2000); let token_balance = locked_tokens + balance!(100); Assets::::mint( - RawOrigin::Signed(asset_owner.clone()).into(), + RawOrigin::Signed(asset_owner).into(), CERES_ASSET_ID.into(), caller.clone(), token_balance diff --git a/pallets/ceres-token-locker/src/lib.rs b/pallets/ceres-token-locker/src/lib.rs index 19b6be02cf..185d5dc56c 100644 --- a/pallets/ceres-token-locker/src/lib.rs +++ b/pallets/ceres-token-locker/src/lib.rs @@ -1,6 +1,4 @@ #![cfg_attr(not(feature = "std"), no_std)] -// TODO #167: fix clippy warnings -#![allow(clippy::all)] pub mod migrations; pub mod weights; diff --git a/pallets/ceres-token-locker/src/mock.rs b/pallets/ceres-token-locker/src/mock.rs index ea833923a0..bc35124c7d 100644 --- a/pallets/ceres-token-locker/src/mock.rs +++ b/pallets/ceres-token-locker/src/mock.rs @@ -9,7 +9,7 @@ use currencies::BasicCurrencyAdapter; use frame_support::traits::{Everything, GenesisBuild, Hooks}; use frame_support::weights::Weight; use frame_support::{construct_runtime, parameter_types}; -use frame_system; + use frame_system::pallet_prelude::BlockNumberFor; use sp_core::H256; use sp_runtime::testing::Header; @@ -61,7 +61,7 @@ parameter_types! { pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); pub GetXykFee: Fixed = fixed!(0.003); - pub GetIncentiveAssetId: AssetId = common::PSWAP.into(); + pub GetIncentiveAssetId: AssetId = common::PSWAP; pub const GetDefaultSubscriptionFrequency: BlockNumber = 10; pub const GetBurnUpdateFrequency: BlockNumber = 14400; pub GetParliamentAccountId: AccountId = 100; @@ -259,6 +259,7 @@ impl pallet_balances::Config for Runtime { type ReserveIdentifier = (); } +#[allow(clippy::type_complexity)] pub struct ExtBuilder { endowed_assets: Vec<( AssetId, diff --git a/pallets/ceres-token-locker/src/tests.rs b/pallets/ceres-token-locker/src/tests.rs index 97234c0fef..4eb0248072 100644 --- a/pallets/ceres-token-locker/src/tests.rs +++ b/pallets/ceres-token-locker/src/tests.rs @@ -1,298 +1,294 @@ -mod tests { - use crate::mock::*; - use crate::{pallet, AccountIdOf, AssetIdOf, Error}; - use common::{balance, generate_storage_instance, AssetInfoProvider, Balance, CERES_ASSET_ID}; - use frame_support::pallet_prelude::StorageMap; - use frame_support::storage::types::ValueQuery; - use frame_support::traits::Hooks; - use frame_support::{assert_err, assert_ok, Identity, PalletId}; - use sp_runtime::traits::AccountIdConversion; - - #[test] - fn lock_tokens_invalid_number_of_tokens() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresTokenLocker::lock_tokens( - RuntimeOrigin::signed(ALICE), - CERES_ASSET_ID, - pallet_timestamp::Pallet::::get() + 1, - balance!(0) - ), - Error::::InvalidNumberOfTokens - ); - }); - } - - #[test] - fn lock_tokens_invalid_unlocking_timestamp() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresTokenLocker::lock_tokens( - RuntimeOrigin::signed(ALICE), - CERES_ASSET_ID, - pallet_timestamp::Pallet::::get(), - balance!(1) - ), - Error::::InvalidUnlockingTimestamp - ); - }); - } - - #[test] - fn lock_tokens_not_enough_funds() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresTokenLocker::lock_tokens( - RuntimeOrigin::signed(ALICE), - CERES_ASSET_ID, - pallet_timestamp::Pallet::::get() + 1, - balance!(3000) - ), - Error::::NotEnoughFunds - ); - }); - } - - #[test] - fn lock_tokens_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let unlocking_timestamp = pallet_timestamp::Pallet::::get() + 1; - let locked_tokens = balance!(2000); - assert_ok!(CeresTokenLocker::lock_tokens( +use crate::mock::*; +use crate::{pallet, AccountIdOf, AssetIdOf, Error}; +use common::{balance, generate_storage_instance, AssetInfoProvider, Balance, CERES_ASSET_ID}; +use frame_support::pallet_prelude::StorageMap; +use frame_support::storage::types::ValueQuery; +use frame_support::traits::Hooks; +use frame_support::{assert_err, assert_ok, Identity, PalletId}; +use sp_runtime::traits::AccountIdConversion; + +#[test] +fn lock_tokens_invalid_number_of_tokens() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresTokenLocker::lock_tokens( RuntimeOrigin::signed(ALICE), CERES_ASSET_ID, - unlocking_timestamp, - locked_tokens - ),); - - // Check ALICE's balances - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(990) - ); - - // Check pallet's balances - let token_locker = PalletId(*b"crstlock").into_account_truncating(); - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &token_locker) - .expect("Failed to query free balance."), - locked_tokens - ); - - // Check fee's balance - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &pallet::FeesAccount::::get()) - .expect("Failed to query free balance."), - balance!(10) - ); - - // Check TokenLockerData map - let token_locker_vec = pallet::TokenLockerData::::get(&ALICE); - assert_eq!(token_locker_vec.len(), 1); - assert_eq!(token_locker_vec.get(0).unwrap().asset_id, CERES_ASSET_ID); - assert_eq!( - token_locker_vec.get(0).unwrap().unlocking_timestamp, - unlocking_timestamp - ); - assert_eq!(token_locker_vec.get(0).unwrap().tokens, locked_tokens); - }); - } - - #[test] - fn withdraw_tokens_invalid_number_of_tokens() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresTokenLocker::withdraw_tokens( - RuntimeOrigin::signed(ALICE), - CERES_ASSET_ID, - pallet_timestamp::Pallet::::get() + 1, - balance!(0) - ), - Error::::InvalidNumberOfTokens - ); - }); - } - - #[test] - fn withdraw_tokens_not_unlocked_yet() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresTokenLocker::withdraw_tokens( - RuntimeOrigin::signed(ALICE), - CERES_ASSET_ID, - pallet_timestamp::Pallet::::get(), - balance!(1) - ), - Error::::NotUnlockedYet - ); - }); - } - - #[test] - fn withdraw_tokens_lock_info_does_not_exist() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let unlocking_timestamp = pallet_timestamp::Pallet::::get() + 1; - pallet_timestamp::Pallet::::set_timestamp(unlocking_timestamp + 1); - assert_err!( - CeresTokenLocker::withdraw_tokens( - RuntimeOrigin::signed(ALICE), - CERES_ASSET_ID, - 1u32.into(), - balance!(1) - ), - Error::::LockInfoDoesNotExist - ); - }); - } - - #[test] - fn withdraw_tokens_lock_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let unlocking_timestamp = pallet_timestamp::Pallet::::get() + 1; - let locked_tokens = balance!(2000); - - // Lock tokens - assert_ok!(CeresTokenLocker::lock_tokens( + pallet_timestamp::Pallet::::get() + 1, + balance!(0) + ), + Error::::InvalidNumberOfTokens + ); + }); +} + +#[test] +fn lock_tokens_invalid_unlocking_timestamp() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresTokenLocker::lock_tokens( RuntimeOrigin::signed(ALICE), CERES_ASSET_ID, - unlocking_timestamp, - locked_tokens - ),); + pallet_timestamp::Pallet::::get(), + balance!(1) + ), + Error::::InvalidUnlockingTimestamp + ); + }); +} - // Check TokenLockerData map - let mut token_locker_vec = pallet::TokenLockerData::::get(&ALICE); - assert_eq!(token_locker_vec.len(), 1); +#[test] +fn lock_tokens_not_enough_funds() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresTokenLocker::lock_tokens( + RuntimeOrigin::signed(ALICE), + CERES_ASSET_ID, + pallet_timestamp::Pallet::::get() + 1, + balance!(3000) + ), + Error::::NotEnoughFunds + ); + }); +} - pallet_timestamp::Pallet::::set_timestamp(unlocking_timestamp + 1); +#[test] +fn lock_tokens_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let unlocking_timestamp = pallet_timestamp::Pallet::::get() + 1; + let locked_tokens = balance!(2000); + assert_ok!(CeresTokenLocker::lock_tokens( + RuntimeOrigin::signed(ALICE), + CERES_ASSET_ID, + unlocking_timestamp, + locked_tokens + ),); + + // Check ALICE's balances + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(990) + ); + + // Check pallet's balances + let token_locker = PalletId(*b"crstlock").into_account_truncating(); + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &token_locker) + .expect("Failed to query free balance."), + locked_tokens + ); + + // Check fee's balance + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &pallet::FeesAccount::::get()) + .expect("Failed to query free balance."), + balance!(10) + ); + + // Check TokenLockerData map + let token_locker_vec = pallet::TokenLockerData::::get(ALICE); + assert_eq!(token_locker_vec.len(), 1); + assert_eq!(token_locker_vec.get(0).unwrap().asset_id, CERES_ASSET_ID); + assert_eq!( + token_locker_vec.get(0).unwrap().unlocking_timestamp, + unlocking_timestamp + ); + assert_eq!(token_locker_vec.get(0).unwrap().tokens, locked_tokens); + }); +} - // Unlock tokens - assert_ok!(CeresTokenLocker::withdraw_tokens( +#[test] +fn withdraw_tokens_invalid_number_of_tokens() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresTokenLocker::withdraw_tokens( RuntimeOrigin::signed(ALICE), CERES_ASSET_ID, - unlocking_timestamp, - locked_tokens - ),); - - // Check TokenLockerData map - token_locker_vec = pallet::TokenLockerData::::get(&ALICE); - assert_eq!(token_locker_vec.len(), 0); - - // Check ALICE's balances - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(2990) - ); - - // Check pallet's balances - let token_locker = PalletId(*b"crstlock").into_account_truncating(); - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &token_locker) - .expect("Failed to query free balance."), + pallet_timestamp::Pallet::::get() + 1, balance!(0) - ); - }); - } - - #[test] - fn change_fee_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - assert_err!( - CeresTokenLocker::change_fee(RuntimeOrigin::signed(ALICE), balance!(0.01)), - Error::::Unauthorized - ); - }); - } - - #[test] - fn change_fee_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let new_fee = balance!(0.01); - - assert_ok!(CeresTokenLocker::change_fee( - RuntimeOrigin::signed(pallet::AuthorityAccount::::get()), - new_fee - )); - - assert_eq!(pallet::FeeAmount::::get(), new_fee); - }); - } - - #[test] - fn token_locker_storage_migration_works() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - generate_storage_instance!(CeresTokenLocker, TokenLockerData); - type OldLockerData = StorageMap< - TokenLockerDataOldInstance, - Identity, - AccountIdOf, - Vec<(Balance, BlockNumber, AssetIdOf)>, - ValueQuery, - >; - - let mut alice_vec: Vec<(Balance, BlockNumber, AssetIdOf)> = Vec::new(); - alice_vec.push((balance!(5), 8660039u64, CERES_ASSET_ID)); - alice_vec.push((balance!(6), 16052893u64, CERES_ASSET_ID)); - - OldLockerData::insert(ALICE, alice_vec); - - let mut bob_vec: Vec<(Balance, BlockNumber, AssetIdOf)> = Vec::new(); - bob_vec.push((balance!(7), 3u64, CERES_ASSET_ID)); - - OldLockerData::insert(BOB, bob_vec); - - pallet_timestamp::Pallet::::set_timestamp(10000000); - run_to_block(5); - - // Storage migration - CeresTokenLocker::on_runtime_upgrade(); - - let lockups_alice = pallet::TokenLockerData::::get(&ALICE); - for lockup in lockups_alice { - if lockup.tokens == balance!(5) { - assert_eq!(lockup.unlocking_timestamp, 51970204000); - } else if lockup.tokens == balance!(6) { - assert_eq!(lockup.unlocking_timestamp, 96327328000); - } - } + ), + Error::::InvalidNumberOfTokens + ); + }); +} - let lockups_bob = pallet::TokenLockerData::::get(&BOB); - for lockup in lockups_bob { - assert_eq!(lockup.unlocking_timestamp, 9988000); - } +#[test] +fn withdraw_tokens_not_unlocked_yet() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresTokenLocker::withdraw_tokens( + RuntimeOrigin::signed(ALICE), + CERES_ASSET_ID, + pallet_timestamp::Pallet::::get(), + balance!(1) + ), + Error::::NotUnlockedYet + ); + }); +} - // Storage version should be V2 so no changes made - pallet_timestamp::Pallet::::set_timestamp(11000000); - run_to_block(10); +#[test] +fn withdraw_tokens_lock_info_does_not_exist() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let unlocking_timestamp = pallet_timestamp::Pallet::::get() + 1; + pallet_timestamp::Pallet::::set_timestamp(unlocking_timestamp + 1); + assert_err!( + CeresTokenLocker::withdraw_tokens( + RuntimeOrigin::signed(ALICE), + CERES_ASSET_ID, + 1u32.into(), + balance!(1) + ), + Error::::LockInfoDoesNotExist + ); + }); +} - // Storage migration - CeresTokenLocker::on_runtime_upgrade(); +#[test] +fn withdraw_tokens_lock_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let unlocking_timestamp = pallet_timestamp::Pallet::::get() + 1; + let locked_tokens = balance!(2000); + + // Lock tokens + assert_ok!(CeresTokenLocker::lock_tokens( + RuntimeOrigin::signed(ALICE), + CERES_ASSET_ID, + unlocking_timestamp, + locked_tokens + ),); + + // Check TokenLockerData map + let mut token_locker_vec = pallet::TokenLockerData::::get(ALICE); + assert_eq!(token_locker_vec.len(), 1); + + pallet_timestamp::Pallet::::set_timestamp(unlocking_timestamp + 1); + + // Unlock tokens + assert_ok!(CeresTokenLocker::withdraw_tokens( + RuntimeOrigin::signed(ALICE), + CERES_ASSET_ID, + unlocking_timestamp, + locked_tokens + ),); + + // Check TokenLockerData map + token_locker_vec = pallet::TokenLockerData::::get(ALICE); + assert_eq!(token_locker_vec.len(), 0); + + // Check ALICE's balances + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(2990) + ); + + // Check pallet's balances + let token_locker = PalletId(*b"crstlock").into_account_truncating(); + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &token_locker) + .expect("Failed to query free balance."), + balance!(0) + ); + }); +} - let lockups_alice = pallet::TokenLockerData::::get(&ALICE); - for lockup in lockups_alice { - if lockup.tokens == balance!(5) { - assert_eq!(lockup.unlocking_timestamp, 51970204000); - } else if lockup.tokens == balance!(6) { - assert_eq!(lockup.unlocking_timestamp, 96327328000); - } - } +#[test] +fn change_fee_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + assert_err!( + CeresTokenLocker::change_fee(RuntimeOrigin::signed(ALICE), balance!(0.01)), + Error::::Unauthorized + ); + }); +} - let lockups_bob = pallet::TokenLockerData::::get(&BOB); - for lockup in lockups_bob { - assert_eq!(lockup.unlocking_timestamp, 9988000); +#[test] +fn change_fee_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let new_fee = balance!(0.01); + + assert_ok!(CeresTokenLocker::change_fee( + RuntimeOrigin::signed(pallet::AuthorityAccount::::get()), + new_fee + )); + + assert_eq!(pallet::FeeAmount::::get(), new_fee); + }); +} + +#[test] +fn token_locker_storage_migration_works() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + generate_storage_instance!(CeresTokenLocker, TokenLockerData); + type OldLockerData = StorageMap< + TokenLockerDataOldInstance, + Identity, + AccountIdOf, + Vec<(Balance, BlockNumber, AssetIdOf)>, + ValueQuery, + >; + + let mut alice_vec: Vec<(Balance, BlockNumber, AssetIdOf)> = Vec::new(); + alice_vec.push((balance!(5), 8660039u64, CERES_ASSET_ID)); + alice_vec.push((balance!(6), 16052893u64, CERES_ASSET_ID)); + + OldLockerData::insert(ALICE, alice_vec); + + let mut bob_vec: Vec<(Balance, BlockNumber, AssetIdOf)> = Vec::new(); + bob_vec.push((balance!(7), 3u64, CERES_ASSET_ID)); + + OldLockerData::insert(BOB, bob_vec); + + pallet_timestamp::Pallet::::set_timestamp(10000000); + run_to_block(5); + + // Storage migration + CeresTokenLocker::on_runtime_upgrade(); + + let lockups_alice = pallet::TokenLockerData::::get(ALICE); + for lockup in lockups_alice { + if lockup.tokens == balance!(5) { + assert_eq!(lockup.unlocking_timestamp, 51970204000); + } else if lockup.tokens == balance!(6) { + assert_eq!(lockup.unlocking_timestamp, 96327328000); + } + } + + let lockups_bob = pallet::TokenLockerData::::get(BOB); + for lockup in lockups_bob { + assert_eq!(lockup.unlocking_timestamp, 9988000); + } + + // Storage version should be V2 so no changes made + pallet_timestamp::Pallet::::set_timestamp(11000000); + run_to_block(10); + + // Storage migration + CeresTokenLocker::on_runtime_upgrade(); + + let lockups_alice = pallet::TokenLockerData::::get(ALICE); + for lockup in lockups_alice { + if lockup.tokens == balance!(5) { + assert_eq!(lockup.unlocking_timestamp, 51970204000); + } else if lockup.tokens == balance!(6) { + assert_eq!(lockup.unlocking_timestamp, 96327328000); } - }); - } + } + + let lockups_bob = pallet::TokenLockerData::::get(BOB); + for lockup in lockups_bob { + assert_eq!(lockup.unlocking_timestamp, 9988000); + } + }); } diff --git a/pallets/demeter-farming-platform/benchmarking/src/lib.rs b/pallets/demeter-farming-platform/benchmarking/src/lib.rs index 875cee09bb..b069cab563 100644 --- a/pallets/demeter-farming-platform/benchmarking/src/lib.rs +++ b/pallets/demeter-farming-platform/benchmarking/src/lib.rs @@ -1,8 +1,6 @@ //! Demeter farming platform module benchmarking. #![cfg_attr(not(feature = "std"), no_std)] -// TODO #167: fix clippy warnings -#![allow(clippy::all)] use codec::Decode; use common::{ @@ -85,7 +83,7 @@ fn setup_benchmark_assets_only() -> Result<(), &'static str> { ); let _ = Assets::::register_asset_id( - owner.clone(), + owner, CERES_ASSET_ID.into(), AssetSymbol(b"CERES".to_vec()), AssetName(b"Ceres".to_vec()), @@ -101,14 +99,12 @@ fn setup_benchmark_assets_only() -> Result<(), &'static str> { fn run_to_block(n: u32) { while frame_system::Pallet::::block_number() < n.into() { - frame_system::Pallet::::on_finalize(frame_system::Pallet::::block_number().into()); + frame_system::Pallet::::on_finalize(frame_system::Pallet::::block_number()); frame_system::Pallet::::set_block_number( frame_system::Pallet::::block_number() + 1u32.into(), ); - frame_system::Pallet::::on_initialize(frame_system::Pallet::::block_number().into()); - DemeterFarmingPlatform::::on_initialize( - frame_system::Pallet::::block_number().into(), - ); + frame_system::Pallet::::on_initialize(frame_system::Pallet::::block_number()); + DemeterFarmingPlatform::::on_initialize(frame_system::Pallet::::block_number()); } } @@ -249,7 +245,7 @@ benchmarks! { let _ = Assets::::mint( RawOrigin::Signed(caller.clone()).into(), reward_asset.into(), - pallet_account.clone(), + pallet_account, balance!(20000) ); @@ -273,7 +269,7 @@ benchmarks! { // Add pool let _ = DemeterFarmingPlatform::::add_pool( - RawOrigin::Signed(authority.clone()).into(), + RawOrigin::Signed(authority).into(), reward_asset.into(), reward_asset.into(), reward_asset.into(), @@ -333,7 +329,7 @@ benchmarks! { let _ = Assets::::mint( RawOrigin::Signed(caller.clone()).into(), reward_asset.into(), - pallet_account.clone(), + pallet_account, balance!(20000) ); @@ -350,7 +346,7 @@ benchmarks! { // Add pool let _ = DemeterFarmingPlatform::::add_pool( - RawOrigin::Signed(authority.clone()).into(), + RawOrigin::Signed(authority).into(), reward_asset.into(), reward_asset.into(), reward_asset.into(), @@ -574,7 +570,7 @@ benchmarks! { let _ = Assets::::mint( RawOrigin::Signed(caller.clone()).into(), reward_asset.into(), - pallet_account.clone(), + pallet_account, balance!(20000) ); @@ -614,7 +610,7 @@ benchmarks! { ); }: _( RawOrigin::Signed(authority.clone()), - caller.clone().into(), + caller.clone(), reward_asset.into(), reward_asset.into(), reward_asset.into(), diff --git a/pallets/demeter-farming-platform/benchmarking/src/mock.rs b/pallets/demeter-farming-platform/benchmarking/src/mock.rs index 74b3057507..c26cab8757 100644 --- a/pallets/demeter-farming-platform/benchmarking/src/mock.rs +++ b/pallets/demeter-farming-platform/benchmarking/src/mock.rs @@ -14,7 +14,7 @@ use currencies::BasicCurrencyAdapter; use frame_support::traits::{Everything, GenesisBuild}; use frame_support::weights::Weight; use frame_support::{construct_runtime, parameter_types}; -use frame_system; + use frame_system::pallet_prelude::BlockNumberFor; use permissions::{Scope, MANAGE_DEX}; use sp_core::H256; @@ -67,7 +67,7 @@ parameter_types! { pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); pub GetXykFee: Fixed = fixed!(0.003); - pub GetIncentiveAssetId: AssetId = common::PSWAP.into(); + pub GetIncentiveAssetId: AssetId = common::PSWAP; pub const GetDefaultSubscriptionFrequency: BlockNumber = 10; pub const GetBurnUpdateFrequency: BlockNumber = 14400; pub GetParliamentAccountId: AccountId = 100; @@ -311,8 +311,8 @@ impl Default for ExtBuilder { }, )], endowed_accounts: vec![ - (ALICE, CERES_ASSET_ID.into(), balance!(1000)), - (BOB, CERES_ASSET_ID.into(), balance!(500)), + (ALICE, CERES_ASSET_ID, balance!(1000)), + (BOB, CERES_ASSET_ID, balance!(500)), ], initial_permission_owners: vec![( MANAGE_DEX, diff --git a/pallets/demeter-farming-platform/src/lib.rs b/pallets/demeter-farming-platform/src/lib.rs index 094a25bdf5..4385a8ae27 100644 --- a/pallets/demeter-farming-platform/src/lib.rs +++ b/pallets/demeter-farming-platform/src/lib.rs @@ -1,6 +1,4 @@ #![cfg_attr(not(feature = "std"), no_std)] -// TODO #167: fix clippy warnings -#![allow(clippy::all)] pub mod migrations; pub mod weights; @@ -65,6 +63,7 @@ use frame_support::dispatch::DispatchError; pub use pallet::*; #[frame_support::pallet] +#[allow(clippy::too_many_arguments)] pub mod pallet { use crate::{migrations, PoolData, StorageVersion, TokenInfo, UserInfo, WeightInfo}; use common::prelude::{AssetInfoProvider, Balance, FixedWrapper}; @@ -303,7 +302,7 @@ pub mod pallet { // Check if token is already registered ensure!( - !>::contains_key(&pool_asset), + !>::contains_key(pool_asset), Error::::TokenAlreadyRegistered ); @@ -326,7 +325,7 @@ pub mod pallet { team_account, }; - >::insert(&pool_asset, &token_info); + >::insert(pool_asset, &token_info); // Emit an event Self::deposit_event(Event::TokenRegistered(user, pool_asset)); @@ -361,11 +360,11 @@ pub mod pallet { ensure!(deposit_fee <= balance!(1), Error::::InvalidDepositFee); // Get token info - let mut token_info = >::get(&reward_asset) - .ok_or(Error::::RewardTokenIsNotRegistered)?; + let mut token_info = + >::get(reward_asset).ok_or(Error::::RewardTokenIsNotRegistered)?; // Check if pool already exists - let pool_infos = >::get(&pool_asset, &reward_asset); + let pool_infos = >::get(pool_asset, reward_asset); for pool_info in pool_infos { if !pool_info.is_removed && pool_info.is_farm == is_farm @@ -393,8 +392,8 @@ pub mod pallet { token_info.staking_total_multiplier += multiplier; } - >::insert(&reward_asset, token_info); - >::append(&pool_asset, &reward_asset, pool_info); + >::insert(reward_asset, token_info); + >::append(pool_asset, reward_asset, pool_info); // Emit an event Self::deposit_event(Event::PoolAdded( @@ -425,7 +424,7 @@ pub mod pallet { let user = ensure_signed(origin)?; // Get pool info and check if pool exists - let mut pool_infos = >::get(&pool_asset, &reward_asset); + let mut pool_infos = >::get(pool_asset, reward_asset); let mut exist = false; let mut pool_info = &mut Default::default(); for p_info in pool_infos.iter_mut() { @@ -480,7 +479,7 @@ pub mod pallet { } Assets::::transfer_from(&pool_asset, &user, &Self::account_id(), pooled_tokens)?; } else { - let pool_account = T::XYKPool::properties_of_pool(base_asset, pool_asset.clone()) + let pool_account = T::XYKPool::properties_of_pool(base_asset, pool_asset) .ok_or(Error::::PoolDoesNotExist)? .0; @@ -511,9 +510,8 @@ pub mod pallet { fee, )?; - lp_tokens = - T::XYKPool::balance_of_pool_provider(pool_account.clone(), user.clone()) - .unwrap_or(0); + lp_tokens = T::XYKPool::balance_of_pool_provider(pool_account, user.clone()) + .unwrap_or(0); } // Handle total LP changed in other XOR or XSTUSD/pool_asset farming pools @@ -522,21 +520,20 @@ pub mod pallet { && u_info.reward_asset != reward_asset && u_info.is_farm && u_info.base_asset == base_asset + && u_info.pooled_tokens > lp_tokens { - if u_info.pooled_tokens > lp_tokens { - let pool_tokens_diff = u_info.pooled_tokens - lp_tokens; - u_info.pooled_tokens = lp_tokens; - let mut pool_data = >::get(&pool_asset, &u_info.reward_asset); - for p_info in pool_data.iter_mut() { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == base_asset - { - p_info.total_tokens_in_pool -= pool_tokens_diff; - } + let pool_tokens_diff = u_info.pooled_tokens - lp_tokens; + u_info.pooled_tokens = lp_tokens; + let mut pool_data = >::get(pool_asset, u_info.reward_asset); + for p_info in pool_data.iter_mut() { + if !p_info.is_removed + && p_info.is_farm == is_farm + && p_info.base_asset == base_asset + { + p_info.total_tokens_in_pool -= pool_tokens_diff; } - >::insert(&pool_asset, &u_info.reward_asset, pool_data); } + >::insert(pool_asset, u_info.reward_asset, pool_data); } } } @@ -568,7 +565,7 @@ pub mod pallet { } } - >::insert(&pool_asset, &reward_asset, pool_infos); + >::insert(pool_asset, reward_asset, pool_infos); // Emit an event Self::deposit_event(Event::Deposited( @@ -599,7 +596,7 @@ pub mod pallet { let user = ensure_signed(origin)?; // Get pool info and check if pool has rewards - let mut pool_infos = >::get(&pool_asset, &reward_asset); + let mut pool_infos = >::get(pool_asset, reward_asset); let mut exist = false; let mut pool_info_rewards = balance!(0); @@ -648,7 +645,7 @@ pub mod pallet { // Update storage >::insert(&user, user_infos); - >::insert(&pool_asset, &reward_asset, pool_infos); + >::insert(pool_asset, reward_asset, pool_infos); // Emit an event Self::deposit_event(Event::::RewardWithdrawn( @@ -693,7 +690,7 @@ pub mod pallet { Error::::InsufficientFunds ); - if is_farm == false { + if !is_farm { Assets::::transfer_from( &pool_asset, &Self::account_id(), @@ -706,7 +703,7 @@ pub mod pallet { } // Get pool info - let mut pool_infos = >::get(&pool_asset, &reward_asset); + let mut pool_infos = >::get(pool_asset, reward_asset); for pool_info in pool_infos.iter_mut() { if pool_info.is_farm == is_farm && pool_info.base_asset == base_asset { pool_info.total_tokens_in_pool -= pooled_tokens; @@ -715,7 +712,7 @@ pub mod pallet { // Update storage >::insert(&user, user_infos); - >::insert(&pool_asset, &reward_asset, &pool_infos); + >::insert(pool_asset, reward_asset, &pool_infos); // Emit an event Self::deposit_event(Event::::Withdrawn( @@ -748,14 +745,14 @@ pub mod pallet { } // Get pool info - let mut pool_infos = >::get(&pool_asset, &reward_asset); + let mut pool_infos = >::get(pool_asset, reward_asset); for pool_info in pool_infos.iter_mut() { if pool_info.is_farm == is_farm && pool_info.base_asset == base_asset { pool_info.is_removed = true; } } - >::insert(&pool_asset, &reward_asset, &pool_infos); + >::insert(pool_asset, reward_asset, &pool_infos); // Emit an event Self::deposit_event(Event::::PoolRemoved( @@ -788,7 +785,7 @@ pub mod pallet { } // Get pool info and check if pool exists - let mut pool_infos = >::get(&pool_asset, &reward_asset); + let mut pool_infos = >::get(pool_asset, reward_asset); let mut old_multiplier = 0; let mut exist = false; @@ -801,8 +798,8 @@ pub mod pallet { } ensure!(exist, Error::::PoolDoesNotExist); - let mut token_info = >::get(&reward_asset) - .ok_or(Error::::RewardTokenIsNotRegistered)?; + let mut token_info = + >::get(reward_asset).ok_or(Error::::RewardTokenIsNotRegistered)?; if is_farm { token_info.farms_total_multiplier = @@ -812,8 +809,8 @@ pub mod pallet { token_info.staking_total_multiplier - old_multiplier + new_multiplier; } - >::insert(&reward_asset, &token_info); - >::insert(&pool_asset, &reward_asset, pool_infos); + >::insert(reward_asset, &token_info); + >::insert(pool_asset, reward_asset, pool_infos); // Emit an event Self::deposit_event(Event::::MultiplierChanged( @@ -847,7 +844,7 @@ pub mod pallet { } // Get pool info - let mut pool_infos = >::get(&pool_asset, &reward_asset); + let mut pool_infos = >::get(pool_asset, reward_asset); let mut exist = false; for p_info in pool_infos.iter_mut() { @@ -861,7 +858,7 @@ pub mod pallet { } ensure!(exist, Error::::PoolDoesNotExist); - >::insert(&pool_asset, &reward_asset, pool_infos); + >::insert(pool_asset, reward_asset, pool_infos); // Emit an event Self::deposit_event(Event::::TotalTokensChanged( @@ -943,7 +940,7 @@ pub mod pallet { // Check if deposit fee is valid ensure!(deposit_fee <= balance!(1), Error::::InvalidDepositFee); - let mut pool_infos = >::get(&pool_asset, &reward_asset); + let mut pool_infos = >::get(pool_asset, reward_asset); let mut exist = false; for p_info in pool_infos.iter_mut() { @@ -957,7 +954,7 @@ pub mod pallet { } ensure!(exist, Error::::PoolDoesNotExist); - >::insert(&pool_asset, &reward_asset, pool_infos); + >::insert(pool_asset, reward_asset, pool_infos); // Emit an event Self::deposit_event(Event::::DepositFeeChanged( @@ -993,7 +990,7 @@ pub mod pallet { // Get token info let mut token_info = - >::get(&pool_asset).ok_or(Error::::RewardTokenIsNotRegistered)?; + >::get(pool_asset).ok_or(Error::::RewardTokenIsNotRegistered)?; // Check if token_per_block is zero ensure!(token_per_block != 0, Error::::TokenPerBlockCantBeZero); @@ -1010,7 +1007,7 @@ pub mod pallet { token_info.team_allocation = team_allocation; token_info.team_account = team_account; - >::insert(&pool_asset, &token_info); + >::insert(pool_asset, &token_info); // Emit an event Self::deposit_event(Event::TokenInfoChanged(user, pool_asset)); @@ -1054,7 +1051,7 @@ pub mod pallet { fn mint_deo() { let blocks = 14400_u32; - let deo_info = if let Some(info) = >::get(&T::DemeterAssetId::get()) { + let deo_info = if let Some(info) = >::get(T::DemeterAssetId::get()) { info } else { return; @@ -1072,7 +1069,7 @@ pub mod pallet { let _ = Assets::::mint( RawOrigin::Signed(AuthorityAccount::::get()).into(), - T::DemeterAssetId::get().into(), + T::DemeterAssetId::get(), Self::account_id(), amount_for_farming_and_staking, ); @@ -1214,26 +1211,24 @@ pub mod pallet { // Calculate number of pool tokens let pool_tokens = - T::XYKPool::balance_of_pool_provider(pool_account.clone(), user.clone()) - .unwrap_or(0); + T::XYKPool::balance_of_pool_provider(pool_account, user.clone()).unwrap_or(0); if pool_tokens == 0 { return false; } let mut pooled_tokens = balance!(0); - let user_infos = >::get(&user); + let user_infos = >::get(user); for user_info in user_infos { if user_info.pool_asset == asset_b && user_info.is_farm && user_info.base_asset == asset_a + && pooled_tokens < user_info.pooled_tokens { - if pooled_tokens < user_info.pooled_tokens { - pooled_tokens = user_info.pooled_tokens; - } + pooled_tokens = user_info.pooled_tokens; } } - let free_pool_tokens = pool_tokens.checked_sub(pooled_tokens).unwrap_or(0); + let free_pool_tokens = pool_tokens.saturating_sub(pooled_tokens); pooled_tokens == balance!(0) || free_pool_tokens >= withdrawing_amount } @@ -1249,19 +1244,20 @@ impl DemeterFarmingPallet for Pallet { ) -> Result<(), DispatchError> { let mut user_infos = >::get(&user); for u_info in user_infos.iter_mut() { - if u_info.pool_asset == pool_asset && u_info.is_farm && u_info.base_asset == base_asset + if u_info.pool_asset == pool_asset + && u_info.is_farm + && u_info.base_asset == base_asset + && u_info.pooled_tokens > pool_tokens { - if u_info.pooled_tokens > pool_tokens { - let pool_tokens_diff = u_info.pooled_tokens - pool_tokens; - u_info.pooled_tokens = pool_tokens; - let mut pool_data = >::get(&pool_asset, &u_info.reward_asset); - for p_info in pool_data.iter_mut() { - if !p_info.is_removed && p_info.is_farm && p_info.base_asset == base_asset { - p_info.total_tokens_in_pool -= pool_tokens_diff; - } + let pool_tokens_diff = u_info.pooled_tokens - pool_tokens; + u_info.pooled_tokens = pool_tokens; + let mut pool_data = >::get(pool_asset, u_info.reward_asset); + for p_info in pool_data.iter_mut() { + if !p_info.is_removed && p_info.is_farm && p_info.base_asset == base_asset { + p_info.total_tokens_in_pool -= pool_tokens_diff; } - >::insert(&pool_asset, &u_info.reward_asset, pool_data); } + >::insert(pool_asset, u_info.reward_asset, pool_data); } } >::insert(user, user_infos); diff --git a/pallets/demeter-farming-platform/src/migrations.rs b/pallets/demeter-farming-platform/src/migrations.rs index c725489616..bad1ec3cb3 100644 --- a/pallets/demeter-farming-platform/src/migrations.rs +++ b/pallets/demeter-farming-platform/src/migrations.rs @@ -55,7 +55,7 @@ pub fn migrate_pool_and_user_data() -> Weight { rewards: old_pool_data.rewards, rewards_to_be_distributed: old_pool_data.rewards_to_be_distributed, is_removed: old_pool_data.is_removed, - base_asset: base_asset.into(), + base_asset, } }) .collect::>>>(), diff --git a/pallets/demeter-farming-platform/src/mock.rs b/pallets/demeter-farming-platform/src/mock.rs index a5956d28e0..3f866d9f80 100644 --- a/pallets/demeter-farming-platform/src/mock.rs +++ b/pallets/demeter-farming-platform/src/mock.rs @@ -11,7 +11,7 @@ use currencies::BasicCurrencyAdapter; use frame_support::traits::{Everything, GenesisBuild, Hooks}; use frame_support::weights::Weight; use frame_support::{construct_runtime, parameter_types}; -use frame_system; + use frame_system::pallet_prelude::BlockNumberFor; use permissions::{Scope, MANAGE_DEX}; use sp_core::H256; @@ -67,7 +67,7 @@ parameter_types! { pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); pub GetXykFee: Fixed = fixed!(0.003); - pub GetIncentiveAssetId: AssetId = common::PSWAP.into(); + pub GetIncentiveAssetId: AssetId = common::PSWAP; pub const GetDefaultSubscriptionFrequency: BlockNumber = 10; pub const GetBurnUpdateFrequency: BlockNumber = 14400; pub GetParliamentAccountId: AccountId = AccountId32::new([100u8; 32]); @@ -303,23 +303,23 @@ impl Default for ExtBuilder { ( DEX_A_ID, DEXInfo { - base_asset_id: XOR.into(), - synthetic_base_asset_id: XST.into(), + base_asset_id: XOR, + synthetic_base_asset_id: XST, is_public: true, }, ), ( DEX_B_ID, DEXInfo { - base_asset_id: XSTUSD.into(), - synthetic_base_asset_id: XST.into(), + base_asset_id: XSTUSD, + synthetic_base_asset_id: XST, is_public: true, }, ), ], endowed_accounts: vec![ - (ALICE, CERES_ASSET_ID.into(), balance!(1000)), - (BOB, CERES_ASSET_ID.into(), balance!(500)), + (ALICE, CERES_ASSET_ID, balance!(1000)), + (BOB, CERES_ASSET_ID, balance!(500)), ], initial_permission_owners: vec![ (MANAGE_DEX, Scope::Limited(hash(&DEX_A_ID)), vec![BOB]), diff --git a/pallets/demeter-farming-platform/src/tests.rs b/pallets/demeter-farming-platform/src/tests.rs index 4c62917b03..b6fbe54d03 100644 --- a/pallets/demeter-farming-platform/src/tests.rs +++ b/pallets/demeter-farming-platform/src/tests.rs @@ -1,406 +1,324 @@ -mod tests { - use crate::mock::*; - use crate::{AccountIdOf, AssetIdOf}; - use common::prelude::FixedWrapper; - use common::{ - balance, generate_storage_instance, AssetId32, AssetInfoProvider, AssetName, AssetSymbol, - Balance, LiquiditySourceType, PoolXykPallet, PredefinedAssetId, ToFeeAccount, - TradingPairSourceManager, CERES_ASSET_ID, DEFAULT_BALANCE_PRECISION, DEMETER_ASSET_ID, XOR, - XSTUSD, - }; - use demeter_farming_platform::{PoolData, TokenInfo, UserInfo}; - use frame_support::pallet_prelude::{StorageDoubleMap, StorageMap}; - use frame_support::storage::types::ValueQuery; - use frame_support::traits::Hooks; - use frame_support::{assert_err, assert_ok, Identity, PalletId}; - use hex_literal::hex; - use sp_runtime::traits::AccountIdConversion; - - fn preset_initial(tests: Fun) - where - Fun: Fn(), - { - let mut ext = ExtBuilder::default().build(); - let dex_id = DEX_A_ID; - let dex_id_xst = DEX_B_ID; - let xor: AssetId = XOR.into(); - let ceres: AssetId = CERES_ASSET_ID.into(); - let xstusd: AssetId = XSTUSD.into(); - let util: AssetId32 = AssetId32::from_bytes(hex!( - "007348eb8f0f3cec730fbf5eec1b6a842c54d1df8bed75a9df084d5ee013e814" +use crate::mock::*; +use crate::{AccountIdOf, AssetIdOf}; +use common::prelude::FixedWrapper; +use common::{ + balance, generate_storage_instance, AssetId32, AssetInfoProvider, AssetName, AssetSymbol, + Balance, LiquiditySourceType, PoolXykPallet, PredefinedAssetId, ToFeeAccount, + TradingPairSourceManager, CERES_ASSET_ID, DEFAULT_BALANCE_PRECISION, DEMETER_ASSET_ID, XOR, + XSTUSD, +}; +use demeter_farming_platform::{PoolData, TokenInfo, UserInfo}; +use frame_support::pallet_prelude::{StorageDoubleMap, StorageMap}; +use frame_support::storage::types::ValueQuery; +use frame_support::traits::Hooks; +use frame_support::{assert_err, assert_ok, Identity, PalletId}; +use hex_literal::hex; +use sp_runtime::traits::AccountIdConversion; + +fn preset_initial(tests: Fun) +where + Fun: Fn(), +{ + let mut ext = ExtBuilder::default().build(); + let dex_id = DEX_A_ID; + let dex_id_xst = DEX_B_ID; + let xor: AssetId = XOR; + let ceres: AssetId = CERES_ASSET_ID; + let xstusd: AssetId = XSTUSD; + let util: AssetId32 = AssetId32::from_bytes(hex!( + "007348eb8f0f3cec730fbf5eec1b6a842c54d1df8bed75a9df084d5ee013e814" + )); + let pallet_account = PalletId(*b"deofarms").into_account_truncating(); + + ext.execute_with(|| { + assert_ok!(assets::Pallet::::register_asset_id( + ALICE, + XOR, + AssetSymbol(b"XOR".to_vec()), + AssetName(b"SORA".to_vec()), + DEFAULT_BALANCE_PRECISION, + Balance::from(0u32), + true, + None, + None, )); - let pallet_account = PalletId(*b"deofarms").into_account_truncating(); - ext.execute_with(|| { - assert_ok!(assets::Pallet::::register_asset_id( - ALICE, - XOR.into(), - AssetSymbol(b"XOR".to_vec()), - AssetName(b"SORA".to_vec()), - DEFAULT_BALANCE_PRECISION, - Balance::from(0u32), - true, - None, - None, - )); - - assert_ok!(assets::Pallet::::register_asset_id( - ALICE, - CERES_ASSET_ID.into(), - AssetSymbol(b"CERES".to_vec()), - AssetName(b"Ceres".to_vec()), - DEFAULT_BALANCE_PRECISION, - Balance::from(0u32), - true, - None, - None, - )); - - assert_ok!(assets::Pallet::::register_asset_id( - ALICE, - XSTUSD.into(), - AssetSymbol(b"XSTUSD".to_vec()), - AssetName(b"SORA Synthetic USD".to_vec()), - DEFAULT_BALANCE_PRECISION, - Balance::from(0u32), - true, - None, - None, - )); - - frame_system::Pallet::::inc_providers( - &demeter_farming_platform::AuthorityAccount::::get(), - ); - assert_ok!(assets::Pallet::::register_asset_id( - demeter_farming_platform::AuthorityAccount::::get(), - DEMETER_ASSET_ID.into(), - AssetSymbol(b"DEO".to_vec()), - AssetName(b"Demeter".to_vec()), - DEFAULT_BALANCE_PRECISION, - Balance::from(0u32), - true, - None, - None, - )); - - assert_ok!(assets::Pallet::::register_asset_id( - ALICE, - util.into(), - AssetSymbol(b"UTIL".to_vec()), - AssetName(b"Util".to_vec()), - DEFAULT_BALANCE_PRECISION, - Balance::from(0u32), - true, - None, - None, - )); - - /************ XOR DEX ************/ - assert_ok!(trading_pair::Pallet::::register( - RuntimeOrigin::signed(BOB), - dex_id.clone(), - XOR.into(), - CERES_ASSET_ID.into() - )); - - assert_ok!(pool_xyk::Pallet::::initialize_pool( - RuntimeOrigin::signed(BOB), - dex_id.clone(), - XOR.into(), - CERES_ASSET_ID.into(), - )); - - assert!( - trading_pair::Pallet::::is_source_enabled_for_trading_pair( - &dex_id, - &XOR.into(), - &CERES_ASSET_ID.into(), - LiquiditySourceType::XYKPool, - ) - .expect("Failed to query trading pair status.") - ); - - let (_tpair, tech_acc_id) = - pool_xyk::Pallet::::tech_account_from_dex_and_asset_pair( - dex_id.clone(), - XOR.into(), - CERES_ASSET_ID.into(), - ) - .unwrap(); - - let fee_acc = tech_acc_id.clone().to_fee_account().unwrap(); - let repr: AccountId = - technical::Pallet::::tech_account_id_to_account_id(&tech_acc_id).unwrap(); - let fee_repr: AccountId = - technical::Pallet::::tech_account_id_to_account_id(&fee_acc).unwrap(); - - assert_eq!( - pool_xyk::Pallet::::properties(xor, ceres), - Some((repr.clone(), fee_repr.clone())) - ); - - /********* XSTUSD DEX ********/ - assert_ok!(trading_pair::Pallet::::register( - RuntimeOrigin::signed(BOB), - dex_id_xst.clone(), - XSTUSD.into(), - CERES_ASSET_ID.into() - )); - - assert_ok!(pool_xyk::Pallet::::initialize_pool( - RuntimeOrigin::signed(BOB), - dex_id_xst.clone(), - XSTUSD.into(), - CERES_ASSET_ID.into(), - )); - - assert!( - trading_pair::Pallet::::is_source_enabled_for_trading_pair( - &dex_id_xst, - &XSTUSD.into(), - &CERES_ASSET_ID.into(), - LiquiditySourceType::XYKPool, - ) - .expect("Failed to query trading pair status.") - ); - - let (_tpair_xst, tech_acc_id_xst) = - pool_xyk::Pallet::::tech_account_from_dex_and_asset_pair( - dex_id_xst.clone(), - XSTUSD.into(), - CERES_ASSET_ID.into(), - ) - .unwrap(); - - let fee_acc_xst = tech_acc_id_xst.clone().to_fee_account().unwrap(); - let repr_xst: AccountId = - technical::Pallet::::tech_account_id_to_account_id(&tech_acc_id_xst) - .unwrap(); - let fee_repr_xst: AccountId = - technical::Pallet::::tech_account_id_to_account_id(&fee_acc_xst).unwrap(); - - assert_eq!( - pool_xyk::Pallet::::properties(xstusd, ceres), - Some((repr_xst.clone(), fee_repr_xst.clone())) - ); - - /********** MINTS ***********/ - assert_ok!(assets::Pallet::::mint_to( - &xor, - &ALICE, - &ALICE, - balance!(2000) - )); + assert_ok!(assets::Pallet::::register_asset_id( + ALICE, + CERES_ASSET_ID, + AssetSymbol(b"CERES".to_vec()), + AssetName(b"Ceres".to_vec()), + DEFAULT_BALANCE_PRECISION, + Balance::from(0u32), + true, + None, + None, + )); - assert_ok!(assets::Pallet::::mint_to( - &xstusd, - &ALICE, - &ALICE, - balance!(2000) - )); + assert_ok!(assets::Pallet::::register_asset_id( + ALICE, + XSTUSD, + AssetSymbol(b"XSTUSD".to_vec()), + AssetName(b"SORA Synthetic USD".to_vec()), + DEFAULT_BALANCE_PRECISION, + Balance::from(0u32), + true, + None, + None, + )); - assert_ok!(assets::Pallet::::mint_to( - &ceres, - &ALICE, - &ALICE, - balance!(2000) - )); + frame_system::Pallet::::inc_providers( + &demeter_farming_platform::AuthorityAccount::::get(), + ); + assert_ok!(assets::Pallet::::register_asset_id( + demeter_farming_platform::AuthorityAccount::::get(), + DEMETER_ASSET_ID, + AssetSymbol(b"DEO".to_vec()), + AssetName(b"Demeter".to_vec()), + DEFAULT_BALANCE_PRECISION, + Balance::from(0u32), + true, + None, + None, + )); - assert_ok!(assets::Pallet::::mint_to( - &xor, - &ALICE, - &BOB, - balance!(2000) - )); + assert_ok!(assets::Pallet::::register_asset_id( + ALICE, + util, + AssetSymbol(b"UTIL".to_vec()), + AssetName(b"Util".to_vec()), + DEFAULT_BALANCE_PRECISION, + Balance::from(0u32), + true, + None, + None, + )); - assert_ok!(assets::Pallet::::mint_to( - &xstusd, - &ALICE, - &BOB, - balance!(2000) - )); + /************ XOR DEX ************/ + assert_ok!(trading_pair::Pallet::::register( + RuntimeOrigin::signed(BOB), + dex_id, + XOR, + CERES_ASSET_ID + )); - assert_ok!(assets::Pallet::::mint_to( - &ceres, - &ALICE, - &BOB, - balance!(2000) - )); + assert_ok!(pool_xyk::Pallet::::initialize_pool( + RuntimeOrigin::signed(BOB), + dex_id, + XOR, + CERES_ASSET_ID, + )); - assert_ok!(assets::Pallet::::mint_to( - &ceres, - &ALICE, - &pallet_account, - balance!(1000) - )); + assert!( + trading_pair::Pallet::::is_source_enabled_for_trading_pair( + &dex_id, + &XOR, + &CERES_ASSET_ID, + LiquiditySourceType::XYKPool, + ) + .expect("Failed to query trading pair status.") + ); - assert_ok!(assets::Pallet::::mint_to( - &xor, - &ALICE, - &pallet_account, - balance!(1000) - )); + let (_tpair, tech_acc_id) = + pool_xyk::Pallet::::tech_account_from_dex_and_asset_pair( + dex_id, + XOR, + CERES_ASSET_ID, + ) + .unwrap(); + + let fee_acc = tech_acc_id.to_fee_account().unwrap(); + let repr: AccountId = + technical::Pallet::::tech_account_id_to_account_id(&tech_acc_id).unwrap(); + let fee_repr: AccountId = + technical::Pallet::::tech_account_id_to_account_id(&fee_acc).unwrap(); + + assert_eq!( + pool_xyk::Pallet::::properties(xor, ceres), + Some((repr, fee_repr)) + ); + + /********* XSTUSD DEX ********/ + assert_ok!(trading_pair::Pallet::::register( + RuntimeOrigin::signed(BOB), + dex_id_xst, + XSTUSD, + CERES_ASSET_ID + )); - assert_ok!(assets::Pallet::::mint_to( - &xstusd, - &ALICE, - &pallet_account, - balance!(1000) - )); + assert_ok!(pool_xyk::Pallet::::initialize_pool( + RuntimeOrigin::signed(BOB), + dex_id_xst, + XSTUSD, + CERES_ASSET_ID, + )); - assert_ok!(assets::Pallet::::mint_to( - &util, - &ALICE, - &ALICE, - balance!(2000) - )); - - assert_eq!( - assets::Pallet::::free_balance(&xor, &ALICE).unwrap(), - balance!(2000) - ); - assert_eq!( - assets::Pallet::::free_balance(&xstusd, &ALICE).unwrap(), - balance!(2000) - ); - assert_eq!( - assets::Pallet::::free_balance(&ceres, &ALICE).unwrap(), - balance!(3000) - ); - - tests(); - }); - } - - #[test] - fn register_token_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_err!( - demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(ALICE), - pool_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::Unauthorized + assert!( + trading_pair::Pallet::::is_source_enabled_for_trading_pair( + &dex_id_xst, + &XSTUSD, + &CERES_ASSET_ID, + LiquiditySourceType::XYKPool, ) - }); - } - - #[test] - fn register_token_token_already_registered() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = XOR; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - let token_info = TokenInfo { - farms_total_multiplier: 0, - staking_total_multiplier: 0, + .expect("Failed to query trading pair status.") + ); + + let (_tpair_xst, tech_acc_id_xst) = + pool_xyk::Pallet::::tech_account_from_dex_and_asset_pair( + dex_id_xst, + XSTUSD, + CERES_ASSET_ID, + ) + .unwrap(); + + let fee_acc_xst = tech_acc_id_xst.to_fee_account().unwrap(); + let repr_xst: AccountId = + technical::Pallet::::tech_account_id_to_account_id(&tech_acc_id_xst).unwrap(); + let fee_repr_xst: AccountId = + technical::Pallet::::tech_account_id_to_account_id(&fee_acc_xst).unwrap(); + + assert_eq!( + pool_xyk::Pallet::::properties(xstusd, ceres), + Some((repr_xst, fee_repr_xst)) + ); + + /********** MINTS ***********/ + assert_ok!(assets::Pallet::::mint_to( + &xor, + &ALICE, + &ALICE, + balance!(2000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &xstusd, + &ALICE, + &ALICE, + balance!(2000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &ceres, + &ALICE, + &ALICE, + balance!(2000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &xor, + &ALICE, + &BOB, + balance!(2000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &xstusd, + &ALICE, + &BOB, + balance!(2000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &ceres, + &ALICE, + &BOB, + balance!(2000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &ceres, + &ALICE, + &pallet_account, + balance!(1000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &xor, + &ALICE, + &pallet_account, + balance!(1000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &xstusd, + &ALICE, + &pallet_account, + balance!(1000) + )); + + assert_ok!(assets::Pallet::::mint_to( + &util, + &ALICE, + &ALICE, + balance!(2000) + )); + + assert_eq!( + assets::Pallet::::free_balance(&xor, &ALICE).unwrap(), + balance!(2000) + ); + assert_eq!( + assets::Pallet::::free_balance(&xstusd, &ALICE).unwrap(), + balance!(2000) + ); + assert_eq!( + assets::Pallet::::free_balance(&ceres, &ALICE).unwrap(), + balance!(3000) + ); + + tests(); + }); +} + +#[test] +fn register_token_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_err!( + demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(ALICE), + pool_asset, token_per_block, farms_allocation, staking_allocation, team_allocation, - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - assert_err!( - demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::TokenAlreadyRegistered - ) - }); - } - - #[test] - fn register_token_token_per_block_cant_be_zero() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = XOR; - let token_per_block = balance!(0); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_err!( - demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::TokenPerBlockCantBeZero - ) - }); - } - - #[test] - fn register_token_invalid_allocation_parameters() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = XOR; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.3); - let team_allocation = balance!(0.2); - - assert_err!( - demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::InvalidAllocationParameters - ) - }); - } - - #[test] - fn register_token_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = XOR; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( + BOB + ), + demeter_farming_platform::Error::::Unauthorized + ) + }); +} + +#[test] +fn register_token_token_already_registered() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = XOR; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + let token_info = TokenInfo { + farms_total_multiplier: 0, + staking_total_multiplier: 0, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, token_info); + + assert_err!( + demeter_farming_platform::Pallet::::register_token( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), reward_asset, token_per_block, @@ -408,148 +326,24 @@ mod tests { staking_allocation, team_allocation, BOB - )); - - let token_info = - demeter_farming_platform::TokenInfos::::get(&reward_asset).unwrap(); - - assert_eq!(token_info.token_per_block, token_per_block); - assert_eq!(token_info.farms_allocation, farms_allocation); - assert_eq!(token_info.staking_allocation, staking_allocation); - assert_eq!(token_info.team_allocation, team_allocation); - }); - } - - #[test] - fn add_pool_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - - assert_err!( - demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core, - ), - demeter_farming_platform::Error::::Unauthorized - ) - }); - } - - #[test] - fn add_pool_invalid_multiplier() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 0; - let deposit_fee = balance!(0.4); - let is_core = true; - - assert_err!( - demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core, - ), - demeter_farming_platform::Error::::InvalidMultiplier - ) - }); - } - - #[test] - fn add_pool_invalid_deposit_fee() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(1.1); - let is_core = true; - - assert_err!( - demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core, - ), - demeter_farming_platform::Error::::InvalidDepositFee - ) - }); - } - - #[test] - fn add_pool_reward_token_is_not_registered() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - - assert_err!( - demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core, - ), - demeter_farming_platform::Error::::RewardTokenIsNotRegistered - ) - }); - } - - #[test] - fn add_pool_pool_already_exists() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( + ), + demeter_farming_platform::Error::::TokenAlreadyRegistered + ) + }); +} + +#[test] +fn register_token_token_per_block_cant_be_zero() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = XOR; + let token_per_block = balance!(0); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_err!( + demeter_farming_platform::Pallet::::register_token( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), reward_asset, token_per_block, @@ -557,60 +351,24 @@ mod tests { staking_allocation, team_allocation, BOB - )); + ), + demeter_farming_platform::Error::::TokenPerBlockCantBeZero + ) + }); +} - let pool_info = PoolData { - multiplier, - deposit_fee, - is_core, - is_farm, - total_tokens_in_pool: 0, - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - pool_info, - ); - - assert_err!( - demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core, - ), - demeter_farming_platform::Error::::PoolAlreadyExists - ) - }); - } - - #[test] - fn add_pool_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( +#[test] +fn register_token_invalid_allocation_parameters() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = XOR; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.3); + let team_allocation = balance!(0.2); + + assert_err!( + demeter_farming_platform::Pallet::::register_token( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), reward_asset, token_per_block, @@ -618,10 +376,56 @@ mod tests { staking_allocation, team_allocation, BOB - )); + ), + demeter_farming_platform::Error::::InvalidAllocationParameters + ) + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), +#[test] +fn register_token_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = XOR; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + let token_info = + demeter_farming_platform::TokenInfos::::get(reward_asset).unwrap(); + + assert_eq!(token_info.token_per_block, token_per_block); + assert_eq!(token_info.farms_allocation, farms_allocation); + assert_eq!(token_info.staking_allocation, staking_allocation); + assert_eq!(token_info.team_allocation, team_allocation); + }); +} + +#[test] +fn add_pool_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + + assert_err!( + demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(ALICE), pool_asset, pool_asset, reward_asset, @@ -629,53 +433,25 @@ mod tests { multiplier, deposit_fee, is_core, - )); - - let token_info = - demeter_farming_platform::TokenInfos::::get(&reward_asset).unwrap(); - assert_eq!(token_info.farms_total_multiplier, multiplier); - - let pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos { - if !pool_info.is_removed - && pool_info.is_farm == is_farm - && pool_info.base_asset == pool_asset - { - assert_eq!(pool_info.multiplier, multiplier); - assert_eq!(pool_info.is_core, is_core); - assert_eq!(pool_info.deposit_fee, deposit_fee); - } - } - }); - } - - #[test] - fn add_pool_xstusd_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + ), + demeter_farming_platform::Error::::Unauthorized + ) + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( +#[test] +fn add_pool_invalid_multiplier() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 0; + let deposit_fee = balance!(0.4); + let is_core = true; + + assert_err!( + demeter_farming_platform::Pallet::::add_pool( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, pool_asset, @@ -684,75 +460,25 @@ mod tests { multiplier, deposit_fee, is_core, - )); - - let token_info = - demeter_farming_platform::TokenInfos::::get(&reward_asset).unwrap(); - assert_eq!(token_info.farms_total_multiplier, multiplier); - - let pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos { - if !pool_info.is_removed - && pool_info.is_farm == is_farm - && pool_info.base_asset == pool_asset - { - assert_eq!(pool_info.multiplier, multiplier); - assert_eq!(pool_info.is_core, is_core); - assert_eq!(pool_info.deposit_fee, deposit_fee); - } - } - }); - } - - #[test] - fn deposit_farming_pool_does_not_exist() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - - assert_err!( - demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - balance!(10), - ), - demeter_farming_platform::Error::::PoolDoesNotExist - ); - }); - } - - #[test] - fn deposit_insufficient_funds() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = false; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + ), + demeter_farming_platform::Error::::InvalidMultiplier + ) + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( +#[test] +fn add_pool_invalid_deposit_fee() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(1.1); + let is_core = true; + + assert_err!( + demeter_farming_platform::Pallet::::add_pool( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, pool_asset, @@ -761,48 +487,25 @@ mod tests { multiplier, deposit_fee, is_core, - )); - - assert_err!( - demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - balance!(10000), - ), - demeter_farming_platform::Error::::InsufficientFunds - ); - }); - } - - #[test] - fn deposit_liquidity_pool_does_not_exist() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + ), + demeter_farming_platform::Error::::InvalidDepositFee + ) + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( +#[test] +fn add_pool_reward_token_is_not_registered() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + + assert_err!( + demeter_farming_platform::Pallet::::add_pool( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, pool_asset, @@ -811,2419 +514,1856 @@ mod tests { multiplier, deposit_fee, is_core, - )); - - assert_err!( - demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - balance!(10000), - ), - demeter_farming_platform::Error::::PoolDoesNotExist - ); - }); - } - - #[test] - fn deposit_insufficient_lp_tokens() { - preset_initial(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.4); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + ), + demeter_farming_platform::Error::::RewardTokenIsNotRegistered + ) + }); +} + +#[test] +fn add_pool_pool_already_exists() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); - assert_ok!(demeter_farming_platform::Pallet::::add_pool( + let pool_info = PoolData { + multiplier, + deposit_fee, + is_core, + is_farm, + total_tokens_in_pool: 0, + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, pool_info); + + assert_err!( + demeter_farming_platform::Pallet::::add_pool( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, - reward_asset, + pool_asset, reward_asset, is_farm, multiplier, deposit_fee, is_core, - )); - - let pooled_tokens = balance!(10000); - assert_err!( - demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - reward_asset, - is_farm, - pooled_tokens, - ), - demeter_farming_platform::Error::::InsufficientLPTokens - ); - }); - } - - #[test] - fn deposit_ok_not_farm() { - preset_initial(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = false; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + ), + demeter_farming_platform::Error::::PoolAlreadyExists + ) + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, +#[test] +fn add_pool_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + let token_info = + demeter_farming_platform::TokenInfos::::get(reward_asset).unwrap(); + assert_eq!(token_info.farms_total_multiplier, multiplier); + + let pool_infos = demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos { + if !pool_info.is_removed + && pool_info.is_farm == is_farm + && pool_info.base_asset == pool_asset + { + assert_eq!(pool_info.multiplier, multiplier); + assert_eq!(pool_info.is_core, is_core); + assert_eq!(pool_info.deposit_fee, deposit_fee); + } + } + }); +} + +#[test] +fn add_pool_xstusd_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + let token_info = + demeter_farming_platform::TokenInfos::::get(reward_asset).unwrap(); + assert_eq!(token_info.farms_total_multiplier, multiplier); + + let pool_infos = demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos { + if !pool_info.is_removed + && pool_info.is_farm == is_farm + && pool_info.base_asset == pool_asset + { + assert_eq!(pool_info.multiplier, multiplier); + assert_eq!(pool_info.is_core, is_core); + assert_eq!(pool_info.deposit_fee, deposit_fee); + } + } + }); +} + +#[test] +fn deposit_farming_pool_does_not_exist() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + + assert_err!( + demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + pool_asset, reward_asset, + is_farm, + balance!(10), + ), + demeter_farming_platform::Error::::PoolDoesNotExist + ); + }); +} + +#[test] +fn deposit_insufficient_funds() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = false; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + assert_err!( + demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + pool_asset, reward_asset, is_farm, - multiplier, - deposit_fee, - is_core, - )); + balance!(10000), + ), + demeter_farming_platform::Error::::InsufficientFunds + ); + }); +} + +#[test] +fn deposit_liquidity_pool_does_not_exist() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core, + )); - let mut pooled_tokens = balance!(10); - assert_ok!(demeter_farming_platform::Pallet::::deposit( + assert_err!( + demeter_farming_platform::Pallet::::deposit( RuntimeOrigin::signed(ALICE), + pool_asset, + pool_asset, reward_asset, + is_farm, + balance!(10000), + ), + demeter_farming_platform::Error::::PoolDoesNotExist + ); + }); +} + +#[test] +fn deposit_insufficient_lp_tokens() { + preset_initial(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.4); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + let pooled_tokens = balance!(10000); + assert_err!( + demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, reward_asset, reward_asset, is_farm, pooled_tokens, - )); - - let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) - .try_into_balance() - .unwrap_or(0); - pooled_tokens -= fee; - - let pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for p_info in &pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == reward_asset - { - assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); - } + ), + demeter_farming_platform::Error::::InsufficientLPTokens + ); + }); +} + +#[test] +fn deposit_ok_not_farm() { + preset_initial(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = false; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + let mut pooled_tokens = balance!(10); + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + reward_asset, + reward_asset, + reward_asset, + is_farm, + pooled_tokens, + )); + + let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) + .try_into_balance() + .unwrap_or(0); + pooled_tokens -= fee; + + let pool_infos = demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == reward_asset + { + assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); } + } - let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - for u_info in &user_infos { - if u_info.is_farm == is_farm && u_info.base_asset == reward_asset { - assert_eq!(u_info.pooled_tokens, pooled_tokens); - } + let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + for u_info in &user_infos { + if u_info.is_farm == is_farm && u_info.base_asset == reward_asset { + assert_eq!(u_info.pooled_tokens, pooled_tokens); } + } - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(2990) - ); - - let pallet_account = PalletId(*b"deofarms").into_account_truncating(); - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &pallet_account) - .expect("Failed to query free balance."), - balance!(1000) + pooled_tokens - ); - - assert_eq!( - Assets::free_balance( - &CERES_ASSET_ID, - &demeter_farming_platform::FeeAccount::::get() - ) + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(2990) + ); + + let pallet_account = PalletId(*b"deofarms").into_account_truncating(); + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &pallet_account) .expect("Failed to query free balance."), - fee - ); - }); - } - - #[test] - fn deposit_ok_farm() { - preset_initial(|| { - let dex_id = DEX_A_ID; - let asset_xor = XOR; - let asset_ceres = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_ceres, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + balance!(1000) + pooled_tokens + ); - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_xor, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + assert_eq!( + Assets::free_balance( + &CERES_ASSET_ID, + &demeter_farming_platform::FeeAccount::::get() + ) + .expect("Failed to query free balance."), + fee + ); + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_xor, - asset_ceres, - asset_ceres, - is_farm, - multiplier, - deposit_fee, - is_core, - )); +#[test] +fn deposit_ok_farm() { + preset_initial(|| { + let dex_id = DEX_A_ID; + let asset_xor = XOR; + let asset_ceres = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_ceres, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_xor, - asset_ceres, - asset_xor, - is_farm, - multiplier, - deposit_fee, - is_core, - )); + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_xor, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, - asset_xor, - asset_ceres, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - asset_xor, - asset_ceres, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let mut pooled_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - asset_xor, - asset_ceres, - asset_ceres, - is_farm, - pooled_tokens, - )); - - let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) - .try_into_balance() - .unwrap_or(0); - pooled_tokens -= fee; - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&asset_ceres, &asset_ceres); - for p_info in &pool_infos { - if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == asset_xor - { - assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); - } - } + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_xor, + asset_ceres, + asset_ceres, + is_farm, + multiplier, + deposit_fee, + is_core, + )); - let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - for u_info in &user_infos { - if u_info.is_farm == is_farm && u_info.base_asset == asset_xor { - assert_eq!(u_info.pooled_tokens, pooled_tokens); - } - } + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_xor, + asset_ceres, + asset_xor, + is_farm, + multiplier, + deposit_fee, + is_core, + )); - let lp_tokens = pool_xyk::Pallet::::balance_of_pool_provider( - pool_account.clone(), - demeter_farming_platform::FeeAccount::::get(), + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + asset_xor, + asset_ceres, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( + asset_xor, + asset_ceres, ) - .unwrap_or(0); - assert_eq!(lp_tokens, fee); + .expect("Pool does not exist") + .0; - // Deposit to other XOR/CERES pool with different reward token - pooled_tokens = ::XYKPool::pool_providers( + // Calculate number of pool tokens of user's account + let mut pooled_tokens: Balance = + ::XYKPool::pool_providers( pool_account.clone(), ALICE, ) .expect("User is not pool provider"); - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - asset_xor, - asset_ceres, - asset_xor, - is_farm, - pooled_tokens, - )); - - let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - let mut first_pool = balance!(0); - let mut second_pool = balance!(0); - for u_info in &user_infos { - if u_info.pool_asset == asset_ceres - && u_info.reward_asset == asset_ceres - && u_info.is_farm == is_farm - && u_info.base_asset == asset_xor - { - first_pool = u_info.pooled_tokens; - } else if u_info.pool_asset == asset_ceres - && u_info.reward_asset == asset_xor - && u_info.is_farm == is_farm - && u_info.base_asset == asset_xor - { - second_pool = u_info.pooled_tokens; - } - } - assert_eq!(first_pool, second_pool); - - pool_infos = - demeter_farming_platform::Pools::::get(&asset_ceres, &asset_ceres); - for p_info in &pool_infos { - if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == asset_xor - { - assert_eq!(p_info.total_tokens_in_pool, first_pool); - } - } - }); - } - - #[test] - fn deposit_xstusd_ok_farm() { - preset_initial(|| { - let dex_id = DEX_B_ID; - let asset_xstusd = XSTUSD; - let asset_ceres = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_ceres, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + asset_xor, + asset_ceres, + asset_ceres, + is_farm, + pooled_tokens, + )); - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_xstusd, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) + .try_into_balance() + .unwrap_or(0); + pooled_tokens -= fee; - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_xstusd, - asset_ceres, - asset_ceres, - is_farm, - multiplier, - deposit_fee, - is_core, - )); + let mut pool_infos = + demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == asset_xor { + assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); + } + } - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_xstusd, - asset_ceres, - asset_xstusd, - is_farm, - multiplier, - deposit_fee, - is_core, - )); + let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + for u_info in &user_infos { + if u_info.is_farm == is_farm && u_info.base_asset == asset_xor { + assert_eq!(u_info.pooled_tokens, pooled_tokens); + } + } + + let lp_tokens = pool_xyk::Pallet::::balance_of_pool_provider( + pool_account.clone(), + demeter_farming_platform::FeeAccount::::get(), + ) + .unwrap_or(0); + assert_eq!(lp_tokens, fee); + + // Deposit to other XOR/CERES pool with different reward token + pooled_tokens = ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); + + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + asset_xor, + asset_ceres, + asset_xor, + is_farm, + pooled_tokens, + )); - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, - asset_xstusd, - asset_ceres, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - asset_xstusd, - asset_ceres, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let mut pooled_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - asset_xstusd, - asset_ceres, - asset_ceres, - is_farm, - pooled_tokens, - )); - let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) - .try_into_balance() - .unwrap_or(0); - pooled_tokens -= fee; - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&asset_ceres, &asset_ceres); - for p_info in &pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == asset_xstusd - { - assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); - } + let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + let mut first_pool = balance!(0); + let mut second_pool = balance!(0); + for u_info in &user_infos { + if u_info.pool_asset == asset_ceres + && u_info.reward_asset == asset_ceres + && u_info.is_farm == is_farm + && u_info.base_asset == asset_xor + { + first_pool = u_info.pooled_tokens; + } else if u_info.pool_asset == asset_ceres + && u_info.reward_asset == asset_xor + && u_info.is_farm == is_farm + && u_info.base_asset == asset_xor + { + second_pool = u_info.pooled_tokens; } + } + assert_eq!(first_pool, second_pool); - let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - for u_info in &user_infos { - if u_info.is_farm == is_farm && u_info.base_asset == asset_xstusd { - assert_eq!(u_info.pooled_tokens, pooled_tokens); - } + pool_infos = demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == asset_xor { + assert_eq!(p_info.total_tokens_in_pool, first_pool); } + } + }); +} - let lp_tokens = pool_xyk::Pallet::::balance_of_pool_provider( - pool_account.clone(), - demeter_farming_platform::FeeAccount::::get(), +#[test] +fn deposit_xstusd_ok_farm() { + preset_initial(|| { + let dex_id = DEX_B_ID; + let asset_xstusd = XSTUSD; + let asset_ceres = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_ceres, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_xstusd, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_xstusd, + asset_ceres, + asset_ceres, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_xstusd, + asset_ceres, + asset_xstusd, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + asset_xstusd, + asset_ceres, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( + asset_xstusd, + asset_ceres, ) - .unwrap_or(0); - assert_eq!(lp_tokens, fee); + .expect("Pool does not exist") + .0; - // Deposit to other XSTUSD/CERES pool with different reward token - pooled_tokens = ::XYKPool::pool_providers( + // Calculate number of pool tokens of user's account + let mut pooled_tokens: Balance = + ::XYKPool::pool_providers( pool_account.clone(), ALICE, ) .expect("User is not pool provider"); - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - asset_xstusd, - asset_ceres, - asset_xstusd, - is_farm, - pooled_tokens, - )); - - let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - let mut first_pool = balance!(0); - let mut second_pool = balance!(0); - for u_info in &user_infos { - if u_info.pool_asset == asset_ceres - && u_info.reward_asset == asset_ceres - && u_info.is_farm == is_farm - && u_info.base_asset == asset_xstusd - { - first_pool = u_info.pooled_tokens; - } else if u_info.pool_asset == asset_ceres - && u_info.reward_asset == asset_xstusd - && u_info.is_farm == is_farm - && u_info.base_asset == asset_xstusd - { - second_pool = u_info.pooled_tokens; - } + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + asset_xstusd, + asset_ceres, + asset_ceres, + is_farm, + pooled_tokens, + )); + let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) + .try_into_balance() + .unwrap_or(0); + pooled_tokens -= fee; + + let mut pool_infos = + demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == asset_xstusd + { + assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); } - assert_eq!(first_pool, second_pool); - - pool_infos = - demeter_farming_platform::Pools::::get(&asset_ceres, &asset_ceres); - for p_info in &pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == asset_xstusd - { - assert_eq!(p_info.total_tokens_in_pool, first_pool); - } + } + + let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + for u_info in &user_infos { + if u_info.is_farm == is_farm && u_info.base_asset == asset_xstusd { + assert_eq!(u_info.pooled_tokens, pooled_tokens); } - }); - } + } + + let lp_tokens = pool_xyk::Pallet::::balance_of_pool_provider( + pool_account.clone(), + demeter_farming_platform::FeeAccount::::get(), + ) + .unwrap_or(0); + assert_eq!(lp_tokens, fee); + + // Deposit to other XSTUSD/CERES pool with different reward token + pooled_tokens = ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); + + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + asset_xstusd, + asset_ceres, + asset_xstusd, + is_farm, + pooled_tokens, + )); - #[test] - fn get_rewards_pool_does_not_exist() { - preset_initial(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let pooled_tokens = 10; - let rewards = 1; + let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + let mut first_pool = balance!(0); + let mut second_pool = balance!(0); + for u_info in &user_infos { + if u_info.pool_asset == asset_ceres + && u_info.reward_asset == asset_ceres + && u_info.is_farm == is_farm + && u_info.base_asset == asset_xstusd + { + first_pool = u_info.pooled_tokens; + } else if u_info.pool_asset == asset_ceres + && u_info.reward_asset == asset_xstusd + && u_info.is_farm == is_farm + && u_info.base_asset == asset_xstusd + { + second_pool = u_info.pooled_tokens; + } + } + assert_eq!(first_pool, second_pool); + + pool_infos = demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == asset_xstusd + { + assert_eq!(p_info.total_tokens_in_pool, first_pool); + } + } + }); +} - let user_info = UserInfo { - base_asset: XOR, - pool_asset, - reward_asset, - is_farm, - pooled_tokens, - rewards, - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); - - assert_err!( - demeter_farming_platform::Pallet::::get_rewards( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - ), - demeter_farming_platform::Error::::PoolDoesNotExist - ); - }); - } - - #[test] - fn get_rewards_zero_rewards() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let pooled_tokens = 10; - let rewards = 0; - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: 0, - rewards, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - pool_info, - ); - - let user_info = UserInfo { - base_asset: XOR, - pool_asset, - reward_asset, - is_farm, - pooled_tokens, - rewards, - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); - - assert_err!( - demeter_farming_platform::Pallet::::get_rewards( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - ), - demeter_farming_platform::Error::::ZeroRewards - ); - }); - } - - #[test] - fn get_rewards_pool_does_not_have_rewards() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let pooled_tokens = 10; - let rewards = 2; - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: 0, - rewards: 1, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - pool_info, - ); - - let user_info = UserInfo { - base_asset: XOR, +#[test] +fn get_rewards_pool_does_not_exist() { + preset_initial(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let pooled_tokens = 10; + let rewards = 1; + + let user_info = UserInfo { + base_asset: XOR, + pool_asset, + reward_asset, + is_farm, + pooled_tokens, + rewards, + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + assert_err!( + demeter_farming_platform::Pallet::::get_rewards( + RuntimeOrigin::signed(ALICE), pool_asset, - reward_asset, - is_farm, - pooled_tokens, - rewards, - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); - - assert_err!( - demeter_farming_platform::Pallet::::get_rewards( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - ), - demeter_farming_platform::Error::::PoolDoesNotHaveRewards - ); - }); - } - - #[test] - fn get_rewards_ok() { - preset_initial(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: balance!(1000), - rewards: balance!(100), - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - let user_info = UserInfo { - base_asset: XOR, pool_asset, reward_asset, is_farm, - pooled_tokens: balance!(1000), - rewards: balance!(100), - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); + ), + demeter_farming_platform::Error::::PoolDoesNotExist + ); + }); +} - assert_ok!(demeter_farming_platform::Pallet::::get_rewards( +#[test] +fn get_rewards_zero_rewards() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let pooled_tokens = 10; + let rewards = 0; + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, pool_info); + + let user_info = UserInfo { + base_asset: XOR, + pool_asset, + reward_asset, + is_farm, + pooled_tokens, + rewards, + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + assert_err!( + demeter_farming_platform::Pallet::::get_rewards( RuntimeOrigin::signed(ALICE), pool_asset, pool_asset, reward_asset, is_farm, - )); - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for p_info in pool_infos.iter_mut() { - if p_info.is_farm == is_farm && p_info.base_asset == pool_asset { - assert_eq!(p_info.rewards, balance!(0)) - } - } - - let mut user_infos = demeter_farming_platform::UserInfos::::get(ALICE); - for u_info in user_infos.iter_mut() { - if u_info.pool_asset == pool_asset - && u_info.reward_asset == reward_asset - && u_info.is_farm == is_farm - && u_info.base_asset == pool_asset - { - assert_eq!(u_info.rewards, balance!(0)) - } - } - - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(3100) - ); - }); - } - - #[test] - fn get_rewards_xstusd_ok() { - preset_initial(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: balance!(1000), - rewards: balance!(100), - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XSTUSD, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - let user_info = UserInfo { - base_asset: XSTUSD, - pool_asset, - reward_asset, - is_farm, - pooled_tokens: balance!(1000), - rewards: balance!(100), - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); + ), + demeter_farming_platform::Error::::ZeroRewards + ); + }); +} - assert_ok!(demeter_farming_platform::Pallet::::get_rewards( +#[test] +fn get_rewards_pool_does_not_have_rewards() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let pooled_tokens = 10; + let rewards = 2; + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 1, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, pool_info); + + let user_info = UserInfo { + base_asset: XOR, + pool_asset, + reward_asset, + is_farm, + pooled_tokens, + rewards, + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + assert_err!( + demeter_farming_platform::Pallet::::get_rewards( RuntimeOrigin::signed(ALICE), pool_asset, pool_asset, reward_asset, is_farm, - )); - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for p_info in pool_infos.iter_mut() { - if p_info.is_farm == is_farm && p_info.base_asset == pool_asset { - assert_eq!(p_info.rewards, balance!(0)) - } - } + ), + demeter_farming_platform::Error::::PoolDoesNotHaveRewards + ); + }); +} + +#[test] +fn get_rewards_ok() { + preset_initial(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: balance!(1000), + rewards: balance!(100), + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + let user_info = UserInfo { + base_asset: XOR, + pool_asset, + reward_asset, + is_farm, + pooled_tokens: balance!(1000), + rewards: balance!(100), + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + assert_ok!(demeter_farming_platform::Pallet::::get_rewards( + RuntimeOrigin::signed(ALICE), + pool_asset, + pool_asset, + reward_asset, + is_farm, + )); - let mut user_infos = demeter_farming_platform::UserInfos::::get(ALICE); - for u_info in user_infos.iter_mut() { - if u_info.pool_asset == pool_asset - && u_info.reward_asset == reward_asset - && u_info.is_farm == is_farm - && u_info.base_asset == pool_asset - { - assert_eq!(u_info.rewards, balance!(0)) - } + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for p_info in pool_infos.iter_mut() { + if p_info.is_farm == is_farm && p_info.base_asset == pool_asset { + assert_eq!(p_info.rewards, balance!(0)) + } + } + + let mut user_infos = demeter_farming_platform::UserInfos::::get(ALICE); + for u_info in user_infos.iter_mut() { + if u_info.pool_asset == pool_asset + && u_info.reward_asset == reward_asset + && u_info.is_farm == is_farm + && u_info.base_asset == pool_asset + { + assert_eq!(u_info.rewards, balance!(0)) } + } - assert_eq!( - Assets::free_balance(&CERES_ASSET_ID, &ALICE) - .expect("Failed to query free balance."), - balance!(3100) - ); - }); - } - - #[test] - fn withdraw_insufficient_funds() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let base_asset = XSTUSD; - let pooled_tokens = 30; - let is_farm = true; - - let user_info = UserInfo { - base_asset, - pool_asset, - reward_asset, - is_farm, - pooled_tokens: 20, - rewards: 1, - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); - - assert_err!( - demeter_farming_platform::Pallet::::withdraw( - RuntimeOrigin::signed(ALICE), - base_asset, - pool_asset, - reward_asset, - pooled_tokens, - is_farm, - ), - demeter_farming_platform::Error::::InsufficientFunds - ); - }); - } - - #[test] - fn withdraw_ok() { - preset_initial(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let base_asset = XSTUSD; - let pooled_tokens = balance!(30); - let is_farm = false; - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: balance!(1000), - rewards: balance!(100), - rewards_to_be_distributed: 0, - is_removed: false, - base_asset, - }; + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(3100) + ); + }); +} - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); +#[test] +fn get_rewards_xstusd_ok() { + preset_initial(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: balance!(1000), + rewards: balance!(100), + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XSTUSD, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + let user_info = UserInfo { + base_asset: XSTUSD, + pool_asset, + reward_asset, + is_farm, + pooled_tokens: balance!(1000), + rewards: balance!(100), + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + assert_ok!(demeter_farming_platform::Pallet::::get_rewards( + RuntimeOrigin::signed(ALICE), + pool_asset, + pool_asset, + reward_asset, + is_farm, + )); - let user_info = UserInfo { - base_asset, - pool_asset, - reward_asset, - is_farm, - pooled_tokens, - rewards: 1, - }; + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for p_info in pool_infos.iter_mut() { + if p_info.is_farm == is_farm && p_info.base_asset == pool_asset { + assert_eq!(p_info.rewards, balance!(0)) + } + } + + let mut user_infos = demeter_farming_platform::UserInfos::::get(ALICE); + for u_info in user_infos.iter_mut() { + if u_info.pool_asset == pool_asset + && u_info.reward_asset == reward_asset + && u_info.is_farm == is_farm + && u_info.base_asset == pool_asset + { + assert_eq!(u_info.rewards, balance!(0)) + } + } - demeter_farming_platform::UserInfos::::append(ALICE, user_info); + assert_eq!( + Assets::free_balance(&CERES_ASSET_ID, &ALICE).expect("Failed to query free balance."), + balance!(3100) + ); + }); +} - assert_ok!(demeter_farming_platform::Pallet::::withdraw( +#[test] +fn withdraw_insufficient_funds() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let base_asset = XSTUSD; + let pooled_tokens = 30; + let is_farm = true; + + let user_info = UserInfo { + base_asset, + pool_asset, + reward_asset, + is_farm, + pooled_tokens: 20, + rewards: 1, + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + assert_err!( + demeter_farming_platform::Pallet::::withdraw( RuntimeOrigin::signed(ALICE), base_asset, pool_asset, reward_asset, pooled_tokens, is_farm, - )); - - let mut user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - - for user_info in user_infos.iter_mut() { - if user_info.pool_asset == pool_asset - && user_info.reward_asset == reward_asset - && user_info.is_farm == is_farm - && user_info.base_asset == base_asset - { - assert_eq!(user_info.pooled_tokens, balance!(0)); - } + ), + demeter_farming_platform::Error::::InsufficientFunds + ); + }); +} + +#[test] +fn withdraw_ok() { + preset_initial(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let base_asset = XSTUSD; + let pooled_tokens = balance!(30); + let is_farm = false; + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: balance!(1000), + rewards: balance!(100), + rewards_to_be_distributed: 0, + is_removed: false, + base_asset, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + let user_info = UserInfo { + base_asset, + pool_asset, + reward_asset, + is_farm, + pooled_tokens, + rewards: 1, + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + assert_ok!(demeter_farming_platform::Pallet::::withdraw( + RuntimeOrigin::signed(ALICE), + base_asset, + pool_asset, + reward_asset, + pooled_tokens, + is_farm, + )); + + let mut user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + + for user_info in user_infos.iter_mut() { + if user_info.pool_asset == pool_asset + && user_info.reward_asset == reward_asset + && user_info.is_farm == is_farm + && user_info.base_asset == base_asset + { + assert_eq!(user_info.pooled_tokens, balance!(0)); } + } - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for p_info in pool_infos.iter_mut() { - if p_info.is_farm == is_farm && p_info.base_asset == base_asset { - assert_eq!(p_info.total_tokens_in_pool, balance!(970)) - } + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for p_info in pool_infos.iter_mut() { + if p_info.is_farm == is_farm && p_info.base_asset == base_asset { + assert_eq!(p_info.total_tokens_in_pool, balance!(970)) } + } - assert_eq!( - Assets::free_balance(&XOR, &ALICE).expect("Failed to query free balance."), - balance!(2030) - ); - }); - } - - #[test] - fn withdraw_xstusd_ok() { - preset_initial(|| { - let dex_id = DEX_B_ID; - let asset_xstusd = XSTUSD; - let asset_ceres = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_ceres, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + assert_eq!( + Assets::free_balance(&XOR, &ALICE).expect("Failed to query free balance."), + balance!(2030) + ); + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - asset_xstusd, - asset_ceres, - asset_ceres, - is_farm, - multiplier, - deposit_fee, - is_core, - )); +#[test] +fn withdraw_xstusd_ok() { + preset_initial(|| { + let dex_id = DEX_B_ID; + let asset_xstusd = XSTUSD; + let asset_ceres = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_ceres, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, - asset_xstusd, - asset_ceres, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - asset_xstusd, - asset_ceres, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let mut pooled_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + asset_xstusd, + asset_ceres, + asset_ceres, + is_farm, + multiplier, + deposit_fee, + is_core, + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + asset_xstusd, + asset_ceres, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( asset_xstusd, asset_ceres, - asset_ceres, - is_farm, - pooled_tokens, - )); - - let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) - .try_into_balance() - .unwrap_or(0); - pooled_tokens -= fee; - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&asset_ceres, &asset_ceres); - for p_info in &pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == asset_xstusd - { - assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); - } - } - - let mut user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - for u_info in &user_infos { - if u_info.is_farm == is_farm && u_info.base_asset == asset_xstusd { - assert_eq!(u_info.pooled_tokens, pooled_tokens); - } - } + ) + .expect("Pool does not exist") + .0; - let lp_tokens = pool_xyk::Pallet::::balance_of_pool_provider( + // Calculate number of pool tokens of user's account + let mut pooled_tokens: Balance = + ::XYKPool::pool_providers( pool_account.clone(), - demeter_farming_platform::FeeAccount::::get(), + ALICE, ) + .expect("User is not pool provider"); + + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + asset_xstusd, + asset_ceres, + asset_ceres, + is_farm, + pooled_tokens, + )); + + let fee = (FixedWrapper::from(pooled_tokens) * FixedWrapper::from(deposit_fee)) + .try_into_balance() .unwrap_or(0); - assert_eq!(lp_tokens, fee); + pooled_tokens -= fee; + + let mut pool_infos = + demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == asset_xstusd + { + assert_eq!(p_info.total_tokens_in_pool, pooled_tokens); + } + } + + let mut user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + for u_info in &user_infos { + if u_info.is_farm == is_farm && u_info.base_asset == asset_xstusd { + assert_eq!(u_info.pooled_tokens, pooled_tokens); + } + } + + let lp_tokens = pool_xyk::Pallet::::balance_of_pool_provider( + pool_account, + demeter_farming_platform::FeeAccount::::get(), + ) + .unwrap_or(0); + assert_eq!(lp_tokens, fee); + + assert_ok!(demeter_farming_platform::Pallet::::withdraw( + RuntimeOrigin::signed(ALICE), + asset_xstusd, + asset_ceres, + asset_xstusd, + pooled_tokens, + is_farm, + )); + + user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + + for user_info in user_infos.iter_mut() { + if user_info.pool_asset == asset_ceres + && user_info.reward_asset == asset_xstusd + && user_info.is_farm == is_farm + && user_info.base_asset == asset_xstusd + { + assert_eq!(user_info.pooled_tokens, balance!(0)); + } + } + + pool_infos = demeter_farming_platform::Pools::::get(asset_ceres, asset_xstusd); + for p_info in pool_infos.iter_mut() { + if p_info.is_farm == is_farm && p_info.base_asset == asset_xstusd { + assert_eq!(p_info.total_tokens_in_pool, balance!(0)) + } + } + }); +} - assert_ok!(demeter_farming_platform::Pallet::::withdraw( +#[test] +fn remove_pool_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + + assert_err!( + demeter_farming_platform::Pallet::::remove_pool( RuntimeOrigin::signed(ALICE), - asset_xstusd, - asset_ceres, - asset_xstusd, - pooled_tokens, + pool_asset, + pool_asset, + reward_asset, is_farm, - )); - - user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - - for user_info in user_infos.iter_mut() { - if user_info.pool_asset == asset_ceres - && user_info.reward_asset == asset_xstusd - && user_info.is_farm == is_farm - && user_info.base_asset == asset_xstusd - { - assert_eq!(user_info.pooled_tokens, balance!(0)); - } + ), + demeter_farming_platform::Error::::Unauthorized + ); + }); +} + +#[test] +fn remove_pool_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 100, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + assert_ok!(demeter_farming_platform::Pallet::::remove_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, + is_farm, + )); + + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos.iter_mut() { + if pool_info.is_farm == is_farm && pool_info.base_asset == pool_asset { + pool_info.is_removed = true; } + assert!(pool_info.is_removed); + } + }); +} + +#[test] +fn remove_pool_xstusd_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 100, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XSTUSD, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + assert_ok!(demeter_farming_platform::Pallet::::remove_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, + is_farm, + )); - pool_infos = - demeter_farming_platform::Pools::::get(&asset_ceres, &asset_xstusd); - for p_info in pool_infos.iter_mut() { - if p_info.is_farm == is_farm && p_info.base_asset == asset_xstusd { - assert_eq!(p_info.total_tokens_in_pool, balance!(0)) - } + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos.iter_mut() { + if pool_info.is_farm == is_farm && pool_info.base_asset == pool_asset { + pool_info.is_removed = true; } - }); - } - - #[test] - fn remove_pool_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - - assert_err!( - demeter_farming_platform::Pallet::::remove_pool( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - ), - demeter_farming_platform::Error::::Unauthorized - ); - }); - } - - #[test] - fn remove_pool_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, + assert!(pool_info.is_removed); + } + }); +} + +#[test] +fn change_pool_multiplier_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let new_multiplier = 1; + + assert_err!( + demeter_farming_platform::Pallet::::change_pool_multiplier( + RuntimeOrigin::signed(ALICE), + pool_asset, + pool_asset, + reward_asset, is_farm, - total_tokens_in_pool: 0, - rewards: 100, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - assert_ok!(demeter_farming_platform::Pallet::::remove_pool( + new_multiplier, + ), + demeter_farming_platform::Error::::Unauthorized + ) + }); +} + +#[test] +fn change_pool_multiplier_pool_does_not_exist() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let new_multiplier = 1; + + let token_info = TokenInfo { + farms_total_multiplier: 0, + staking_total_multiplier: 0, + token_per_block: balance!(1), + farms_allocation: balance!(0.2), + staking_allocation: balance!(0.4), + team_allocation: balance!(0.4), + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, token_info); + + assert_err!( + demeter_farming_platform::Pallet::::change_pool_multiplier( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, pool_asset, reward_asset, is_farm, - )); - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos.iter_mut() { - if pool_info.is_farm == is_farm && pool_info.base_asset == pool_asset { - pool_info.is_removed = true; - } - assert_eq!(pool_info.is_removed, true); - } - }); - } - - #[test] - fn remove_pool_xstusd_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: 0, - rewards: 100, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XSTUSD, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - assert_ok!(demeter_farming_platform::Pallet::::remove_pool( + new_multiplier, + ), + demeter_farming_platform::Error::::PoolDoesNotExist + ) + }); +} + +#[test] +fn change_pool_multiplier_is_farm_true() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let new_multiplier = 2; + + let mut token_info = TokenInfo { + farms_total_multiplier: 1, + staking_total_multiplier: 0, + token_per_block: balance!(1), + farms_allocation: balance!(0.2), + staking_allocation: balance!(0.4), + team_allocation: balance!(0.4), + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, &token_info); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + assert_ok!( + demeter_farming_platform::Pallet::::change_pool_multiplier( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, pool_asset, reward_asset, is_farm, - )); - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos.iter_mut() { - if pool_info.is_farm == is_farm && pool_info.base_asset == pool_asset { - pool_info.is_removed = true; - } - assert_eq!(pool_info.is_removed, true); - } - }); - } - - #[test] - fn change_pool_multiplier_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let new_multiplier = 1; - - assert_err!( - demeter_farming_platform::Pallet::::change_pool_multiplier( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - new_multiplier, - ), - demeter_farming_platform::Error::::Unauthorized + new_multiplier, ) - }); - } - - #[test] - fn change_pool_multiplier_pool_does_not_exist() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let new_multiplier = 1; - - let token_info = TokenInfo { - farms_total_multiplier: 0, - staking_total_multiplier: 0, - token_per_block: balance!(1), - farms_allocation: balance!(0.2), - staking_allocation: balance!(0.4), - team_allocation: balance!(0.4), - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - assert_err!( - demeter_farming_platform::Pallet::::change_pool_multiplier( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - new_multiplier, - ), - demeter_farming_platform::Error::::PoolDoesNotExist - ) - }); - } - - #[test] - fn change_pool_multiplier_is_farm_true() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let new_multiplier = 2; - - let mut token_info = TokenInfo { - farms_total_multiplier: 1, - staking_total_multiplier: 0, - token_per_block: balance!(1), - farms_allocation: balance!(0.2), - staking_allocation: balance!(0.4), - team_allocation: balance!(0.4), - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: 0, - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - assert_ok!( - demeter_farming_platform::Pallet::::change_pool_multiplier( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - new_multiplier, - ) - ); - - token_info = - demeter_farming_platform::TokenInfos::::get(&reward_asset).unwrap(); - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos.iter_mut() { - if pool_info.is_farm == is_farm - && !pool_info.is_removed - && pool_info.base_asset == pool_asset - { - assert_eq!(pool_info.multiplier, new_multiplier); - } - } - assert_eq!(token_info.farms_total_multiplier, new_multiplier); - }); - } - - #[test] - fn change_pool_multiplier_is_farm_false() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = false; - let new_multiplier = 2; - - let mut token_info = TokenInfo { - farms_total_multiplier: 1, - staking_total_multiplier: 1, - token_per_block: balance!(1), - farms_allocation: balance!(0.2), - staking_allocation: balance!(0.4), - team_allocation: balance!(0.4), - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: 0, - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - assert_ok!( - demeter_farming_platform::Pallet::::change_pool_multiplier( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - new_multiplier, - ) - ); - - token_info = - demeter_farming_platform::TokenInfos::::get(&reward_asset).unwrap(); - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos.iter_mut() { - if pool_info.is_farm == is_farm - && !pool_info.is_removed - && pool_info.base_asset == pool_asset - { - assert_eq!(pool_info.multiplier, new_multiplier); - } + ); + + token_info = demeter_farming_platform::TokenInfos::::get(reward_asset).unwrap(); + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos.iter_mut() { + if pool_info.is_farm == is_farm + && !pool_info.is_removed + && pool_info.base_asset == pool_asset + { + assert_eq!(pool_info.multiplier, new_multiplier); } - assert_eq!(token_info.staking_total_multiplier, new_multiplier); - }); - } - - #[test] - fn change_pool_multiplier_is_farm_xstusd_true() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let new_multiplier = 2; - - let mut token_info = TokenInfo { - farms_total_multiplier: 1, - staking_total_multiplier: 0, - token_per_block: balance!(1), - farms_allocation: balance!(0.2), - staking_allocation: balance!(0.4), - team_allocation: balance!(0.4), - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: 0, - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XSTUSD, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - assert_ok!( - demeter_farming_platform::Pallet::::change_pool_multiplier( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - new_multiplier, - ) - ); - - token_info = - demeter_farming_platform::TokenInfos::::get(&reward_asset).unwrap(); - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos.iter_mut() { - if pool_info.is_farm == is_farm - && !pool_info.is_removed - && pool_info.base_asset == pool_asset - { - assert_eq!(pool_info.multiplier, new_multiplier); - } - } - assert_eq!(token_info.farms_total_multiplier, new_multiplier); - }); - } - - #[test] - fn change_pool_deposit_fee_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let deposit_fee = balance!(1); - - assert_err!( - demeter_farming_platform::Pallet::::change_pool_deposit_fee( - RuntimeOrigin::signed(ALICE), - pool_asset, - pool_asset, - reward_asset, - is_farm, - deposit_fee, - ), - demeter_farming_platform::Error::::Unauthorized - ) - }); - } - - #[test] - fn change_pool_deposit_fee_pool_does_not_exist() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let deposit_fee = balance!(1); - - assert_err!( - demeter_farming_platform::Pallet::::change_pool_deposit_fee( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - deposit_fee, - ), - demeter_farming_platform::Error::::PoolDoesNotExist - ) - }); - } - - #[test] - fn change_pool_deposit_fee_invalid_deposit_fee() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let mut deposit_fee = balance!(0.8); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee, - is_core: true, + } + assert_eq!(token_info.farms_total_multiplier, new_multiplier); + }); +} + +#[test] +fn change_pool_multiplier_is_farm_false() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = false; + let new_multiplier = 2; + + let mut token_info = TokenInfo { + farms_total_multiplier: 1, + staking_total_multiplier: 1, + token_per_block: balance!(1), + farms_allocation: balance!(0.2), + staking_allocation: balance!(0.4), + team_allocation: balance!(0.4), + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, &token_info); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + assert_ok!( + demeter_farming_platform::Pallet::::change_pool_multiplier( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, is_farm, - total_tokens_in_pool: 0, - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - deposit_fee = balance!(1.2); - - assert_err!( - demeter_farming_platform::Pallet::::change_pool_deposit_fee( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - deposit_fee, - ), - demeter_farming_platform::Error::::InvalidDepositFee + new_multiplier, ) - }); - } - - #[test] - fn change_pool_deposit_fee_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let mut deposit_fee = balance!(1); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee, - is_core: true, - is_farm, - total_tokens_in_pool: 0, - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - deposit_fee = balance!(0.8); - assert_ok!( - demeter_farming_platform::Pallet::::change_pool_deposit_fee( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - deposit_fee, - ) - ); - - let pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for p_info in pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == pool_asset - { - assert_eq!(p_info.deposit_fee, deposit_fee) - } + ); + + token_info = demeter_farming_platform::TokenInfos::::get(reward_asset).unwrap(); + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos.iter_mut() { + if pool_info.is_farm == is_farm + && !pool_info.is_removed + && pool_info.base_asset == pool_asset + { + assert_eq!(pool_info.multiplier, new_multiplier); } - }); - } - - #[test] - fn change_pool_deposit_fee_xstusd_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let mut deposit_fee = balance!(1); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee, - is_core: true, + } + assert_eq!(token_info.staking_total_multiplier, new_multiplier); + }); +} + +#[test] +fn change_pool_multiplier_is_farm_xstusd_true() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let new_multiplier = 2; + + let mut token_info = TokenInfo { + farms_total_multiplier: 1, + staking_total_multiplier: 0, + token_per_block: balance!(1), + farms_allocation: balance!(0.2), + staking_allocation: balance!(0.4), + team_allocation: balance!(0.4), + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, &token_info); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XSTUSD, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + assert_ok!( + demeter_farming_platform::Pallet::::change_pool_multiplier( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + pool_asset, + reward_asset, is_farm, - total_tokens_in_pool: 0, - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XSTUSD, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - deposit_fee = balance!(0.8); - assert_ok!( - demeter_farming_platform::Pallet::::change_pool_deposit_fee( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - deposit_fee, - ) - ); - - let pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for p_info in pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == pool_asset - { - assert_eq!(p_info.deposit_fee, deposit_fee) - } - } - }); - } - - #[test] - fn change_token_info_unauthorized() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_err!( - demeter_farming_platform::Pallet::::change_token_info( - RuntimeOrigin::signed(ALICE), - pool_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::Unauthorized - ) - }); - } - - #[test] - fn change_token_info_reward_token_is_not_registered() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = CERES_ASSET_ID; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.2); - let staking_allocation = balance!(0.4); - let team_allocation = balance!(0.4); - - assert_err!( - demeter_farming_platform::Pallet::::change_token_info( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::RewardTokenIsNotRegistered - ) - }); - } - - #[test] - fn change_token_info_reward_token_per_block_cant_be_zero() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = CERES_ASSET_ID; - let token_per_block = balance!(0); - let farms_allocation = balance!(0.2); - let staking_allocation = balance!(0.4); - let team_allocation = balance!(0.4); - - let token_info = TokenInfo { - farms_total_multiplier: 1, - staking_total_multiplier: 1, - token_per_block: balance!(1), - farms_allocation, - staking_allocation, - team_allocation, - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - assert_err!( - demeter_farming_platform::Pallet::::change_token_info( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::TokenPerBlockCantBeZero - ) - }); - } - - #[test] - fn change_token_info_invalid_allocation_parameters() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = CERES_ASSET_ID; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.1); - let staking_allocation = balance!(0.4); - let team_allocation = balance!(0.4); - - let token_info = TokenInfo { - farms_total_multiplier: 1, - staking_total_multiplier: 1, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - assert_err!( - demeter_farming_platform::Pallet::::change_token_info( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - ), - demeter_farming_platform::Error::::InvalidAllocationParameters + new_multiplier, ) - }); - } - - #[test] - fn change_token_info_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let reward_asset = CERES_ASSET_ID; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.2); - let staking_allocation = balance!(0.4); - let team_allocation = balance!(0.4); - - let token_info = TokenInfo { - farms_total_multiplier: 1, - staking_total_multiplier: 1, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - team_account: BOB, - }; - - demeter_farming_platform::TokenInfos::::insert(&reward_asset, &token_info); - - assert_ok!( - demeter_farming_platform::Pallet::::change_token_info( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - reward_asset, - token_per_block, - farms_allocation, - team_allocation, - staking_allocation, - BOB - ) - ); - - assert_eq!(token_info.token_per_block, token_per_block); - assert_eq!(token_info.farms_allocation, farms_allocation); - assert_eq!(token_info.staking_allocation, staking_allocation); - assert_eq!(token_info.team_allocation, team_allocation); - }); - } - - #[test] - fn change_total_tokens_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let total_tokens = balance!(200); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: balance!(100), - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XOR, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - assert_ok!( - demeter_farming_platform::Pallet::::change_total_tokens( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - total_tokens, - ) - ); - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos.iter_mut() { - if pool_info.is_farm == is_farm - && !pool_info.is_removed - && pool_info.base_asset == pool_asset - { - assert_eq!(pool_info.total_tokens_in_pool, total_tokens); - } + ); + + token_info = demeter_farming_platform::TokenInfos::::get(reward_asset).unwrap(); + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos.iter_mut() { + if pool_info.is_farm == is_farm + && !pool_info.is_removed + && pool_info.base_asset == pool_asset + { + assert_eq!(pool_info.multiplier, new_multiplier); } - }); - } - - #[test] - fn change_total_tokens_xstusd_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let total_tokens = balance!(200); - - let pool_info = PoolData { - multiplier: 1, - deposit_fee: balance!(0), - is_core: true, - is_farm, - total_tokens_in_pool: balance!(100), - rewards: 0, - rewards_to_be_distributed: 0, - is_removed: false, - base_asset: XSTUSD, - }; - - demeter_farming_platform::Pools::::append( - &pool_asset, - &reward_asset, - &pool_info, - ); - - assert_ok!( - demeter_farming_platform::Pallet::::change_total_tokens( - RuntimeOrigin::signed( - demeter_farming_platform::AuthorityAccount::::get() - ), - pool_asset, - pool_asset, - reward_asset, - is_farm, - total_tokens, - ) - ); - - let mut pool_infos = - demeter_farming_platform::Pools::::get(&pool_asset, &reward_asset); - for pool_info in pool_infos.iter_mut() { - if pool_info.is_farm == is_farm - && !pool_info.is_removed - && pool_info.base_asset == pool_asset - { - assert_eq!(pool_info.total_tokens_in_pool, total_tokens); - } - } - }); - } - - #[test] - fn change_info_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let base_asset = XSTUSD; - let is_farm = true; - let pooled_tokens = 10; - let rewards = 1; - - let user_info = UserInfo { - base_asset, + } + assert_eq!(token_info.farms_total_multiplier, new_multiplier); + }); +} + +#[test] +fn change_pool_deposit_fee_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let deposit_fee = balance!(1); + + assert_err!( + demeter_farming_platform::Pallet::::change_pool_deposit_fee( + RuntimeOrigin::signed(ALICE), + pool_asset, pool_asset, reward_asset, is_farm, - pooled_tokens, - rewards, - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); + deposit_fee, + ), + demeter_farming_platform::Error::::Unauthorized + ) + }); +} - let pool_tokens = balance!(69); - assert_ok!(demeter_farming_platform::Pallet::::change_info( +#[test] +fn change_pool_deposit_fee_pool_does_not_exist() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let deposit_fee = balance!(1); + + assert_err!( + demeter_farming_platform::Pallet::::change_pool_deposit_fee( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - ALICE.into(), - base_asset, pool_asset, - reward_asset, - is_farm, - pool_tokens, - )); - - let user_info_alice = demeter_farming_platform::UserInfos::::get(&ALICE); - for user_info in &user_info_alice { - if user_info.pool_asset == pool_asset - && user_info.reward_asset == reward_asset - && user_info.is_farm == is_farm - && user_info.base_asset == base_asset - { - assert_eq!(user_info.pooled_tokens, pool_tokens); - } - } - }); - } - - #[test] - fn change_info_xstusd_ok() { - let mut ext = ExtBuilder::default().build(); - ext.execute_with(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let pooled_tokens = 10; - let rewards = 1; - - let user_info = UserInfo { - base_asset: XSTUSD, pool_asset, reward_asset, is_farm, - pooled_tokens, - rewards, - }; - - demeter_farming_platform::UserInfos::::append(ALICE, user_info); + deposit_fee, + ), + demeter_farming_platform::Error::::PoolDoesNotExist + ) + }); +} - let pool_tokens = balance!(69); - assert_ok!(demeter_farming_platform::Pallet::::change_info( +#[test] +fn change_pool_deposit_fee_invalid_deposit_fee() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let mut deposit_fee = balance!(0.8); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee, + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, pool_info); + + deposit_fee = balance!(1.2); + + assert_err!( + demeter_farming_platform::Pallet::::change_pool_deposit_fee( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - ALICE.into(), pool_asset, pool_asset, reward_asset, is_farm, - pool_tokens, - )); - - let user_info_alice = demeter_farming_platform::UserInfos::::get(&ALICE); - for user_info in &user_info_alice { - if user_info.pool_asset == pool_asset - && user_info.reward_asset == reward_asset - && user_info.is_farm == is_farm - && user_info.base_asset == pool_asset - { - assert_eq!(user_info.pooled_tokens, pool_tokens); - } - } - }); - } - - #[test] - fn on_initialize_ok() { - preset_initial(|| { - let dex_id = DEX_A_ID; - let xor = XOR; - let ceres = CERES_ASSET_ID; - let deo = DEMETER_ASSET_ID; - let util = AssetId32::from_bytes(hex!( - "007348eb8f0f3cec730fbf5eec1b6a842c54d1df8bed75a9df084d5ee013e814" - )); - - let is_farm = true; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.36); - let team_allocation = balance!(0.04); - - // Register DEO - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - deo, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); - - let token_per_block = balance!(0.01); - let farms_allocation = balance!(0.5); - let staking_allocation = balance!(0.4); - let team_allocation = balance!(0.1); - // Register UTIL - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - util, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - CHARLES - )); - - // XOR/CERES - reward DEO - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - xor, - ceres, - deo, - is_farm, - 10, deposit_fee, - is_core - )); + ), + demeter_farming_platform::Error::::InvalidDepositFee + ) + }); +} - // XOR/CERES - reward UTIL - assert_ok!(demeter_farming_platform::Pallet::::add_pool( +#[test] +fn change_pool_deposit_fee_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let mut deposit_fee = balance!(1); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee, + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + deposit_fee = balance!(0.8); + assert_ok!( + demeter_farming_platform::Pallet::::change_pool_deposit_fee( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - xor, - ceres, - util, + pool_asset, + pool_asset, + reward_asset, is_farm, - 5, deposit_fee, - is_core - )); + ) + ); - // CERES - reward DEO - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - ceres, - ceres, - deo, - !is_farm, - 1, - deposit_fee, - is_core - )); + let pool_infos = demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for p_info in pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == pool_asset { + assert_eq!(p_info.deposit_fee, deposit_fee) + } + } + }); +} - // CERES - reward UTIL - assert_ok!(demeter_farming_platform::Pallet::::add_pool( +#[test] +fn change_pool_deposit_fee_xstusd_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let mut deposit_fee = balance!(1); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee, + is_core: true, + is_farm, + total_tokens_in_pool: 0, + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XSTUSD, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + deposit_fee = balance!(0.8); + assert_ok!( + demeter_farming_platform::Pallet::::change_pool_deposit_fee( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - ceres, - ceres, - util, - !is_farm, - 1, - deposit_fee, - is_core - )); - - let pallet_account = PalletId(*b"deofarms").into_account_truncating(); - assert_ok!(assets::Pallet::::transfer_from( - &util, - &ALICE, - &pallet_account, - balance!(1500), - )); - - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, - xor, - ceres, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(BOB), - dex_id, - xor, - ceres, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // DEPOSIT TO XOR/CERES POOL - reward DEO - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - xor, - ceres, - deo, - is_farm, - balance!(2) - )); - - // DEPOSIT TO XOR/CERES POOL - reward DEO - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(BOB), - xor, - ceres, - deo, - is_farm, - balance!(2) - )); - - // DEPOSIT TO XOR/CERES POOL - reward UTIL - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - xor, - ceres, - util, + pool_asset, + pool_asset, + reward_asset, is_farm, - balance!(1) - )); - - // DEPOSIT TO CERES POOL - reward DEO - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(BOB), - ceres, - ceres, - deo, - !is_farm, - balance!(100) - )); - - // DEPOSIT TO CERES POOL - reward UTIL - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(BOB), - ceres, - ceres, - util, - !is_farm, - balance!(100) - )); - - run_to_block(16201); - - // Check XOR/CERES pool and CERES pool - reward DEO - let pool_infos = demeter_farming_platform::Pools::::get(&ceres, &deo); - for pool_info in pool_infos { - if pool_info.is_farm && pool_info.base_asset == xor { - assert_eq!(pool_info.total_tokens_in_pool, balance!(3.84)); - assert_eq!(pool_info.rewards_to_be_distributed, balance!(8640)); - assert_eq!(pool_info.rewards, balance!(1080)); - } else { - assert_eq!(pool_info.total_tokens_in_pool, balance!(96)); - assert_eq!(pool_info.rewards_to_be_distributed, balance!(5184)); - assert_eq!(pool_info.rewards, balance!(648)); - } - } - let token_info = demeter_farming_platform::TokenInfos::::get(&deo).unwrap(); - assert_eq!( - assets::Pallet::::free_balance(&deo, &token_info.team_account).unwrap(), - balance!(576) - ); - let user_info_alice = demeter_farming_platform::UserInfos::::get(&ALICE); - for user_info in &user_info_alice { - if user_info.pool_asset == ceres - && user_info.reward_asset == deo - && user_info.is_farm - && user_info.base_asset == xor - { - assert_eq!(user_info.rewards, balance!(540)); - } - } - let user_info_bob = demeter_farming_platform::UserInfos::::get(&BOB); - for user_info in &user_info_bob { - if user_info.pool_asset == ceres - && user_info.reward_asset == deo - && user_info.is_farm - && user_info.base_asset == xor - { - assert_eq!(user_info.rewards, balance!(540)); - } else if user_info.pool_asset == ceres - && user_info.reward_asset == deo - && !user_info.is_farm - { - assert_eq!(user_info.rewards, balance!(648)); - } - } + deposit_fee, + ) + ); - // Check XOR/CERES pool and CERES pool - reward UTIL - let pool_infos = demeter_farming_platform::Pools::::get(&ceres, &util); - for pool_info in pool_infos { - if pool_info.is_farm && pool_info.base_asset == xor { - assert_eq!(pool_info.total_tokens_in_pool, balance!(0.96)); - assert_eq!(pool_info.rewards_to_be_distributed, balance!(72)); - assert_eq!(pool_info.rewards, balance!(9)); - } else { - assert_eq!(pool_info.total_tokens_in_pool, balance!(96)); - assert_eq!(pool_info.rewards_to_be_distributed, balance!(57.6)); - assert_eq!(pool_info.rewards, balance!(7.2)); - } - } - let token_info = demeter_farming_platform::TokenInfos::::get(&util).unwrap(); - assert_eq!( - assets::Pallet::::free_balance(&util, &token_info.team_account).unwrap(), - balance!(14.4) - ); - for user_info in &user_info_alice { - if user_info.pool_asset == ceres - && user_info.reward_asset == util - && user_info.is_farm - && user_info.base_asset == xor - { - assert_eq!(user_info.rewards, balance!(9)); - } - } - for user_info in &user_info_bob { - if user_info.pool_asset == ceres - && user_info.reward_asset == util - && !user_info.is_farm - { - assert_eq!(user_info.rewards, balance!(7.2)); - } + let pool_infos = demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for p_info in pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == pool_asset { + assert_eq!(p_info.deposit_fee, deposit_fee) } + } + }); +} - assert_ok!(demeter_farming_platform::Pallet::::get_rewards( +#[test] +fn change_token_info_unauthorized() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_err!( + demeter_farming_platform::Pallet::::change_token_info( RuntimeOrigin::signed(ALICE), - xor, - ceres, - deo, - is_farm - )); - - assert_eq!( - Assets::free_balance(&deo, &ALICE).expect("Failed to query free balance."), - balance!(540) - ); - - assert_ok!(demeter_farming_platform::Pallet::::get_rewards( - RuntimeOrigin::signed(BOB), - ceres, - ceres, - util, - !is_farm - )); - - assert_eq!( - Assets::free_balance(&util, &BOB).expect("Failed to query free balance."), - balance!(7.2) - ); - }); - } - - #[test] - fn check_if_has_enough_liquidity_out_of_farming_true() { - preset_initial(|| { - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, + pool_asset, token_per_block, farms_allocation, staking_allocation, team_allocation, BOB - )); + ), + demeter_farming_platform::Error::::Unauthorized + ) + }); +} - assert_ok!(demeter_farming_platform::Pallet::::register_token( +#[test] +fn change_token_info_reward_token_is_not_registered() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = CERES_ASSET_ID; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.2); + let staking_allocation = balance!(0.4); + let team_allocation = balance!(0.4); + + assert_err!( + demeter_farming_platform::Pallet::::change_token_info( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - pool_asset, + reward_asset, token_per_block, farms_allocation, staking_allocation, team_allocation, BOB - )); - - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - pool_asset, - reward_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core - )); - - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - pool_asset, - reward_asset, - pool_asset, - is_farm, - multiplier, - deposit_fee, - is_core - )); - - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - DEX_A_ID.into(), - pool_asset, - reward_asset, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - pool_asset, - reward_asset, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let pool_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - reward_asset, - is_farm, - pool_tokens - )); - - let pooled_tokens = (FixedWrapper::from(pool_tokens) - * FixedWrapper::from(balance!(0.96))) - .try_into_balance() - .unwrap_or(0); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - pool_asset, - is_farm, - pooled_tokens - )); - - let mut pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) - / FixedWrapper::from(balance!(2))) - .try_into_balance() - .unwrap_or(0); - - assert_ok!(demeter_farming_platform::Pallet::::withdraw( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - reward_asset, - pooled_tokens_to_withdraw, - is_farm - )); - - assert_ok!(demeter_farming_platform::Pallet::::withdraw( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - pool_asset, - pooled_tokens_to_withdraw, - is_farm - )); - - pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) - / FixedWrapper::from(balance!(3))) - .try_into_balance() - .unwrap_or(0); + ), + demeter_farming_platform::Error::::RewardTokenIsNotRegistered + ) + }); +} - assert_eq!( - demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( - &ALICE, - pool_asset, - reward_asset, - pooled_tokens_to_withdraw, - ), - true - ); - }); - } - - #[test] - fn check_if_has_enough_liquidity_out_of_farming_false() { - preset_initial(|| { - let dex_id = DEX_A_ID; - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( +#[test] +fn change_token_info_reward_token_per_block_cant_be_zero() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = CERES_ASSET_ID; + let token_per_block = balance!(0); + let farms_allocation = balance!(0.2); + let staking_allocation = balance!(0.4); + let team_allocation = balance!(0.4); + + let token_info = TokenInfo { + farms_total_multiplier: 1, + staking_total_multiplier: 1, + token_per_block: balance!(1), + farms_allocation, + staking_allocation, + team_allocation, + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, token_info); + + assert_err!( + demeter_farming_platform::Pallet::::change_token_info( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), reward_asset, token_per_block, @@ -3231,82 +2371,36 @@ mod tests { staking_allocation, team_allocation, BOB - )); - - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - pool_asset, - reward_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core - )); + ), + demeter_farming_platform::Error::::TokenPerBlockCantBeZero + ) + }); +} - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, - pool_asset, - reward_asset, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - pool_asset, - reward_asset, - ) - .expect("Pool does not exist") - .0; - // Calculate number of pool tokens of user's account - let pool_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - reward_asset, - is_farm, - pool_tokens - balance!(1) - )); - - assert_eq!( - demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( - &ALICE, - pool_asset, - reward_asset, - pool_tokens, - ), - false - ); - }); - } - - #[test] - fn check_if_has_enough_liquidity_out_of_farming_xstusd_true() { - preset_initial(|| { - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( +#[test] +fn change_token_info_invalid_allocation_parameters() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = CERES_ASSET_ID; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.1); + let staking_allocation = balance!(0.4); + let team_allocation = balance!(0.4); + + let token_info = TokenInfo { + farms_total_multiplier: 1, + staking_total_multiplier: 1, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, token_info); + + assert_err!( + demeter_farming_platform::Pallet::::change_token_info( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), reward_asset, token_per_block, @@ -3314,675 +2408,1430 @@ mod tests { staking_allocation, team_allocation, BOB - )); + ), + demeter_farming_platform::Error::::InvalidAllocationParameters + ) + }); +} - assert_ok!(demeter_farming_platform::Pallet::::register_token( +#[test] +fn change_token_info_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let reward_asset = CERES_ASSET_ID; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.2); + let staking_allocation = balance!(0.4); + let team_allocation = balance!(0.4); + + let token_info = TokenInfo { + farms_total_multiplier: 1, + staking_total_multiplier: 1, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + team_account: BOB, + }; + + demeter_farming_platform::TokenInfos::::insert(reward_asset, &token_info); + + assert_ok!( + demeter_farming_platform::Pallet::::change_token_info( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - pool_asset, + reward_asset, token_per_block, farms_allocation, - staking_allocation, team_allocation, + staking_allocation, BOB - )); + ) + ); - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - pool_asset, - reward_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core - )); + assert_eq!(token_info.token_per_block, token_per_block); + assert_eq!(token_info.farms_allocation, farms_allocation); + assert_eq!(token_info.staking_allocation, staking_allocation); + assert_eq!(token_info.team_allocation, team_allocation); + }); +} - assert_ok!(demeter_farming_platform::Pallet::::add_pool( +#[test] +fn change_total_tokens_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let total_tokens = balance!(200); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: balance!(100), + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XOR, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + assert_ok!( + demeter_farming_platform::Pallet::::change_total_tokens( RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, - reward_asset, pool_asset, + reward_asset, is_farm, - multiplier, - deposit_fee, - is_core - )); + total_tokens, + ) + ); + + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos.iter_mut() { + if pool_info.is_farm == is_farm + && !pool_info.is_removed + && pool_info.base_asset == pool_asset + { + assert_eq!(pool_info.total_tokens_in_pool, total_tokens); + } + } + }); +} - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - DEX_B_ID.into(), +#[test] +fn change_total_tokens_xstusd_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let total_tokens = balance!(200); + + let pool_info = PoolData { + multiplier: 1, + deposit_fee: balance!(0), + is_core: true, + is_farm, + total_tokens_in_pool: balance!(100), + rewards: 0, + rewards_to_be_distributed: 0, + is_removed: false, + base_asset: XSTUSD, + }; + + demeter_farming_platform::Pools::::append(pool_asset, reward_asset, &pool_info); + + assert_ok!( + demeter_farming_platform::Pallet::::change_total_tokens( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), pool_asset, - reward_asset, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - pool_asset, - reward_asset, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let pool_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), pool_asset, reward_asset, - reward_asset, is_farm, - pool_tokens - )); + total_tokens, + ) + ); + + let mut pool_infos = + demeter_farming_platform::Pools::::get(pool_asset, reward_asset); + for pool_info in pool_infos.iter_mut() { + if pool_info.is_farm == is_farm + && !pool_info.is_removed + && pool_info.base_asset == pool_asset + { + assert_eq!(pool_info.total_tokens_in_pool, total_tokens); + } + } + }); +} - let pooled_tokens = (FixedWrapper::from(pool_tokens) - * FixedWrapper::from(balance!(0.96))) - .try_into_balance() - .unwrap_or(0); +#[test] +fn change_info_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let base_asset = XSTUSD; + let is_farm = true; + let pooled_tokens = 10; + let rewards = 1; + + let user_info = UserInfo { + base_asset, + pool_asset, + reward_asset, + is_farm, + pooled_tokens, + rewards, + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + let pool_tokens = balance!(69); + assert_ok!(demeter_farming_platform::Pallet::::change_info( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + ALICE, + base_asset, + pool_asset, + reward_asset, + is_farm, + pool_tokens, + )); - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), + let user_info_alice = demeter_farming_platform::UserInfos::::get(&ALICE); + for user_info in &user_info_alice { + if user_info.pool_asset == pool_asset + && user_info.reward_asset == reward_asset + && user_info.is_farm == is_farm + && user_info.base_asset == base_asset + { + assert_eq!(user_info.pooled_tokens, pool_tokens); + } + } + }); +} + +#[test] +fn change_info_xstusd_ok() { + let mut ext = ExtBuilder::default().build(); + ext.execute_with(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let pooled_tokens = 10; + let rewards = 1; + + let user_info = UserInfo { + base_asset: XSTUSD, + pool_asset, + reward_asset, + is_farm, + pooled_tokens, + rewards, + }; + + demeter_farming_platform::UserInfos::::append(ALICE, user_info); + + let pool_tokens = balance!(69); + assert_ok!(demeter_farming_platform::Pallet::::change_info( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + ALICE, + pool_asset, + pool_asset, + reward_asset, + is_farm, + pool_tokens, + )); + + let user_info_alice = demeter_farming_platform::UserInfos::::get(&ALICE); + for user_info in &user_info_alice { + if user_info.pool_asset == pool_asset + && user_info.reward_asset == reward_asset + && user_info.is_farm == is_farm + && user_info.base_asset == pool_asset + { + assert_eq!(user_info.pooled_tokens, pool_tokens); + } + } + }); +} + +#[test] +fn on_initialize_ok() { + preset_initial(|| { + let dex_id = DEX_A_ID; + let xor = XOR; + let ceres = CERES_ASSET_ID; + let deo = DEMETER_ASSET_ID; + let util = AssetId32::from_bytes(hex!( + "007348eb8f0f3cec730fbf5eec1b6a842c54d1df8bed75a9df084d5ee013e814" + )); + + let is_farm = true; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.36); + let team_allocation = balance!(0.04); + + // Register DEO + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + deo, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + let token_per_block = balance!(0.01); + let farms_allocation = balance!(0.5); + let staking_allocation = balance!(0.4); + let team_allocation = balance!(0.1); + // Register UTIL + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + util, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + CHARLES + )); + + // XOR/CERES - reward DEO + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + xor, + ceres, + deo, + is_farm, + 10, + deposit_fee, + is_core + )); + + // XOR/CERES - reward UTIL + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + xor, + ceres, + util, + is_farm, + 5, + deposit_fee, + is_core + )); + + // CERES - reward DEO + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + ceres, + ceres, + deo, + !is_farm, + 1, + deposit_fee, + is_core + )); + + // CERES - reward UTIL + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + ceres, + ceres, + util, + !is_farm, + 1, + deposit_fee, + is_core + )); + + let pallet_account = PalletId(*b"deofarms").into_account_truncating(); + assert_ok!(assets::Pallet::::transfer_from( + &util, + &ALICE, + &pallet_account, + balance!(1500), + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + xor, + ceres, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(BOB), + dex_id, + xor, + ceres, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // DEPOSIT TO XOR/CERES POOL - reward DEO + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + xor, + ceres, + deo, + is_farm, + balance!(2) + )); + + // DEPOSIT TO XOR/CERES POOL - reward DEO + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(BOB), + xor, + ceres, + deo, + is_farm, + balance!(2) + )); + + // DEPOSIT TO XOR/CERES POOL - reward UTIL + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + xor, + ceres, + util, + is_farm, + balance!(1) + )); + + // DEPOSIT TO CERES POOL - reward DEO + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(BOB), + ceres, + ceres, + deo, + !is_farm, + balance!(100) + )); + + // DEPOSIT TO CERES POOL - reward UTIL + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(BOB), + ceres, + ceres, + util, + !is_farm, + balance!(100) + )); + + run_to_block(16201); + + // Check XOR/CERES pool and CERES pool - reward DEO + let pool_infos = demeter_farming_platform::Pools::::get(ceres, deo); + for pool_info in pool_infos { + if pool_info.is_farm && pool_info.base_asset == xor { + assert_eq!(pool_info.total_tokens_in_pool, balance!(3.84)); + assert_eq!(pool_info.rewards_to_be_distributed, balance!(8640)); + assert_eq!(pool_info.rewards, balance!(1080)); + } else { + assert_eq!(pool_info.total_tokens_in_pool, balance!(96)); + assert_eq!(pool_info.rewards_to_be_distributed, balance!(5184)); + assert_eq!(pool_info.rewards, balance!(648)); + } + } + let token_info = demeter_farming_platform::TokenInfos::::get(deo).unwrap(); + assert_eq!( + assets::Pallet::::free_balance(&deo, &token_info.team_account).unwrap(), + balance!(576) + ); + let user_info_alice = demeter_farming_platform::UserInfos::::get(&ALICE); + for user_info in &user_info_alice { + if user_info.pool_asset == ceres + && user_info.reward_asset == deo + && user_info.is_farm + && user_info.base_asset == xor + { + assert_eq!(user_info.rewards, balance!(540)); + } + } + let user_info_bob = demeter_farming_platform::UserInfos::::get(&BOB); + for user_info in &user_info_bob { + if user_info.pool_asset == ceres + && user_info.reward_asset == deo + && user_info.is_farm + && user_info.base_asset == xor + { + assert_eq!(user_info.rewards, balance!(540)); + } else if user_info.pool_asset == ceres + && user_info.reward_asset == deo + && !user_info.is_farm + { + assert_eq!(user_info.rewards, balance!(648)); + } + } + + // Check XOR/CERES pool and CERES pool - reward UTIL + let pool_infos = demeter_farming_platform::Pools::::get(ceres, util); + for pool_info in pool_infos { + if pool_info.is_farm && pool_info.base_asset == xor { + assert_eq!(pool_info.total_tokens_in_pool, balance!(0.96)); + assert_eq!(pool_info.rewards_to_be_distributed, balance!(72)); + assert_eq!(pool_info.rewards, balance!(9)); + } else { + assert_eq!(pool_info.total_tokens_in_pool, balance!(96)); + assert_eq!(pool_info.rewards_to_be_distributed, balance!(57.6)); + assert_eq!(pool_info.rewards, balance!(7.2)); + } + } + let token_info = demeter_farming_platform::TokenInfos::::get(util).unwrap(); + assert_eq!( + assets::Pallet::::free_balance(&util, &token_info.team_account).unwrap(), + balance!(14.4) + ); + for user_info in &user_info_alice { + if user_info.pool_asset == ceres + && user_info.reward_asset == util + && user_info.is_farm + && user_info.base_asset == xor + { + assert_eq!(user_info.rewards, balance!(9)); + } + } + for user_info in &user_info_bob { + if user_info.pool_asset == ceres && user_info.reward_asset == util && !user_info.is_farm + { + assert_eq!(user_info.rewards, balance!(7.2)); + } + } + + assert_ok!(demeter_farming_platform::Pallet::::get_rewards( + RuntimeOrigin::signed(ALICE), + xor, + ceres, + deo, + is_farm + )); + + assert_eq!( + Assets::free_balance(&deo, &ALICE).expect("Failed to query free balance."), + balance!(540) + ); + + assert_ok!(demeter_farming_platform::Pallet::::get_rewards( + RuntimeOrigin::signed(BOB), + ceres, + ceres, + util, + !is_farm + )); + + assert_eq!( + Assets::free_balance(&util, &BOB).expect("Failed to query free balance."), + balance!(7.2) + ); + }); +} + +#[test] +fn check_if_has_enough_liquidity_out_of_farming_true() { + preset_initial(|| { + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + pool_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + DEX_A_ID, + pool_asset, + reward_asset, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( pool_asset, reward_asset, - pool_asset, - is_farm, - pooled_tokens - )); + ) + .expect("Pool does not exist") + .0; + + // Calculate number of pool tokens of user's account + let pool_tokens: Balance = + ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); + + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + is_farm, + pool_tokens + )); - let mut pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) - / FixedWrapper::from(balance!(2))) + let pooled_tokens = (FixedWrapper::from(pool_tokens) * FixedWrapper::from(balance!(0.96))) .try_into_balance() .unwrap_or(0); - assert_ok!(demeter_farming_platform::Pallet::::withdraw( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - reward_asset, - pooled_tokens_to_withdraw, - is_farm - )); + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + pool_asset, + is_farm, + pooled_tokens + )); - assert_ok!(demeter_farming_platform::Pallet::::withdraw( - RuntimeOrigin::signed(ALICE), + let mut pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) + / FixedWrapper::from(balance!(2))) + .try_into_balance() + .unwrap_or(0); + + assert_ok!(demeter_farming_platform::Pallet::::withdraw( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + pooled_tokens_to_withdraw, + is_farm + )); + + assert_ok!(demeter_farming_platform::Pallet::::withdraw( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + pool_asset, + pooled_tokens_to_withdraw, + is_farm + )); + + pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) + / FixedWrapper::from(balance!(3))) + .try_into_balance() + .unwrap_or(0); + + assert!( + demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( + &ALICE, pool_asset, reward_asset, - pool_asset, pooled_tokens_to_withdraw, - is_farm - )); + ) + ); + }); +} - pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) - / FixedWrapper::from(balance!(3))) - .try_into_balance() - .unwrap_or(0); +#[test] +fn check_if_has_enough_liquidity_out_of_farming_false() { + preset_initial(|| { + let dex_id = DEX_A_ID; + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); - assert_eq!( - demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( - &ALICE, - pool_asset, - reward_asset, - pooled_tokens_to_withdraw, - ), - true - ); - }); - } - - #[test] - fn check_if_has_enough_liquidity_out_of_farming_xstusd_false() { - preset_initial(|| { - let dex_id = DEX_B_ID; - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + pool_asset, + reward_asset, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( pool_asset, reward_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core - )); + ) + .expect("Pool does not exist") + .0; + // Calculate number of pool tokens of user's account + let pool_tokens: Balance = + ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + is_farm, + pool_tokens - balance!(1) + )); + + assert!( + !demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( + &ALICE, pool_asset, reward_asset, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - pool_asset, - reward_asset, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let pool_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), + pool_tokens, + ) + ); + }); +} + +#[test] +fn check_if_has_enough_liquidity_out_of_farming_xstusd_true() { + preset_initial(|| { + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + pool_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + DEX_B_ID, + pool_asset, + reward_asset, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( pool_asset, reward_asset, - reward_asset, - is_farm, - pool_tokens - balance!(1) - )); - - assert_eq!( - demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( - &ALICE, - pool_asset, - reward_asset, - pool_tokens, - ), - false - ); - }); - } - - #[test] - fn check_if_user_lp_is_changed_after_locking() { - preset_initial(|| { - let dex_id = DEX_A_ID; - let pool_asset = XOR; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + ) + .expect("Pool does not exist") + .0; - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + // Calculate number of pool tokens of user's account + let pool_tokens: Balance = + ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); + + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + is_farm, + pool_tokens + )); + + let pooled_tokens = (FixedWrapper::from(pool_tokens) * FixedWrapper::from(balance!(0.96))) + .try_into_balance() + .unwrap_or(0); + + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + pool_asset, + is_farm, + pooled_tokens + )); + + let mut pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) + / FixedWrapper::from(balance!(2))) + .try_into_balance() + .unwrap_or(0); + + assert_ok!(demeter_farming_platform::Pallet::::withdraw( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + pooled_tokens_to_withdraw, + is_farm + )); + + assert_ok!(demeter_farming_platform::Pallet::::withdraw( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + pool_asset, + pooled_tokens_to_withdraw, + is_farm + )); + + pooled_tokens_to_withdraw = (FixedWrapper::from(pooled_tokens) + / FixedWrapper::from(balance!(3))) + .try_into_balance() + .unwrap_or(0); + + assert!( + demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( + &ALICE, pool_asset, reward_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core - )); + pooled_tokens_to_withdraw, + ) + ); + }); +} - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, +#[test] +fn check_if_has_enough_liquidity_out_of_farming_xstusd_false() { + preset_initial(|| { + let dex_id = DEX_B_ID; + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + pool_asset, + reward_asset, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( pool_asset, reward_asset, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - pool_asset, - reward_asset, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let mut pool_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), + ) + .expect("Pool does not exist") + .0; + + // Calculate number of pool tokens of user's account + let pool_tokens: Balance = + ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); + + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + is_farm, + pool_tokens - balance!(1) + )); + + assert!( + !demeter_farming_platform::Pallet::::check_if_has_enough_liquidity_out_of_farming( + &ALICE, pool_asset, reward_asset, - reward_asset, - is_farm, - pool_tokens - )); + pool_tokens, + ) + ); + }); +} - assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( - RuntimeOrigin::signed(ALICE), +#[test] +fn check_if_user_lp_is_changed_after_locking() { + preset_initial(|| { + let dex_id = DEX_A_ID; + let pool_asset = XOR; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + pool_asset, + reward_asset, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( pool_asset, reward_asset, - 10u32.into(), - balance!(1), - true - )); + ) + .expect("Pool does not exist") + .0; - pool_tokens = ::XYKPool::pool_providers( + // Calculate number of pool tokens of user's account + let mut pool_tokens: Balance = + ::XYKPool::pool_providers( pool_account.clone(), ALICE, ) .expect("User is not pool provider"); - let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - for u_info in &user_infos { - if u_info.is_farm == is_farm && u_info.base_asset == pool_asset { - assert_eq!(u_info.pooled_tokens, pool_tokens); - } - } - let pool_infos = - demeter_farming_platform::Pools::::get(&reward_asset, &reward_asset); - for p_info in &pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == pool_asset - { - assert_eq!(p_info.total_tokens_in_pool, pool_tokens); - } - } + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + is_farm, + pool_tokens + )); - assert_err!( - demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - reward_asset, - is_farm, - balance!(14000) - ), - demeter_farming_platform::Error::::InsufficientLPTokens - ); - }); - } - - #[test] - fn check_if_user_lp_is_changed_after_locking_xstusd() { - preset_initial(|| { - let dex_id = DEX_B_ID; - let pool_asset = XSTUSD; - let reward_asset = CERES_ASSET_ID; - let is_farm = true; - let multiplier = 1; - let deposit_fee = balance!(0.04); - let is_core = true; - let token_per_block = balance!(1); - let farms_allocation = balance!(0.6); - let staking_allocation = balance!(0.2); - let team_allocation = balance!(0.2); - - assert_ok!(demeter_farming_platform::Pallet::::register_token( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - reward_asset, - token_per_block, - farms_allocation, - staking_allocation, - team_allocation, - BOB - )); + assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + 10u32.into(), + balance!(1), + true + )); - assert_ok!(demeter_farming_platform::Pallet::::add_pool( - RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), - pool_asset, - reward_asset, - reward_asset, - is_farm, - multiplier, - deposit_fee, - is_core - )); + pool_tokens = ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); - assert_ok!(pool_xyk::Pallet::::deposit_liquidity( - RuntimeOrigin::signed(ALICE), - dex_id, - pool_asset, - reward_asset, - balance!(500), - balance!(700), - balance!(500), - balance!(700), - )); - - // Get pool account - let pool_account: AccountId = - ::XYKPool::properties( - pool_asset, - reward_asset, - ) - .expect("Pool does not exist") - .0; - - // Calculate number of pool tokens of user's account - let mut pool_tokens: Balance = - ::XYKPool::pool_providers( - pool_account.clone(), - ALICE, - ) - .expect("User is not pool provider"); - - assert_ok!(demeter_farming_platform::Pallet::::deposit( + let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + for u_info in &user_infos { + if u_info.is_farm == is_farm && u_info.base_asset == pool_asset { + assert_eq!(u_info.pooled_tokens, pool_tokens); + } + } + let pool_infos = + demeter_farming_platform::Pools::::get(reward_asset, reward_asset); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == pool_asset { + assert_eq!(p_info.total_tokens_in_pool, pool_tokens); + } + } + + assert_err!( + demeter_farming_platform::Pallet::::deposit( RuntimeOrigin::signed(ALICE), pool_asset, reward_asset, reward_asset, is_farm, - pool_tokens - )); + balance!(14000) + ), + demeter_farming_platform::Error::::InsufficientLPTokens + ); + }); +} - assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( - RuntimeOrigin::signed(ALICE), +#[test] +fn check_if_user_lp_is_changed_after_locking_xstusd() { + preset_initial(|| { + let dex_id = DEX_B_ID; + let pool_asset = XSTUSD; + let reward_asset = CERES_ASSET_ID; + let is_farm = true; + let multiplier = 1; + let deposit_fee = balance!(0.04); + let is_core = true; + let token_per_block = balance!(1); + let farms_allocation = balance!(0.6); + let staking_allocation = balance!(0.2); + let team_allocation = balance!(0.2); + + assert_ok!(demeter_farming_platform::Pallet::::register_token( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + reward_asset, + token_per_block, + farms_allocation, + staking_allocation, + team_allocation, + BOB + )); + + assert_ok!(demeter_farming_platform::Pallet::::add_pool( + RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::::get()), + pool_asset, + reward_asset, + reward_asset, + is_farm, + multiplier, + deposit_fee, + is_core + )); + + assert_ok!(pool_xyk::Pallet::::deposit_liquidity( + RuntimeOrigin::signed(ALICE), + dex_id, + pool_asset, + reward_asset, + balance!(500), + balance!(700), + balance!(500), + balance!(700), + )); + + // Get pool account + let pool_account: AccountId = + ::XYKPool::properties( pool_asset, reward_asset, - 10u32.into(), - balance!(1), - true - )); + ) + .expect("Pool does not exist") + .0; - pool_tokens = ::XYKPool::pool_providers( + // Calculate number of pool tokens of user's account + let mut pool_tokens: Balance = + ::XYKPool::pool_providers( pool_account.clone(), ALICE, ) .expect("User is not pool provider"); - let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); - for u_info in &user_infos { - if u_info.is_farm == is_farm && u_info.base_asset == pool_asset { - assert_eq!(u_info.pooled_tokens, pool_tokens); - } + assert_ok!(demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + is_farm, + pool_tokens + )); + + assert_ok!(ceres_liquidity_locker::Pallet::::lock_liquidity( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + 10u32.into(), + balance!(1), + true + )); + + pool_tokens = ::XYKPool::pool_providers( + pool_account, + ALICE, + ) + .expect("User is not pool provider"); + + let user_infos = demeter_farming_platform::UserInfos::::get(&ALICE); + for u_info in &user_infos { + if u_info.is_farm == is_farm && u_info.base_asset == pool_asset { + assert_eq!(u_info.pooled_tokens, pool_tokens); } - let pool_infos = - demeter_farming_platform::Pools::::get(&reward_asset, &reward_asset); - for p_info in &pool_infos { - if !p_info.is_removed - && p_info.is_farm == is_farm - && p_info.base_asset == pool_asset - { - assert_eq!(p_info.total_tokens_in_pool, pool_tokens); - } + } + let pool_infos = + demeter_farming_platform::Pools::::get(reward_asset, reward_asset); + for p_info in &pool_infos { + if !p_info.is_removed && p_info.is_farm == is_farm && p_info.base_asset == pool_asset { + assert_eq!(p_info.total_tokens_in_pool, pool_tokens); } + } - assert_err!( - demeter_farming_platform::Pallet::::deposit( - RuntimeOrigin::signed(ALICE), - pool_asset, - reward_asset, - reward_asset, - is_farm, - balance!(14000) - ), - demeter_farming_platform::Error::::InsufficientLPTokens - ); - }); - } - - #[test] - fn demeter_farming_platform_storage_migration_works() { - preset_initial(|| { - generate_storage_instance!(DemeterFarmingPlatform, Pools); - type OldPools = StorageDoubleMap< - PoolsOldInstance, - Identity, - AssetIdOf, - Identity, - AssetIdOf, - Vec<(u32, Balance, bool, bool, Balance, Balance, Balance, bool)>, - ValueQuery, - >; - - generate_storage_instance!(DemeterFarmingPlatform, UserInfos); - type OldUserInfos = StorageMap< - UserInfosOldInstance, - Identity, - AccountIdOf, - Vec<( - AssetIdOf, - AssetIdOf, - bool, - Balance, - Balance, - )>, - ValueQuery, - >; - - let asset_xor: AssetId = XOR.into(); - let asset_ceres: AssetId = CERES_ASSET_ID.into(); - let asset_xstusd: AssetId = XSTUSD.into(); - - let mut vec_a: Vec<(u32, Balance, bool, bool, Balance, Balance, Balance, bool)> = - Vec::new(); - vec_a.push(( - 2u32, - balance!(0.02), - false, - true, - balance!(100), - balance!(20), - balance!(12), - false, - )); - vec_a.push(( - 3u32, - balance!(0.01), - true, - false, - balance!(120), - balance!(10), - balance!(2), - false, - )); - OldPools::insert(asset_ceres, asset_xstusd, vec_a); - - let mut vec_b: Vec<(u32, Balance, bool, bool, Balance, Balance, Balance, bool)> = - Vec::new(); - vec_b.push(( - 4u32, - balance!(0.03), - false, - true, - balance!(130), - balance!(25), - balance!(8), - false, - )); - OldPools::insert(asset_ceres, asset_ceres, vec_b); - - let mut user_a: Vec<( - AssetIdOf, - AssetIdOf, - bool, - Balance, - Balance, - )> = Vec::new(); - user_a.push((asset_ceres, asset_xstusd, true, balance!(5), balance!(10))); - user_a.push((asset_ceres, asset_xstusd, false, balance!(3), balance!(9))); - OldUserInfos::insert(ALICE, user_a); + assert_err!( + demeter_farming_platform::Pallet::::deposit( + RuntimeOrigin::signed(ALICE), + pool_asset, + reward_asset, + reward_asset, + is_farm, + balance!(14000) + ), + demeter_farming_platform::Error::::InsufficientLPTokens + ); + }); +} - let mut user_b: Vec<( +#[test] +fn demeter_farming_platform_storage_migration_works() { + preset_initial(|| { + generate_storage_instance!(DemeterFarmingPlatform, Pools); + type OldPools = StorageDoubleMap< + PoolsOldInstance, + Identity, + AssetIdOf, + Identity, + AssetIdOf, + Vec<(u32, Balance, bool, bool, Balance, Balance, Balance, bool)>, + ValueQuery, + >; + + generate_storage_instance!(DemeterFarmingPlatform, UserInfos); + type OldUserInfos = StorageMap< + UserInfosOldInstance, + Identity, + AccountIdOf, + Vec<( AssetIdOf, AssetIdOf, bool, Balance, Balance, - )> = Vec::new(); - user_b.push((asset_ceres, asset_ceres, false, balance!(2), balance!(4))); - OldUserInfos::insert(BOB, user_b); - - // Storage migration - demeter_farming_platform::Pallet::::on_runtime_upgrade(); - - let pools_a = - demeter_farming_platform::Pools::::get(asset_ceres, asset_xstusd); - for p_info in pools_a.iter() { - if p_info.is_farm { - assert_eq!(p_info.multiplier, 2u32); - assert_eq!(p_info.deposit_fee, balance!(0.02)); - assert_eq!(p_info.is_core, false); - assert_eq!(p_info.total_tokens_in_pool, balance!(100)); - assert_eq!(p_info.rewards, balance!(20)); - assert_eq!(p_info.rewards_to_be_distributed, balance!(12)); - assert_eq!(p_info.is_removed, false); - assert_eq!(p_info.base_asset, asset_xor); - } else { - assert_eq!(p_info.multiplier, 3u32); - assert_eq!(p_info.deposit_fee, balance!(0.01)); - assert_eq!(p_info.is_core, true); - assert_eq!(p_info.total_tokens_in_pool, balance!(120)); - assert_eq!(p_info.rewards, balance!(10)); - assert_eq!(p_info.rewards_to_be_distributed, balance!(2)); - assert_eq!(p_info.is_removed, false); - assert_eq!(p_info.base_asset, asset_ceres); - } - } - - let pools_b = demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); - for p_info in pools_b.iter() { - assert_eq!(p_info.multiplier, 4u32); - assert_eq!(p_info.deposit_fee, balance!(0.03)); - assert_eq!(p_info.is_core, false); - assert_eq!(p_info.total_tokens_in_pool, balance!(130)); - assert_eq!(p_info.rewards, balance!(25)); - assert_eq!(p_info.rewards_to_be_distributed, balance!(8)); - assert_eq!(p_info.is_removed, false); + )>, + ValueQuery, + >; + + let asset_xor: AssetId = XOR; + let asset_ceres: AssetId = CERES_ASSET_ID; + let asset_xstusd: AssetId = XSTUSD; + + #[allow(clippy::type_complexity)] + let mut vec_a: Vec<(u32, Balance, bool, bool, Balance, Balance, Balance, bool)> = + Vec::new(); + vec_a.push(( + 2u32, + balance!(0.02), + false, + true, + balance!(100), + balance!(20), + balance!(12), + false, + )); + vec_a.push(( + 3u32, + balance!(0.01), + true, + false, + balance!(120), + balance!(10), + balance!(2), + false, + )); + OldPools::insert(asset_ceres, asset_xstusd, vec_a); + + #[allow(clippy::type_complexity)] + let mut vec_b: Vec<(u32, Balance, bool, bool, Balance, Balance, Balance, bool)> = + Vec::new(); + vec_b.push(( + 4u32, + balance!(0.03), + false, + true, + balance!(130), + balance!(25), + balance!(8), + false, + )); + OldPools::insert(asset_ceres, asset_ceres, vec_b); + + #[allow(clippy::type_complexity)] + let mut user_a: Vec<( + AssetIdOf, + AssetIdOf, + bool, + Balance, + Balance, + )> = Vec::new(); + user_a.push((asset_ceres, asset_xstusd, true, balance!(5), balance!(10))); + user_a.push((asset_ceres, asset_xstusd, false, balance!(3), balance!(9))); + OldUserInfos::insert(ALICE, user_a); + + #[allow(clippy::type_complexity)] + let mut user_b: Vec<( + AssetIdOf, + AssetIdOf, + bool, + Balance, + Balance, + )> = Vec::new(); + user_b.push((asset_ceres, asset_ceres, false, balance!(2), balance!(4))); + OldUserInfos::insert(BOB, user_b); + + // Storage migration + demeter_farming_platform::Pallet::::on_runtime_upgrade(); + + let pools_a = demeter_farming_platform::Pools::::get(asset_ceres, asset_xstusd); + for p_info in pools_a.iter() { + if p_info.is_farm { + assert_eq!(p_info.multiplier, 2u32); + assert_eq!(p_info.deposit_fee, balance!(0.02)); + assert!(!p_info.is_core); + assert_eq!(p_info.total_tokens_in_pool, balance!(100)); + assert_eq!(p_info.rewards, balance!(20)); + assert_eq!(p_info.rewards_to_be_distributed, balance!(12)); + assert!(!p_info.is_removed); assert_eq!(p_info.base_asset, asset_xor); + } else { + assert_eq!(p_info.multiplier, 3u32); + assert_eq!(p_info.deposit_fee, balance!(0.01)); + assert!(p_info.is_core); + assert_eq!(p_info.total_tokens_in_pool, balance!(120)); + assert_eq!(p_info.rewards, balance!(10)); + assert_eq!(p_info.rewards_to_be_distributed, balance!(2)); + assert!(!p_info.is_removed); + assert_eq!(p_info.base_asset, asset_ceres); } - - let users_a = demeter_farming_platform::UserInfos::::get(ALICE); - for u_info in users_a.iter() { - if u_info.is_farm { - assert_eq!(u_info.base_asset, asset_xor); - assert_eq!(u_info.pool_asset, asset_ceres); - assert_eq!(u_info.reward_asset, asset_xstusd); - assert_eq!(u_info.pooled_tokens, balance!(5)); - assert_eq!(u_info.rewards, balance!(10)); - } else { - assert_eq!(u_info.base_asset, asset_ceres); - assert_eq!(u_info.pool_asset, asset_ceres); - assert_eq!(u_info.reward_asset, asset_xstusd); - assert_eq!(u_info.pooled_tokens, balance!(3)); - assert_eq!(u_info.rewards, balance!(9)); - } - } - - let users_b = demeter_farming_platform::UserInfos::::get(BOB); - for u_info in users_b.iter() { + } + + let pools_b = demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); + for p_info in pools_b.iter() { + assert_eq!(p_info.multiplier, 4u32); + assert_eq!(p_info.deposit_fee, balance!(0.03)); + assert!(!p_info.is_core); + assert_eq!(p_info.total_tokens_in_pool, balance!(130)); + assert_eq!(p_info.rewards, balance!(25)); + assert_eq!(p_info.rewards_to_be_distributed, balance!(8)); + assert!(!p_info.is_removed); + assert_eq!(p_info.base_asset, asset_xor); + } + + let users_a = demeter_farming_platform::UserInfos::::get(ALICE); + for u_info in users_a.iter() { + if u_info.is_farm { + assert_eq!(u_info.base_asset, asset_xor); + assert_eq!(u_info.pool_asset, asset_ceres); + assert_eq!(u_info.reward_asset, asset_xstusd); + assert_eq!(u_info.pooled_tokens, balance!(5)); + assert_eq!(u_info.rewards, balance!(10)); + } else { assert_eq!(u_info.base_asset, asset_ceres); assert_eq!(u_info.pool_asset, asset_ceres); - assert_eq!(u_info.reward_asset, asset_ceres); - assert_eq!(u_info.pooled_tokens, balance!(2)); - assert_eq!(u_info.rewards, balance!(4)); + assert_eq!(u_info.reward_asset, asset_xstusd); + assert_eq!(u_info.pooled_tokens, balance!(3)); + assert_eq!(u_info.rewards, balance!(9)); } - - // Storage migration (no change) - demeter_farming_platform::Pallet::::on_runtime_upgrade(); - - let pools_a = - demeter_farming_platform::Pools::::get(asset_ceres, asset_xstusd); - for p_info in pools_a.iter() { - if p_info.is_farm { - assert_eq!(p_info.multiplier, 2u32); - assert_eq!(p_info.deposit_fee, balance!(0.02)); - assert_eq!(p_info.is_core, false); - assert_eq!(p_info.total_tokens_in_pool, balance!(100)); - assert_eq!(p_info.rewards, balance!(20)); - assert_eq!(p_info.rewards_to_be_distributed, balance!(12)); - assert_eq!(p_info.is_removed, false); - assert_eq!(p_info.base_asset, asset_xor); - } else { - assert_eq!(p_info.multiplier, 3u32); - assert_eq!(p_info.deposit_fee, balance!(0.01)); - assert_eq!(p_info.is_core, true); - assert_eq!(p_info.total_tokens_in_pool, balance!(120)); - assert_eq!(p_info.rewards, balance!(10)); - assert_eq!(p_info.rewards_to_be_distributed, balance!(2)); - assert_eq!(p_info.is_removed, false); - assert_eq!(p_info.base_asset, asset_ceres); - } - } - - let pools_b = demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); - for p_info in pools_b.iter() { - assert_eq!(p_info.multiplier, 4u32); - assert_eq!(p_info.deposit_fee, balance!(0.03)); - assert_eq!(p_info.is_core, false); - assert_eq!(p_info.total_tokens_in_pool, balance!(130)); - assert_eq!(p_info.rewards, balance!(25)); - assert_eq!(p_info.rewards_to_be_distributed, balance!(8)); - assert_eq!(p_info.is_removed, false); + } + + let users_b = demeter_farming_platform::UserInfos::::get(BOB); + for u_info in users_b.iter() { + assert_eq!(u_info.base_asset, asset_ceres); + assert_eq!(u_info.pool_asset, asset_ceres); + assert_eq!(u_info.reward_asset, asset_ceres); + assert_eq!(u_info.pooled_tokens, balance!(2)); + assert_eq!(u_info.rewards, balance!(4)); + } + + // Storage migration (no change) + demeter_farming_platform::Pallet::::on_runtime_upgrade(); + + let pools_a = demeter_farming_platform::Pools::::get(asset_ceres, asset_xstusd); + for p_info in pools_a.iter() { + if p_info.is_farm { + assert_eq!(p_info.multiplier, 2u32); + assert_eq!(p_info.deposit_fee, balance!(0.02)); + assert!(!p_info.is_core); + assert_eq!(p_info.total_tokens_in_pool, balance!(100)); + assert_eq!(p_info.rewards, balance!(20)); + assert_eq!(p_info.rewards_to_be_distributed, balance!(12)); + assert!(!p_info.is_removed); assert_eq!(p_info.base_asset, asset_xor); + } else { + assert_eq!(p_info.multiplier, 3u32); + assert_eq!(p_info.deposit_fee, balance!(0.01)); + assert!(p_info.is_core); + assert_eq!(p_info.total_tokens_in_pool, balance!(120)); + assert_eq!(p_info.rewards, balance!(10)); + assert_eq!(p_info.rewards_to_be_distributed, balance!(2)); + assert!(!p_info.is_removed); + assert_eq!(p_info.base_asset, asset_ceres); } - - let users_a = demeter_farming_platform::UserInfos::::get(ALICE); - for u_info in users_a.iter() { - if u_info.is_farm { - assert_eq!(u_info.base_asset, asset_xor); - assert_eq!(u_info.pool_asset, asset_ceres); - assert_eq!(u_info.reward_asset, asset_xstusd); - assert_eq!(u_info.pooled_tokens, balance!(5)); - assert_eq!(u_info.rewards, balance!(10)); - } else { - assert_eq!(u_info.base_asset, asset_ceres); - assert_eq!(u_info.pool_asset, asset_ceres); - assert_eq!(u_info.reward_asset, asset_xstusd); - assert_eq!(u_info.pooled_tokens, balance!(3)); - assert_eq!(u_info.rewards, balance!(9)); - } - } - - let users_b = demeter_farming_platform::UserInfos::::get(BOB); - for u_info in users_b.iter() { + } + + let pools_b = demeter_farming_platform::Pools::::get(asset_ceres, asset_ceres); + for p_info in pools_b.iter() { + assert_eq!(p_info.multiplier, 4u32); + assert_eq!(p_info.deposit_fee, balance!(0.03)); + assert!(!p_info.is_core); + assert_eq!(p_info.total_tokens_in_pool, balance!(130)); + assert_eq!(p_info.rewards, balance!(25)); + assert_eq!(p_info.rewards_to_be_distributed, balance!(8)); + assert!(!p_info.is_removed); + assert_eq!(p_info.base_asset, asset_xor); + } + + let users_a = demeter_farming_platform::UserInfos::::get(ALICE); + for u_info in users_a.iter() { + if u_info.is_farm { + assert_eq!(u_info.base_asset, asset_xor); + assert_eq!(u_info.pool_asset, asset_ceres); + assert_eq!(u_info.reward_asset, asset_xstusd); + assert_eq!(u_info.pooled_tokens, balance!(5)); + assert_eq!(u_info.rewards, balance!(10)); + } else { assert_eq!(u_info.base_asset, asset_ceres); assert_eq!(u_info.pool_asset, asset_ceres); - assert_eq!(u_info.reward_asset, asset_ceres); - assert_eq!(u_info.pooled_tokens, balance!(2)); - assert_eq!(u_info.rewards, balance!(4)); + assert_eq!(u_info.reward_asset, asset_xstusd); + assert_eq!(u_info.pooled_tokens, balance!(3)); + assert_eq!(u_info.rewards, balance!(9)); } - }); - } + } + + let users_b = demeter_farming_platform::UserInfos::::get(BOB); + for u_info in users_b.iter() { + assert_eq!(u_info.base_asset, asset_ceres); + assert_eq!(u_info.pool_asset, asset_ceres); + assert_eq!(u_info.reward_asset, asset_ceres); + assert_eq!(u_info.pooled_tokens, balance!(2)); + assert_eq!(u_info.rewards, balance!(4)); + } + }); } diff --git a/pallets/dex-api/src/lib.rs b/pallets/dex-api/src/lib.rs index b00c7b769c..474aa1bb54 100644 --- a/pallets/dex-api/src/lib.rs +++ b/pallets/dex-api/src/lib.rs @@ -29,8 +29,6 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #![cfg_attr(not(feature = "std"), no_std)] -// TODO #167: fix clippy warnings -#![allow(clippy::all)] use common::prelude::{Balance, QuoteAmount, SwapAmount, SwapOutcome}; use common::{ @@ -315,10 +313,7 @@ impl output_asset_id, ) { - Some(LiquiditySourceId::new( - filter.dex_id.clone(), - source_type.clone(), - )) + Some(LiquiditySourceId::new(filter.dex_id, *source_type)) } else { None } @@ -418,19 +413,11 @@ pub mod pallet { pub type EnabledSourceTypes = StorageValue<_, Vec, ValueQuery>; #[pallet::genesis_config] + #[cfg_attr(feature = "std", derive(Default))] pub struct GenesisConfig { pub source_types: Vec, } - #[cfg(feature = "std")] - impl Default for GenesisConfig { - fn default() -> Self { - Self { - source_types: Default::default(), - } - } - } - #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { diff --git a/pallets/dex-api/src/mock.rs b/pallets/dex-api/src/mock.rs index 66655c60ca..04d11be424 100644 --- a/pallets/dex-api/src/mock.rs +++ b/pallets/dex-api/src/mock.rs @@ -86,7 +86,7 @@ parameter_types! { pub GetPswapDistributionAccountId: AccountId = AccountId32::from([3; 32]); pub const GetDefaultSubscriptionFrequency: BlockNumber = 10; pub const GetBurnUpdateFrequency: BlockNumber = 14400; - pub GetIncentiveAssetId: AssetId = common::PSWAP.into(); + pub GetIncentiveAssetId: AssetId = common::PSWAP; pub GetParliamentAccountId: AccountId = AccountId32::from([8; 32]); pub GetXykFee: Fixed = fixed!(0.003); pub const MinimumPeriod: u64 = 5;