mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-23 07:21:17 +00:00
Deprecate Currency; introduce holds and freezing into fungible traits (#12951)
* First reworking of fungibles API * New API and docs * More fungible::* API improvements * New ref-counting logic for old API * Missing files * Fixes * Use the new transfer logic * Use fungibles for the dispatchables * Use shelve/restore names * Locking works with total balance. * repotting and removal * Separate Holds from Reserves * Introduce freezes * Missing files * Tests for freezing * Fix hold+freeze combo * More tests * Fee-free dispatchable for upgrading accounts * Benchmarks and a few fixes * Another test * Docs and refactor to avoid blanket impls * Repot * Fit out ItemOf fully * Add events to Balanced traits * Introduced events into Hold traits * Fix Assets pallet tests * Assets benchmarks pass * Missing files and fixes * Fixes * Fixes * Benchmarks fixes * Fix balance benchmarks * Formatting * Expose fungible sub modules * Move NIS to fungible API * Fix broken impl and add test * Fix tests * API for `transfer_and_hold` * Use composite APIs * Formatting * Upgraded event * Fixes * Fixes * Fixes * Fixes * Repot tests and some fixed * Fix some bits * Fix dust tests * Rename `set_balance` - `Balances::set_balance` becomes `Balances::force_set_balance` - `Unbalanced::set_balance` becomes `Unbalances::write_balance` * becomes * Move dust handling to fungibles API * Formatting * Fixes and more refactoring * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Use reducible_balance for better correctness on fees * Reducing hold to zero should remove entry. * Add test * Docs * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Update frame/support/src/traits/tokens/fungibles/regular.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Update frame/support/src/traits/tokens/fungible/regular.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Docs * Docs * Docs * Fix NIS benchmarks * Doc comment * Remove post_mutation * Fix some tests * Fix some grumbles * Enumify bool args to fungible(s) functions * Fix up assets and balances * Formatting * Fix contracts * Fix tests & benchmarks build * Typify minted boolean arg * Typify on_hold boolean arg; renames * Fix numerous tests * Fix dependency issue * Privatize dangerous API mutate_account * Fix contracts (@alext - please check this commit) * Remove println * Fix tests for contracts * Fix broken rename * Fix broken rename * Fix broken rename * Docs * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * remove from_ref_time * Update frame/executive/src/lib.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/executive/src/lib.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Reenable test * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/currency.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/lottery/src/tests.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/mod.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/regular.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/freeze.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/regular.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Rename UnwantedRemoval to UnwantedAccountRemoval * Docs * Formatting * Update frame/balances/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update primitives/runtime/src/lib.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * handle_raw_dust oes nothing * Formatting * Fixes * Grumble * Fixes * Add test * Add test * Tests for reducible_balance * Fixes * Fix Salary * Fixes * Disable broken test * Disable nicely * Fixes * Fixes * Fixes * Rename some events * Fix nomination pools breakage * Add compatibility stub for transfer tx * Reinstate a safely compatible version of Balances set_balance * Fixes * Grumble * Update frame/nis/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_balances * disable flakey tests * Update frame/balances/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Grumbles * Grumble --------- Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> Co-authored-by: Alexander Theißen <alex.theissen@me.com> Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: command-bot <>
This commit is contained in:
@@ -22,19 +22,22 @@ use crate::{mock::*, Error};
|
||||
use frame_support::{
|
||||
assert_noop, assert_ok,
|
||||
traits::{
|
||||
fungible::{hold::Inspect as InspectHold, Inspect as FunInspect, Mutate as FunMutate},
|
||||
nonfungible::{Inspect, Transfer},
|
||||
Currency,
|
||||
tokens::{Fortitude::Force, Precision::Exact},
|
||||
},
|
||||
};
|
||||
use pallet_balances::{Error as BalancesError, Instance1};
|
||||
use sp_arithmetic::Perquintill;
|
||||
use sp_runtime::{Saturating, TokenError};
|
||||
use sp_runtime::{
|
||||
Saturating,
|
||||
TokenError::{self, FundsUnavailable},
|
||||
};
|
||||
|
||||
fn pot() -> u64 {
|
||||
fn pot() -> Balance {
|
||||
Balances::free_balance(&Nis::account_id())
|
||||
}
|
||||
|
||||
fn holdings() -> u64 {
|
||||
fn holdings() -> Balance {
|
||||
Nis::issuance().holdings
|
||||
}
|
||||
|
||||
@@ -42,8 +45,8 @@ fn signed(who: u64) -> RuntimeOrigin {
|
||||
RuntimeOrigin::signed(who)
|
||||
}
|
||||
|
||||
fn enlarge(amount: u64, max_bids: u32) {
|
||||
let summary: SummaryRecord<u64, u64> = Summary::<Test>::get();
|
||||
fn enlarge(amount: Balance, max_bids: u32) {
|
||||
let summary: SummaryRecord<u64, Balance> = Summary::<Test>::get();
|
||||
let increase_in_proportion_owed = Perquintill::from_rational(amount, Nis::issuance().effective);
|
||||
let target = summary.proportion_owed.saturating_add(increase_in_proportion_owed);
|
||||
Nis::process_queues(target, u32::max_value(), max_bids, &mut WeightCounter::unlimited());
|
||||
@@ -75,10 +78,7 @@ fn place_bid_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
assert_noop!(Nis::place_bid(signed(1), 1, 2), Error::<Test>::AmountTooSmall);
|
||||
assert_noop!(
|
||||
Nis::place_bid(signed(1), 101, 2),
|
||||
BalancesError::<Test, Instance1>::InsufficientBalance
|
||||
);
|
||||
assert_noop!(Nis::place_bid(signed(1), 101, 2), FundsUnavailable);
|
||||
assert_noop!(Nis::place_bid(signed(1), 10, 4), Error::<Test>::DurationTooBig);
|
||||
assert_ok!(Nis::place_bid(signed(1), 10, 2));
|
||||
assert_eq!(Balances::reserved_balance(1), 10);
|
||||
@@ -451,16 +451,16 @@ fn communify_works() {
|
||||
assert_noop!(Nis::thaw_communal(signed(1), 1), Error::<Test>::UnknownReceipt);
|
||||
|
||||
// Transfer some of the fungibles away.
|
||||
assert_ok!(NisBalances::transfer(signed(1), 2, 100_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(1), 2, 100_000));
|
||||
assert_eq!(NisBalances::free_balance(&1), 2_000_000);
|
||||
assert_eq!(NisBalances::free_balance(&2), 100_000);
|
||||
|
||||
// Communal thawing with the correct index is not possible now.
|
||||
assert_noop!(Nis::thaw_communal(signed(1), 0), TokenError::NoFunds);
|
||||
assert_noop!(Nis::thaw_communal(signed(2), 0), TokenError::NoFunds);
|
||||
assert_noop!(Nis::thaw_communal(signed(1), 0), TokenError::FundsUnavailable);
|
||||
assert_noop!(Nis::thaw_communal(signed(2), 0), TokenError::FundsUnavailable);
|
||||
|
||||
// Transfer the rest to 2...
|
||||
assert_ok!(NisBalances::transfer(signed(1), 2, 2_000_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(1), 2, 2_000_000));
|
||||
assert_eq!(NisBalances::free_balance(&1), 0);
|
||||
assert_eq!(NisBalances::free_balance(&2), 2_100_000);
|
||||
|
||||
@@ -487,8 +487,8 @@ fn privatize_works() {
|
||||
assert_ok!(Nis::communify(signed(1), 0));
|
||||
|
||||
// Transfer the fungibles to #2
|
||||
assert_ok!(NisBalances::transfer(signed(1), 2, 2_100_000));
|
||||
assert_noop!(Nis::privatize(signed(1), 0), TokenError::NoFunds);
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(1), 2, 2_100_000));
|
||||
assert_noop!(Nis::privatize(signed(1), 0), TokenError::FundsUnavailable);
|
||||
|
||||
// Privatize
|
||||
assert_ok!(Nis::privatize(signed(2), 0));
|
||||
@@ -513,16 +513,16 @@ fn privatize_and_thaw_with_another_receipt_works() {
|
||||
assert_ok!(Nis::communify(signed(2), 1));
|
||||
|
||||
// Transfer half of fungibles to #3 from each of #1 and #2, and the other half from #2 to #4
|
||||
assert_ok!(NisBalances::transfer(signed(1), 3, 1_050_000));
|
||||
assert_ok!(NisBalances::transfer(signed(2), 3, 1_050_000));
|
||||
assert_ok!(NisBalances::transfer(signed(2), 4, 1_050_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(1), 3, 1_050_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(2), 3, 1_050_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(2), 4, 1_050_000));
|
||||
|
||||
// #3 privatizes, partially thaws, then re-communifies with #0, then transfers the fungibles
|
||||
// to #2
|
||||
assert_ok!(Nis::privatize(signed(3), 0));
|
||||
assert_ok!(Nis::thaw_private(signed(3), 0, Some(Perquintill::from_percent(5))));
|
||||
assert_ok!(Nis::communify(signed(3), 0));
|
||||
assert_ok!(NisBalances::transfer(signed(3), 1, 1_050_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(3), 1, 1_050_000));
|
||||
|
||||
// #1 now has enough to thaw using receipt 1
|
||||
assert_ok!(Nis::thaw_communal(signed(1), 1));
|
||||
@@ -536,17 +536,21 @@ fn privatize_and_thaw_with_another_receipt_works() {
|
||||
fn communal_thaw_when_issuance_higher_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
assert_ok!(Balances::transfer_allow_death(signed(2), 1, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 100, 1));
|
||||
enlarge(100, 1);
|
||||
assert_eq!(Balances::total_balance(&1), 101);
|
||||
|
||||
assert_ok!(Nis::communify(signed(1), 0));
|
||||
assert_eq!(Balances::total_balance_on_hold(&1), 0);
|
||||
assert_eq!(Balances::total_balance(&1), 1);
|
||||
|
||||
assert_eq!(NisBalances::free_balance(1), 5_250_000); // (25% of 21m)
|
||||
assert_eq!(NisBalances::free_balance(1), 5_250_000); // (12.5% of 21m)
|
||||
|
||||
// Everybody else's balances goes up by 50%
|
||||
Balances::make_free_balance_be(&2, 150);
|
||||
Balances::make_free_balance_be(&3, 150);
|
||||
Balances::make_free_balance_be(&4, 150);
|
||||
assert_ok!(Balances::mint_into(&2, 50));
|
||||
assert_ok!(Balances::mint_into(&3, 50));
|
||||
assert_ok!(Balances::mint_into(&4, 50));
|
||||
|
||||
run_to_block(4);
|
||||
|
||||
@@ -556,16 +560,20 @@ fn communal_thaw_when_issuance_higher_works() {
|
||||
assert_ok!(Nis::fund_deficit(signed(1)));
|
||||
|
||||
// Transfer counterparts away...
|
||||
assert_ok!(NisBalances::transfer(signed(1), 2, 250_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(1), 2, 125_000));
|
||||
// ...and it's not thawable.
|
||||
assert_noop!(Nis::thaw_communal(signed(1), 0), TokenError::NoFunds);
|
||||
assert_noop!(Nis::thaw_communal(signed(1), 0), TokenError::FundsUnavailable);
|
||||
|
||||
// Transfer counterparts back...
|
||||
assert_ok!(NisBalances::transfer(signed(2), 1, 250_000));
|
||||
assert_ok!(NisBalances::transfer_allow_death(signed(2), 1, 125_000));
|
||||
// ...and it is.
|
||||
assert_ok!(Nis::thaw_communal(signed(1), 0));
|
||||
assert_eq!(Balances::total_balance(&1), 151);
|
||||
|
||||
assert_ok!(Balances::transfer_allow_death(signed(1), 2, 1));
|
||||
assert_eq!(Balances::total_balance(&1), 150);
|
||||
assert_eq!(Balances::free_balance(1), 150);
|
||||
assert_eq!(Balances::total_balance_on_hold(&1), 0);
|
||||
assert_eq!(Balances::reserved_balance(1), 0);
|
||||
});
|
||||
}
|
||||
@@ -574,13 +582,14 @@ fn communal_thaw_when_issuance_higher_works() {
|
||||
fn private_thaw_when_issuance_higher_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
assert_ok!(Balances::transfer_allow_death(signed(2), 1, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 100, 1));
|
||||
enlarge(100, 1);
|
||||
|
||||
// Everybody else's balances goes up by 50%
|
||||
Balances::make_free_balance_be(&2, 150);
|
||||
Balances::make_free_balance_be(&3, 150);
|
||||
Balances::make_free_balance_be(&4, 150);
|
||||
assert_ok!(Balances::mint_into(&2, 50));
|
||||
assert_ok!(Balances::mint_into(&3, 50));
|
||||
assert_ok!(Balances::mint_into(&4, 50));
|
||||
|
||||
run_to_block(4);
|
||||
|
||||
@@ -591,6 +600,7 @@ fn private_thaw_when_issuance_higher_works() {
|
||||
|
||||
assert_ok!(Nis::thaw_private(signed(1), 0, None));
|
||||
|
||||
assert_ok!(Balances::transfer_allow_death(signed(1), 2, 1));
|
||||
assert_eq!(Balances::free_balance(1), 150);
|
||||
assert_eq!(Balances::reserved_balance(1), 0);
|
||||
});
|
||||
@@ -601,15 +611,16 @@ fn thaw_with_ignored_issuance_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
// Give account zero some balance.
|
||||
Balances::make_free_balance_be(&0, 200);
|
||||
assert_ok!(Balances::mint_into(&0, 200));
|
||||
|
||||
assert_ok!(Balances::transfer_allow_death(signed(2), 1, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 100, 1));
|
||||
enlarge(100, 1);
|
||||
|
||||
// Account zero transfers 50 into everyone else's accounts.
|
||||
assert_ok!(Balances::transfer(signed(0), 2, 50));
|
||||
assert_ok!(Balances::transfer(signed(0), 3, 50));
|
||||
assert_ok!(Balances::transfer(signed(0), 4, 50));
|
||||
assert_ok!(Balances::transfer_allow_death(signed(0), 2, 50));
|
||||
assert_ok!(Balances::transfer_allow_death(signed(0), 3, 50));
|
||||
assert_ok!(Balances::transfer_allow_death(signed(0), 4, 50));
|
||||
|
||||
run_to_block(4);
|
||||
// Unfunded initially...
|
||||
@@ -620,6 +631,7 @@ fn thaw_with_ignored_issuance_works() {
|
||||
assert_ok!(Nis::thaw_private(signed(1), 0, None));
|
||||
|
||||
// Account zero changes have been ignored.
|
||||
assert_ok!(Balances::transfer_allow_death(signed(1), 2, 1));
|
||||
assert_eq!(Balances::free_balance(1), 150);
|
||||
assert_eq!(Balances::reserved_balance(1), 0);
|
||||
});
|
||||
@@ -629,17 +641,19 @@ fn thaw_with_ignored_issuance_works() {
|
||||
fn thaw_when_issuance_lower_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
assert_ok!(Balances::transfer_allow_death(signed(2), 1, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 100, 1));
|
||||
enlarge(100, 1);
|
||||
|
||||
// Everybody else's balances goes down by 25%
|
||||
Balances::make_free_balance_be(&2, 75);
|
||||
Balances::make_free_balance_be(&3, 75);
|
||||
Balances::make_free_balance_be(&4, 75);
|
||||
assert_ok!(Balances::burn_from(&2, 25, Exact, Force));
|
||||
assert_ok!(Balances::burn_from(&3, 25, Exact, Force));
|
||||
assert_ok!(Balances::burn_from(&4, 25, Exact, Force));
|
||||
|
||||
run_to_block(4);
|
||||
assert_ok!(Nis::thaw_private(signed(1), 0, None));
|
||||
|
||||
assert_ok!(Balances::transfer_allow_death(signed(1), 2, 1));
|
||||
assert_eq!(Balances::free_balance(1), 75);
|
||||
assert_eq!(Balances::reserved_balance(1), 0);
|
||||
});
|
||||
@@ -649,15 +663,16 @@ fn thaw_when_issuance_lower_works() {
|
||||
fn multiple_thaws_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
assert_ok!(Balances::transfer_allow_death(signed(3), 1, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 40, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 60, 1));
|
||||
assert_ok!(Nis::place_bid(signed(2), 50, 1));
|
||||
enlarge(200, 3);
|
||||
|
||||
// Double everyone's free balances.
|
||||
Balances::make_free_balance_be(&2, 100);
|
||||
Balances::make_free_balance_be(&3, 200);
|
||||
Balances::make_free_balance_be(&4, 200);
|
||||
assert_ok!(Balances::mint_into(&2, 50));
|
||||
assert_ok!(Balances::mint_into(&3, 100));
|
||||
assert_ok!(Balances::mint_into(&4, 100));
|
||||
assert_ok!(Nis::fund_deficit(signed(1)));
|
||||
|
||||
run_to_block(4);
|
||||
@@ -667,8 +682,11 @@ fn multiple_thaws_works() {
|
||||
run_to_block(5);
|
||||
assert_ok!(Nis::thaw_private(signed(2), 2, None));
|
||||
|
||||
assert_ok!(Balances::transfer_allow_death(signed(1), 3, 1));
|
||||
assert_eq!(Balances::free_balance(1), 200);
|
||||
assert_eq!(Balances::free_balance(2), 200);
|
||||
assert_eq!(Balances::total_balance(&1), 200);
|
||||
assert_eq!(Balances::total_balance(&2), 200);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -676,15 +694,16 @@ fn multiple_thaws_works() {
|
||||
fn multiple_thaws_works_in_alternative_thaw_order() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
assert_ok!(Balances::transfer_allow_death(signed(3), 1, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 40, 1));
|
||||
assert_ok!(Nis::place_bid(signed(1), 60, 1));
|
||||
assert_ok!(Nis::place_bid(signed(2), 50, 1));
|
||||
enlarge(200, 3);
|
||||
|
||||
// Double everyone's free balances.
|
||||
Balances::make_free_balance_be(&2, 100);
|
||||
Balances::make_free_balance_be(&3, 200);
|
||||
Balances::make_free_balance_be(&4, 200);
|
||||
assert_ok!(Balances::mint_into(&2, 50));
|
||||
assert_ok!(Balances::mint_into(&3, 100));
|
||||
assert_ok!(Balances::mint_into(&4, 100));
|
||||
assert_ok!(Nis::fund_deficit(signed(1)));
|
||||
|
||||
run_to_block(4);
|
||||
@@ -695,8 +714,11 @@ fn multiple_thaws_works_in_alternative_thaw_order() {
|
||||
run_to_block(5);
|
||||
assert_ok!(Nis::thaw_private(signed(1), 1, None));
|
||||
|
||||
assert_ok!(Balances::transfer_allow_death(signed(1), 3, 1));
|
||||
assert_eq!(Balances::free_balance(1), 200);
|
||||
assert_eq!(Balances::free_balance(2), 200);
|
||||
assert_eq!(Balances::total_balance(&1), 200);
|
||||
assert_eq!(Balances::total_balance(&2), 200);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user