3838
3939#![ cfg_attr( not( feature = "std" ) , no_std) ]
4040
41- use codec:: { Codec , FullCodec } ;
41+ use codec:: Codec ;
4242use frame_support:: {
4343 decl_error, decl_event, decl_module, decl_storage,
4444 traits:: {
@@ -49,7 +49,7 @@ use frame_support::{
4949} ;
5050use frame_system:: { ensure_root, ensure_signed} ;
5151use sp_runtime:: {
52- traits:: { AtLeast32BitUnsigned , MaybeSerializeDeserialize , StaticLookup , Zero } ,
52+ traits:: { CheckedSub , MaybeSerializeDeserialize , StaticLookup , Zero } ,
5353 DispatchError , DispatchResult ,
5454} ;
5555use sp_std:: {
@@ -542,102 +542,85 @@ where
542542pub type NativeCurrencyOf < T > = Currency < T , <T as Trait >:: GetNativeCurrencyId > ;
543543
544544/// Adapt other currency traits implementation to `BasicCurrency`.
545- pub struct BasicCurrencyAdapter < Currency , Balance , BalanceConvert , Amount , Moment > (
546- marker:: PhantomData < ( Currency , Balance , BalanceConvert , Amount , Moment ) > ,
547- ) ;
545+ pub struct BasicCurrencyAdapter < T , Currency , Amount , Moment > ( marker:: PhantomData < ( T , Currency , Amount , Moment ) > ) ;
548546
549547type PalletBalanceOf < A , Currency > = <Currency as PalletCurrency < A > >:: Balance ;
550548
551549// Adapt `frame_support::traits::Currency`
552- impl < AccountId , Currency , Balance , BalanceConvert , Amount , Moment > BasicCurrency < AccountId >
553- for BasicCurrencyAdapter < Currency , Balance , BalanceConvert , Amount , Moment >
550+ impl < T , AccountId , Currency , Amount , Moment > BasicCurrency < AccountId >
551+ for BasicCurrencyAdapter < T , Currency , Amount , Moment >
554552where
555553 Currency : PalletCurrency < AccountId > ,
556- Balance : AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default ,
557- BalanceConvert : From < PalletBalanceOf < AccountId , Currency > >
558- + Into < PalletBalanceOf < AccountId , Currency > >
559- + From < Balance >
560- + Into < Balance > ,
554+ T : Trait ,
561555{
562- type Balance = Balance ;
556+ type Balance = PalletBalanceOf < AccountId , Currency > ;
563557
564558 fn total_issuance ( ) -> Self :: Balance {
565- BalanceConvert :: from ( Currency :: total_issuance ( ) ) . into ( )
559+ Currency :: total_issuance ( )
566560 }
567561
568562 fn total_balance ( who : & AccountId ) -> Self :: Balance {
569- BalanceConvert :: from ( Currency :: total_balance ( who) ) . into ( )
563+ Currency :: total_balance ( who)
570564 }
571565
572566 fn free_balance ( who : & AccountId ) -> Self :: Balance {
573- BalanceConvert :: from ( Currency :: free_balance ( who) ) . into ( )
567+ Currency :: free_balance ( who)
574568 }
575569
576570 fn ensure_can_withdraw ( who : & AccountId , amount : Self :: Balance ) -> DispatchResult {
577- let new_balance_pallet = {
578- let new_balance = Self :: free_balance ( who)
579- . checked_sub ( & amount)
580- . ok_or ( "InsufficientBalance" ) ?;
581- BalanceConvert :: from ( new_balance) . into ( )
582- } ;
583- let amount_pallet = BalanceConvert :: from ( amount) . into ( ) ;
584- Currency :: ensure_can_withdraw ( who, amount_pallet, WithdrawReasons :: all ( ) , new_balance_pallet)
571+ let new_balance = Self :: free_balance ( who)
572+ . checked_sub ( & amount)
573+ . ok_or ( Error :: < T > :: BalanceTooLow ) ?;
574+
575+ Currency :: ensure_can_withdraw ( who, amount, WithdrawReasons :: all ( ) , new_balance)
585576 }
586577
587578 fn transfer ( from : & AccountId , to : & AccountId , amount : Self :: Balance ) -> DispatchResult {
588- let amount_pallet = BalanceConvert :: from ( amount) . into ( ) ;
589- Currency :: transfer ( from, to, amount_pallet, ExistenceRequirement :: AllowDeath )
579+ Currency :: transfer ( from, to, amount, ExistenceRequirement :: AllowDeath )
590580 }
591581
592582 fn deposit ( who : & AccountId , amount : Self :: Balance ) -> DispatchResult {
593- let _ = Currency :: deposit_creating ( who, BalanceConvert :: from ( amount) . into ( ) ) ;
583+ let _ = Currency :: deposit_creating ( who, amount) ;
594584 Ok ( ( ) )
595585 }
596586
597587 fn withdraw ( who : & AccountId , amount : Self :: Balance ) -> DispatchResult {
598- Currency :: withdraw (
599- who,
600- BalanceConvert :: from ( amount) . into ( ) ,
601- WithdrawReasons :: all ( ) ,
602- ExistenceRequirement :: AllowDeath ,
603- )
604- . map ( |_| ( ) )
588+ Currency :: withdraw ( who, amount, WithdrawReasons :: all ( ) , ExistenceRequirement :: AllowDeath ) . map ( |_| ( ) )
605589 }
606590
607591 fn can_slash ( who : & AccountId , amount : Self :: Balance ) -> bool {
608- Currency :: can_slash ( who, BalanceConvert :: from ( amount) . into ( ) )
592+ Currency :: can_slash ( who, amount)
609593 }
610594
611595 fn slash ( who : & AccountId , amount : Self :: Balance ) -> Self :: Balance {
612- let ( _, gap) = Currency :: slash ( who, BalanceConvert :: from ( amount) . into ( ) ) ;
613- BalanceConvert :: from ( gap) . into ( )
596+ let ( _, gap) = Currency :: slash ( who, amount) ;
597+ gap
614598 }
615599}
616600
617601// Adapt `frame_support::traits::Currency`
618- impl < AccountId , Currency , Balance , BalanceConvert , Amount , Moment > BasicCurrencyExtended < AccountId >
619- for BasicCurrencyAdapter < Currency , Balance , BalanceConvert , Amount , Moment >
602+ impl < T , AccountId , Currency , Amount , Moment > BasicCurrencyExtended < AccountId >
603+ for BasicCurrencyAdapter < T , Currency , Amount , Moment >
620604where
621- Balance : AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default ,
622605 Amount : Signed
623- + TryInto < Balance >
624- + TryFrom < Balance >
606+ + TryInto < PalletBalanceOf < AccountId , Currency > >
607+ + TryFrom < PalletBalanceOf < AccountId , Currency > >
625608 + SimpleArithmetic
626609 + Codec
627610 + Copy
628611 + MaybeSerializeDeserialize
629612 + Debug
630613 + Default ,
631614 Currency : PalletCurrency < AccountId > ,
632- BalanceConvert : From < PalletBalanceOf < AccountId , Currency > >
633- + Into < PalletBalanceOf < AccountId , Currency > >
634- + From < Balance >
635- + Into < Balance > ,
615+ T : Trait ,
636616{
637617 type Amount = Amount ;
638618
639619 fn update_balance ( who : & AccountId , by_amount : Self :: Amount ) -> DispatchResult {
640- let by_balance = by_amount. abs ( ) . try_into ( ) . map_err ( |_| "AmountIntoBalanceFailed" ) ?;
620+ let by_balance = by_amount
621+ . abs ( )
622+ . try_into ( )
623+ . map_err ( |_| Error :: < T > :: AmountIntoBalanceFailed ) ?;
641624 if by_amount. is_positive ( ) {
642625 Self :: deposit ( who, by_balance)
643626 } else {
@@ -647,34 +630,20 @@ where
647630}
648631
649632// Adapt `frame_support::traits::LockableCurrency`
650- impl < AccountId , Currency , Balance , BalanceConvert , Amount , Moment > BasicLockableCurrency < AccountId >
651- for BasicCurrencyAdapter < Currency , Balance , BalanceConvert , Amount , Moment >
633+ impl < T , AccountId , Currency , Amount , Moment > BasicLockableCurrency < AccountId >
634+ for BasicCurrencyAdapter < T , Currency , Amount , Moment >
652635where
653- Balance : AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default ,
654636 Currency : PalletLockableCurrency < AccountId > ,
655- BalanceConvert : From < PalletBalanceOf < AccountId , Currency > >
656- + Into < PalletBalanceOf < AccountId , Currency > >
657- + From < Balance >
658- + Into < Balance > ,
637+ T : Trait ,
659638{
660639 type Moment = Moment ;
661640
662641 fn set_lock ( lock_id : LockIdentifier , who : & AccountId , amount : Self :: Balance ) {
663- Currency :: set_lock (
664- lock_id,
665- who,
666- BalanceConvert :: from ( amount) . into ( ) ,
667- WithdrawReasons :: all ( ) ,
668- ) ;
642+ Currency :: set_lock ( lock_id, who, amount, WithdrawReasons :: all ( ) ) ;
669643 }
670644
671645 fn extend_lock ( lock_id : LockIdentifier , who : & AccountId , amount : Self :: Balance ) {
672- Currency :: extend_lock (
673- lock_id,
674- who,
675- BalanceConvert :: from ( amount) . into ( ) ,
676- WithdrawReasons :: all ( ) ,
677- ) ;
646+ Currency :: extend_lock ( lock_id, who, amount, WithdrawReasons :: all ( ) ) ;
678647 }
679648
680649 fn remove_lock ( lock_id : LockIdentifier , who : & AccountId ) {
@@ -683,35 +652,31 @@ where
683652}
684653
685654// Adapt `frame_support::traits::ReservableCurrency`
686- impl < AccountId , Currency , Balance , BalanceConvert , Amount , Moment > BasicReservableCurrency < AccountId >
687- for BasicCurrencyAdapter < Currency , Balance , BalanceConvert , Amount , Moment >
655+ impl < T , AccountId , Currency , Amount , Moment > BasicReservableCurrency < AccountId >
656+ for BasicCurrencyAdapter < T , Currency , Amount , Moment >
688657where
689- Balance : AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default ,
690658 Currency : PalletReservableCurrency < AccountId > ,
691- BalanceConvert : From < PalletBalanceOf < AccountId , Currency > >
692- + Into < PalletBalanceOf < AccountId , Currency > >
693- + From < Balance >
694- + Into < Balance > ,
659+ T : Trait ,
695660{
696661 fn can_reserve ( who : & AccountId , value : Self :: Balance ) -> bool {
697- Currency :: can_reserve ( who, BalanceConvert :: from ( value) . into ( ) )
662+ Currency :: can_reserve ( who, value)
698663 }
699664
700665 fn slash_reserved ( who : & AccountId , value : Self :: Balance ) -> Self :: Balance {
701- let ( _, gap) = Currency :: slash_reserved ( who, BalanceConvert :: from ( value) . into ( ) ) ;
702- BalanceConvert :: from ( gap) . into ( )
666+ let ( _, gap) = Currency :: slash_reserved ( who, value) ;
667+ gap
703668 }
704669
705670 fn reserved_balance ( who : & AccountId ) -> Self :: Balance {
706- BalanceConvert :: from ( Currency :: reserved_balance ( who) ) . into ( )
671+ Currency :: reserved_balance ( who)
707672 }
708673
709674 fn reserve ( who : & AccountId , value : Self :: Balance ) -> DispatchResult {
710- Currency :: reserve ( who, BalanceConvert :: from ( value) . into ( ) )
675+ Currency :: reserve ( who, value)
711676 }
712677
713678 fn unreserve ( who : & AccountId , value : Self :: Balance ) -> Self :: Balance {
714- BalanceConvert :: from ( Currency :: unreserve ( who, BalanceConvert :: from ( value) . into ( ) ) ) . into ( )
679+ Currency :: unreserve ( who, value)
715680 }
716681
717682 fn repatriate_reserved (
@@ -720,7 +685,6 @@ where
720685 value : Self :: Balance ,
721686 status : BalanceStatus ,
722687 ) -> result:: Result < Self :: Balance , DispatchError > {
723- Currency :: repatriate_reserved ( slashed, beneficiary, BalanceConvert :: from ( value) . into ( ) , status)
724- . map ( |a| BalanceConvert :: from ( a) . into ( ) )
688+ Currency :: repatriate_reserved ( slashed, beneficiary, value, status)
725689 }
726690}
0 commit comments