Skip to content

Implement necessary Balanced types #931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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