|
1 | | -import { amount as sdkAmount } from "@wormhole-foundation/sdk-base"; |
| 1 | +import { |
| 2 | + Chain, |
| 3 | + Network, |
| 4 | + toChainId, |
| 5 | + amount as sdkAmount, |
| 6 | +} from "@wormhole-foundation/sdk-base"; |
| 7 | +import { SignedQuote } from "@wormhole-foundation/sdk-definitions"; |
| 8 | +import axios from "axios"; |
| 9 | +import { apiBaseUrl } from "./consts.js"; |
2 | 10 | import { NttRoute } from "../types.js"; |
3 | 11 |
|
| 12 | +export enum RelayStatus { |
| 13 | + Pending = "pending", |
| 14 | + Failed = "failed", |
| 15 | + Unsupported = "unsupported", |
| 16 | + Submitted = "submitted", |
| 17 | + Underpaid = "underpaid", |
| 18 | + Aborted = "aborted", |
| 19 | +} |
| 20 | + |
| 21 | +export type RequestForExecution = { |
| 22 | + quoterAddress: `0x${string}`; |
| 23 | + amtPaid: string; |
| 24 | + dstChain: number; |
| 25 | + dstAddr: `0x${string}`; |
| 26 | + refundAddr: `0x${string}`; |
| 27 | + signedQuoteBytes: `0x${string}`; |
| 28 | + requestBytes: `0x${string}`; |
| 29 | + relayInstructionsBytes: `0x${string}`; |
| 30 | + timestamp: Date; |
| 31 | +}; |
| 32 | + |
| 33 | +export type TxInfo = { |
| 34 | + txHash: string; |
| 35 | + chainId: number; |
| 36 | + blockNumber: string; |
| 37 | + blockTime: Date | null; |
| 38 | + cost: string; |
| 39 | +}; |
| 40 | + |
| 41 | +export type RelayData = { |
| 42 | + id: `0x${string}`; |
| 43 | + txHash: string; |
| 44 | + chainId: number; |
| 45 | + status: string; |
| 46 | + estimatedCost: string; |
| 47 | + requestForExecution: RequestForExecution; |
| 48 | + instruction?: Request; |
| 49 | + txs?: TxInfo[]; |
| 50 | + indexed_at: Date; |
| 51 | +}; |
| 52 | + |
| 53 | +export enum RequestPrefix { |
| 54 | + ERM1 = "ERM1", // MM |
| 55 | + ERV1 = "ERV1", // VAA_V1 |
| 56 | + ERN1 = "ERN1", // NTT_V1 |
| 57 | + ERC1 = "ERC1", // CCTP_V1 |
| 58 | + ERC2 = "ERC2", // CCTP_V2 |
| 59 | +} |
| 60 | + |
| 61 | +export type Capabilities = { |
| 62 | + requestPrefixes: Array<keyof typeof RequestPrefix>; |
| 63 | + gasDropOffLimit: string; |
| 64 | + maxGasLimit: string; |
| 65 | + maxMsgValue: string; // the maximum msgValue, inclusive of the gasDropOffLimit |
| 66 | +}; |
| 67 | + |
| 68 | +export interface CapabilitiesResponse { |
| 69 | + [chainId: string]: Capabilities; |
| 70 | +} |
| 71 | + |
| 72 | +export async function fetchCapabilities( |
| 73 | + network: Network |
| 74 | +): Promise<CapabilitiesResponse> { |
| 75 | + const url = `${apiBaseUrl[network]}/v0/capabilities`; |
| 76 | + |
| 77 | + try { |
| 78 | + const response = await axios.get<CapabilitiesResponse>(url); |
| 79 | + return response.data; |
| 80 | + } catch (error) { |
| 81 | + throw new Error(`Failed to fetch capabilities for network: ${network}.`); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export interface QuoteResponse { |
| 86 | + signedQuote: `0x${string}`; |
| 87 | + estimatedCost?: string; |
| 88 | +} |
| 89 | + |
| 90 | +export async function fetchSignedQuote( |
| 91 | + network: Network, |
| 92 | + srcChain: Chain, |
| 93 | + dstChain: Chain, |
| 94 | + relayInstructions: string // TODO: `0x:${string}` |
| 95 | +): Promise<QuoteResponse> { |
| 96 | + const url = `${apiBaseUrl[network]}/v0/quote`; |
| 97 | + |
| 98 | + try { |
| 99 | + const response = await axios.post<QuoteResponse>(url, { |
| 100 | + srcChain: toChainId(srcChain), |
| 101 | + dstChain: toChainId(dstChain), |
| 102 | + relayInstructions, |
| 103 | + }); |
| 104 | + return response.data; |
| 105 | + } catch (error) { |
| 106 | + throw new Error(`Failed to fetch signed quote.`); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export interface StatusResponse extends RelayData { |
| 111 | + signedQuote: SignedQuote; |
| 112 | + estimatedCost: string; |
| 113 | +} |
| 114 | + |
| 115 | +// Fetch Status |
| 116 | +export async function fetchStatus( |
| 117 | + network: Network, |
| 118 | + txHash: string, |
| 119 | + chain: Chain |
| 120 | +): Promise<StatusResponse[]> { |
| 121 | + const url = `${apiBaseUrl[network]}/v0/status/tx`; |
| 122 | + |
| 123 | + try { |
| 124 | + const response = await axios.post<StatusResponse[]>(url, { |
| 125 | + txHash, |
| 126 | + chainId: toChainId(chain), |
| 127 | + }); |
| 128 | + return response.data; |
| 129 | + } catch (error) { |
| 130 | + throw new Error(`Failed to fetch status for txHash: ${txHash}.`); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export function isRelayStatusFailed(status: string): boolean { |
| 135 | + return ( |
| 136 | + status === RelayStatus.Failed || // this could happen if simulation fails |
| 137 | + status === RelayStatus.Underpaid || // only happens if you don't pay at least the costEstimate |
| 138 | + status === RelayStatus.Unsupported || // capabilities check didn't pass |
| 139 | + status === RelayStatus.Aborted |
| 140 | + ); |
| 141 | +} |
| 142 | + |
4 | 143 | const MAX_U16 = 65_535n; |
5 | 144 | export function calculateReferrerFee( |
6 | 145 | _amount: sdkAmount.Amount, |
|
0 commit comments