Skip to content

Commit 92c3d45

Browse files
authored
Feat/send dust to treasury (#193)
* feat: send the dust to the BOT * test: check the dust and the total issuance * chore: bump spec version
1 parent 8ad0014 commit 92c3d45

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

integration-tests/src/constants.rs

+2
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ pub mod polimec_base {
469469
use super::*;
470470
use crate::PolimecBase;
471471
use pallet_funding::AcceptedFundingAsset;
472+
use polimec_base_runtime::PayMaster;
472473
use xcm::{prelude::Parachain, v3::Parent};
473474

474475
pub const PARA_ID: u32 = 3344;
@@ -496,6 +497,7 @@ pub mod polimec_base {
496497
funded_accounts.extend(accounts::init_balances().iter().cloned().map(|k| (k, INITIAL_DEPOSIT)));
497498
funded_accounts.extend(collators::initial_authorities().iter().cloned().map(|(acc, _)| (acc, 20_005 * PLMC)));
498499
funded_accounts.push((get_account_id_from_seed::<sr25519::Public>("TREASURY_STASH"), 20_005 * PLMC));
500+
funded_accounts.push((PayMaster::get(), 20_005 * PLMC));
499501

500502
let genesis_config = polimec_base_runtime::RuntimeGenesisConfig {
501503
system: polimec_base_runtime::SystemConfig {

integration-tests/src/tests/vest.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use frame_support::traits::fungible::Mutate;
2323
use macros::generate_accounts;
2424
use pallet_funding::assert_close_enough;
2525
use pallet_vesting::VestingInfo;
26-
use polimec_base_runtime::{Balances, ParachainStaking, RuntimeOrigin, Vesting};
26+
use polimec_base_runtime::{Balances, ParachainStaking, PayMaster, RuntimeOrigin, Vesting};
2727
use sp_runtime::Perquintill;
2828
use tests::defaults::*;
2929
use xcm_emulator::get_account_id_from_seed;
@@ -144,3 +144,40 @@ fn base_can_withdraw_when_free_is_below_frozen_with_hold() {
144144
assert_eq!(Balances::reserved_balance(&CARLOS.into()), 2_000 * PLMC);
145145
})
146146
}
147+
148+
// Tests the behavior of transferring the dust to the Blockchain Operation Treasury.
149+
// When an account's balance falls below the Existential Deposit (ED) threshold following a transfer,
150+
// the account is killed and the dust is sent to the treasury.
151+
#[test]
152+
fn dust_to_treasury() {
153+
PolimecBase::execute_with(|| {
154+
// Create two new accounts: a sender and a receiver.
155+
let sender = get_account_id_from_seed::<sr25519::Public>("SENDER");
156+
let receiver = get_account_id_from_seed::<sr25519::Public>("RECEIVER");
157+
158+
// Set the sender's initial balance to 1 PLMC.
159+
let initial_sender_balance = 1 * PLMC;
160+
Balances::set_balance(&sender, initial_sender_balance);
161+
162+
// Get the total issuance and Treasury balance before the transfer.
163+
let initial_total_issuance = Balances::total_issuance();
164+
let initial_treasury_balance = Balances::free_balance(PayMaster::get());
165+
166+
// Transfer funds from sender to receiver, designed to deplete the sender's balance below the ED.
167+
// The sender account will be killed and the dust will be sent to the treasury.
168+
// This happens because at the end of the transfer, the user has free_balance < ED.
169+
assert_ok!(Balances::transfer_allow_death(
170+
RuntimeOrigin::signed(sender),
171+
sp_runtime::MultiAddress::Id(receiver),
172+
initial_sender_balance - ED + 1
173+
));
174+
175+
// Confirm the total issuance remains unchanged post-transfer.
176+
let post_total_issuance = Balances::total_issuance();
177+
assert_eq!(initial_total_issuance, post_total_issuance);
178+
179+
// Verify the Treasury has received the dust from the sender's account.
180+
let final_treasury_balance = Balances::free_balance(PayMaster::get());
181+
assert_eq!(initial_treasury_balance + ED - 1, final_treasury_balance);
182+
})
183+
}

runtimes/base/src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
195195
spec_name: create_runtime_str!("polimec-mainnet"),
196196
impl_name: create_runtime_str!("polimec-mainnet"),
197197
authoring_version: 1,
198-
spec_version: 0_005_005,
198+
spec_version: 0_005_006,
199199
impl_version: 0,
200200
apis: RUNTIME_API_VERSIONS,
201201
transaction_version: 1,
@@ -340,10 +340,19 @@ impl pallet_authorship::Config for Runtime {
340340
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Aura>;
341341
}
342342

343+
pub struct DustRemovalAdapter;
344+
345+
impl tokens::imbalance::OnUnbalanced<CreditOf<Runtime>> for DustRemovalAdapter {
346+
fn on_nonzero_unbalanced(amount: CreditOf<Runtime>) {
347+
let treasury_account = PayMaster::get();
348+
let _ = <Balances as tokens::fungible::Balanced<AccountId>>::resolve(&treasury_account, amount);
349+
}
350+
}
351+
343352
impl pallet_balances::Config for Runtime {
344353
type AccountStore = System;
345354
type Balance = Balance;
346-
type DustRemoval = ();
355+
type DustRemoval = DustRemovalAdapter;
347356
type ExistentialDeposit = ExistentialDeposit;
348357
type FreezeIdentifier = RuntimeFreezeReason;
349358
type MaxFreezes = MaxReserves;

0 commit comments

Comments
 (0)