Skip to content

Commit

Permalink
Update buy back (#1387)
Browse files Browse the repository at this point in the history
* style: 💄 format

* style: 💄 check-all

* fix: 🐛 bench

* feat: 🎸 add destruction_ratio

* fix: 🐛 benchmark
  • Loading branch information
yooml authored Aug 20, 2024
1 parent 036119e commit aea421d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pallets/buy-back/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use sp_runtime::traits::UniqueSaturatedFrom;
benchmarks! {
set_vtoken {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
}: _<T::RuntimeOrigin>(origin,VDOT,1_000_000u32.into(),Permill::from_percent(2),1000u32.into(),1000u32.into(),true)
}: _<T::RuntimeOrigin>(origin,VDOT,1_000_000u32.into(),Permill::from_percent(2),1000u32.into(),1000u32.into(),true,Some(Permill::from_percent(2)))

charge {
let test_account: T::AccountId = account("seed",1,1);
Expand All @@ -51,7 +51,8 @@ benchmarks! {
Permill::from_percent(2),
1000u32.into(),
1000u32.into(),
true
true,
Some(Permill::from_percent(2))
));
}: _<T::RuntimeOrigin>(origin,VDOT)

Expand All @@ -65,7 +66,8 @@ benchmarks! {
Permill::from_percent(2),
1000u32.into(),
1000u32.into(),
true
true,
Some(Permill::from_percent(2))
));
}: {
BuyBack::<T>::on_idle(BlockNumberFor::<T>::from(0u32),Weight::from_parts(0, u64::MAX));
Expand Down
8 changes: 8 additions & 0 deletions pallets/buy-back/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub mod pallet {
add_liquidity_duration: BlockNumberFor,
/// The last time liquidity was added.
last_add_liquidity: BlockNumberFor,
destruction_ratio: Option<Permill>,
}

#[pallet::hooks]
Expand Down Expand Up @@ -221,6 +222,7 @@ pub mod pallet {
buyback_duration: BlockNumberFor<T>,
add_liquidity_duration: BlockNumberFor<T>,
if_auto: bool,
destruction_ratio: Option<Permill>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;

Expand All @@ -237,6 +239,7 @@ pub mod pallet {
last_buyback: Zero::zero(),
add_liquidity_duration,
last_add_liquidity: Zero::zero(),
destruction_ratio,
};
Infos::<T>::insert(currency_id, info.clone());

Expand Down Expand Up @@ -309,6 +312,11 @@ pub mod pallet {
&buyback_address,
)?;

if let Some(ratio) = info.destruction_ratio {
let bnc_balance_before_burn = T::MultiCurrency::free_balance(BNC, &buyback_address);
let destruction_amount = ratio * bnc_balance_before_burn;
T::MultiCurrency::withdraw(BNC, &buyback_address, destruction_amount)?;
}
let bnc_balance = T::MultiCurrency::free_balance(BNC, &buyback_address);
let pool_id = 0;
T::VeMinting::notify_reward(
Expand Down
82 changes: 82 additions & 0 deletions pallets/buy-back/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const LIQUID_PROPORTION: Permill = Permill::from_percent(2);
#[test]
fn set_vtoken_should_not_work() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let destruction_ratio = Some(Permill::from_percent(2));
assert_noop!(
BuyBack::set_vtoken(
RuntimeOrigin::signed(ALICE),
Expand All @@ -42,6 +43,7 @@ fn set_vtoken_should_not_work() {
BUYBACK_DURATION,
LIQUID_DURATION,
true,
destruction_ratio
),
Error::<Runtime>::CurrencyIdError
);
Expand All @@ -55,6 +57,7 @@ fn set_vtoken_should_not_work() {
0,
LIQUID_DURATION,
true,
destruction_ratio
),
Error::<Runtime>::ZeroDuration
);
Expand All @@ -68,16 +71,54 @@ fn set_vtoken_should_not_work() {
BUYBACK_DURATION,
LIQUID_DURATION,
true,
destruction_ratio
),
Error::<Runtime>::ZeroMinSwapValue
);
});
}

#[test]
fn buy_back_with_burn_should_work() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let zenlink_pair_account_id = init_zenlink(PARAID);
let destruction_ratio = Some(Permill::from_percent(2));

