diff --git a/pallets/channel-commission/src/benchmarking.rs b/pallets/channel-commission/src/benchmarking.rs index 1930af0f92..12f183048b 100644 --- a/pallets/channel-commission/src/benchmarking.rs +++ b/pallets/channel-commission/src/benchmarking.rs @@ -46,7 +46,7 @@ benchmarks! { let commission_token = CurrencyId::Token2(i); assert_ok!(ChannelCommission::::set_commission_tokens( origin.clone(), - vtoken, commission_token + vtoken, Some(commission_token) )); } @@ -89,7 +89,7 @@ benchmarks! { assert_ok!(ChannelCommission::::set_commission_tokens( origin.clone(), - vtoken, commission_token + vtoken, Some(commission_token) )); assert_ok!(ChannelCommission::::register_channel( @@ -103,7 +103,7 @@ benchmarks! { let commission_token = CurrencyId::Token2(0); let vtoken = CurrencyId::VToken2(0); - }: _(origin.clone(), vtoken, commission_token) + }: _(origin.clone(), vtoken, Some(commission_token)) claim_commissions { let test_account: T::AccountId = account("seed",1,1); @@ -117,7 +117,7 @@ benchmarks! { assert_ok!(ChannelCommission::::set_commission_tokens( origin.clone(), - vtoken, commission_token + vtoken, Some(commission_token) )); assert_ok!(ChannelCommission::::register_channel( @@ -153,7 +153,7 @@ benchmarks! { // set_commission_tokens assert_ok!(ChannelCommission::::set_commission_tokens( origin.clone(), - vtoken, commission_token + vtoken, Some(commission_token) )); let old_amount: BalanceOf = 9000u128.unique_saturated_into(); @@ -202,7 +202,7 @@ benchmarks! { // set_commission_tokens assert_ok!(ChannelCommission::::set_commission_tokens( origin.clone(), - vtoken_set, CurrencyId::Token2(0) + vtoken_set, Some(CurrencyId::Token2(0)) )); // register channels diff --git a/pallets/channel-commission/src/lib.rs b/pallets/channel-commission/src/lib.rs index 4e7d1d0306..948cc88f14 100644 --- a/pallets/channel-commission/src/lib.rs +++ b/pallets/channel-commission/src/lib.rs @@ -453,31 +453,65 @@ pub mod pallet { pub fn set_commission_tokens( origin: OriginFor, vtoken: CurrencyId, - commission_token: CurrencyId, + commission_token_op: Option, ) -> DispatchResult { T::ControlOrigin::ensure_origin(origin)?; - ensure!(vtoken.is_vtoken(), Error::::InvalidVtoken); - // if old commission token is the same as the new one, do nothing - if let Some(old_commission_token) = CommissionTokens::::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::::get(vtoken) { + if old_commission_token == commission_token { + return Ok(()); + } } - } - // set the commission token - CommissionTokens::::insert(vtoken, commission_token); + // set the commission token + CommissionTokens::::insert(vtoken, commission_token); - // set VtokenIssuanceSnapshots for the vtoken - let issuance = T::MultiCurrency::total_issuance(vtoken); - let zero_balance: BalanceOf = Zero::zero(); - VtokenIssuanceSnapshots::::insert(vtoken, (zero_balance, issuance)); + // set VtokenIssuanceSnapshots for the vtoken + let issuance = T::MultiCurrency::total_issuance(vtoken); + let zero_balance: BalanceOf = Zero::zero(); + VtokenIssuanceSnapshots::::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::::remove(vtoken); + + // remove the vtoken from VtokenIssuanceSnapshots + let _ = VtokenIssuanceSnapshots::::remove(vtoken); + + // remove the vtoken from PeriodVtokenTotalMint storage + let _ = PeriodVtokenTotalMint::::remove(vtoken); + + // remove the vtoken from PeriodVtokenTotalRedeem storage + let _ = PeriodVtokenTotalRedeem::::remove(vtoken); + + // for all channel_ids + Channels::::iter_keys().for_each(|channel_id| { + // remove the vtoken from ChannelCommissionTokenRates storage + let _ = ChannelCommissionTokenRates::::remove(channel_id, vtoken); + // remove the vtoken from ChannelVtokenShares storage + let _ = ChannelVtokenShares::::remove(channel_id, vtoken); + // remove the vtoken from PeriodChannelVtokenMint storage + let _ = PeriodChannelVtokenMint::::remove(channel_id, vtoken); + }); + + // remove the vtoken from PeriodTotalCommissions storage + let _ = PeriodTotalCommissions::::remove(vtoken); + + // remove the vtoken from PeriodClearedCommissions storage + let _ = PeriodClearedCommissions::::remove(vtoken); + + // only ChannelClaimableCommissions not removed. Channel can still claim the + // previous commission + + Self::deposit_event(Event::CommissionTokenSet { vtoken, commission_token: None }); + } Ok(()) } diff --git a/pallets/channel-commission/src/tests.rs b/pallets/channel-commission/src/tests.rs index b8b9f8b8cc..3ab8bd008f 100644 --- a/pallets/channel-commission/src/tests.rs +++ b/pallets/channel-commission/src/tests.rs @@ -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( @@ -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::::get(VKSM), Some(KSM)); + + assert_ok!(ChannelCommission::set_commission_tokens( + RuntimeOrigin::signed(ALICE), + VKSM, + None, + )); + + assert_eq!(CommissionTokens::::get(VKSM), None); + assert_eq!(VtokenIssuanceSnapshots::::get(VKSM), Default::default()); }); } @@ -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(