Skip to content

Commit

Permalink
Feat/send dust to treasury (#193)
Browse files Browse the repository at this point in the history
* feat: send the dust to the BOT

* test: check the dust and the total issuance

* chore: bump spec version
  • Loading branch information
lrazovic authored Mar 18, 2024
1 parent 8ad0014 commit 92c3d45
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ pub mod polimec_base {
use super::*;
use crate::PolimecBase;
use pallet_funding::AcceptedFundingAsset;
use polimec_base_runtime::PayMaster;
use xcm::{prelude::Parachain, v3::Parent};

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

let genesis_config = polimec_base_runtime::RuntimeGenesisConfig {
system: polimec_base_runtime::SystemConfig {
Expand Down
39 changes: 38 additions & 1 deletion integration-tests/src/tests/vest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use frame_support::traits::fungible::Mutate;
use macros::generate_accounts;
use pallet_funding::assert_close_enough;
use pallet_vesting::VestingInfo;
use polimec_base_runtime::{Balances, ParachainStaking, RuntimeOrigin, Vesting};
use polimec_base_runtime::{Balances, ParachainStaking, PayMaster, RuntimeOrigin, Vesting};
use sp_runtime::Perquintill;
use tests::defaults::*;
use xcm_emulator::get_account_id_from_seed;
Expand Down Expand Up @@ -144,3 +144,40 @@ fn base_can_withdraw_when_free_is_below_frozen_with_hold() {
assert_eq!(Balances::reserved_balance(&CARLOS.into()), 2_000 * PLMC);
})
}

// Tests the behavior of transferring the dust to the Blockchain Operation Treasury.
// When an account's balance falls below the Existential Deposit (ED) threshold following a transfer,
// the account is killed and the dust is sent to the treasury.
#[test]
fn dust_to_treasury() {
PolimecBase::execute_with(|| {
// Create two new accounts: a sender and a receiver.
let sender = get_account_id_from_seed::<sr25519::Public>("SENDER");
let receiver = get_account_id_from_seed::<sr25519::Public>("RECEIVER");

// Set the sender's initial balance to 1 PLMC.
let initial_sender_balance = 1 * PLMC;
Balances::set_balance(&sender, initial_sender_balance);

// Get the total issuance and Treasury balance before the transfer.
let initial_total_issuance = Balances::total_issuance();
let initial_treasury_balance = Balances::free_balance(PayMaster::get());

// Transfer funds from sender to receiver, designed to deplete the sender's balance below the ED.
// The sender account will be killed and the dust will be sent to the treasury.
// This happens because at the end of the transfer, the user has free_balance < ED.
assert_ok!(Balances::transfer_allow_death(
RuntimeOrigin::signed(sender),
sp_runtime::MultiAddress::Id(receiver),
initial_sender_balance - ED + 1
));

// Confirm the total issuance remains unchanged post-transfer.
let post_total_issuance = Balances::total_issuance();
assert_eq!(initial_total_issuance, post_total_issuance);

// Verify the Treasury has received the dust from the sender's account.
let final_treasury_balance = Balances::free_balance(PayMaster::get());
assert_eq!(initial_treasury_balance + ED - 1, final_treasury_balance);
})
}
13 changes: 11 additions & 2 deletions runtimes/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polimec-mainnet"),
impl_name: create_runtime_str!("polimec-mainnet"),
authoring_version: 1,
spec_version: 0_005_005,
spec_version: 0_005_006,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -340,10 +340,19 @@ impl pallet_authorship::Config for Runtime {
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Aura>;
}

pub struct DustRemovalAdapter;

impl tokens::imbalance::OnUnbalanced<CreditOf<Runtime>> for DustRemovalAdapter {
fn on_nonzero_unbalanced(amount: CreditOf<Runtime>) {
let treasury_account = PayMaster::get();
let _ = <Balances as tokens::fungible::Balanced<AccountId>>::resolve(&treasury_account, amount);
}
}

impl pallet_balances::Config for Runtime {
type AccountStore = System;
type Balance = Balance;
type DustRemoval = ();
type DustRemoval = DustRemovalAdapter;
type ExistentialDeposit = ExistentialDeposit;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = MaxReserves;
Expand Down

0 comments on commit 92c3d45

Please sign in to comment.