sdk/js: simplify getSignedVAA

Change-Id: Ieaecd9070d0b4284467e5097f912c1a44e1827be
This commit is contained in:
Evan Gray 2021-08-16 22:22:36 -04:00
parent 1c2e7444e8
commit c7b61b7470
5 changed files with 40 additions and 24 deletions

View File

@ -1,7 +1,6 @@
import { import {
CHAIN_ID_ETH, CHAIN_ID_ETH,
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
getSignedVAA,
ixFromRust, ixFromRust,
Bridge__factory, Bridge__factory,
Implementation__factory, Implementation__factory,
@ -23,6 +22,7 @@ import {
SOL_BRIDGE_ADDRESS, SOL_BRIDGE_ADDRESS,
SOL_TOKEN_BRIDGE_ADDRESS, SOL_TOKEN_BRIDGE_ADDRESS,
} from "./consts"; } from "./consts";
import { getSignedVAAWithRetry } from "./getSignedVAAWithRetry";
// TODO: allow for / handle cancellation? // TODO: allow for / handle cancellation?
// TODO: overall better input checking and error handling // TODO: overall better input checking and error handling
@ -57,7 +57,7 @@ export async function attestFromEth(
const emitterAddress = Buffer.from( const emitterAddress = Buffer.from(
zeroPad(arrayify(ETH_TOKEN_BRIDGE_ADDRESS), 32) zeroPad(arrayify(ETH_TOKEN_BRIDGE_ADDRESS), 32)
).toString("hex"); ).toString("hex");
const { vaaBytes } = await getSignedVAA( const { vaaBytes } = await getSignedVAAWithRetry(
CHAIN_ID_ETH, CHAIN_ID_ETH,
emitterAddress, emitterAddress,
sequence.toString() sequence.toString()
@ -146,7 +146,7 @@ export async function attestFromSolana(
32 32
) )
).toString("hex"); ).toString("hex");
const { vaaBytes } = await getSignedVAA( const { vaaBytes } = await getSignedVAAWithRetry(
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
emitterAddress, emitterAddress,
sequence.toString() sequence.toString()

View File

@ -34,6 +34,7 @@ export const CHAINS_BY_ID: ChainsById = CHAINS.reduce((obj, chain) => {
obj[chain.id] = chain; obj[chain.id] = chain;
return obj; return obj;
}, {} as ChainsById); }, {} as ChainsById);
export const WORMHOLE_RPC_HOST = "http://localhost:8080";
export const SOLANA_HOST = "http://localhost:8899"; export const SOLANA_HOST = "http://localhost:8899";
export const ETH_TEST_TOKEN_ADDRESS = getAddress( export const ETH_TEST_TOKEN_ADDRESS = getAddress(
"0x0290FB167208Af455bB137780163b7B7a9a10C16" "0x0290FB167208Af455bB137780163b7B7a9a10C16"

View File

@ -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;
}

View File

@ -3,7 +3,6 @@ import {
ChainId, ChainId,
CHAIN_ID_ETH, CHAIN_ID_ETH,
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
getSignedVAA,
Implementation__factory, Implementation__factory,
ixFromRust, ixFromRust,
TokenImplementation__factory, TokenImplementation__factory,
@ -27,6 +26,7 @@ import {
SOL_BRIDGE_ADDRESS, SOL_BRIDGE_ADDRESS,
SOL_TOKEN_BRIDGE_ADDRESS, SOL_TOKEN_BRIDGE_ADDRESS,
} from "./consts"; } from "./consts";
import { getSignedVAAWithRetry } from "./getSignedVAAWithRetry";
// TODO: allow for / handle cancellation? // TODO: allow for / handle cancellation?
// TODO: overall better input checking and error handling // TODO: overall better input checking and error handling
@ -91,7 +91,7 @@ export async function transferFromEth(
const emitterAddress = Buffer.from( const emitterAddress = Buffer.from(
zeroPad(arrayify(ETH_TOKEN_BRIDGE_ADDRESS), 32) zeroPad(arrayify(ETH_TOKEN_BRIDGE_ADDRESS), 32)
).toString("hex"); ).toString("hex");
const { vaaBytes } = await getSignedVAA( const { vaaBytes } = await getSignedVAAWithRetry(
CHAIN_ID_ETH, CHAIN_ID_ETH,
emitterAddress, emitterAddress,
sequence.toString() sequence.toString()
@ -239,7 +239,7 @@ export async function transferFromSolana(
32 32
) )
).toString("hex"); ).toString("hex");
const { vaaBytes } = await getSignedVAA( const { vaaBytes } = await getSignedVAAWithRetry(
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
emitterAddress, emitterAddress,
sequence sequence

View File

@ -5,27 +5,18 @@ import {
} from "../proto/publicrpc/v1/publicrpc"; } from "../proto/publicrpc/v1/publicrpc";
export async function getSignedVAA( export async function getSignedVAA(
host: string,
emitterChain: ChainId, emitterChain: ChainId,
emitterAddress: string, emitterAddress: string,
sequence: 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); const api = new PublicrpcClientImpl(rpc);
// TODO: move this loop outside sdk return await api.GetSignedVAA({
let result; messageId: {
while (!result) { emitterChain,
await new Promise((resolve) => setTimeout(resolve, 1000)); emitterAddress,
try { sequence,
result = await api.GetSignedVAA({ },
messageId: { });
emitterChain,
emitterAddress,
sequence,
},
});
} catch (e) {
// TODO: instead of try/catch, simply return api.GetSignedVAA
}
}
return result;
} }