Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feature/update conditions for pswap lp strategic bonus rewards #961

Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a0773ad
Merge pull request #953 from sora-xor/merge-develop-to-master
vovac12 Mar 27, 2024
2f153ed
Update build.sh
f33r0 Mar 28, 2024
437deb4
Update build.sh
f33r0 Mar 28, 2024
18acce7
Update build.sh
f33r0 Mar 28, 2024
f354a9e
Merge pull request #958 from sora-xor/f33r0-patch-1
f33r0 Mar 28, 2024
a412190
Update build.sh
f33r0 Mar 29, 2024
8bcbd08
Update build.sh
f33r0 Mar 29, 2024
79b4bdc
set 0 as int
Mar 29, 2024
97fdd71
set threads
Mar 29, 2024
df283dd
Update build.sh
f33r0 Mar 29, 2024
ced7e9c
update sudo status check (new subwasm)
Mar 29, 2024
1fd2c2c
Merge pull request #960 from sora-xor/duty/fix_sudo_check_status
f33r0 Mar 30, 2024
045c5bd
feat(farming): add storage value to constrain lp bonus reward
HaidarJbeily7 Apr 1, 2024
7839e18
feat(farming): support new functionality with tests
HaidarJbeily7 Apr 1, 2024
7ec4cf0
Merge branch 'develop' into feature/update-conditions-for-pswap-lp-st…
HaidarJbeily7 Apr 1, 2024
277b882
revert build.sh from merge
HaidarJbeily7 Apr 2, 2024
99ce09c
revert build.sh from merge
HaidarJbeily7 Apr 2, 2024
e580718
feat(farming): handle error and add default value
HaidarJbeily7 Apr 2, 2024
e57d4b6
feat(farming): change default value and rollback added check
HaidarJbeily7 Apr 2, 2024
6c5f65a
feat(farming): update weights aligned with benchmarking
HaidarJbeily7 Apr 2, 2024
0575d1d
feat(farming): add `Call` to mock
HaidarJbeily7 Apr 2, 2024
d98e332
feat(farming): unwrap in tests
HaidarJbeily7 Apr 2, 2024
0d1ac66
feat(farming): add asset_has_event on test
HaidarJbeily7 Apr 3, 2024
3144198
Merge branch 'develop' into feature/update-conditions-for-pswap-lp-st…
Alexey-N-Chernyshov Apr 4, 2024
f1dbc47
Merge branch 'develop' into feature/update-conditions-for-pswap-lp-st…
Alexey-N-Chernyshov Apr 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion pallets/farming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl<T: Config> Pallet<T> {
.try_into_balance()
.unwrap_or(0);

if base_asset_amt < balance!(1) {
if base_asset_amt < Self::lp_min_xor_for_bonus_reward() {
wer1st marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

Expand Down Expand Up @@ -316,6 +316,7 @@ pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_support::traits::schedule::Anon;
use frame_support::traits::StorageVersion;
use frame_system::{ensure_root, pallet_prelude::*};
use sp_runtime::traits::Zero;
use sp_runtime::AccountId32;

Expand All @@ -331,6 +332,7 @@ pub mod pallet {
+ pool_xyk::Config
+ vested_rewards::Config
{
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
const PSWAP_PER_DAY: Balance;
const REFRESH_FREQUENCY: BlockNumberFor<Self>;
const VESTING_COEFF: u32;
Expand Down Expand Up @@ -377,6 +379,23 @@ pub mod pallet {
pub enum Error<T> {
/// Increment account reference error.
IncRefError,
/// Invalid New xor Min amount for Liquidity Provider Bonus Reward.
InvalidNewLpMinXorForBonusRewardError,
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// When Minimum XOR amount for Liquidity Provider Bonus Reward is updated
LpMinXorForBonusRewardUpdated {
wer1st marked this conversation as resolved.
Show resolved Hide resolved
new_lp_min_xor_for_bonus_reward: Balance,
old_lp_min_xor_for_bonus_reward: Balance,
},
}

#[pallet::type_value]
pub fn DefaultLpMinXorForBonusReward<T: Config>() -> Balance {
balance!(1)
wer1st marked this conversation as resolved.
Show resolved Hide resolved
}

/// Pools whose farmers are refreshed at the specific block. Block => Pools
Expand All @@ -388,6 +407,34 @@ pub mod pallet {
#[pallet::storage]
pub type PoolFarmers<T: Config> =
StorageMap<_, Identity, T::AccountId, Vec<PoolFarmer<T>>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn lp_min_xor_for_bonus_reward)]
pub type LpMinXorForBonusReward<T: Config> =
StorageValue<_, Balance, ValueQuery, DefaultLpMinXorForBonusReward<T>>;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::set_lp_min_xor_for_bonus_reward())]
pub fn set_lp_min_xor_for_bonus_reward(
origin: OriginFor<T>,
new_lp_min_xor_for_bonus_reward: Balance,
wer1st marked this conversation as resolved.
Show resolved Hide resolved
) -> DispatchResult {
ensure_root(origin)?;
ensure!(
new_lp_min_xor_for_bonus_reward >= balance!(1),
wer1st marked this conversation as resolved.
Show resolved Hide resolved
<Error<T>>::InvalidNewLpMinXorForBonusRewardError
);
let old_lp_min_xor_for_bonus_reward = <LpMinXorForBonusReward<T>>::get();
<LpMinXorForBonusReward<T>>::put(new_lp_min_xor_for_bonus_reward);
Self::deposit_event(Event::LpMinXorForBonusRewardUpdated {
new_lp_min_xor_for_bonus_reward,
old_lp_min_xor_for_bonus_reward,
});
Ok(())
}
}
}

pub mod rpc {
Expand Down
3 changes: 2 additions & 1 deletion pallets/farming/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ construct_runtime! {
MBCPool: multicollateral_bonding_curve_pool::{Pallet, Call, Storage, Event<T>},
VestedRewards: vested_rewards::{Pallet, Storage, Event<T>},
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>},
Farming: farming::{Pallet, Storage},
Farming: farming::{Pallet, Storage, Event<T>},
CeresLiquidityLocker: ceres_liquidity_locker::{Pallet, Call, Storage, Event<T>},
DemeterFarmingPlatform: demeter_farming_platform::{Pallet, Call, Storage, Event<T>},
}
Expand Down Expand Up @@ -398,6 +398,7 @@ impl Config for Runtime {
type RewardDoublingAssets = RewardDoublingAssets;
type TradingPairSourceManager = trading_pair::Pallet<Runtime>;
type WeightInfo = ();
type RuntimeEvent = RuntimeEvent;
}

pub struct ExtBuilder {
Expand Down
36 changes: 34 additions & 2 deletions pallets/farming/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use frame_support::assert_ok;
use frame_support::{assert_noop, assert_ok};

use common::{balance, RewardReason, DOT, PSWAP, VAL, XOR, XSTUSD};
use frame_support::log::debug;
use frame_system::RawOrigin;
use pool_xyk::Properties;
use sp_runtime::traits::BadOrigin;
use vested_rewards::Rewards;

use crate::mock::{
self, run_to_block, AssetId, DEXId, ExtBuilder, Runtime, RuntimeOrigin, ALICE, BOB, CHARLIE,
DAVE, DEX_A_ID, DEX_B_ID, EVE, REFRESH_FREQUENCY, VESTING_FREQUENCY,
};
use crate::{PoolFarmer, PoolFarmers};
use crate::{Pallet, PoolFarmer, PoolFarmers};

fn init_pool(dex_id: DEXId, base_asset: AssetId, other_asset: AssetId) {
assert_ok!(trading_pair::Pallet::<Runtime>::register(
Expand All @@ -63,6 +65,10 @@ fn init_pool(dex_id: DEXId, base_asset: AssetId, other_asset: AssetId) {
fn test() {
let dex_id = DEX_A_ID;
ExtBuilder::default().build().execute_with(|| {
assert_ok!(Pallet::<Runtime>::set_lp_min_xor_for_bonus_reward(
RawOrigin::Root.into(),
balance!(1)
));
init_pool(DEX_A_ID, XOR, DOT);
init_pool(DEX_A_ID, XOR, PSWAP);
init_pool(DEX_A_ID, XOR, XSTUSD);
Expand Down Expand Up @@ -406,3 +412,29 @@ fn test() {
assert_eq!(eve_reward, balance!(375576.575413164636883116));
});
}

#[test]
fn set_lp_min_xor_for_bonus_reward_should_forbid_for_non_root_call() {
ExtBuilder::default().build().execute_with(|| {
assert_noop!(
Pallet::<Runtime>::set_lp_min_xor_for_bonus_reward(
RuntimeOrigin::signed(ALICE()),
balance!(100000)
),
BadOrigin
);
assert!(Pallet::<Runtime>::lp_min_xor_for_bonus_reward() == balance!(0));
});
}

#[test]
fn set_lp_min_xor_for_bonus_reward_should_work() {
ExtBuilder::default().build().execute_with(|| {
let modified_min_xor = balance!(3 * (10_i32.pow(6)));
assert_ok!(Pallet::<Runtime>::set_lp_min_xor_for_bonus_reward(
RawOrigin::Root.into(),
modified_min_xor
));
assert!(Pallet::<Runtime>::lp_min_xor_for_bonus_reward() == modified_min_xor);
Alexey-N-Chernyshov marked this conversation as resolved.
Show resolved Hide resolved
});
}
11 changes: 11 additions & 0 deletions pallets/farming/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub trait WeightInfo {
fn refresh_pool(a: u32, ) -> Weight;
fn prepare_accounts_for_vesting(a: u32, b: u32, ) -> Weight;
fn vest_account_rewards(a: u32, ) -> Weight;
fn set_lp_min_xor_for_bonus_reward() -> Weight;
wer1st marked this conversation as resolved.
Show resolved Hide resolved
}

/// Weights for farming using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -132,6 +133,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(a.into())))
.saturating_add(Weight::from_parts(0, 5468).saturating_mul(a.into()))
}

fn set_lp_min_xor_for_bonus_reward() -> Weight{
Weight::from_parts(5_967_353, 631)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -200,4 +206,9 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(a.into())))
.saturating_add(Weight::from_parts(0, 5468).saturating_mul(a.into()))
}

fn set_lp_min_xor_for_bonus_reward() -> Weight{
Weight::from_parts(5_967_353, 631)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
3 changes: 2 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,7 @@ impl farming::Config for Runtime {
type RewardDoublingAssets = FarmingRewardDoublingAssets;
type TradingPairSourceManager = trading_pair::Pallet<Runtime>;
type WeightInfo = ();
type RuntimeEvent = RuntimeEvent;
}

impl pswap_distribution::Config for Runtime {
Expand Down Expand Up @@ -2410,7 +2411,7 @@ construct_runtime! {
ElectionsPhragmen: pallet_elections_phragmen::{Pallet, Call, Storage, Event<T>, Config<T>} = 39,
VestedRewards: vested_rewards::{Pallet, Call, Storage, Event<T>} = 40,
Identity: pallet_identity::{Pallet, Call, Storage, Event<T>} = 41,
Farming: farming::{Pallet, Storage} = 42,
Farming: farming::{Pallet, Storage, Event<T>} = 42,
wer1st marked this conversation as resolved.
Show resolved Hide resolved
XSTPool: xst::{Pallet, Call, Storage, Config<T>, Event<T>} = 43,
PriceTools: price_tools::{Pallet, Storage, Event<T>} = 44,
CeresStaking: ceres_staking::{Pallet, Call, Storage, Event<T>} = 45,
Expand Down
Loading