1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Module adapting `eq_balances::EqCurrency` to work as `BasicCurrency` in
//! Substrate runtime

#![cfg_attr(not(feature = "std"), no_std)]

pub use super::imbalances::{NegativeImbalance, PositiveImbalance};
use frame_support::traits::{Currency, Get};
use sp_std::marker;

/// Adapts [`EqCurrency`](../trait.EqCurrency.html) to work as `BasicCurrency`
/// in Substrate runtime
pub struct BalanceAdapter<T, R, CurrencyGetter>(marker::PhantomData<(T, R, CurrencyGetter)>);

impl<AccountId, T, R, CurrencyGetter> Currency<AccountId> for BalanceAdapter<T, R, CurrencyGetter>
where
    T: super::Trait,
    R: super::EqCurrency<AccountId, T::Balance>,
    CurrencyGetter: Get<eq_primitives::currency::Currency>,
{
    type Balance = T::Balance;
    type PositiveImbalance = PositiveImbalance<T::Balance>;
    type NegativeImbalance = NegativeImbalance<T::Balance>;
    fn total_balance(who: &AccountId) -> Self::Balance {
        R::total_balance(CurrencyGetter::get(), who)
    }
    fn can_slash(who: &AccountId, value: Self::Balance) -> bool {
        R::can_slash(CurrencyGetter::get(), who, value)
    }
    fn total_issuance() -> Self::Balance {
        R::currency_total_issuance(CurrencyGetter::get())
    }
    fn minimum_balance() -> Self::Balance {
        R::currency_minimum_balance(CurrencyGetter::get())
    }
    fn burn(amount: Self::Balance) -> Self::PositiveImbalance {
        R::burn(CurrencyGetter::get(), amount)
    }
    fn issue(amount: Self::Balance) -> Self::NegativeImbalance {
        R::issue(CurrencyGetter::get(), amount)
    }
    fn free_balance(who: &AccountId) -> Self::Balance {
        R::free_balance(CurrencyGetter::get(), who)
    }
    fn ensure_can_withdraw(
        who: &AccountId,
        _amount: Self::Balance,
        reasons: frame_support::traits::WithdrawReasons,
        new_balance: Self::Balance,
    ) -> sp_runtime::DispatchResult {
        R::ensure_can_withdraw(CurrencyGetter::get(), who, _amount, reasons, new_balance)
    }
    fn transfer(
        source: &AccountId,
        dest: &AccountId,
        value: Self::Balance,
        existence_requirement: frame_support::traits::ExistenceRequirement,
    ) -> sp_runtime::DispatchResult {
        R::currency_transfer(
            CurrencyGetter::get(),
            source,
            dest,
            value,
            existence_requirement,
            eq_primitives::TransferReason::Common,
            true,
        )
    }
    fn slash(who: &AccountId, value: Self::Balance) -> (Self::NegativeImbalance, Self::Balance) {
        R::slash(CurrencyGetter::get(), who, value)
    }
    fn deposit_into_existing(
        who: &AccountId,
        value: Self::Balance,
    ) -> Result<Self::PositiveImbalance, sp_runtime::DispatchError> {
        R::deposit_into_existing(CurrencyGetter::get(), who, value)
    }
    fn deposit_creating(who: &AccountId, value: Self::Balance) -> Self::PositiveImbalance {
        R::deposit_creating(CurrencyGetter::get(), who, value, true)
    }
    fn withdraw(
        who: &AccountId,
        value: Self::Balance,
        reasons: frame_support::traits::WithdrawReasons,
        liveness: frame_support::traits::ExistenceRequirement,
    ) -> Result<Self::NegativeImbalance, sp_runtime::DispatchError> {
        R::withdraw(CurrencyGetter::get(), who, value, reasons, liveness, true)
    }
    fn make_free_balance_be(
        who: &AccountId,
        balance: Self::Balance,
    ) -> frame_support::traits::SignedImbalance<Self::Balance, Self::PositiveImbalance> {
        R::make_free_balance_be(CurrencyGetter::get(), who, balance)
    }
}