Skip to content

Commit

Permalink
fix: ensure correct affiliate account creation/removal
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Feb 17, 2025
1 parent 6a9acf3 commit e62b4ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion state-chain/pallets/cf-swapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use frame_support::{
DispatchError, Permill, TransactionOutcome,
},
storage::with_transaction_unchecked,
traits::Defensive,
traits::{Defensive, HandleLifetime},
transactional, CloneNoBound, Hashable,
};
use frame_system::pallet_prelude::*;
Expand Down Expand Up @@ -1052,6 +1052,15 @@ pub mod pallet {
T::BalanceApi::get_balance(&affiliate_account_id, Asset::Usdc).is_zero(),
Error::<T>::AffiliateEarnedFeesNotWithdrawn
);
frame_system::Provider::<T>::killed(&affiliate_account_id).unwrap_or_else(|e| {
// This shouldn't happen, and not much we can do if it does except fix it on a
// subsequent release. Consequences are minor.
log::error!(
"Unexpected reference count error while reaping the affiliate {:?}: {:?}.",
affiliate_account_id,
e
);
})
}

// Clear the affiliate account details and affiliate id mapping.
Expand Down Expand Up @@ -1264,6 +1273,10 @@ pub mod pallet {
.map_err(|_| Error::<T>::AffiliateAccountIdDerivationFailed)?;

AffiliateIdMapping::<T>::insert(&broker_id, short_id, &affiliate_id);
if !frame_system::Pallet::<T>::account_exists(&affiliate_id) {
// Creates an account
let _ = frame_system::Provider::<T>::created(&affiliate_id);
}

AffiliateAccountDetails::<T>::insert(
&broker_id,
Expand Down
13 changes: 13 additions & 0 deletions state-chain/pallets/cf-swapping/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,10 @@ mod affiliates {
let affiliate_account_id = AffiliateIdMapping::<Test>::get(BROKER, SHORT_ID)
.expect("Affiliate must be registered!");

assert!(
frame_system::Account::<Test>::contains_key(affiliate_account_id),
"Account not created"
);
System::assert_has_event(RuntimeEvent::Swapping(
Event::<Test>::AffiliateRegistration {
broker_id: BROKER,
Expand Down Expand Up @@ -1806,6 +1810,7 @@ mod affiliates {

assert_event_sequence!(
Test,
RuntimeEvent::System(frame_system::Event::NewAccount { .. }),
RuntimeEvent::Swapping(Event::AffiliateRegistration { .. }),
RuntimeEvent::Swapping(Event::WithdrawalRequested { .. }),
);
Expand Down Expand Up @@ -1887,6 +1892,10 @@ mod affiliates {
let affiliate_account_id = AffiliateIdMapping::<Test>::get(BROKER, SHORT_ID)
.expect("Affiliate must be registered!");

assert!(
frame_system::Account::<Test>::contains_key(affiliate_account_id),
"Account not created"
);
MockBalance::credit_account(&affiliate_account_id, Asset::Usdc, BALANCE);

assert_noop!(
Expand All @@ -1900,6 +1909,10 @@ mod affiliates {

assert!(AffiliateAccountDetails::<Test>::get(BROKER, affiliate_account_id).is_none());
assert!(AffiliateIdMapping::<Test>::get(BROKER, SHORT_ID).is_none());
assert!(
!frame_system::Account::<Test>::contains_key(affiliate_account_id),
"Account not deleted"
);
});
}
}

0 comments on commit e62b4ba

Please sign in to comment.