From c7b61b74700aa80de6a74409afc9cd47a6ebae60 Mon Sep 17 00:00:00 2001 From: Evan Gray Date: Mon, 16 Aug 2021 22:22:36 -0400 Subject: [PATCH] sdk/js: simplify getSignedVAA Change-Id: Ieaecd9070d0b4284467e5097f912c1a44e1827be --- bridge_ui/src/utils/attestFrom.ts | 6 ++--- bridge_ui/src/utils/consts.ts | 1 + bridge_ui/src/utils/getSignedVAAWithRetry.ts | 24 +++++++++++++++++ bridge_ui/src/utils/transferFrom.ts | 6 ++--- sdk/js/src/rpc/getSignedVAA.ts | 27 +++++++------------- 5 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 bridge_ui/src/utils/getSignedVAAWithRetry.ts diff --git a/bridge_ui/src/utils/attestFrom.ts b/bridge_ui/src/utils/attestFrom.ts index 01d2759c..a850a216 100644 --- a/bridge_ui/src/utils/attestFrom.ts +++ b/bridge_ui/src/utils/attestFrom.ts @@ -1,7 +1,6 @@ import { CHAIN_ID_ETH, CHAIN_ID_SOLANA, - getSignedVAA, ixFromRust, Bridge__factory, Implementation__factory, @@ -23,6 +22,7 @@ import { SOL_BRIDGE_ADDRESS, SOL_TOKEN_BRIDGE_ADDRESS, } from "./consts"; +import { getSignedVAAWithRetry } from "./getSignedVAAWithRetry"; // TODO: allow for / handle cancellation? // TODO: overall better input checking and error handling @@ -57,7 +57,7 @@ export async function attestFromEth( const emitterAddress = Buffer.from( zeroPad(arrayify(ETH_TOKEN_BRIDGE_ADDRESS), 32) ).toString("hex"); - const { vaaBytes } = await getSignedVAA( + const { vaaBytes } = await getSignedVAAWithRetry( CHAIN_ID_ETH, emitterAddress, sequence.toString() @@ -146,7 +146,7 @@ export async function attestFromSolana( 32 ) ).toString("hex"); - const { vaaBytes } = await getSignedVAA( + const { vaaBytes } = await getSignedVAAWithRetry( CHAIN_ID_SOLANA, emitterAddress, sequence.toString() diff --git a/bridge_ui/src/utils/consts.ts b/bridge_ui/src/utils/consts.ts index 9cb9a5e8..2d2cfd6a 100644 --- a/bridge_ui/src/utils/consts.ts +++ b/bridge_ui/src/utils/consts.ts @@ -34,6 +34,7 @@ export const CHAINS_BY_ID: ChainsById = CHAINS.reduce((obj, chain) => { obj[chain.id] = chain; return obj; }, {} as ChainsById); +export const WORMHOLE_RPC_HOST = "http://localhost:8080"; export const SOLANA_HOST = "http://localhost:8899"; export const ETH_TEST_TOKEN_ADDRESS = getAddress( "0x0290FB167208Af455bB137780163b7B7a9a10C16" diff --git a/bridge_ui/src/utils/getSignedVAAWithRetry.ts b/bridge_ui/src/utils/getSignedVAAWithRetry.ts new file mode 100644 index 00000000..01862519 --- /dev/null +++ b/bridge_ui/src/utils/getSignedVAAWithRetry.ts @@ -0,0 +1,24 @@ +import { ChainId, getSignedVAA } from "@certusone/wormhole-sdk"; +import { WORMHOLE_RPC_HOST } from "./consts"; + +export async function getSignedVAAWithRetry( + emitterChain: ChainId, + emitterAddress: string, + sequence: string +) { + let result; + while (!result) { + await new Promise((resolve) => setTimeout(resolve, 1000)); + try { + result = await getSignedVAA( + WORMHOLE_RPC_HOST, + emitterChain, + emitterAddress, + sequence + ); + } catch (e) { + console.log(e); + } + } + return result; +} diff --git a/bridge_ui/src/utils/transferFrom.ts b/bridge_ui/src/utils/transferFrom.ts index bd05b2a4..78eb9035 100644 --- a/bridge_ui/src/utils/transferFrom.ts +++ b/bridge_ui/src/utils/transferFrom.ts @@ -3,7 +3,6 @@ import { ChainId, CHAIN_ID_ETH, CHAIN_ID_SOLANA, - getSignedVAA, Implementation__factory, ixFromRust, TokenImplementation__factory, @@ -27,6 +26,7 @@ import { SOL_BRIDGE_ADDRESS, SOL_TOKEN_BRIDGE_ADDRESS, } from "./consts"; +import { getSignedVAAWithRetry } from "./getSignedVAAWithRetry"; // TODO: allow for / handle cancellation? // TODO: overall better input checking and error handling @@ -91,7 +91,7 @@ export async function transferFromEth( const emitterAddress = Buffer.from( zeroPad(arrayify(ETH_TOKEN_BRIDGE_ADDRESS), 32) ).toString("hex"); - const { vaaBytes } = await getSignedVAA( + const { vaaBytes } = await getSignedVAAWithRetry( CHAIN_ID_ETH, emitterAddress, sequence.toString() @@ -239,7 +239,7 @@ export async function transferFromSolana( 32 ) ).toString("hex"); - const { vaaBytes } = await getSignedVAA( + const { vaaBytes } = await getSignedVAAWithRetry( CHAIN_ID_SOLANA, emitterAddress, sequence diff --git a/sdk/js/src/rpc/getSignedVAA.ts b/sdk/js/src/rpc/getSignedVAA.ts index c4b13fe8..c3c1bde2 100644 --- a/sdk/js/src/rpc/getSignedVAA.ts +++ b/sdk/js/src/rpc/getSignedVAA.ts @@ -5,27 +5,18 @@ import { } from "../proto/publicrpc/v1/publicrpc"; export async function getSignedVAA( + host: string, emitterChain: ChainId, emitterAddress: string, sequence: string ) { - const rpc = new GrpcWebImpl("http://localhost:8080", {}); // TODO: make this a parameter + const rpc = new GrpcWebImpl(host, {}); const api = new PublicrpcClientImpl(rpc); - // TODO: move this loop outside sdk - let result; - while (!result) { - await new Promise((resolve) => setTimeout(resolve, 1000)); - try { - result = await api.GetSignedVAA({ - messageId: { - emitterChain, - emitterAddress, - sequence, - }, - }); - } catch (e) { - // TODO: instead of try/catch, simply return api.GetSignedVAA - } - } - return result; + return await api.GetSignedVAA({ + messageId: { + emitterChain, + emitterAddress, + sequence, + }, + }); }