Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Pallet: Atomic Swap #6349

Merged
merged 21 commits into from
Jun 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add additional expire handler
  • Loading branch information
sorpaas committed Jun 16, 2020
commit d51b1ded6affc1d7a3d52fb4b489ef3c93004c9c
36 changes: 24 additions & 12 deletions frame/atomic-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use frame_support::{
};
use frame_system::{self as system, ensure_signed};
use codec::{Encode, Decode};
use sp_runtime::RuntimeDebug;
use sp_runtime::{RuntimeDebug, traits::Saturating};

/// Pending atomic swap operation.
#[derive(Clone, RuntimeDebug, Eq, PartialEq, Encode, Decode)]
Expand Down Expand Up @@ -70,6 +70,8 @@ pub trait Trait: frame_system::Trait {
type Currency: ReservableCurrency<Self::AccountId>;
/// Limit of proof size.
type ProofLimit: Get<u32>;
/// Block when the swap completely expires.
type ExpireDuration: Get<BlockNumberFor<Self>>;
}

decl_storage! {
Expand Down Expand Up @@ -110,9 +112,8 @@ decl_event!(
NewSwap(AccountId, HashedProof, PendingSwap),
/// Swap claimed.
SwapClaimed(AccountId, HashedProof, Balance),
/// Swap claim failed its execution. However, the swap is still marked as claimed, and the
/// caller can try again later if it believes that this is a temporary error.
SwapClaimFailed(AccountId, HashedProof),
/// Swap claim failed its execution. The last parameter indicates whether retry is possible.
SwapClaimFailed(AccountId, HashedProof, bool),
/// Swap cancelled.
SwapCancelled(AccountId, HashedProof),
}
Expand Down Expand Up @@ -195,6 +196,25 @@ decl_module! {
swap.balance,
BalanceStatus::Free,
) {
Err(e) => {
let expired = frame_system::Module::<T>::block_number() >
swap.end_block.saturating_add(T::ExpireDuration::get());
if expired {
PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());

Self::deposit_event(
RawEvent::SwapClaimFailed(target, hashed_proof, false)
);
} else {
PendingSwaps::<T>::insert(target.clone(), hashed_proof.clone(), swap);

Self::deposit_event(
RawEvent::SwapClaimFailed(target, hashed_proof, true)
);
}

Err(e.into())
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
},
Ok(_) => {
PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());

Expand All @@ -204,15 +224,7 @@ decl_module! {

Ok(())
},
Err(e) => {
PendingSwaps::<T>::insert(target.clone(), hashed_proof.clone(), swap);

Self::deposit_event(
RawEvent::SwapClaimFailed(target, hashed_proof)
);

Err(e.into())
},
}
}

Expand Down