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
Mark swap on claim
This handles the additional case if `repatriate_reserved` fails.
  • Loading branch information
sorpaas committed Jun 16, 2020
commit 47877fa9033cca07f103b36331defeb86c0e69c1
51 changes: 40 additions & 11 deletions frame/atomic-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use frame_support::{
decl_module, decl_storage, decl_event, decl_error, ensure,
traits::{Get, Currency, ReservableCurrency, BalanceStatus},
weights::Weight,
dispatch::DispatchResult,
};
use frame_system::{self as system, ensure_signed};
use codec::{Encode, Decode};
Expand All @@ -42,6 +43,8 @@ pub struct PendingSwap<AccountId, Balance, BlockNumber> {
pub balance: Balance,
/// End block of the lock.
pub end_block: BlockNumber,
/// Whether the swap has already been claimed.
pub claimed: bool,
}

/// Balance type from the pallet's point of view.
Expand Down Expand Up @@ -87,6 +90,8 @@ decl_error! {
ProofTooLarge,
/// Source does not match.
SourceMismatch,
/// Swap has already been claimed.
AlreadyClaimed,
/// Swap does not exist.
NotExist,
/// Duration has not yet passed for the swap to be cancelled.
Expand All @@ -105,6 +110,9 @@ 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 cancelled.
SwapCancelled(AccountId, HashedProof),
}
Expand Down Expand Up @@ -137,7 +145,7 @@ decl_module! {
) {
let source = ensure_signed(origin)?;
ensure!(
PendingSwaps::<T>::get(&target, hashed_proof).is_none(),
!PendingSwaps::<T>::contains_key(&target, hashed_proof),
Error::<T>::AlreadyExist
);

Expand All @@ -147,6 +155,7 @@ decl_module! {
source,
balance,
end_block: frame_system::Module::<T>::block_number() + duration,
claimed: false,
};
PendingSwaps::<T>::insert(target.clone(), hashed_proof.clone(), swap.clone());

Expand All @@ -167,7 +176,7 @@ decl_module! {
fn claim_swap(
origin,
proof: Vec<u8>,
) {
) -> DispatchResult {
ensure!(
proof.len() <= T::ProofLimit::get() as usize,
Error::<T>::ProofTooLarge,
Expand All @@ -176,19 +185,35 @@ decl_module! {
let target = ensure_signed(origin)?;
let hashed_proof = blake2_256(&proof);

let swap = PendingSwaps::<T>::get(&target, hashed_proof)
let mut swap = PendingSwaps::<T>::get(&target, hashed_proof)
.ok_or(Error::<T>::InvalidProof)?;
T::Currency::repatriate_reserved(
swap.claimed = true;

match T::Currency::repatriate_reserved(
&swap.source,
&target,
swap.balance,
BalanceStatus::Free,
)?;
PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());
) {
Ok(_) => {
PendingSwaps::<T>::remove(target.clone(), hashed_proof.clone());

Self::deposit_event(
RawEvent::SwapClaimed(target, hashed_proof, swap.balance)
);
Self::deposit_event(
RawEvent::SwapClaimed(target, hashed_proof, swap.balance)
);

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

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

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

/// Cancel an atomic swap. Only possible after the originally set duration has passed.
Expand All @@ -208,13 +233,17 @@ decl_module! {
let swap = PendingSwaps::<T>::get(&target, hashed_proof)
.ok_or(Error::<T>::NotExist)?;
ensure!(
frame_system::Module::<T>::block_number() >= swap.end_block,
Error::<T>::DurationNotPassed,
!swap.claimed,
Error::<T>::AlreadyClaimed,
);
ensure!(
swap.source == source,
sorpaas marked this conversation as resolved.
Show resolved Hide resolved
Error::<T>::SourceMismatch,
);
ensure!(
frame_system::Module::<T>::block_number() >= swap.end_block,
Error::<T>::DurationNotPassed,
);

T::Currency::unreserve(
&swap.source,
Expand Down