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

enable removing vtoken from channelCommission pools #1227

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions pallets/channel-commission/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ benchmarks! {
let commission_token = CurrencyId::Token2(i);
assert_ok!(ChannelCommission::<T>::set_commission_tokens(
origin.clone(),
vtoken, commission_token
vtoken, Some(commission_token)
));
}

Expand Down Expand Up @@ -89,7 +89,7 @@ benchmarks! {

assert_ok!(ChannelCommission::<T>::set_commission_tokens(
origin.clone(),
vtoken, commission_token
vtoken, Some(commission_token)
));

assert_ok!(ChannelCommission::<T>::register_channel(
Expand All @@ -103,7 +103,7 @@ benchmarks! {
let commission_token = CurrencyId::Token2(0);
let vtoken = CurrencyId::VToken2(0);

}: _<T::RuntimeOrigin>(origin.clone(), vtoken, commission_token)
}: _<T::RuntimeOrigin>(origin.clone(), vtoken, Some(commission_token))

claim_commissions {
let test_account: T::AccountId = account("seed",1,1);
Expand All @@ -117,7 +117,7 @@ benchmarks! {

assert_ok!(ChannelCommission::<T>::set_commission_tokens(
origin.clone(),
vtoken, commission_token
vtoken, Some(commission_token)
));

assert_ok!(ChannelCommission::<T>::register_channel(
Expand Down Expand Up @@ -153,7 +153,7 @@ benchmarks! {
// set_commission_tokens
assert_ok!(ChannelCommission::<T>::set_commission_tokens(
origin.clone(),
vtoken, commission_token
vtoken, Some(commission_token)
));

let old_amount: BalanceOf<T> = 9000u128.unique_saturated_into();
Expand Down Expand Up @@ -202,7 +202,7 @@ benchmarks! {
// set_commission_tokens
assert_ok!(ChannelCommission::<T>::set_commission_tokens(
origin.clone(),
vtoken_set, CurrencyId::Token2(0)
vtoken_set, Some(CurrencyId::Token2(0))
));

// register channels
Expand Down
68 changes: 51 additions & 17 deletions pallets/channel-commission/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,31 +453,65 @@ pub mod pallet {
pub fn set_commission_tokens(
origin: OriginFor<T>,
vtoken: CurrencyId,
commission_token: CurrencyId,
commission_token_op: Option<CurrencyId>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;

ensure!(vtoken.is_vtoken(), Error::<T>::InvalidVtoken);

// if old commission token is the same as the new one, do nothing
if let Some(old_commission_token) = CommissionTokens::<T>::get(vtoken) {
if old_commission_token == commission_token {
return Ok(());
if let Some(commission_token) = commission_token_op {
// if old commission token is the same as the new one, do nothing
if let Some(old_commission_token) = CommissionTokens::<T>::get(vtoken) {
if old_commission_token == commission_token {
return Ok(());
}
}
}

// set the commission token
CommissionTokens::<T>::insert(vtoken, commission_token);
// set the commission token
CommissionTokens::<T>::insert(vtoken, commission_token);

// set VtokenIssuanceSnapshots for the vtoken
let issuance = T::MultiCurrency::total_issuance(vtoken);
let zero_balance: BalanceOf<T> = Zero::zero();
VtokenIssuanceSnapshots::<T>::insert(vtoken, (zero_balance, issuance));
// set VtokenIssuanceSnapshots for the vtoken
let issuance = T::MultiCurrency::total_issuance(vtoken);
let zero_balance: BalanceOf<T> = Zero::zero();
VtokenIssuanceSnapshots::<T>::insert(vtoken, (zero_balance, issuance));

Self::deposit_event(Event::CommissionTokenSet {
vtoken,
commission_token: Some(commission_token),
});
Self::deposit_event(Event::CommissionTokenSet {
vtoken,
commission_token: Some(commission_token),
});
} else {
// remove the commission token
let _ = CommissionTokens::<T>::remove(vtoken);

// remove the vtoken from VtokenIssuanceSnapshots
let _ = VtokenIssuanceSnapshots::<T>::remove(vtoken);

// remove the vtoken from PeriodVtokenTotalMint storage
let _ = PeriodVtokenTotalMint::<T>::remove(vtoken);

// remove the vtoken from PeriodVtokenTotalRedeem storage
let _ = PeriodVtokenTotalRedeem::<T>::remove(vtoken);

// for all channel_ids
Channels::<T>::iter_keys().for_each(|channel_id| {
// remove the vtoken from ChannelCommissionTokenRates storage
let _ = ChannelCommissionTokenRates::<T>::remove(channel_id, vtoken);
// remove the vtoken from ChannelVtokenShares storage
let _ = ChannelVtokenShares::<T>::remove(channel_id, vtoken);
// remove the vtoken from PeriodChannelVtokenMint storage
let _ = PeriodChannelVtokenMint::<T>::remove(channel_id, vtoken);
});

// remove the vtoken from PeriodTotalCommissions storage
let _ = PeriodTotalCommissions::<T>::remove(vtoken);

// remove the vtoken from PeriodClearedCommissions storage
let _ = PeriodClearedCommissions::<T>::remove(vtoken);

// only ChannelClaimableCommissions not removed. Channel can still claim the
// previous commission

Self::deposit_event(Event::CommissionTokenSet { vtoken, commission_token: None });
}

Ok(())
}
Expand Down
27 changes: 22 additions & 5 deletions pallets/channel-commission/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ const CHANNEL_A_BACKUP_RECEIVER: AccountId = AccountId32::new([5u8; 32]);

fn setup() {
// set commission tokens: VKSM -> KSM
assert_ok!(ChannelCommission::set_commission_tokens(RuntimeOrigin::signed(ALICE), VKSM, KSM,));
assert_ok!(ChannelCommission::set_commission_tokens(
RuntimeOrigin::signed(ALICE),
VKSM,
Some(KSM),
));

// set commission tokens: VBNC -> BNC
assert_ok!(ChannelCommission::set_commission_tokens(RuntimeOrigin::signed(ALICE), VBNC, BNC,));
assert_ok!(ChannelCommission::set_commission_tokens(
RuntimeOrigin::signed(ALICE),
VBNC,
Some(BNC),
));

// register channel A
assert_ok!(ChannelCommission::register_channel(
Expand All @@ -58,11 +66,20 @@ fn set_commission_tokens_should_work() {
assert_ok!(ChannelCommission::set_commission_tokens(
RuntimeOrigin::signed(ALICE),
VKSM,
KSM,
Some(KSM),
));

// Channel A is registered
assert_eq!(CommissionTokens::<Runtime>::get(VKSM), Some(KSM));

assert_ok!(ChannelCommission::set_commission_tokens(
RuntimeOrigin::signed(ALICE),
VKSM,
None,
));

assert_eq!(CommissionTokens::<Runtime>::get(VKSM), None);
assert_eq!(VtokenIssuanceSnapshots::<Runtime>::get(VKSM), Default::default());
});
}

Expand All @@ -73,14 +90,14 @@ fn register_channel_should_work() {
assert_ok!(ChannelCommission::set_commission_tokens(
RuntimeOrigin::signed(ALICE),
VKSM,
KSM,
Some(KSM),
));

// set commission tokens: VBNC -> BNC
assert_ok!(ChannelCommission::set_commission_tokens(
RuntimeOrigin::signed(ALICE),
VBNC,
BNC,
Some(BNC),
));

assert_ok!(ChannelCommission::register_channel(
Expand Down