Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wormholescan: update relays api method #790

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 57 additions & 43 deletions connect/src/whscan-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,42 +71,51 @@ export interface TransactionStatus {
};
}

export interface RelayData {
from: {
chain: string;
chainId: number;
txHash: string;
senderAddress: string;
symbol: string;
amountSent: number;
amountToSwap: number;
estimatedNativeAssetAmount: number;
};
vaa: string;
status: string;
fee: {
amount: number;
symbol: string;
};
error: any;
to: {
chain: string;
chainId: number;
recipientAddress: string;
txHash: string;
gasUsed: number;
nativeAssetSymbol: string;
nativeAssetReceived: number;
};
metrics: {
receivedAt: string;
completedAt: string;
failedAt: any;
attempts: number;
export interface RelayStatus {
completedAt: string;
data: {
delivery: {
budget: string;
execution: {
detail: string;
gasUsed: string;
refundStatus: string;
revertString: string;
status: string;
transactionHash: string;
};
maxRefund: string;
relayGasUsed: number;
targetChainDecimals: number;
};
fromTxHash: string;
instructions: {
encodedExecutionInfo: string;
extraReceiverValue: {
_hex: string;
_isBigNumber: boolean;
};
refundAddress: string;
refundChainId: number;
refundDeliveryProvider: string;
requestedReceiverValue: {
_hex: string;
_isBigNumber: boolean;
};
senderAddress: string;
sourceDeliveryProvider: string;
targetAddress: string;
targetChainId: number;
vaaKeys: string[];
};
maxAttempts: number;
waitingForTxInMs: number;
waitingForWalletInMs: number;
toTxHash: string;
};
failedAt: string;
id: string;
receivedAt: string;
relayer: string;
status: string;
}

export interface ApiVaa {
Expand Down Expand Up @@ -235,11 +244,17 @@ export async function getTransactionStatusWithRetry(
);
}

export async function getRelayStatus(rpcUrl: string, txid: TxHash): Promise<RelayData | null> {
const url = `${rpcUrl}/v1/relays?txHash=${txid}`;
export async function getRelayStatus(
rpcUrl: string,
whm: WormholeMessageId,
): Promise<RelayStatus | null> {
const { chain, emitter, sequence } = whm;
const chainId = toChainId(chain);
const emitterAddress = emitter.toUniversalAddress().toString();
const url = `${rpcUrl}/api/v1/relays/${chainId}/${emitterAddress}/${sequence}`;
try {
const response = await axios.get<{ data: RelayData }>(url);
if (response.data.data.to.txHash) return response.data.data;
const response = await axios.get<RelayStatus>(url);
return response.data || null;
} catch (error) {
if (!error) return null;
if (typeof error === "object") {
Expand All @@ -250,16 +265,15 @@ export async function getRelayStatus(rpcUrl: string, txid: TxHash): Promise<Rela
}
throw error;
}
return null;
}

export async function getRelayStatusWithRetry(
rpcUrl: string,
txid: TxHash,
whm: WormholeMessageId,
timeout: number,
): Promise<RelayData | null> {
const task = () => getRelayStatus(rpcUrl, txid);
return retry<RelayData>(task, WHSCAN_RETRY_INTERVAL, timeout, "Wormholescan:GetRelayStatus");
): Promise<RelayStatus | null> {
const task = () => getRelayStatus(rpcUrl, whm);
return retry<RelayStatus>(task, WHSCAN_RETRY_INTERVAL, timeout, "Wormholescan:GetRelayStatus");
}

export async function getVaaByTxHash(rpcUrl: string, txid: string): Promise<ApiVaa | null> {
Expand Down
13 changes: 12 additions & 1 deletion connect/src/wormhole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import { TokenTransfer } from "./protocols/tokenBridge/tokenTransfer.js";
import type { RouteConstructor } from "./routes/index.js";
import { RouteResolver } from "./routes/resolver.js";
import { retry } from "./tasks.js";
import type { TransactionStatus } from "./whscan-api.js";
import type { RelayStatus, TransactionStatus } from "./whscan-api.js";
import {
getIsVaaEnqueued,
getRelayStatus,
getTransactionStatusWithRetry,
getTxsByAddress,
getVaaByTxHashWithRetry,
Expand Down Expand Up @@ -333,6 +334,16 @@ export class Wormhole<N extends Network> {
return await getVaaWithRetry(this.config.api, id, decodeAs, timeout);
}

/**
* Gets the RelayStatus for a given WormholeMessageId
*
* @param wormholeMessageId The WormholeMessageId corresponding to the relay status to be fetched
* @returns The RelayStatus if available otherwise null
*/
async getRelayStatus(wormholeMessageId: WormholeMessageId): Promise<RelayStatus | null> {
return await getRelayStatus(this.config.api, wormholeMessageId);
}

/**
* Gets if the token bridge transfer VAA has been enqueued by the Governor.
* @param id The WormholeMessageId corresponding to the token bridge transfer VAA to check
Expand Down