From 4698ab1798ce8ab3924ad71f511d10f68e4a3ddf Mon Sep 17 00:00:00 2001 From: Ben Guidarelli Date: Thu, 28 Dec 2023 10:29:22 -0500 Subject: [PATCH] yield the receipt from the tracker generator --- connect/src/protocols/tokenTransfer.ts | 38 ++++++++++++++------------ connect/src/wormholeTransfer.ts | 9 ++++-- examples/src/helpers/helpers.ts | 19 +++++++++---- examples/src/tokenBridge.ts | 2 +- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/connect/src/protocols/tokenTransfer.ts b/connect/src/protocols/tokenTransfer.ts index eea8537a6..3c212b284 100644 --- a/connect/src/protocols/tokenTransfer.ts +++ b/connect/src/protocols/tokenTransfer.ts @@ -38,11 +38,11 @@ import { WormholeTransfer, } from "../wormholeTransfer"; -type TransferProtocol = "TokenBridge" | "AutomaticTokenBridge"; -type TransferVAA = TokenBridge.TransferVAA | AutomaticTokenBridge.VAA; +type TokenTransferProtocol = "TokenBridge" | "AutomaticTokenBridge"; +type TokenTransferVAA = TokenBridge.TransferVAA | AutomaticTokenBridge.VAA; export class TokenTransfer - implements WormholeTransfer + implements WormholeTransfer { private readonly wh: Wormhole; @@ -59,7 +59,7 @@ export class TokenTransfer // on the source chain (if its been completed and finalized) vaas?: { id: WormholeMessageId; - vaa?: TransferVAA; + vaa?: TokenTransferVAA; }[]; private constructor(wh: Wormhole, transfer: TokenTransferDetails) { @@ -257,15 +257,13 @@ export class TokenTransfer return redeemTxids.map(({ txid }) => txid); } - // Async fn that produces status updates through an async generator + // AsyncGenerator fn that produces status updates through an async generator // eventually producing a receipt // can be called repeatedly so the receipt is updated as it moves through the // steps of the transfer - // Note: it should be possible to call this fn as many times - // as it takes until the destination is finalized static async *track( wh: Wormhole, - receipt: TransferReceipt, + receipt: TransferReceipt, timeout: number = DEFAULT_TASK_TIMEOUT, // Optional parameters to override chain context (typically for custom rpc) _fromChain?: ChainContext, typeof receipt.from>, @@ -292,7 +290,7 @@ export class TokenTransfer ); receipt.attestation = { id: xfermsg }; receipt.state = TransferState.SourceFinalized; - yield receipt.state; + yield receipt; } } @@ -311,7 +309,7 @@ export class TokenTransfer ); receipt.attestation.attestation = vaa; receipt.state = TransferState.Attested; - yield receipt.state; + yield receipt; } } @@ -325,7 +323,10 @@ export class TokenTransfer receipt.attestation.id!, leftover(start, timeout), ); - if (!txStatus) return receipt; + if (!txStatus) { + yield receipt; + return; + } if (txStatus.globalTx?.destinationTx?.txHash) { const { chainId, txHash } = txStatus.globalTx.destinationTx; @@ -338,7 +339,7 @@ export class TokenTransfer ]; receipt.state = TransferState.DestinationFinalized; - yield receipt.state; + yield receipt; } // Fall back to asking the destination chain if this VAA has been redeemed @@ -347,15 +348,16 @@ export class TokenTransfer receipt.attestation.attestation && (await TokenTransfer.isTransferComplete( _toChain, - receipt.attestation.attestation as TransferVAA, + receipt.attestation.attestation as TokenTransferVAA, ), leftover(start, timeout)) ) { receipt.state = TransferState.DestinationFinalized; - yield receipt.state; + yield receipt; } } - return receipt; + yield receipt; + return; } // Static method to perform the transfer so a custom RPC may be used @@ -387,7 +389,7 @@ export class TokenTransfer // Static method to allow passing a custom RPC static async redeem( toChain: ChainContext, - vaa: TokenBridge.TransferVAA | AutomaticTokenBridge.VAA, + vaa: TokenTransferVAA, signer: Signer, ): Promise { const signerAddress = toNative(signer.chain(), signer.address()); @@ -404,7 +406,7 @@ export class TokenTransfer N extends Network, P extends Platform, C extends PlatformToChains

, - >(toChain: ChainContext, vaa: TransferVAA): Promise { + >(toChain: ChainContext, vaa: TokenTransferVAA): Promise { // TODO: converter? if (vaa.protocolName === "AutomaticTokenBridge") vaa = deserialize("TokenBridge:TransferWithPayload", serialize(vaa)); @@ -428,7 +430,7 @@ export class TokenTransfer wh: Wormhole, key: WormholeMessageId | TxHash, timeout?: number, - ): Promise { + ): Promise { const vaa = typeof key === "string" ? await wh.getVaaByTxHash(key, TokenBridge.getTransferDiscriminator(), timeout) diff --git a/connect/src/wormholeTransfer.ts b/connect/src/wormholeTransfer.ts index 5f32af1ec..06ad3b1d9 100644 --- a/connect/src/wormholeTransfer.ts +++ b/connect/src/wormholeTransfer.ts @@ -86,20 +86,23 @@ export type TransferQuote = { }; // Static methods on the Transfer protocol types +// e.g. `TokenTransfer.constructor` export interface TransferProtocol { isTransferComplete>( toChain: ChainContext, vaa: VAA, ): Promise; - isAutomatic(wh: Wormhole, vaa: VAA): boolean; - //validateTransfer(wh: Wormhole, transfer: ) + validateTransferDetails( + wh: Wormhole, + transfer: TransferRequest, + ): Promise; quoteTransfer(xfer: WormholeTransfer): Promise; getReceipt(xfer: WormholeTransfer): TransferReceipt; track( wh: Wormhole, xfer: WormholeTransfer, timeout: number, - ): AsyncGenerator, unknown>; + ): AsyncGenerator, unknown, unknown>; } // WormholeTransfer abstracts the process and state transitions diff --git a/examples/src/helpers/helpers.ts b/examples/src/helpers/helpers.ts index 0de3ed6fe..79fb47d15 100644 --- a/examples/src/helpers/helpers.ts +++ b/examples/src/helpers/helpers.ts @@ -11,6 +11,7 @@ import { Wormhole, api, tasks, + DEFAULT_TASK_TIMEOUT, } from "@wormhole-foundation/connect-sdk"; // Importing from src so we dont have to rebuild to see debug stuff in signer @@ -70,12 +71,18 @@ export async function getStuff< }; } -export async function waitLog(wh: Wormhole, xfer: TokenTransfer) { - const it = TokenTransfer.track(wh, TokenTransfer.getReceipt(xfer)); - let res; - for (res = await it.next(); !res.done; res = await it.next()) - console.log("Current Transfer State: ", TransferState[res.value as TransferState]); - return res.value; +export async function waitLog( + wh: Wormhole, + xfer: TokenTransfer, + tag: string = "WaitLog", + timeout: number = DEFAULT_TASK_TIMEOUT, +) { + const tracker = TokenTransfer.track(wh, TokenTransfer.getReceipt(xfer), timeout); + let receipt; + for await (receipt of tracker) { + console.log(`${tag}: Current trasfer state: `, TransferState[receipt.state]); + } + return receipt; } // Note: This API may change but it is currently the best place to pull diff --git a/examples/src/tokenBridge.ts b/examples/src/tokenBridge.ts index 5d841844e..51290d518 100644 --- a/examples/src/tokenBridge.ts +++ b/examples/src/tokenBridge.ts @@ -49,7 +49,7 @@ import "@wormhole-foundation/connect-sdk-solana-tokenbridge"; // of the token // On the destination side, a wrapped version of the token will be minted // to the address specified in the transfer VAA - const automatic = true; + const automatic = false; // The automatic relayer has the ability to deliver some native gas funds to the destination account // The amount specified for native gas will be swapped for the native gas token according