Skip to content

Commit 2b7361b

Browse files
authored
Protocols: Refactor protocols to provide better utility methods (#546)
* initial pass for refactor * Move protocols to directories * Eliminate from/to addresses from quote arg requirements
1 parent fffeaa3 commit 2b7361b

File tree

18 files changed

+561
-575
lines changed

18 files changed

+561
-575
lines changed

connect/src/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ export * from "./config.js";
33
export * from "./common.js";
44
export * from "./types.js";
55

6-
export * from "./protocols/wormholeTransfer.js";
7-
export * from "./protocols/tokenTransfer.js";
8-
export * from "./protocols/cctpTransfer.js";
9-
export * from "./protocols/gatewayTransfer.js";
6+
export * from "./protocols/index.js";
107

118
export * as tasks from "./tasks.js";
129
export * as circleApi from "./circle-api.js";

connect/src/protocols/cctpTransfer.ts connect/src/protocols/cctp/cctpTransfer.ts

+210-225
Large diffs are not rendered by default.

connect/src/protocols/gatewayTransfer.ts connect/src/protocols/gateway/gatewayTransfer.ts

+24-30
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import {
2727
toGatewayMsg,
2828
toNative,
2929
} from "@wormhole-foundation/sdk-definitions";
30-
import { signSendWait } from "../common.js";
31-
import { fetchIbcXfer, isTokenBridgeVaaRedeemed, retry } from "../tasks.js";
32-
import { TransferState } from "../types.js";
33-
import { Wormhole } from "../wormhole.js";
34-
import type { WormholeTransfer } from "./wormholeTransfer.js";
30+
import { signSendWait } from "../../common.js";
31+
import { fetchIbcXfer, isTokenBridgeVaaRedeemed, retry } from "../../tasks.js";
32+
import { TransferState } from "../../types.js";
33+
import { Wormhole } from "../../wormhole.js";
34+
import type { WormholeTransfer } from "../wormholeTransfer.js";
3535

3636
type GatewayContext<N extends Network> = ChainContext<N, typeof GatewayTransfer.chain>;
3737

@@ -285,12 +285,6 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
285285
// start the WormholeTransfer by submitting transactions to the source chain
286286
// returns a transaction hash
287287
async initiateTransfer(signer: Signer): Promise<TxHash[]> {
288-
/*
289-
0) Check current `state` is valid to call this (eg: state == Created)
290-
1) Figure out where to call and issue transactions
291-
2) Update state
292-
3) return transaction ids
293-
*/
294288
if (this._state !== TransferState.Created)
295289
throw new Error("Invalid state transition in `start`");
296290

@@ -496,12 +490,6 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
496490
// finish the WormholeTransfer by submitting transactions to the destination chain
497491
// returns a transaction hash
498492
async completeTransfer(signer: Signer): Promise<TxHash[]> {
499-
/*
500-
0) check that the current `state` is valid to call this (eg: state == Ready)
501-
1) prepare the transactions and sign them given the signer
502-
2) submit the VAA and transactions on chain
503-
3) return txid of submission
504-
*/
505493
if (this._state < TransferState.Attested)
506494
throw new Error("Invalid state transition in `finish`. Be sure to call `fetchAttestation`.");
507495

@@ -527,7 +515,19 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
527515
return redeemTxs.map(({ txid }) => txid);
528516
}
529517

530-
static async getTransferVaa<N extends Network>(
518+
// Implicitly determine if the chain is Gateway enabled by
519+
// checking to see if the Gateway IBC bridge has a transfer channel setup
520+
// If this is a new chain, add the channels to the constants file
521+
private fromGateway(): boolean {
522+
return this.gatewayIbcBridge.getTransferChannel(this.transfer.from.chain) !== null;
523+
}
524+
private toGateway(): boolean {
525+
return this.gatewayIbcBridge.getTransferChannel(this.transfer.to.chain) !== null;
526+
}
527+
}
528+
529+
export namespace GatewayTransfer {
530+
export async function getTransferVaa<N extends Network>(
531531
wh: Wormhole<N>,
532532
whm: WormholeMessageId,
533533
timeout?: number,
@@ -537,7 +537,7 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
537537
return vaa;
538538
}
539539

540-
static async destinationOverrides<N extends Network>(
540+
export async function destinationOverrides<N extends Network>(
541541
srcChain: ChainContext<N>,
542542
dstChain: ChainContext<N>,
543543
gatewayChain: ChainContext<N, "Wormchain">,
@@ -563,7 +563,11 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
563563

564564
// Lookup the token id for the destination chain given the source chain
565565
// and token id
566-
static async lookupDestinationToken<N extends Network, SC extends Chain, DC extends Chain>(
566+
export async function lookupDestinationToken<
567+
N extends Network,
568+
SC extends Chain,
569+
DC extends Chain,
570+
>(
567571
srcChain: ChainContext<N, SC>,
568572
dstChain: ChainContext<N, DC>,
569573
gatewayChain: ChainContext<N, "Wormchain">,
@@ -601,14 +605,4 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
601605
const dstAddress = await dstTb.getWrappedAsset(lookup);
602606
return { chain: dstChain.chain, address: dstAddress };
603607
}
604-
605-
// Implicitly determine if the chain is Gateway enabled by
606-
// checking to see if the Gateway IBC bridge has a transfer channel setup
607-
// If this is a new chain, add the channels to the constants file
608-
private fromGateway(): boolean {
609-
return this.gatewayIbcBridge.getTransferChannel(this.transfer.from.chain) !== null;
610-
}
611-
private toGateway(): boolean {
612-
return this.gatewayIbcBridge.getTransferChannel(this.transfer.to.chain) !== null;
613-
}
614608
}

connect/src/protocols/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./wormholeTransfer.js";
2+
export * from "./tokenBridge/tokenTransfer.js";
3+
export * from "./cctp/cctpTransfer.js";
4+
export * from "./gateway/gatewayTransfer.js";

0 commit comments

Comments
 (0)