11import { Chain , Network } from "@wormhole-foundation/sdk-base" ;
22import {
3- AxelarQueryAPI ,
3+ AxelarGMPRecoveryAPI ,
44 Environment ,
5- EvmChain ,
5+ GMPStatusResponse ,
66} from "@axelar-network/axelarjs-sdk" ;
77
8- export const axelarChains : Partial < Record < Chain , EvmChain > > = {
9- Sepolia : EvmChain . SEPOLIA ,
10- // Monad: EvmChain.MONAD,
11- Ethereum : EvmChain . ETHEREUM ,
8+ // See: https://github.com/axelarnetwork/axelarjs-sdk/blob/main/src/constants/EvmChain.ts
9+ export const axelarChains : Partial < Record < Chain , string > > = {
10+ Ethereum : "ethereum" ,
11+ Monad : "monad" ,
12+ Sepolia : "ethereum-sepolia" ,
1213 // add more as needed
1314} ;
1415
1516export async function getAxelarGasFee (
1617 network : Network ,
1718 sourceChain : Chain ,
1819 destinationChain : Chain ,
19- gasLimit : bigint
20+ gasLimit : bigint ,
21+ timeoutMs = 10000
2022) : Promise < bigint > {
21- const api = new AxelarQueryAPI ( {
22- environment :
23- network === "Mainnet" ? Environment . MAINNET : Environment . TESTNET ,
24- } ) ;
23+ const baseUrl =
24+ network === "Mainnet"
25+ ? "https://api.axelarscan.io/gmp/estimateGasFee"
26+ : "https://testnet.api.axelarscan.io/gmp/estimateGasFee" ;
2527
2628 const axelarSourceChain = axelarChains [ sourceChain ] ;
2729 if ( ! axelarSourceChain ) {
@@ -33,15 +35,54 @@ export async function getAxelarGasFee(
3335 throw new Error ( `Unsupported destination chain: ${ destinationChain } ` ) ;
3436 }
3537
36- const response = await api . estimateGasFee (
37- axelarSourceChain ,
38- axelarDestinationChain ,
39- gasLimit
40- ) ;
38+ const controller = new AbortController ( ) ;
39+ const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeoutMs ) ;
40+
41+ try {
42+ const response = await fetch ( baseUrl , {
43+ method : "POST" ,
44+ headers : {
45+ "Content-Type" : "application/json" ,
46+ } ,
47+ body : JSON . stringify ( {
48+ sourceChain : axelarSourceChain ,
49+ destinationChain : axelarDestinationChain ,
50+ sourceTokenAddress : "0x0000000000000000000000000000000000000000" ,
51+ gasMultiplier : "auto" ,
52+ gasLimit : gasLimit . toString ( ) ,
53+ } ) ,
54+ signal : controller . signal ,
55+ } ) ;
4156
42- if ( typeof response !== "string" ) {
43- throw new Error ( `Unexpected response type: ${ typeof response } ` ) ;
57+ if ( ! response . ok ) {
58+ const errorText = await response . text ( ) ;
59+ throw new Error (
60+ `Failed to estimate gas fee: ${ response . status } ${ errorText } `
61+ ) ;
62+ }
63+
64+ const result = await response . json ( ) ;
65+
66+ return BigInt ( result ) ;
67+ } finally {
68+ clearTimeout ( timeoutId ) ;
4469 }
70+ }
71+
72+ export async function getAxelarTransactionStatus (
73+ network : Network ,
74+ txHash : string
75+ ) : Promise < GMPStatusResponse > {
76+ const api = new AxelarGMPRecoveryAPI ( {
77+ environment :
78+ network === "Mainnet" ? Environment . MAINNET : Environment . TESTNET ,
79+ } ) ;
80+ const status = await api . queryTransactionStatus ( txHash ) ;
81+ return status ;
82+ }
4583
46- return BigInt ( response ) ;
84+ export function getAxelarExplorerUrl ( network : Network , txHash : string ) : string {
85+ return network === "Mainnet"
86+ ? `https://axelarscan.io/gmp/${ txHash } `
87+ : `https://testnet.axelarscan.io/gmp/${ txHash } ` ;
4788}
0 commit comments