Skip to content

Commit

Permalink
simplify sourceTx fetchVaaHash contract (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
mat1asm authored Oct 30, 2023
1 parent 7f97bcc commit 6c56f6e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions relayer/middleware/source-tx.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export interface SourceTxContext extends Context {
}

export const wormscanEndpoints: { [k in Environment]: string | undefined } = {
[Environment.MAINNET]: "https://api.wormscan.io",
[Environment.TESTNET]: "https://api.testnet.wormscan.io",
[Environment.MAINNET]: "https://api.wormholescan.io",
[Environment.TESTNET]: "https://api.testnet.wormholescan.io",
[Environment.DEVNET]: undefined,
};

Expand Down Expand Up @@ -65,26 +65,19 @@ function ifVAAFinalized(vaa: ParsedVaaWithBytes) {
return consistencyLevel !== 200 && consistencyLevel !== 201;
}

let wormholescan: WormholescanClient;

export function sourceTx(
optsWithoutDefaults?: SourceTxOpts,
): Middleware<SourceTxContext> {
let opts: SourceTxOpts;
let wormholescan: WormholescanClient;
const alreadyFetchedHashes = new LRUCache({ max: 1_000 });

return async (ctx, next) => {
if (!opts) {
// initialize options now that we know the environment from context
opts = Object.assign({}, defaultOptsByEnv[ctx.env], optsWithoutDefaults);
}
if (!wormholescan) {
wormholescan = new WormholescanClient(new URL(opts.wormscanEndpoint), {
retries: opts.retries,
initialDelay: opts.initialDelay,
maxDelay: opts.maxDelay,
timeout: opts.timeout,
});
}

const vaaId = `${ctx.vaa.id.emitterChain}-${ctx.vaa.id.emitterAddress}-${ctx.vaa.id.sequence}`;
const txHashFromCache = alreadyFetchedHashes.get(vaaId) as
Expand All @@ -105,7 +98,8 @@ export function sourceTx(
emitterAddress,
sequence,
ctx.logger,
wormholescan,
ctx.env,
opts,
);
if (txHash === "") {
ctx.logger?.debug("Could not retrieve tx hash.");
Expand All @@ -126,8 +120,19 @@ export async function fetchVaaHash(
emitterAddress: Buffer,
sequence: bigint,
logger: Logger,
wormholescan: WormholescanClient,
env: Environment,
sourceTxOpts?: SourceTxOpts,
) {
if (!wormholescan) {
const opts = sourceTxOpts || defaultOptsByEnv[env];
wormholescan = new WormholescanClient(new URL(opts.wormscanEndpoint), {
retries: opts.retries,
initialDelay: opts.initialDelay,
maxDelay: opts.maxDelay,
timeout: opts.timeout,
});
}

const response = await wormholescan.getVaa(
emitterChain,
emitterAddress.toString("hex"),
Expand Down

0 comments on commit 6c56f6e

Please sign in to comment.