Skip to content

Commit

Permalink
Merge pull request #949 from iov-one/922-swap-kind-helpers
Browse files Browse the repository at this point in the history
Add atomic swap type guard helpers to BCP
  • Loading branch information
willclarktech authored Apr 30, 2019
2 parents 8bc8d25 + 1481380 commit 0d8e596
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.15.0

- @iov/bcp: Add `isSwapTransaction` helper function.
- @iov/bcp: Add `isOpenSwap`, `isClaimedSwap` and `isAbortedSwap` helper
functions.
- @iov/ethereum: Add `createEtherSwapId` and `createErc20SwapId` static methods.
- @iov/ethereum: Add `SwapIdPrefix` enum.
- @iov/ethereum: Add `Erc20TokensMap` type.
Expand Down
12 changes: 12 additions & 0 deletions packages/iov-bcp/src/atomicswaptypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ export interface AbortedSwap {

export type AtomicSwap = OpenSwap | ClaimedSwap | AbortedSwap;

export function isOpenSwap(swap: AtomicSwap): swap is OpenSwap {
return isSwapProcessStateOpen(swap.kind);
}

export function isClaimedSwap(swap: AtomicSwap): swap is ClaimedSwap {
return isSwapProcessStateClaimed(swap.kind);
}

export function isAbortedSwap(swap: AtomicSwap): swap is AbortedSwap {
return isSwapProcessStateAborted(swap.kind);
}

export interface AtomicSwapRecipientQuery {
readonly recipient: Address;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/iov-bcp/types/atomicswaptypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export interface AbortedSwap {
readonly data: SwapData;
}
export declare type AtomicSwap = OpenSwap | ClaimedSwap | AbortedSwap;
export declare function isOpenSwap(swap: AtomicSwap): swap is OpenSwap;
export declare function isClaimedSwap(swap: AtomicSwap): swap is ClaimedSwap;
export declare function isAbortedSwap(swap: AtomicSwap): swap is AbortedSwap;
export interface AtomicSwapRecipientQuery {
readonly recipient: Address;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
AtomicSwap,
AtomicSwapConnection,
AtomicSwapHelpers,
ClaimedSwap,
createTimestampTimeout,
isBlockInfoPending,
isBlockInfoSucceeded,
isClaimedSwap,
Preimage,
PublicIdentity,
SendTransaction,
Expand Down Expand Up @@ -207,11 +207,14 @@ class Actor {
claim: AtomicSwap,
unclaimedId: SwapId,
): Promise<Uint8Array | undefined> {
if (!isClaimedSwap(claim)) {
throw new Error("Expected swap to be claimed");
}
const transaction = await this.bnsConnection.withDefaultFee<SwapClaimTransaction>({
kind: "bcp/swap_claim",
creator: this.bnsIdentity,
swapId: unclaimedId,
preimage: (claim as ClaimedSwap).preimage, // public data now!
preimage: claim.preimage, // public data now!
});
return this.sendTransaction(transaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
AtomicSwap,
AtomicSwapConnection,
AtomicSwapHelpers,
ClaimedSwap,
createTimestampTimeout,
isBlockInfoPending,
isBlockInfoSucceeded,
isClaimedSwap,
Preimage,
PublicIdentity,
SendTransaction,
Expand Down Expand Up @@ -236,11 +236,14 @@ class Actor {
claim: AtomicSwap,
unclaimedId: SwapId,
): Promise<Uint8Array | undefined> {
if (!isClaimedSwap(claim)) {
throw new Error("Expected swap to be claimed");
}
const transaction = await this.bnsConnection.withDefaultFee<SwapClaimTransaction>({
kind: "bcp/swap_claim",
creator: this.bnsIdentity,
swapId: unclaimedId,
preimage: (claim as ClaimedSwap).preimage, // public data now!
preimage: claim.preimage, // public data now!
});
return this.sendTransaction(transaction);
}
Expand Down
13 changes: 9 additions & 4 deletions packages/iov-ethereum/src/integrationtests/atomicswap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
ClaimedSwap,
isBlockInfoPending,
isBlockInfoSucceeded,
isClaimedSwap,
isOpenSwap,
OpenSwap,
Preimage,
PublicIdentity,
PublicKeyBundle,
SendTransaction,
SwapClaimTransaction,
SwapIdBytes,
SwapOfferTransaction,
SwapProcessState,
TokenTicker,
Expand Down Expand Up @@ -288,13 +289,17 @@ describe("Full atomic swap", () => {
// find claim
const bobSenderSwaps = await bob.getSenderSwaps();
const aliceClaimed = bobSenderSwaps[bobSenderSwaps.length - 1];
expect(aliceClaimed.kind).toEqual(SwapProcessState.Claimed);
if (!isClaimedSwap(aliceClaimed)) {
throw new Error("Expected swap to be claimed");
}

const bobReceiverSwaps2 = await bob.getReceiverSwaps();
const aliceOffer2 = bobReceiverSwaps2[bobReceiverSwaps2.length - 1];
expect(aliceOffer2.kind).toEqual(SwapProcessState.Open);
if (!isOpenSwap(aliceOffer2)) {
throw new Error("Expected swap to be open");
}

await bob.claimFromRevealedPreimage(aliceOffer2 as OpenSwap, aliceClaimed as ClaimedSwap);
await bob.claimFromRevealedPreimage(aliceOffer2, aliceClaimed);

// Bob used Alice's preimage to claim unlock his funds
expect((await bob.getReceiverBalance()).sub(bobInitialReceiver).gtn(4900000000000000000)).toEqual(true);
Expand Down

0 comments on commit 0d8e596

Please sign in to comment.