Skip to content

Commit

Permalink
Protocols: Refactor protocols to provide better utility methods (#546)
Browse files Browse the repository at this point in the history
* initial pass for refactor

* Move protocols to directories

* Eliminate from/to addresses from quote arg requirements
  • Loading branch information
barnjamin authored May 15, 2024
1 parent fffeaa3 commit 2b7361b
Show file tree
Hide file tree
Showing 18 changed files with 561 additions and 575 deletions.
5 changes: 1 addition & 4 deletions connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ export * from "./config.js";
export * from "./common.js";
export * from "./types.js";

export * from "./protocols/wormholeTransfer.js";
export * from "./protocols/tokenTransfer.js";
export * from "./protocols/cctpTransfer.js";
export * from "./protocols/gatewayTransfer.js";
export * from "./protocols/index.js";

export * as tasks from "./tasks.js";
export * as circleApi from "./circle-api.js";
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import {
toGatewayMsg,
toNative,
} from "@wormhole-foundation/sdk-definitions";
import { signSendWait } from "../common.js";
import { fetchIbcXfer, isTokenBridgeVaaRedeemed, retry } from "../tasks.js";
import { TransferState } from "../types.js";
import { Wormhole } from "../wormhole.js";
import type { WormholeTransfer } from "./wormholeTransfer.js";
import { signSendWait } from "../../common.js";
import { fetchIbcXfer, isTokenBridgeVaaRedeemed, retry } from "../../tasks.js";
import { TransferState } from "../../types.js";
import { Wormhole } from "../../wormhole.js";
import type { WormholeTransfer } from "../wormholeTransfer.js";

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

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

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

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

static async getTransferVaa<N extends Network>(
// Implicitly determine if the chain is Gateway enabled by
// checking to see if the Gateway IBC bridge has a transfer channel setup
// If this is a new chain, add the channels to the constants file
private fromGateway(): boolean {
return this.gatewayIbcBridge.getTransferChannel(this.transfer.from.chain) !== null;
}
private toGateway(): boolean {
return this.gatewayIbcBridge.getTransferChannel(this.transfer.to.chain) !== null;
}
}

export namespace GatewayTransfer {
export async function getTransferVaa<N extends Network>(
wh: Wormhole<N>,
whm: WormholeMessageId,
timeout?: number,
Expand All @@ -537,7 +537,7 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
return vaa;
}

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

// Lookup the token id for the destination chain given the source chain
// and token id
static async lookupDestinationToken<N extends Network, SC extends Chain, DC extends Chain>(
export async function lookupDestinationToken<
N extends Network,
SC extends Chain,
DC extends Chain,
>(
srcChain: ChainContext<N, SC>,
dstChain: ChainContext<N, DC>,
gatewayChain: ChainContext<N, "Wormchain">,
Expand Down Expand Up @@ -601,14 +605,4 @@ export class GatewayTransfer<N extends Network = Network> implements WormholeTra
const dstAddress = await dstTb.getWrappedAsset(lookup);
return { chain: dstChain.chain, address: dstAddress };
}

// Implicitly determine if the chain is Gateway enabled by
// checking to see if the Gateway IBC bridge has a transfer channel setup
// If this is a new chain, add the channels to the constants file
private fromGateway(): boolean {
return this.gatewayIbcBridge.getTransferChannel(this.transfer.from.chain) !== null;
}
private toGateway(): boolean {
return this.gatewayIbcBridge.getTransferChannel(this.transfer.to.chain) !== null;
}
}
4 changes: 4 additions & 0 deletions connect/src/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./wormholeTransfer.js";
export * from "./tokenBridge/tokenTransfer.js";
export * from "./cctp/cctpTransfer.js";
export * from "./gateway/gatewayTransfer.js";
Loading

0 comments on commit 2b7361b

Please sign in to comment.