assert_ok!(BuyBack::set_vtoken(
RuntimeOrigin::signed(ALICE),
VKSM,
VALUE,
LIQUID_PROPORTION,
BUYBACK_DURATION,
LIQUID_DURATION,
true,
destruction_ratio
));
let buyback_account = <Runtime as Config>::BuyBackAccount::get().into_account_truncating();
let incentive_account = IncentivePalletId::get().into_account_truncating();
assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000);
assert_eq!(Currencies::free_balance(VKSM, &zenlink_pair_account_id), 2200);
assert_eq!(Currencies::free_balance(BNC, &zenlink_pair_account_id), 2000);
assert_eq!(Currencies::free_balance(BNC, &buyback_account), 0);
assert_eq!(Currencies::free_balance(BNC, &incentive_account), 0);
VeMinting::set_incentive(0, Some(7 * 86400 / 12), Some(buyback_account.clone()));
assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000));
let infos = Infos::<Runtime>::get(VKSM).unwrap();
assert_ok!(BuyBack::buy_back(&buyback_account, VKSM, &infos));
System::set_block_number(System::block_number() + 1);
assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000);
assert_eq!(Currencies::free_balance(VKSM, &zenlink_pair_account_id), 3200);
assert_eq!(Currencies::free_balance(BNC, &zenlink_pair_account_id), 1377);
assert_eq!(Currencies::free_balance(BNC, &buyback_account), 0);
assert_eq!(Currencies::free_balance(BNC, &incentive_account), 611);
});
}

#[test]
fn buy_back_no_burn_should_work() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let zenlink_pair_account_id = init_zenlink(PARAID);
let destruction_ratio = Some(Permill::from_percent(0));

assert_ok!(BuyBack::set_vtoken(
RuntimeOrigin::signed(ALICE),
Expand All @@ -87,6 +128,7 @@ fn buy_back_no_burn_should_work() {
BUYBACK_DURATION,
LIQUID_DURATION,
true,
destruction_ratio
));
let buyback_account = <Runtime as Config>::BuyBackAccount::get().into_account_truncating();
let incentive_account = IncentivePalletId::get().into_account_truncating();
Expand All @@ -112,6 +154,7 @@ fn buy_back_no_burn_should_work() {
fn on_idle_no_burn_should_work() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let zenlink_pair_account_id = init_zenlink(PARAID);
let destruction_ratio = None;

assert_ok!(BuyBack::set_vtoken(
RuntimeOrigin::signed(ALICE),
Expand All @@ -121,6 +164,7 @@ fn on_idle_no_burn_should_work() {
BUYBACK_DURATION,
LIQUID_DURATION,
true,
destruction_ratio
));
let buyback_account = <Runtime as Config>::BuyBackAccount::get().into_account_truncating();
let incentive_account = IncentivePalletId::get().into_account_truncating();
Expand All @@ -144,6 +188,44 @@ fn on_idle_no_burn_should_work() {
});
}

#[test]
fn on_idle_with_burn_should_work() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let zenlink_pair_account_id = init_zenlink(PARAID);
let destruction_ratio = Some(Permill::from_percent(10));

assert_ok!(BuyBack::set_vtoken(
RuntimeOrigin::signed(ALICE),
VKSM,
1_000_000u128,
LIQUID_PROPORTION,
BUYBACK_DURATION,
LIQUID_DURATION,
true,
destruction_ratio
));
let buyback_account = <Runtime as Config>::BuyBackAccount::get().into_account_truncating();
let incentive_account = IncentivePalletId::get().into_account_truncating();
assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000);
assert_eq!(Currencies::free_balance(VKSM, &zenlink_pair_account_id), 2200);
assert_eq!(Currencies::free_balance(BNC, &zenlink_pair_account_id), 2000);
assert_eq!(Currencies::free_balance(BNC, &buyback_account), 0);
assert_eq!(Currencies::free_balance(BNC, &incentive_account), 0);
VeMinting::set_incentive(0, Some(7 * 86400 / 12), Some(buyback_account.clone()));
assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000));
BuyBack::on_idle(
<frame_system::Pallet<Runtime>>::block_number(),
Weight::from_parts(100000000, 0),
);
System::set_block_number(System::block_number() + 1);
assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 0);
assert_eq!(Currencies::free_balance(VKSM, &zenlink_pair_account_id), 12200);
assert_eq!(Currencies::free_balance(BNC, &zenlink_pair_account_id), 362);
assert_eq!(Currencies::free_balance(BNC, &buyback_account), 0);
assert_eq!(Currencies::free_balance(BNC, &incentive_account), 1474); // 1638 - 164
});
}

fn init_zenlink(_para_id: u32) -> AccountIdOf<Runtime> {
let asset_0_currency_id: AssetId = AssetId::try_convert_from(BNC, PARAID).unwrap();
let asset_1_currency_id: AssetId = AssetId::try_convert_from(VKSM, PARAID).unwrap();
Expand Down

0 comments on commit aea421d

Please sign in to comment.