From 7d3b86def1ae6e956be58013ee34d091b28bc44e Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 30 Apr 2019 12:45:40 +0200 Subject: [PATCH 1/3] Add atomic swap type guard helpers to BCP --- packages/iov-bcp/src/atomicswaptypes.ts | 12 ++++++++++++ packages/iov-bcp/types/atomicswaptypes.d.ts | 3 +++ 2 files changed, 15 insertions(+) diff --git a/packages/iov-bcp/src/atomicswaptypes.ts b/packages/iov-bcp/src/atomicswaptypes.ts index 1ec097c86..08a7c2d90 100644 --- a/packages/iov-bcp/src/atomicswaptypes.ts +++ b/packages/iov-bcp/src/atomicswaptypes.ts @@ -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; } diff --git a/packages/iov-bcp/types/atomicswaptypes.d.ts b/packages/iov-bcp/types/atomicswaptypes.d.ts index eb5aa882f..c29d5c41b 100644 --- a/packages/iov-bcp/types/atomicswaptypes.d.ts +++ b/packages/iov-bcp/types/atomicswaptypes.d.ts @@ -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; } From a25ab47ec2d0824eea62db3c5ace5d25aa01a502 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 30 Apr 2019 12:51:52 +0200 Subject: [PATCH 2/3] Update CHANGELOG for atomic swap helpers --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc6ff2446..b3994dfb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. From 1481380d93a90cd7e6fa4768b4193ec0e79cb920 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 30 Apr 2019 13:00:09 +0200 Subject: [PATCH 3/3] Use atomic swap type guard helpers in packages --- .../src/integrationtests/atomicswapbnsbcp.spec.ts | 7 +++++-- .../src/integrationtests/atomicswapbnseth.spec.ts | 7 +++++-- .../src/integrationtests/atomicswap.spec.ts | 13 +++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/iov-core/src/integrationtests/atomicswapbnsbcp.spec.ts b/packages/iov-core/src/integrationtests/atomicswapbnsbcp.spec.ts index 00ba236d7..b97d16e60 100644 --- a/packages/iov-core/src/integrationtests/atomicswapbnsbcp.spec.ts +++ b/packages/iov-core/src/integrationtests/atomicswapbnsbcp.spec.ts @@ -6,10 +6,10 @@ import { AtomicSwap, AtomicSwapConnection, AtomicSwapHelpers, - ClaimedSwap, createTimestampTimeout, isBlockInfoPending, isBlockInfoSucceeded, + isClaimedSwap, Preimage, PublicIdentity, SendTransaction, @@ -207,11 +207,14 @@ class Actor { claim: AtomicSwap, unclaimedId: SwapId, ): Promise { + if (!isClaimedSwap(claim)) { + throw new Error("Expected swap to be claimed"); + } const transaction = await this.bnsConnection.withDefaultFee({ 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); } diff --git a/packages/iov-core/src/integrationtests/atomicswapbnseth.spec.ts b/packages/iov-core/src/integrationtests/atomicswapbnseth.spec.ts index 7d4227f4b..6d482b4dc 100644 --- a/packages/iov-core/src/integrationtests/atomicswapbnseth.spec.ts +++ b/packages/iov-core/src/integrationtests/atomicswapbnseth.spec.ts @@ -6,10 +6,10 @@ import { AtomicSwap, AtomicSwapConnection, AtomicSwapHelpers, - ClaimedSwap, createTimestampTimeout, isBlockInfoPending, isBlockInfoSucceeded, + isClaimedSwap, Preimage, PublicIdentity, SendTransaction, @@ -236,11 +236,14 @@ class Actor { claim: AtomicSwap, unclaimedId: SwapId, ): Promise { + if (!isClaimedSwap(claim)) { + throw new Error("Expected swap to be claimed"); + } const transaction = await this.bnsConnection.withDefaultFee({ 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); } diff --git a/packages/iov-ethereum/src/integrationtests/atomicswap.spec.ts b/packages/iov-ethereum/src/integrationtests/atomicswap.spec.ts index ef7460dd9..af09cb3c4 100644 --- a/packages/iov-ethereum/src/integrationtests/atomicswap.spec.ts +++ b/packages/iov-ethereum/src/integrationtests/atomicswap.spec.ts @@ -9,13 +9,14 @@ import { ClaimedSwap, isBlockInfoPending, isBlockInfoSucceeded, + isClaimedSwap, + isOpenSwap, OpenSwap, Preimage, PublicIdentity, PublicKeyBundle, SendTransaction, SwapClaimTransaction, - SwapIdBytes, SwapOfferTransaction, SwapProcessState, TokenTicker, @@ -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);