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 all 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
9 changes: 9 additions & 0 deletions pallets/farming/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ benchmarks! {
}: {
Pallet::<T>::vest_account_rewards(accounts);
}

set_lp_min_xor_for_bonus_reward{
let root = RawOrigin::Root;
let balance = balance!(10000000);
}: {
Pallet::<T>::set_lp_min_xor_for_bonus_reward(root.into(), balance).unwrap();
}

}

#[cfg(test)]
Expand All @@ -176,6 +184,7 @@ mod tests {
assert_ok!(Pallet::<Runtime>::test_benchmark_refresh_pool());
assert_ok!(Pallet::<Runtime>::test_benchmark_prepare_accounts_for_vesting());
assert_ok!(Pallet::<Runtime>::test_benchmark_vest_account_rewards());
assert_ok!(Pallet::<Runtime>::test_benchmark_set_lp_min_xor_for_bonus_reward());
});
}
}
45 changes: 43 additions & 2 deletions pallets/farming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod mock;
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod utils;
mod weights;
pub mod weights;

use assets::AssetIdOf;
use codec::{Decode, Encode};
Expand Down 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 @@ -379,6 +381,21 @@ pub mod pallet {
IncRefError,
}

#[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!(3000000)
}

/// Pools whose farmers are refreshed at the specific block. Block => Pools
#[pallet::storage]
pub type Pools<T: Config> =
Expand All @@ -388,6 +405,30 @@ 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)?;
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, Call, 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
59 changes: 57 additions & 2 deletions pallets/farming/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@
// 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;
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::{Event, Pallet, PoolFarmer, PoolFarmers};

type System = frame_system::Pallet<Runtime>;

fn init_pool(dex_id: DEXId, base_asset: AssetId, other_asset: AssetId) {
assert_ok!(trading_pair::Pallet::<Runtime>::register(
Expand All @@ -63,6 +68,21 @@ 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(|| {
// Check default value for lp_min_xor_for_bonus_reward
assert_eq!(
<Pallet<Runtime>>::lp_min_xor_for_bonus_reward(),
balance!(3000000)
);
// Update lp_min_xor_for_bonus_reward
<Pallet<Runtime>>::set_lp_min_xor_for_bonus_reward(RawOrigin::Root.into(), balance!(1))
.unwrap();

// Check lp_min_xor_for_bonus_reward updated
assert_eq!(
<Pallet<Runtime>>::lp_min_xor_for_bonus_reward(),
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 +426,38 @@ 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
);
});
}

#[test]
fn set_lp_min_xor_for_bonus_reward_should_work() {
ExtBuilder::default().build().execute_with(|| {
System::set_block_number(1);
let modified_min_xor = balance!(3 * (10_i32.pow(6)));
let old_lp_min_xor_for_bonus_reward = Pallet::<Runtime>::lp_min_xor_for_bonus_reward();
assert_ok!(Pallet::<Runtime>::set_lp_min_xor_for_bonus_reward(
RawOrigin::Root.into(),
modified_min_xor
));
let new_lp_min_xor_for_bonus_reward = Pallet::<Runtime>::lp_min_xor_for_bonus_reward();
assert_eq!(new_lp_min_xor_for_bonus_reward, modified_min_xor);
System::assert_has_event(
Event::LpMinXorForBonusRewardUpdated {
new_lp_min_xor_for_bonus_reward,
old_lp_min_xor_for_bonus_reward,
}
.into(),
);
});
}
25 changes: 25 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,18 @@ 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()))
}

/// Storage: Farming LpMinXorForBonusReward (r:1 w:1)
/// Proof Skipped: Farming LpMinXorForBonusReward (max_values: Some(1), max_size: None, mode: Measured)
fn set_lp_min_xor_for_bonus_reward() -> Weight {
// Proof Size summary in bytes:
// Measured: `109`
// Estimated: `604`
// Minimum execution time: 8_000 nanoseconds.
Weight::from_parts(9_000_000, 604)
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -200,4 +213,16 @@ 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()))
}

/// Storage: Farming LpMinXorForBonusReward (r:1 w:1)
/// Proof Skipped: Farming LpMinXorForBonusReward (max_values: Some(1), max_size: None, mode: Measured)
fn set_lp_min_xor_for_bonus_reward() -> Weight {
// Proof Size summary in bytes:
// Measured: `109`
// Estimated: `604`
// Minimum execution time: 8_000 nanoseconds.
Weight::from_parts(9_000_000, 604)
.saturating_add(RocksDbWeight::get().reads(1))
.saturating_add(RocksDbWeight::get().writes(1))
}
}
5 changes: 3 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,8 @@ impl farming::Config for Runtime {
type Scheduler = Scheduler;
type RewardDoublingAssets = FarmingRewardDoublingAssets;
type TradingPairSourceManager = trading_pair::Pallet<Runtime>;
type WeightInfo = ();
type WeightInfo = farming::weights::SubstrateWeight<Runtime>;
type RuntimeEvent = RuntimeEvent;
}

impl pswap_distribution::Config for Runtime {
Expand Down Expand Up @@ -2436,7 +2437,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, Call, Storage, Event<T>} = 42,
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