| 
 | 1 | +import { Chain, Network } from "@wormhole-foundation/sdk-base";  | 
 | 2 | + | 
 | 3 | +// The point if this module is to use direct API calls instead of importing the entire @axelar-network/axelarjs-sdk to stay lightweight.  | 
 | 4 | + | 
 | 5 | +// Axelar chains: https://github.com/axelarnetwork/axelarjs-sdk/blob/53a957deb1209325b1e3d109e0985a64db6d9901/src/constants/EvmChain.ts#L1  | 
 | 6 | +export const axelarChains: Partial<Record<Chain, string>> = {  | 
 | 7 | +  Ethereum: "ethereum",  | 
 | 8 | +  Monad: "monad",  | 
 | 9 | +  Sepolia: "ethereum-sepolia",  | 
 | 10 | +  // add more as needed  | 
 | 11 | +};  | 
 | 12 | + | 
 | 13 | +// https://github.com/axelarnetwork/axelarjs-sdk/blob/53a957deb1209325b1e3d109e0985a64db6d9901/src/libs/TransactionRecoveryApi/AxelarRecoveryApi.ts#L16  | 
 | 14 | +export enum GMPStatus {  | 
 | 15 | +  SRC_GATEWAY_CALLED = "source_gateway_called",  | 
 | 16 | +  DEST_GATEWAY_APPROVED = "destination_gateway_approved",  | 
 | 17 | +  DEST_EXECUTED = "destination_executed",  | 
 | 18 | +  EXPRESS_EXECUTED = "express_executed",  | 
 | 19 | +  DEST_EXECUTE_ERROR = "error",  | 
 | 20 | +  DEST_EXECUTING = "executing",  | 
 | 21 | +  APPROVING = "approving",  | 
 | 22 | +  FORECALLED = "forecalled",  | 
 | 23 | +  FORECALLED_WITHOUT_GAS_PAID = "forecalled_without_gas_paid",  | 
 | 24 | +  NOT_EXECUTED = "not_executed",  | 
 | 25 | +  NOT_EXECUTED_WITHOUT_GAS_PAID = "not_executed_without_gas_paid",  | 
 | 26 | +  INSUFFICIENT_FEE = "insufficient_fee",  | 
 | 27 | +  UNKNOWN_ERROR = "unknown_error",  | 
 | 28 | +  CANNOT_FETCH_STATUS = "cannot_fetch_status",  | 
 | 29 | +  SRC_GATEWAY_CONFIRMED = "confirmed",  | 
 | 30 | +}  | 
 | 31 | + | 
 | 32 | +export interface GMPError {  | 
 | 33 | +  txHash: string;  | 
 | 34 | +  chain: string;  | 
 | 35 | +  message: string;  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +export function getAxelarApiUrl(network: Network): string {  | 
 | 39 | +  return network === "Mainnet"  | 
 | 40 | +    ? "https://api.axelarscan.io"  | 
 | 41 | +    : "https://testnet.api.axelarscan.io";  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +export function getAxelarChain(chain: Chain): string {  | 
 | 45 | +  const axelarChain = axelarChains[chain];  | 
 | 46 | +  if (!axelarChain) {  | 
 | 47 | +    throw new Error(`Unsupported axelar chain: ${chain}`);  | 
 | 48 | +  }  | 
 | 49 | +  return axelarChain;  | 
 | 50 | +}  | 
 | 51 | + | 
 | 52 | +export async function getAxelarGasFee(  | 
 | 53 | +  network: Network,  | 
 | 54 | +  sourceChain: Chain,  | 
 | 55 | +  destinationChain: Chain,  | 
 | 56 | +  gasLimit: bigint,  | 
 | 57 | +  timeoutMs = 10000  | 
 | 58 | +): Promise<bigint> {  | 
 | 59 | +  const url = `${getAxelarApiUrl(network)}/gmp/estimateGasFee`;  | 
 | 60 | +  const axelarSourceChain = getAxelarChain(sourceChain);  | 
 | 61 | +  const axelarDestinationChain = getAxelarChain(destinationChain);  | 
 | 62 | + | 
 | 63 | +  const controller = new AbortController();  | 
 | 64 | +  const timeoutId = setTimeout(() => controller.abort(), timeoutMs);  | 
 | 65 | + | 
 | 66 | +  // Set a minimum fee of 1 to avoid 0-fee issue with relays not proceeding  | 
 | 67 | +  // past the gas paid step  | 
 | 68 | +  let fee = 1n;  | 
 | 69 | + | 
 | 70 | +  try {  | 
 | 71 | +    const response = await fetch(url, {  | 
 | 72 | +      method: "POST",  | 
 | 73 | +      headers: {  | 
 | 74 | +        "Content-Type": "application/json",  | 
 | 75 | +      },  | 
 | 76 | +      body: JSON.stringify({  | 
 | 77 | +        sourceChain: axelarSourceChain,  | 
 | 78 | +        destinationChain: axelarDestinationChain,  | 
 | 79 | +        gasMultiplier: "auto",  | 
 | 80 | +        gasLimit: gasLimit.toString(),  | 
 | 81 | +      }),  | 
 | 82 | +      signal: controller.signal,  | 
 | 83 | +    });  | 
 | 84 | + | 
 | 85 | +    if (!response.ok) {  | 
 | 86 | +      const errorText = await response.text();  | 
 | 87 | +      throw new Error(  | 
 | 88 | +        `Failed to estimate gas fee: ${response.status} ${errorText}`  | 
 | 89 | +      );  | 
 | 90 | +    }  | 
 | 91 | + | 
 | 92 | +    const result = await response.json();  | 
 | 93 | + | 
 | 94 | +    const parsedFee = BigInt(result);  | 
 | 95 | +    if (parsedFee > 0n) {  | 
 | 96 | +      fee = parsedFee;  | 
 | 97 | +    }  | 
 | 98 | +  } finally {  | 
 | 99 | +    clearTimeout(timeoutId);  | 
 | 100 | +  }  | 
 | 101 | + | 
 | 102 | +  return fee;  | 
 | 103 | +}  | 
 | 104 | + | 
 | 105 | +export async function getAxelarTransactionStatus(  | 
 | 106 | +  network: Network,  | 
 | 107 | +  sourceChain: Chain,  | 
 | 108 | +  txHash: string,  | 
 | 109 | +  timeoutMs = 10000  | 
 | 110 | +): Promise<{ status: GMPStatus | string; error?: GMPError }> {  | 
 | 111 | +  const url = `${getAxelarApiUrl(network)}/gmp/searchGMP`;  | 
 | 112 | + | 
 | 113 | +  const axelarSourceChain = getAxelarChain(sourceChain);  | 
 | 114 | + | 
 | 115 | +  const controller = new AbortController();  | 
 | 116 | +  const timeoutId = setTimeout(() => controller.abort(), timeoutMs);  | 
 | 117 | + | 
 | 118 | +  try {  | 
 | 119 | +    const response = await fetch(url, {  | 
 | 120 | +      method: "POST",  | 
 | 121 | +      headers: {  | 
 | 122 | +        "Content-Type": "application/json",  | 
 | 123 | +      },  | 
 | 124 | +      body: JSON.stringify({  | 
 | 125 | +        sourceChain: axelarSourceChain,  | 
 | 126 | +        txHash: txHash,  | 
 | 127 | +      }),  | 
 | 128 | +      signal: controller.signal,  | 
 | 129 | +    });  | 
 | 130 | + | 
 | 131 | +    if (!response.ok) {  | 
 | 132 | +      const errorText = await response.text();  | 
 | 133 | +      throw new Error(  | 
 | 134 | +        `Failed to get transaction status: ${response.status} ${errorText}`  | 
 | 135 | +      );  | 
 | 136 | +    }  | 
 | 137 | + | 
 | 138 | +    const result = await response.json();  | 
 | 139 | +    if (!result.data || result.data.length === 0) {  | 
 | 140 | +      throw new Error("No transaction details found");  | 
 | 141 | +    }  | 
 | 142 | + | 
 | 143 | +    const txDetails = result.data[0];  | 
 | 144 | +    return {  | 
 | 145 | +      status: parseGMPStatus(txDetails),  | 
 | 146 | +      error: parseGMPError(txDetails),  | 
 | 147 | +    };  | 
 | 148 | +  } finally {  | 
 | 149 | +    clearTimeout(timeoutId);  | 
 | 150 | +  }  | 
 | 151 | +}  | 
 | 152 | + | 
 | 153 | +export function parseGMPStatus(response: any): GMPStatus | string {  | 
 | 154 | +  const { error, status } = response;  | 
 | 155 | + | 
 | 156 | +  if (status === "error" && error) return GMPStatus.DEST_EXECUTE_ERROR;  | 
 | 157 | +  else if (status === "executed") return GMPStatus.DEST_EXECUTED;  | 
 | 158 | +  else if (status === "approved") return GMPStatus.DEST_GATEWAY_APPROVED;  | 
 | 159 | +  else if (status === "called") return GMPStatus.SRC_GATEWAY_CALLED;  | 
 | 160 | +  else if (status === "executing") return GMPStatus.DEST_EXECUTING;  | 
 | 161 | +  else {  | 
 | 162 | +    return status;  | 
 | 163 | +  }  | 
 | 164 | +}  | 
 | 165 | + | 
 | 166 | +export function parseGMPError(response: any): GMPError | undefined {  | 
 | 167 | +  if (response.error) {  | 
 | 168 | +    return {  | 
 | 169 | +      message: response.error.error.message,  | 
 | 170 | +      txHash: response.error.sourceTransactionHash,  | 
 | 171 | +      chain: response.error.chain,  | 
 | 172 | +    };  | 
 | 173 | +  } else if (response.is_insufficient_fee) {  | 
 | 174 | +    return {  | 
 | 175 | +      message: "Insufficient gas",  | 
 | 176 | +      txHash: response.call.transaction.hash,  | 
 | 177 | +      chain: response.call.chain,  | 
 | 178 | +    };  | 
 | 179 | +  }  | 
 | 180 | +}  | 
 | 181 | + | 
 | 182 | +export function getAxelarExplorerUrl(network: Network, txHash: string): string {  | 
 | 183 | +  return network === "Mainnet"  | 
 | 184 | +    ? `https://axelarscan.io/gmp/${txHash}`  | 
 | 185 | +    : `https://testnet.axelarscan.io/gmp/${txHash}`;  | 
 | 186 | +}  | 
0 commit comments