git.delta.rocks / unique-network / refs/commits / feed42593483

difftreelog

Foreign assets pallet fixes

Dev2022-09-13parent: #fd1272e.patch.diff
in: master

4 files changed

modifiedpallets/foreign-assets/src/impl_fungibles.rsdiffbeforeafterboth
26use up_data_structs::budget::Unlimited;26use up_data_structs::budget::Unlimited;
27use sp_runtime::traits::{CheckedAdd, CheckedSub};27use sp_runtime::traits::{CheckedAdd, CheckedSub};
28
29// type BalanceSelf<T> =
30// <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
31type BalanceRelay<T> =
32 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
2833
29impl<T: Config> fungibles::Inspect<<T as SystemConfig>::AccountId> for Pallet<T>34impl<T: Config> fungibles::Inspect<<T as SystemConfig>::AccountId> for Pallet<T>
30where35where
31 T: orml_tokens::Config<CurrencyId = AssetIds>,36 T: orml_tokens::Config<CurrencyId = AssetIds>,
37 BalanceRelay<T>: From<BalanceOf<T>>,
38 BalanceOf<T>: From<BalanceRelay<T>>,
39 BalanceRelay<T>: From<<T as pallet_balances::Config>::Balance>,
40 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,
41 <T as pallet_balances::Config>::Balance: From<BalanceRelay<T>>,
42 <T as orml_tokens::Config>::Balance: From<BalanceRelay<T>>,
32{43{
33 type AssetId = AssetIds;44 type AssetId = AssetIds;
34 type Balance = BalanceOf<T>;45 type Balance = BalanceOf<T>;
3849
39 match asset {50 match asset {
40 AssetIds::NativeAssetId(NativeCurrency::Here) => {51 AssetIds::NativeAssetId(NativeCurrency::Here) => {
41 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<52 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::total_issuance()
42 T::AccountId,53 .into()
43 >>::total_issuance();
44
45 let value: u128 = match parent_amount.try_into() {
46 Ok(val) => val,
47 Err(_) => return Zero::zero(),
48 };
49
50 let ti: Self::Balance = match value.try_into() {
51 Ok(val) => val,
52 Err(_) => return Zero::zero(),
53 };
54
55 ti
56 }54 }
57 AssetIds::NativeAssetId(NativeCurrency::Parent) => {55 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
58 let amount =
59 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::total_issuance(56 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::total_issuance(
60 AssetIds::NativeAssetId(NativeCurrency::Parent),57 AssetIds::NativeAssetId(NativeCurrency::Parent),
61 );58 )
62
63 let value: u128 = match amount.try_into() {59 .into()
64 Ok(val) => val,
65 Err(_) => return Zero::zero(),
66 };
67
68 let ti: Self::Balance = match value.try_into() {
69 Ok(val) => val,
70 Err(_) => return Zero::zero(),
71 };
72
73 ti
74 }60 }
75 AssetIds::ForeignAssetId(fid) => {61 AssetIds::ForeignAssetId(fid) => {
76 let target_collection_id = match <AssetBinding<T>>::get(fid) {62 let target_collection_id = match <AssetBinding<T>>::get(fid) {
91 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible minimum_balance");77 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible minimum_balance");
92 match asset {78 match asset {
93 AssetIds::NativeAssetId(NativeCurrency::Here) => {79 AssetIds::NativeAssetId(NativeCurrency::Here) => {
94 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<80 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::minimum_balance()
95 T::AccountId,81 .into()
96 >>::minimum_balance();
97
98 let value: u128 = match parent_amount.try_into() {
99 Ok(val) => val,
100 Err(_) => return Zero::zero(),
101 };
102
103 let ti: Self::Balance = match value.try_into() {
104 Ok(val) => val,
105 Err(_) => return Zero::zero(),
106 };
107
108 ti
109 }82 }
110 AssetIds::NativeAssetId(NativeCurrency::Parent) => {83 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
111 let amount =
112 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::minimum_balance(84 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::minimum_balance(
113 AssetIds::NativeAssetId(NativeCurrency::Parent),85 AssetIds::NativeAssetId(NativeCurrency::Parent),
114 );86 )
115
116 let value: u128 = match amount.try_into() {87 .into()
117 Ok(val) => val,
118 Err(_) => return Zero::zero(),
119 };
120
121 let ti: Self::Balance = match value.try_into() {
122 Ok(val) => val,
123 Err(_) => return Zero::zero(),
124 };
125
126 ti
127 }88 }
128 AssetIds::ForeignAssetId(fid) => {89 AssetIds::ForeignAssetId(fid) => {
129 AssetMetadatas::<T>::get(AssetIds::ForeignAssetId(fid))90 AssetMetadatas::<T>::get(AssetIds::ForeignAssetId(fid))
137 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible balance");98 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible balance");
138 match asset {99 match asset {
139 AssetIds::NativeAssetId(NativeCurrency::Here) => {100 AssetIds::NativeAssetId(NativeCurrency::Here) => {
140 let parent_amount =
141 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::balance(who);101 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::balance(who).into()
142
143 let value: u128 = match parent_amount.try_into() {
144 Ok(val) => val,
145 Err(_) => return Zero::zero(),
146 };
147
148 let ti: Self::Balance = match value.try_into() {
149 Ok(val) => val,
150 Err(_) => return Zero::zero(),
151 };
152
153 ti
154 }102 }
155 AssetIds::NativeAssetId(NativeCurrency::Parent) => {103 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
156 let amount = <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::balance(104 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::balance(
157 AssetIds::NativeAssetId(NativeCurrency::Parent),105 AssetIds::NativeAssetId(NativeCurrency::Parent),
158 who,106 who,
159 );107 )
160
161 let value: u128 = match amount.try_into() {108 .into()
162 Ok(val) => val,
163 Err(_) => return Zero::zero(),
164 };
165
166 let ti: Self::Balance = match value.try_into() {
167 Ok(val) => val,
168 Err(_) => return Zero::zero(),
169 };
170
171 ti
172 }109 }
173 AssetIds::ForeignAssetId(fid) => {110 AssetIds::ForeignAssetId(fid) => {
174 let target_collection_id = match <AssetBinding<T>>::get(fid) {111 let target_collection_id = match <AssetBinding<T>>::get(fid) {
197134
198 match asset {135 match asset {
199 AssetIds::NativeAssetId(NativeCurrency::Here) => {136 AssetIds::NativeAssetId(NativeCurrency::Here) => {
200 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<137 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::reducible_balance(
201 T::AccountId,
202 >>::reducible_balance(who, keep_alive);138 who, keep_alive,
203139 )
204 let value: u128 = match parent_amount.try_into() {140 .into()
205 Ok(val) => val,
206 Err(_) => return Zero::zero(),
207 };
208
209 let ti: Self::Balance = match value.try_into() {
210 Ok(val) => val,
211 Err(_) => return Zero::zero(),
212 };
213
214 ti
215 }141 }
216 AssetIds::NativeAssetId(NativeCurrency::Parent) => {142 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
217 let amount =
218 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(143 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(
219 AssetIds::NativeAssetId(NativeCurrency::Parent),144 AssetIds::NativeAssetId(NativeCurrency::Parent),
220 who,145 who,
221 keep_alive,146 keep_alive,
222 );147 )
223
224 let value: u128 = match amount.try_into() {148 .into()
225 Ok(val) => val,
226 Err(_) => return Zero::zero(),
227 };
228
229 let ti: Self::Balance = match value.try_into() {
230 Ok(val) => val,
231 Err(_) => return Zero::zero(),
232 };
233
234 ti
235 }149 }
236 _ => Self::balance(asset, who),150 _ => Self::balance(asset, who),
237 }151 }
245 ) -> DepositConsequence {159 ) -> DepositConsequence {
246 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_deposit");160 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_deposit");
247
248 let value: u128 = match amount.try_into() {
249 Ok(val) => val,
250 Err(_) => return DepositConsequence::CannotCreate,
251 };
252161
253 match asset {162 match asset {
254 AssetIds::NativeAssetId(NativeCurrency::Here) => {163 AssetIds::NativeAssetId(NativeCurrency::Here) => {
255 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {
256 Ok(val) => val,
257 Err(_) => {
258 return DepositConsequence::CannotCreate;
259 }
260 };
261 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_deposit(164 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_deposit(
262 who,165 who,
263 this_amount,166 amount.into(),
264 mint,167 mint,
265 )168 )
266 }169 }
267 AssetIds::NativeAssetId(NativeCurrency::Parent) => {170 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
268 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {
269 Ok(val) => val,
270 Err(_) => {
271 return DepositConsequence::CannotCreate;
272 }
273 };
274 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_deposit(171 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_deposit(
275 AssetIds::NativeAssetId(NativeCurrency::Parent),172 AssetIds::NativeAssetId(NativeCurrency::Parent),
276 who,173 who,
277 parent_amount,174 amount.into(),
278 mint,175 mint,
279 )176 )
280 }177 }
373impl<T: Config> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T>270impl<T: Config> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T>
374where271where
375 T: orml_tokens::Config<CurrencyId = AssetIds>,272 T: orml_tokens::Config<CurrencyId = AssetIds>,
273 BalanceRelay<T>: From<BalanceOf<T>>,
274 BalanceOf<T>: From<BalanceRelay<T>>,
275 BalanceRelay<T>: From<<T as pallet_balances::Config>::Balance>,
276 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,
277 <T as pallet_balances::Config>::Balance: From<BalanceRelay<T>>,
278 <T as orml_tokens::Config>::Balance: From<BalanceRelay<T>>,
279 u128: From<BalanceRelay<T>>,
376{280{
377 fn mint_into(281 fn mint_into(
378 asset: Self::AssetId,282 asset: Self::AssetId,
382 //Self::do_mint(asset, who, amount, None)286 //Self::do_mint(asset, who, amount, None)
383 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible mint_into {:?}", asset);287 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible mint_into {:?}", asset);
384
385 let value: u128 = match amount.try_into() {
386 Ok(val) => val,
387 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),
388 };
389288
390 match asset {289 match asset {
391 AssetIds::NativeAssetId(NativeCurrency::Here) => {290 AssetIds::NativeAssetId(NativeCurrency::Here) => {
392 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {
393 Ok(val) => val,
394 Err(_) => {
395 return Err(DispatchError::Other(
396 "Bad amount to this parachain value conversion",
397 ))
398 }
399 };
400
401 <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::mint_into(291 <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::mint_into(
402 who,292 who,
403 this_amount,293 amount.into(),
404 )294 )
295 .into()
405 }296 }
406 AssetIds::NativeAssetId(NativeCurrency::Parent) => {297 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
407 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {
408 Ok(val) => val,
409 Err(_) => {
410 return Err(DispatchError::Other(
411 "Bad amount to relay chain value conversion",
412 ))
413 }
414 };
415
416 <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::mint_into(298 <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::mint_into(
417 AssetIds::NativeAssetId(NativeCurrency::Parent),299 AssetIds::NativeAssetId(NativeCurrency::Parent),
418 who,300 who,
419 parent_amount,301 amount.into(),
420 )302 )
303 .into()
421 }304 }
422 AssetIds::ForeignAssetId(fid) => {305 AssetIds::ForeignAssetId(fid) => {
423 let target_collection_id = match <AssetBinding<T>>::get(fid) {306 let target_collection_id = match <AssetBinding<T>>::get(fid) {
432 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);315 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);
433 let account = T::CrossAccountId::from_sub(who.clone());316 let account = T::CrossAccountId::from_sub(who.clone());
434317
435 let amount_data: pallet_fungible::CreateItemData<T> = (account.clone(), value);318 let amount_data: pallet_fungible::CreateItemData<T> =
319 (account.clone(), amount.into());
436320
437 pallet_fungible::Pallet::<T>::create_item_foreign(321 pallet_fungible::Pallet::<T>::create_item_foreign(
438 &collection,322 &collection,
454 // let f = DebitFlags { keep_alive: false, best_effort: false };338 // let f = DebitFlags { keep_alive: false, best_effort: false };
455 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible burn_from");339 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible burn_from");
456
457 let value: u128 = match amount.try_into() {
458 Ok(val) => val,
459 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),
460 };
461340
462 match asset {341 match asset {
463 AssetIds::NativeAssetId(NativeCurrency::Here) => {342 AssetIds::NativeAssetId(NativeCurrency::Here) => {
464 let this_amount: <T as pallet_balances::Config>::Balance =
465 value.try_into().map_err(|_| {
466 DispatchError::Other("Bad amount to this parachain value conversion")
467 })?;
468
469 match <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::burn_from(343 match <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::burn_from(
470 who,344 who,
471 this_amount,345 amount.into(),
472 ) {346 ) {
473 Ok(_) => Ok(amount),347 Ok(v) => Ok(v.into()),
474 Err(e) => Err(e),348 Err(e) => Err(e),
475 }349 }
476 }350 }
477 AssetIds::NativeAssetId(NativeCurrency::Parent) => {351 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
478 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {
479 Ok(val) => val,
480 Err(_) => {
481 return Err(DispatchError::Other(
482 "Bad amount to relay chain value conversion",
483 ))
484 }
485 };
486
487 match <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::burn_from(352 match <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::burn_from(
488 AssetIds::NativeAssetId(NativeCurrency::Parent),353 AssetIds::NativeAssetId(NativeCurrency::Parent),
489 who,354 who,
490 parent_amount,355 amount.into(),
491 ) {356 ) {
492 Ok(_) => Ok(amount),357 Ok(v) => Ok(v.into()),
493 Err(e) => Err(e),358 Err(e) => Err(e),
494 }359 }
495 }360 }
507 pallet_fungible::Pallet::<T>::burn_foreign(372 pallet_fungible::Pallet::<T>::burn_foreign(
508 &collection,373 &collection,
509 &T::CrossAccountId::from_sub(who.clone()),374 &T::CrossAccountId::from_sub(who.clone()),
510 value,375 amount.into(),
511 )?;376 )?;
512377
513 Ok(amount)378 Ok(amount)
522 ) -> Result<Self::Balance, DispatchError> {387 ) -> Result<Self::Balance, DispatchError> {
523 // let f = DebitFlags { keep_alive: false, best_effort: true };388 // let f = DebitFlags { keep_alive: false, best_effort: true };
524 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible slash");389 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible slash");
525 Self::burn_from(asset, who, amount)?;390 Ok(Self::burn_from(asset, who, amount)?)
526 Ok(amount)
527 }391 }
528}392}
529393
530impl<T: Config> fungibles::Transfer<T::AccountId> for Pallet<T>394impl<T: Config> fungibles::Transfer<T::AccountId> for Pallet<T>
531where395where
532 T: orml_tokens::Config<CurrencyId = AssetIds>,396 T: orml_tokens::Config<CurrencyId = AssetIds>,
397 BalanceRelay<T>: From<BalanceOf<T>>,
398 BalanceOf<T>: From<BalanceRelay<T>>,
399 BalanceRelay<T>: From<<T as pallet_balances::Config>::Balance>,
400 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,
401 <T as pallet_balances::Config>::Balance: From<BalanceRelay<T>>,
402 <T as orml_tokens::Config>::Balance: From<BalanceRelay<T>>,
403 u128: From<BalanceRelay<T>>,
533{404{
534 fn transfer(405 fn transfer(
535 asset: Self::AssetId,406 asset: Self::AssetId,
541 // let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false };412 // let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false };
542 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible transfer");413 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible transfer");
543
544 let value: u128 = match amount.try_into() {
545 Ok(val) => val,
546 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),
547 };
548414
549 match asset {415 match asset {
550 AssetIds::NativeAssetId(NativeCurrency::Here) => {416 AssetIds::NativeAssetId(NativeCurrency::Here) => {
551 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {
552 Ok(val) => val,
553 Err(_) => {
554 return Err(DispatchError::Other(
555 "Bad amount to this parachain value conversion",
556 ))
557 }
558 };
559
560 match <pallet_balances::Pallet<T> as fungible::Transfer<T::AccountId>>::transfer(417 match <pallet_balances::Pallet<T> as fungible::Transfer<T::AccountId>>::transfer(
561 source,418 source,
562 dest,419 dest,
563 this_amount,420 amount.into(),
564 keep_alive,421 keep_alive,
565 ) {422 ) {
566 Ok(_) => Ok(amount),423 Ok(_) => Ok(amount),
570 }427 }
571 }428 }
572 AssetIds::NativeAssetId(NativeCurrency::Parent) => {429 AssetIds::NativeAssetId(NativeCurrency::Parent) => {
573 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {
574 Ok(val) => val,
575 Err(_) => {
576 return Err(DispatchError::Other(
577 "Bad amount to relay chain value conversion",
578 ))
579 }
580 };
581
582 match <orml_tokens::Pallet<T> as fungibles::Transfer<T::AccountId>>::transfer(430 match <orml_tokens::Pallet<T> as fungibles::Transfer<T::AccountId>>::transfer(
583 AssetIds::NativeAssetId(NativeCurrency::Parent),431 AssetIds::NativeAssetId(NativeCurrency::Parent),
584 source,432 source,
585 dest,433 dest,
586 parent_amount,434 amount.into(),
587 keep_alive,435 keep_alive,
588 ) {436 ) {
589 Ok(_) => Ok(amount),437 Ok(_) => Ok(amount),
606 &collection,454 &collection,
607 &T::CrossAccountId::from_sub(source.clone()),455 &T::CrossAccountId::from_sub(source.clone()),
608 &T::CrossAccountId::from_sub(dest.clone()),456 &T::CrossAccountId::from_sub(dest.clone()),
609 value,457 amount.into(),
610 &Unlimited,458 &Unlimited,
611 )?;459 )?;
612460
modifiedpallets/foreign-assets/src/lib.rsdiffbeforeafterboth
122pub type CurrencyId = AssetIds;122pub type CurrencyId = AssetIds;
123123
124mod impl_fungibles;124mod impl_fungibles;
125mod weights;125pub mod weights;
126126
127#[cfg(feature = "runtime-benchmarks")]127#[cfg(feature = "runtime-benchmarks")]
128mod benchmarking;128mod benchmarking;
modifiedpallets/foreign-assets/src/weights.rsdiffbeforeafterboth
13 fn update_foreign_asset() -> Weight;13 fn update_foreign_asset() -> Weight;
14}14}
1515
16/// Weights for module_asset_registry using the Acala node and recommended hardware.16/// Weights for pallet_fungible using the Substrate node and recommended hardware.
17pub struct AcalaWeight<T>(PhantomData<T>);17pub struct SubstrateWeight<T>(PhantomData<T>);
18impl<T: frame_system::Config> WeightInfo for AcalaWeight<T> {18impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
19 fn register_foreign_asset() -> Weight {19 fn register_foreign_asset() -> Weight {
20 (29_819_000 as Weight)20 (29_819_000 as Weight)
21 .saturating_add(T::DbWeight::get().reads(2 as Weight))21 .saturating_add(T::DbWeight::get().reads(2 as Weight))
modifiedruntime/common/config/pallets/foreign_asset.rsdiffbeforeafterboth
5 type Event = Event;5 type Event = Event;
6 type Currency = Balances;6 type Currency = Balances;
7 type RegisterOrigin = frame_system::EnsureRoot<AccountId>;7 type RegisterOrigin = frame_system::EnsureRoot<AccountId>;
8 type WeightInfo = ();8 type WeightInfo = pallet_foreign_assets::weights::SubstrateWeight<Self>;
9}9}
1010