Skip to content

Commit eed3851

Browse files
authored
pallet-swap-action: Change BalanceSwapAction signature (#6580)
Instead of requiring `T: Trait` in `BalanceSwapAction`, we directly depend on `AccountId`. This fixes a compilation error on wasm, where `Runtime` does not implement `Debug`, but `BalanceSwapAction` required it.
1 parent e321896 commit eed3851

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

src/lib.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,69 +74,63 @@ pub type HashedProof = [u8; 32];
7474
/// succeeds with best efforts.
7575
/// - **Claim**: claim any resources reserved in the first phrase.
7676
/// - **Cancel**: cancel any resources reserved in the first phrase.
77-
pub trait SwapAction<T: Trait> {
77+
pub trait SwapAction<AccountId, T: Trait> {
7878
/// Reserve the resources needed for the swap, from the given `source`. The reservation is
7979
/// allowed to fail. If that is the case, the the full swap creation operation is cancelled.
80-
fn reserve(&self, source: &T::AccountId) -> DispatchResult;
80+
fn reserve(&self, source: &AccountId) -> DispatchResult;
8181
/// Claim the reserved resources, with `source` and `target`. Returns whether the claim
8282
/// succeeds.
83-
fn claim(&self, source: &T::AccountId, target: &T::AccountId) -> bool;
83+
fn claim(&self, source: &AccountId, target: &AccountId) -> bool;
8484
/// Weight for executing the operation.
8585
fn weight(&self) -> Weight;
8686
/// Cancel the resources reserved in `source`.
87-
fn cancel(&self, source: &T::AccountId);
87+
fn cancel(&self, source: &AccountId);
8888
}
8989

9090
/// A swap action that only allows transferring balances.
9191
#[derive(Clone, RuntimeDebug, Eq, PartialEq, Encode, Decode)]
92-
pub struct BalanceSwapAction<T: Trait, C: ReservableCurrency<T::AccountId>> {
93-
value: <C as Currency<<T as frame_system::Trait>::AccountId>>::Balance,
92+
pub struct BalanceSwapAction<AccountId, C: ReservableCurrency<AccountId>> {
93+
value: <C as Currency<AccountId>>::Balance,
9494
_marker: PhantomData<C>,
9595
}
9696

97-
impl<T: Trait, C> BalanceSwapAction<T, C> where
98-
C: ReservableCurrency<T::AccountId>,
99-
{
97+
impl<AccountId, C> BalanceSwapAction<AccountId, C> where C: ReservableCurrency<AccountId> {
10098
/// Create a new swap action value of balance.
101-
pub fn new(value: <C as Currency<<T as frame_system::Trait>::AccountId>>::Balance) -> Self {
99+
pub fn new(value: <C as Currency<AccountId>>::Balance) -> Self {
102100
Self { value, _marker: PhantomData }
103101
}
104102
}
105103

106-
impl<T: Trait, C> Deref for BalanceSwapAction<T, C> where
107-
C: ReservableCurrency<T::AccountId>,
108-
{
109-
type Target = <C as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
104+
impl<AccountId, C> Deref for BalanceSwapAction<AccountId, C> where C: ReservableCurrency<AccountId> {
105+
type Target = <C as Currency<AccountId>>::Balance;
110106

111107
fn deref(&self) -> &Self::Target {
112108
&self.value
113109
}
114110
}
115111

116-
impl<T: Trait, C> DerefMut for BalanceSwapAction<T, C> where
117-
C: ReservableCurrency<T::AccountId>,
118-
{
112+
impl<AccountId, C> DerefMut for BalanceSwapAction<AccountId, C> where C: ReservableCurrency<AccountId> {
119113
fn deref_mut(&mut self) -> &mut Self::Target {
120114
&mut self.value
121115
}
122116
}
123117

124-
impl<T: Trait, C> SwapAction<T> for BalanceSwapAction<T, C> where
125-
C: ReservableCurrency<T::AccountId>,
118+
impl<T: Trait, AccountId, C> SwapAction<AccountId, T> for BalanceSwapAction<AccountId, C>
119+
where C: ReservableCurrency<AccountId>
126120
{
127-
fn reserve(&self, source: &T::AccountId) -> DispatchResult {
121+
fn reserve(&self, source: &AccountId) -> DispatchResult {
128122
C::reserve(&source, self.value)
129123
}
130124

131-
fn claim(&self, source: &T::AccountId, target: &T::AccountId) -> bool {
125+
fn claim(&self, source: &AccountId, target: &AccountId) -> bool {
132126
C::repatriate_reserved(source, target, self.value, BalanceStatus::Free).is_ok()
133127
}
134128

135129
fn weight(&self) -> Weight {
136130
T::DbWeight::get().reads_writes(1, 1)
137131
}
138132

139-
fn cancel(&self, source: &T::AccountId) {
133+
fn cancel(&self, source: &AccountId) {
140134
C::unreserve(source, self.value);
141135
}
142136
}
@@ -146,7 +140,7 @@ pub trait Trait: frame_system::Trait {
146140
/// The overarching event type.
147141
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
148142
/// Swap action.
149-
type SwapAction: SwapAction<Self> + Parameter;
143+
type SwapAction: SwapAction<Self::AccountId, Self> + Parameter;
150144
/// Limit of proof size.
151145
///
152146
/// Atomic swap is only atomic if once the proof is revealed, both parties can submit the proofs

src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ parameter_types! {
6868
}
6969
impl Trait for Test {
7070
type Event = ();
71-
type SwapAction = BalanceSwapAction<Test, Balances>;
71+
type SwapAction = BalanceSwapAction<u64, Balances>;
7272
type ProofLimit = ProofLimit;
7373
}
7474
type System = frame_system::Module<Test>;

0 commit comments

Comments
 (0)