Implement fungible::* for Balances (#8454)

* Reservable, Transferrable Fungible(s), plus adapters.

* Repot into new dir

* Imbalances for Fungibles

* Repot and balanced fungible.

* Clean up names and bridge-over Imbalanced.

* Repot frame_support::trait. Finally.

* Make build.

* Docs

* Good errors

* Fix tests. Implement fungible::Inspect for Balances.

* Implement additional traits for Balances.

* Revert UI test "fixes"

* Fix UI error

* Fix UI test

* More work on fungibles

* Fixes

* More work.

* Update lock

* Make fungible::reserved work for Balances

* Introduce Freezer to Assets, ready for a reserve & locks pallet. Some renaming/refactoring.

* Cleanup errors

* Imbalances working with Assets

* Test for freezer.

* Grumbles

* Grumbles

* Fixes

* Extra "side-car" data for a user's asset balance.

* Fix

* Fix test

* Fixes

* Line lengths

* Comments

* Update frame/assets/src/tests.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/support/src/traits/tokens/fungibles.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/assets/src/lib.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/support/src/traits/tokens/fungible.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Introduce `transfer_reserved`

* Rename fungible Reserve -> Hold, add flag structs

* Avoid the `melted` API - its too complex and gives little help

* Repot Assets pallet

Co-authored-by: Bastian Köcher <info@kchr.de>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Gavin Wood
2021-03-28 20:59:34 +02:00
committed by GitHub
parent c2dd5e21a4
commit d0eee4f1cb
17 changed files with 1537 additions and 554 deletions
+75 -10
View File
@@ -19,6 +19,7 @@
use super::*;
use crate::{Error, mock::*};
use sp_runtime::TokenError;
use frame_support::{assert_ok, assert_noop, traits::Currency};
use pallet_balances::Error as BalancesError;
@@ -198,11 +199,11 @@ fn non_providing_should_work() {
assert_ok!(Assets::mint(Origin::signed(1), 0, 0, 100));
// Cannot mint into account 2 since it doesn't (yet) exist...
assert_noop!(Assets::mint(Origin::signed(1), 0, 1, 100), Error::<Test>::NoProvider);
assert_noop!(Assets::mint(Origin::signed(1), 0, 1, 100), TokenError::CannotCreate);
// ...or transfer...
assert_noop!(Assets::transfer(Origin::signed(0), 0, 1, 50), Error::<Test>::NoProvider);
assert_noop!(Assets::transfer(Origin::signed(0), 0, 1, 50), TokenError::CannotCreate);
// ...or force-transfer
assert_noop!(Assets::force_transfer(Origin::signed(1), 0, 0, 1, 50), Error::<Test>::NoProvider);
assert_noop!(Assets::force_transfer(Origin::signed(1), 0, 0, 1, 50), TokenError::CannotCreate);
Balances::make_free_balance_be(&1, 100);
Balances::make_free_balance_be(&2, 100);
@@ -219,12 +220,11 @@ fn min_balance_should_work() {
assert_eq!(Asset::<Test>::get(0).unwrap().accounts, 1);
// Cannot create a new account with a balance that is below minimum...
assert_noop!(Assets::mint(Origin::signed(1), 0, 2, 9), Error::<Test>::BalanceLow);
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 9), Error::<Test>::BalanceLow);
assert_noop!(Assets::force_transfer(Origin::signed(1), 0, 1, 2, 9), Error::<Test>::BalanceLow);
assert_noop!(Assets::mint(Origin::signed(1), 0, 2, 9), TokenError::BelowMinimum);
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 9), TokenError::BelowMinimum);
assert_noop!(Assets::force_transfer(Origin::signed(1), 0, 1, 2, 9), TokenError::BelowMinimum);
// When deducting from an account to below minimum, it should be reaped.
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 91));
assert!(Assets::balance(0, 1).is_zero());
assert_eq!(Assets::balance(0, 2), 100);
@@ -277,7 +277,7 @@ fn transferring_enough_to_kill_source_when_keep_alive_should_fail() {
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 10));
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_noop!(Assets::transfer_keep_alive(Origin::signed(1), 0, 2, 91), Error::<Test>::WouldDie);
assert_noop!(Assets::transfer_keep_alive(Origin::signed(1), 0, 2, 91), Error::<Test>::BalanceLow);
assert_ok!(Assets::transfer_keep_alive(Origin::signed(1), 0, 2, 90));
assert_eq!(Assets::balance(0, 1), 10);
assert_eq!(Assets::balance(0, 2), 90);
@@ -430,12 +430,14 @@ fn burning_asset_balance_with_positive_balance_should_work() {
}
#[test]
fn burning_asset_balance_with_zero_balance_should_not_work() {
fn burning_asset_balance_with_zero_balance_does_nothing() {
new_test_ext().execute_with(|| {
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 1));
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
assert_eq!(Assets::balance(0, 2), 0);
assert_noop!(Assets::burn(Origin::signed(1), 0, 2, u64::max_value()), Error::<Test>::BalanceZero);
assert_ok!(Assets::burn(Origin::signed(1), 0, 2, u64::max_value()));
assert_eq!(Assets::balance(0, 2), 0);
assert_eq!(Assets::total_supply(0), 100);
});
}
@@ -491,3 +493,66 @@ fn set_metadata_should_work() {
}
// TODO: tests for force_set_metadata, force_clear_metadata, force_asset_status
// https://github.com/paritytech/substrate/issues/8470
#[test]
fn freezer_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 10));
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
assert_eq!(Assets::balance(0, 1), 100);
// freeze 50 of it.
set_frozen_balance(0, 1, 50);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 20));
// cannot transfer another 21 away as this would take the non-frozen balance (30) to below
// the minimum balance (10).
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 21), Error::<Test>::BalanceLow);
// create an approved transfer...
Balances::make_free_balance_be(&1, 100);
assert_ok!(Assets::approve_transfer(Origin::signed(1), 0, 2, 50));
let e = Error::<Test>::BalanceLow;
// ...but that wont work either:
assert_noop!(Assets::transfer_approved(Origin::signed(2), 0, 1, 2, 21), e);
// a force transfer won't work also.
let e = Error::<Test>::BalanceLow;
assert_noop!(Assets::force_transfer(Origin::signed(1), 0, 1, 2, 21), e);
// reduce it to only 49 frozen...
set_frozen_balance(0, 1, 49);
// ...and it's all good:
assert_ok!(Assets::force_transfer(Origin::signed(1), 0, 1, 2, 21));
// and if we clear it, we can remove the account completely.
clear_frozen_balance(0, 1);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
assert_eq!(hooks(), vec![Hook::Died(0, 1)]);
});
}
#[test]
fn imbalances_should_work() {
use frame_support::traits::tokens::fungibles::Balanced;
new_test_ext().execute_with(|| {
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 1));
let imb = Assets::issue(0, 100);
assert_eq!(Assets::total_supply(0), 100);
assert_eq!(imb.peek(), 100);
let (imb1, imb2) = imb.split(30);
assert_eq!(imb1.peek(), 30);
assert_eq!(imb2.peek(), 70);
drop(imb2);
assert_eq!(Assets::total_supply(0), 30);
assert!(Assets::resolve(&1, imb1).is_ok());
assert_eq!(Assets::balance(0, 1), 30);
assert_eq!(Assets::total_supply(0), 30);
});
}