Skip to content

Commit 734c2ed

Browse files
committed
use asset id as an argument
1 parent 0e83096 commit 734c2ed

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

tokens/src/impls.rs

+49-15
Original file line numberDiff line numberDiff line change
@@ -121,46 +121,64 @@ where
121121
}
122122

123123
pub trait ConvertBalance<A, B> {
124-
fn convert_balance(amount: A) -> B;
125-
fn convert_balance_back(amount: B) -> A;
124+
type AssetId;
125+
fn convert_balance(amount: A, asset_id: Self::AssetId) -> B;
126+
fn convert_balance_back(amount: B, asset_id: Self::AssetId) -> A;
126127
}
127128

128129
pub struct Mapper<AccountId, T, C, B, GetCurrencyId>(sp_std::marker::PhantomData<(AccountId, T, C, B, GetCurrencyId)>);
129130
impl<AccountId, T, C, B, GetCurrencyId> fungible::Inspect<AccountId> for Mapper<AccountId, T, C, B, GetCurrencyId>
130131
where
131132
T: fungibles::Inspect<AccountId>,
132-
C: ConvertBalance<<T as fungibles::Inspect<AccountId>>::Balance, B>,
133+
C: ConvertBalance<
134+
<T as fungibles::Inspect<AccountId>>::Balance,
135+
B,
136+
AssetId = <T as fungibles::Inspect<AccountId>>::AssetId,
137+
>,
133138
// TOOD: use trait Balance after https://github.com/paritytech/substrate/pull/9863 is available
134139
B: AtLeast32BitUnsigned + FullCodec + Copy + Default + Debug,
135140
GetCurrencyId: Get<<T as fungibles::Inspect<AccountId>>::AssetId>,
136141
{
137142
type Balance = B;
138143

139144
fn total_issuance() -> Self::Balance {
140-
C::convert_balance(T::total_issuance(GetCurrencyId::get()))
145+
C::convert_balance(T::total_issuance(GetCurrencyId::get()), GetCurrencyId::get())
141146
}
142147

143148
fn minimum_balance() -> Self::Balance {
144-
C::convert_balance(T::minimum_balance(GetCurrencyId::get()))
149+
C::convert_balance(T::minimum_balance(GetCurrencyId::get()), GetCurrencyId::get())
145150
}
146151

147152
fn balance(who: &AccountId) -> Self::Balance {
148-
C::convert_balance(T::balance(GetCurrencyId::get(), who))
153+
C::convert_balance(T::balance(GetCurrencyId::get(), who), GetCurrencyId::get())
149154
}
150155

151156
fn reducible_balance(who: &AccountId, keep_alive: bool) -> Self::Balance {
152-
C::convert_balance(T::reducible_balance(GetCurrencyId::get(), who, keep_alive))
157+
C::convert_balance(
158+
T::reducible_balance(GetCurrencyId::get(), who, keep_alive),
159+
GetCurrencyId::get(),
160+
)
153161
}
154162

155163
fn can_deposit(who: &AccountId, amount: Self::Balance) -> DepositConsequence {
156-
T::can_deposit(GetCurrencyId::get(), who, C::convert_balance_back(amount))
164+
T::can_deposit(
165+
GetCurrencyId::get(),
166+
who,
167+
C::convert_balance_back(amount, GetCurrencyId::get()),
168+
)
157169
}
158170

159171
fn can_withdraw(who: &AccountId, amount: Self::Balance) -> WithdrawConsequence<Self::Balance> {
160172
use WithdrawConsequence::*;
161-
let res = T::can_withdraw(GetCurrencyId::get(), who, C::convert_balance_back(amount));
173+
let res = T::can_withdraw(
174+
GetCurrencyId::get(),
175+
who,
176+
C::convert_balance_back(amount, GetCurrencyId::get()),
177+
);
162178
match res {
163-
WithdrawConsequence::ReducedToZero(b) => WithdrawConsequence::ReducedToZero(C::convert_balance(b)),
179+
WithdrawConsequence::ReducedToZero(b) => {
180+
WithdrawConsequence::ReducedToZero(C::convert_balance(b, GetCurrencyId::get()))
181+
}
164182
NoFunds => NoFunds,
165183
WouldDie => WouldDie,
166184
UnknownAsset => UnknownAsset,
@@ -175,7 +193,11 @@ where
175193
impl<AccountId, T, C, B, GetCurrencyId> fungible::Transfer<AccountId> for Mapper<AccountId, T, C, B, GetCurrencyId>
176194
where
177195
T: fungibles::Transfer<AccountId, Balance = B>,
178-
C: ConvertBalance<<T as fungibles::Inspect<AccountId>>::Balance, B>,
196+
C: ConvertBalance<
197+
<T as fungibles::Inspect<AccountId>>::Balance,
198+
B,
199+
AssetId = <T as fungibles::Inspect<AccountId>>::AssetId,
200+
>,
179201
// TOOD: use trait Balance after https://github.com/paritytech/substrate/pull/9863 is available
180202
B: AtLeast32BitUnsigned + FullCodec + Copy + Default + Debug,
181203
GetCurrencyId: Get<<T as fungibles::Inspect<AccountId>>::AssetId>,
@@ -185,7 +207,7 @@ where
185207
GetCurrencyId::get(),
186208
source,
187209
dest,
188-
C::convert_balance_back(amount),
210+
C::convert_balance_back(amount, GetCurrencyId::get()),
189211
keep_alive,
190212
)
191213
}
@@ -194,16 +216,28 @@ where
194216
impl<AccountId, T, C, B, GetCurrencyId> fungible::Mutate<AccountId> for Mapper<AccountId, T, C, B, GetCurrencyId>
195217
where
196218
T: fungibles::Mutate<AccountId, Balance = B>,
197-
C: ConvertBalance<<T as fungibles::Inspect<AccountId>>::Balance, B>,
219+
C: ConvertBalance<
220+
<T as fungibles::Inspect<AccountId>>::Balance,
221+
B,
222+
AssetId = <T as fungibles::Inspect<AccountId>>::AssetId,
223+
>,
198224
// TOOD: use trait Balance after https://github.com/paritytech/substrate/pull/9863 is available
199225
B: AtLeast32BitUnsigned + FullCodec + Copy + Default + Debug,
200226
GetCurrencyId: Get<<T as fungibles::Inspect<AccountId>>::AssetId>,
201227
{
202228
fn mint_into(dest: &AccountId, amount: Self::Balance) -> DispatchResult {
203-
T::mint_into(GetCurrencyId::get(), dest, C::convert_balance_back(amount))
229+
T::mint_into(
230+
GetCurrencyId::get(),
231+
dest,
232+
C::convert_balance_back(amount, GetCurrencyId::get()),
233+
)
204234
}
205235

206236
fn burn_from(dest: &AccountId, amount: Self::Balance) -> Result<Self::Balance, DispatchError> {
207-
T::burn_from(GetCurrencyId::get(), dest, C::convert_balance_back(amount))
237+
T::burn_from(
238+
GetCurrencyId::get(),
239+
dest,
240+
C::convert_balance_back(amount, GetCurrencyId::get()),
241+
)
208242
}
209243
}

tokens/src/tests.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -2084,11 +2084,12 @@ fn fungibles_mutate_hold_trait_should_work() {
20842084
fn fungibles_inspect_convert_should_work() {
20852085
pub struct ConvertBalanceTest;
20862086
impl ConvertBalance<Balance, Balance> for ConvertBalanceTest {
2087-
fn convert_balance(balance: Balance) -> Balance {
2087+
type AssetId = CurrencyId;
2088+
fn convert_balance(balance: Balance, _asset_id: CurrencyId) -> Balance {
20882089
balance * 100
20892090
}
20902091

2091-
fn convert_balance_back(balance: Balance) -> Balance {
2092+
fn convert_balance_back(balance: Balance, _asset_id: CurrencyId) -> Balance {
20922093
balance / 100
20932094
}
20942095
}
@@ -2133,11 +2134,12 @@ fn fungibles_inspect_convert_should_work() {
21332134
fn fungibles_transfers_convert_should_work() {
21342135
pub struct ConvertBalanceTest;
21352136
impl ConvertBalance<Balance, Balance> for ConvertBalanceTest {
2136-
fn convert_balance(balance: Balance) -> Balance {
2137+
type AssetId = CurrencyId;
2138+
fn convert_balance(balance: Balance, _asset_id: CurrencyId) -> Balance {
21372139
balance * 100
21382140
}
21392141

2140-
fn convert_balance_back(balance: Balance) -> Balance {
2142+
fn convert_balance_back(balance: Balance, _asset_id: CurrencyId) -> Balance {
21412143
balance / 100
21422144
}
21432145
}
@@ -2185,11 +2187,12 @@ fn fungibles_transfers_convert_should_work() {
21852187
fn fungibles_mutate_convert_should_work() {
21862188
pub struct ConvertBalanceTest;
21872189
impl ConvertBalance<Balance, Balance> for ConvertBalanceTest {
2188-
fn convert_balance(balance: Balance) -> Balance {
2190+
type AssetId = CurrencyId;
2191+
fn convert_balance(balance: Balance, _asset_id: CurrencyId) -> Balance {
21892192
balance * 100
21902193
}
21912194

2192-
fn convert_balance_back(balance: Balance) -> Balance {
2195+
fn convert_balance_back(balance: Balance, _asset_id: CurrencyId) -> Balance {
21932196
balance / 100
21942197
}
21952198
}

0 commit comments

Comments
 (0)