Skip to content

Commit be4e526

Browse files
committed
xtokens consume rate-limiter by CurrencyId
1 parent 9d70e37 commit be4e526

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

xtokens/src/lib.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ pub mod module {
144144
type CurrencyId: Parameter + Member + Clone;
145145

146146
/// Convert `T::CurrencyId` to `Location`.
147-
type CurrencyIdConvert: Convert<Self::CurrencyId, Option<Location>>;
147+
type CurrencyIdConvert: Convert<Self::CurrencyId, Option<Location>>
148+
+ Convert<Location, Option<Self::CurrencyId>>;
148149

149150
/// Convert `T::AccountId` to `Location`.
150151
type AccountIdToLocation: Convert<Self::AccountId, Location>;
@@ -468,25 +469,27 @@ pub mod module {
468469
for i in 0..asset_len {
469470
let asset = assets.get(i).ok_or(Error::<T>::AssetIndexNonExistent)?;
470471

471-
// per asset check
472-
let amount = match asset.fun {
473-
Fungibility::Fungible(amount) => amount,
474-
Fungibility::NonFungible(_) => 1,
475-
};
476-
477-
// try consume quota of the rate limiter.
478-
// NOTE: use AssetId as the key, use AccountId as whitelist filter key.
479-
match T::RateLimiter::try_consume(rate_limiter_id, asset.id.clone(), amount, Some(who)) {
480-
Ok(_) => {}
481-
Err(_e @ RateLimiterError::Deny) => {
482-
return Err(Error::<T>::RateLimiterDeny.into());
483-
}
484-
Err(ref _e @ RateLimiterError::Delay { duration }) => {
485-
if duration > need_delay.unwrap_or_default() {
486-
need_delay = Some(duration);
472+
if let Some(currency_id) = T::CurrencyIdConvert::convert(asset.id.0.clone()) {
473+
// per asset check
474+
let amount = match asset.fun {
475+
Fungibility::Fungible(amount) => amount,
476+
Fungibility::NonFungible(_) => 1,
477+
};
478+
479+
// try consume quota of the rate limiter.
480+
// NOTE: use CurrencyId as the key, use AccountId as whitelist filter key.
481+
match T::RateLimiter::try_consume(rate_limiter_id, currency_id, amount, Some(who)) {
482+
Ok(_) => {}
483+
Err(_e @ RateLimiterError::Deny) => {
484+
return Err(Error::<T>::RateLimiterDeny.into());
487485
}
488-
}
489-
};
486+
Err(ref _e @ RateLimiterError::Delay { duration }) => {
487+
if duration > need_delay.unwrap_or_default() {
488+
need_delay = Some(duration);
489+
}
490+
}
491+
};
492+
}
490493
}
491494

492495
Ok(need_delay)

xtokens/src/mock/para.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,9 @@ impl RateLimiter<BlockNumberFor<Runtime>> for MockRateLimiter {
248248
value: u128,
249249
) -> Result<(), RateLimiterError<BlockNumberFor<Runtime>>> {
250250
let encoded_limit_key = limit_key.encode();
251-
let r_multi_location: Location = CurrencyIdConvert::convert(CurrencyId::R).unwrap();
252-
let r_asset_id = AssetId(r_multi_location);
253-
let encoded_r_asset_id = r_asset_id.encode();
251+
let encoded_currency_id = CurrencyId::R.encode();
254252

255-
if encoded_limit_key == encoded_r_asset_id {
253+
if encoded_limit_key == encoded_currency_id {
256254
let accumulation = R_ACCUMULATION.with(|v| *v.borrow());
257255
ensure!((accumulation + value) <= 2000, RateLimiterError::Deny);
258256
}
@@ -262,11 +260,9 @@ impl RateLimiter<BlockNumberFor<Runtime>> for MockRateLimiter {
262260

263261
fn consume(_: Self::RateLimiterId, limit_key: impl Encode, value: u128) {
264262
let encoded_limit_key = limit_key.encode();
265-
let r_multi_location: Location = CurrencyIdConvert::convert(CurrencyId::R).unwrap();
266-
let r_asset_id = AssetId(r_multi_location);
267-
let encoded_r_asset_id = r_asset_id.encode();
263+
let encoded_currency_id = CurrencyId::R.encode();
268264

269-
if encoded_limit_key == encoded_r_asset_id {
265+
if encoded_limit_key == encoded_currency_id {
270266
R_ACCUMULATION.with(|v| *v.borrow_mut() += value);
271267
}
272268
}

0 commit comments

Comments
 (0)