@@ -4,6 +4,7 @@ import { Chain } from "@defillama/sdk/build/general";
44import * as yaml from "js-yaml" ;
55
66import { BridgeAdapter } from "../../helpers/bridgeAdapter.type" ;
7+ import { NonRetryableError } from "../../utils/errors" ;
78
89const baseUri = "https://raw.githubusercontent.com/hyperlane-xyz/hyperlane-registry/main" ;
910const kyveApiBaseUri = "https://data.services.hyperlane.xyz" ;
@@ -24,7 +25,7 @@ export async function setUp(): Promise<string[]> {
2425 return chains ;
2526}
2627
27- interface KyveEvent {
28+ export interface KyveEvent {
2829 blockNumber : number ;
2930 chain : string ;
3031 from : string ;
@@ -36,47 +37,75 @@ interface KyveEvent {
3637 usdAmount : string ;
3738}
3839
39- export const getEvents = async ( fromTimestamp : number , toTimestamp : number ) : Promise < any [ ] > => {
40+ type HyperlaneFetch = (
41+ input : string ,
42+ init ?: { headers ?: Record < string , string > }
43+ ) => Promise < {
44+ ok : boolean ;
45+ status : number ;
46+ statusText : string ;
47+ text ( ) : Promise < string > ;
48+ json ( ) : Promise < unknown > ;
49+ } > ;
50+
51+ export const fetchHyperlaneEvents = async (
52+ fromTimestamp : number ,
53+ toTimestamp : number ,
54+ fetchImpl : HyperlaneFetch = fetch as HyperlaneFetch
55+ ) : Promise < KyveEvent [ ] > => {
4056 const apiUrl = `${ kyveApiBaseUri } /events?fromTimestamp=${ fromTimestamp } &toTimestamp=${ toTimestamp } ` ;
4157 console . log ( apiUrl ) ;
42- try {
43- const response = await fetch ( apiUrl ) ;
44- if ( ! response . ok ) {
45- console . error ( `Error fetching data from Kyve API: ${ response . statusText } ` ) ;
46- return [ ] ;
58+ const response = await fetchImpl ( apiUrl , {
59+ headers : {
60+ Accept : "application/json" ,
61+ "User-Agent" : "defillama-bridges-server/1.0" ,
62+ } ,
63+ } ) ;
64+ if ( ! response . ok ) {
65+ const responseBody = await response . text ( ) . catch ( ( ) => "" ) ;
66+ const detail = responseBody . trim ( ) . slice ( 0 , 300 ) ;
67+ const message = `Hyperlane events API HTTP ${ response . status } ${ response . statusText } ${ detail ? `: ${ detail } ` : "" } ` ;
68+ if ( response . status >= 400 && response . status < 500 && response . status !== 429 ) {
69+ throw new NonRetryableError ( message ) ;
4770 }
48- const events = ( await response . json ( ) ) as KyveEvent [ ] ;
49-
50- const txData : any [ ] = events . map ( ( event ) => {
51- let usdAmount : number | undefined = undefined ;
52- try {
53- usdAmount = parseFloat ( event . usdAmount ) ;
54- if ( isNaN ( usdAmount ) ) {
55- usdAmount = undefined ;
56- }
57- } catch {
71+ throw new Error ( message ) ;
72+ }
73+
74+ const events = await response . json ( ) ;
75+ if ( ! Array . isArray ( events ) ) {
76+ throw new NonRetryableError ( "Hyperlane events API returned a non-array response." ) ;
77+ }
78+ return events as KyveEvent [ ] ;
79+ } ;
80+
81+ export const getEvents = async ( fromTimestamp : number , toTimestamp : number ) : Promise < any [ ] > => {
82+ const events = await fetchHyperlaneEvents ( fromTimestamp , toTimestamp ) ;
83+ const txData : any [ ] = events . map ( ( event ) => {
84+ let usdAmount : number | undefined = undefined ;
85+ try {
86+ usdAmount = parseFloat ( event . usdAmount ) ;
87+ if ( isNaN ( usdAmount ) ) {
5888 usdAmount = undefined ;
5989 }
90+ } catch {
91+ usdAmount = undefined ;
92+ }
6093
61- return {
62- blockNumber : event . blockNumber ,
63- chain : event . chain ,
64- from : event . from ,
65- isDeposit : event . isDeposit ,
66- timestamp : event . timestamp ,
67- to : event . to ,
68- token : event . token ,
69- txHash : event . txHash ,
70- amount : usdAmount ,
71- isUSDVolume : true ,
72- } ;
73- } ) ;
74-
75- return txData ;
76- } catch ( error ) {
77- console . error ( `Error processing Kyve API response:` , error ) ;
78- return [ ] ;
79- }
94+ return {
95+ blockNumber : event . blockNumber ,
96+ chain : event . chain ,
97+ from : event . from ,
98+ isDeposit : event . isDeposit ,
99+ timestamp : event . timestamp ,
100+ to : event . to ,
101+ token : event . token ,
102+ txHash : event . txHash ,
103+ amount : usdAmount ,
104+ isUSDVolume : true ,
105+ } ;
106+ } ) ;
107+
108+ return txData ;
80109} ;
81110
82111export async function build ( ) : Promise < BridgeAdapter > {
0 commit comments