Skip to content

Commit

Permalink
Implement necessary Balanced types (#931)
Browse files Browse the repository at this point in the history
* implement Balanced

* implement OnDropDebt and OnDropCredit

* add done events

* add tests

* use existing events and improve tests

* rename asset to currency_id

* rename to currency id
  • Loading branch information
Chralt98 authored Jul 6, 2023
1 parent 28a2e6f commit 7ecebea
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ pub mod module {
who: T::AccountId,
amount: T::Balance,
},
Issued {
currency_id: T::CurrencyId,
amount: T::Balance,
},
Rescinded {
currency_id: T::CurrencyId,
amount: T::Balance,
},
}

/// The total issuance of a token type.
Expand Down Expand Up @@ -1928,6 +1936,32 @@ impl<T: Config> fungibles::Unbalanced<T::AccountId> for Pallet<T> {
}
}

impl<T: Config> fungibles::Balanced<T::AccountId> for Pallet<T> {
type OnDropDebt = fungibles::IncreaseIssuance<T::AccountId, Self>;
type OnDropCredit = fungibles::DecreaseIssuance<T::AccountId, Self>;

fn done_deposit(currency_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance) {
Self::deposit_event(Event::Deposited {
currency_id,
who: who.clone(),
amount,
});
}
fn done_withdraw(currency_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance) {
Self::deposit_event(Event::Withdrawn {
currency_id,
who: who.clone(),
amount,
});
}
fn done_issue(currency_id: Self::AssetId, amount: Self::Balance) {
Self::deposit_event(Event::Issued { currency_id, amount });
}
fn done_rescind(currency_id: Self::AssetId, amount: Self::Balance) {
Self::deposit_event(Event::Rescinded { currency_id, amount });
}
}

type ReasonOf<P, T> = <P as fungibles::InspectHold<<T as frame_system::Config>::AccountId>>::Reason;
impl<T: Config> fungibles::InspectHold<T::AccountId> for Pallet<T> {
type Reason = ();
Expand Down
96 changes: 96 additions & 0 deletions tokens/src/tests_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,102 @@ fn fungibles_unbalanced_trait_should_work() {
});
}

#[test]
fn fungibles_balanced_deposit_works() {
ExtBuilder::default()
.balances(vec![(ALICE, DOT, 100)])
.build()
.execute_with(|| {
let amount = 42;
let alice_old_balance = <Tokens as fungibles::Inspect<_>>::balance(DOT, &ALICE);
let debt = <Tokens as fungibles::Balanced<_>>::deposit(DOT, &ALICE, amount, Precision::Exact).unwrap();
assert_eq!(debt.asset(), DOT);
assert_eq!(debt.peek(), amount);
let alice_new_balance = <Tokens as fungibles::Inspect<_>>::balance(DOT, &ALICE);
assert_eq!(alice_old_balance + amount, alice_new_balance);

System::assert_last_event(RuntimeEvent::Tokens(crate::Event::Deposited {
currency_id: DOT,
who: ALICE,
amount,
}));
});
}

#[test]
fn fungibles_balanced_withdraw_works() {
ExtBuilder::default()
.balances(vec![(ALICE, DOT, 100)])
.build()
.execute_with(|| {
let amount = 42;
let alice_old_balance = <Tokens as fungibles::Inspect<_>>::balance(DOT, &ALICE);
let credit = <Tokens as fungibles::Balanced<_>>::withdraw(
DOT,
&ALICE,
amount,
Precision::Exact,
Preservation::Protect,
Fortitude::Polite,
)
.unwrap();
assert_eq!(credit.asset(), DOT);
assert_eq!(credit.peek(), amount);
let alice_new_balance = <Tokens as fungibles::Inspect<_>>::balance(DOT, &ALICE);
assert_eq!(alice_old_balance - amount, alice_new_balance);

System::assert_last_event(RuntimeEvent::Tokens(crate::Event::Withdrawn {
currency_id: DOT,
who: ALICE,
amount,
}));
});
}

#[test]
fn fungibles_balanced_issue_works() {
ExtBuilder::default()
.balances(vec![(ALICE, DOT, 100)])
.build()
.execute_with(|| {
let amount = 42;

let old_total_issuance = <Tokens as fungibles::Inspect<_>>::total_issuance(DOT);
let credit = <Tokens as fungibles::Balanced<_>>::issue(DOT, amount);
assert_eq!(credit.asset(), DOT);
assert_eq!(credit.peek(), amount);
let new_total_issuance = <Tokens as fungibles::Inspect<_>>::total_issuance(DOT);
assert_eq!(old_total_issuance + amount, new_total_issuance);

System::assert_last_event(RuntimeEvent::Tokens(crate::Event::Issued {
currency_id: DOT,
amount,
}));
});
}

#[test]
fn fungibles_balanced_rescind_works() {
ExtBuilder::default()
.balances(vec![(ALICE, DOT, 100)])
.build()
.execute_with(|| {
let amount = 42;

let old_total_issuance = <Tokens as fungibles::Inspect<_>>::total_issuance(DOT);
let debt = <Tokens as fungibles::Balanced<_>>::rescind(DOT, amount);
assert_eq!(debt.asset(), DOT);
assert_eq!(debt.peek(), amount);
let new_total_issuance = <Tokens as fungibles::Inspect<_>>::total_issuance(DOT);
assert_eq!(old_total_issuance - amount, new_total_issuance);

System::assert_last_event(RuntimeEvent::Tokens(crate::Event::Rescinded {
currency_id: DOT,
amount,
}));
});
}

#[test]
fn fungibles_inspect_hold_trait_should_work() {
ExtBuilder::default()
Expand Down

0 comments on commit 7ecebea

Please sign in to comment.