1- import axios , { AxiosResponse } from 'axios' ;
21import { BigNumber } from 'ethers' ;
32import { BridgeAdapter , PartialContractEventParams } from "../../helpers/bridgeAdapter.type" ;
43import { Chain } from "@defillama/sdk/build/general" ;
@@ -7,6 +6,8 @@ import { fromHex } from 'tron-format-address';
76import { getConnection } from '../../helpers/solana' ;
87import { getTxDataFromEVMEventLogs } from "../../helpers/processTransactions" ;
98import { getTxDataFromTronEventLogs } from './eventParsing' ;
9+ import { getEventsFromAnalyticsApi } from './analyticsApi' ;
10+ import { getClient as getSuiClient } from '../../helpers/sui' ;
1011
1112const adapterName = "allbridge-core" ;
1213
@@ -59,6 +60,10 @@ const lpAddresses = {
5960 [ chain : string ] : string [ ] ;
6061} ;
6162
63+ const suiFullTokenAddressMap : Record < string , string > = {
64+ "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7" : "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC" ,
65+ } ;
66+
6267const constructParams = ( chain : string ) => {
6368 let eventParams = [ ] as PartialContractEventParams [ ] ;
6469 const lps = lpAddresses [ chain ] ;
@@ -111,21 +116,6 @@ const constructParams = (chain: string) => {
111116 getTxDataFromEVMEventLogs ( adapterName , chain as Chain , fromBlock , toBlock , eventParams ) ;
112117} ;
113118
114- interface SolanaEvent {
115- blockTime : number ;
116- txHash : string ;
117- from : string ;
118- to : string ;
119- token : string ;
120- amount : string ;
121- isDeposit : boolean ;
122- }
123-
124- function interpolateNumber ( x1 : number , y1 : number , x2 : number , y2 : number , y : number ) : number {
125- const x = x1 + ( x2 - x1 ) / ( y2 - y1 ) * ( y - y1 ) ;
126- return Math . round ( x ) ;
127- }
128-
129119const getSolanaEvents = async ( fromSlot : number , toSlot : number ) : Promise < EventData [ ] > => {
130120 const connection = getConnection ( ) ;
131121 const timestampFrom = await connection . getBlockTime ( fromSlot ) ;
@@ -134,28 +124,18 @@ const getSolanaEvents = async (fromSlot: number, toSlot: number): Promise<EventD
134124 if ( ! timestampFrom || ! timestampTo ) {
135125 return [ ] ;
136126 }
137- const from = new Date ( timestampFrom * 1000 ) . toISOString ( ) ;
138- const to = new Date ( timestampTo * 1000 ) . toISOString ( ) ;
127+ return await getEventsFromAnalyticsApi ( 'SOL' , fromSlot , timestampFrom * 1000 , toSlot , timestampTo * 1000 ) ;
128+ } ;
139129
140- let response : AxiosResponse < SolanaEvent [ ] > ;
141- try {
142- response = await axios . get < SolanaEvent [ ] > (
143- `https://core.api.allbridgecoreapi.net/analytics/inflows?chain=SOL&from=${ from } &to=${ to } `
144- ) ;
145- } catch ( e ) {
146- console . error ( "Error fetching Solana events" , e ) ;
147- return [ ] ;
148- }
130+ const getSuiEvents = async ( fromCheckpointNumber : number , toCheckpointNumber : number ) : Promise < EventData [ ] > => {
131+ const client = getSuiClient ( ) ;
132+ const fromCheckpoint = await client . getCheckpoint ( { id : fromCheckpointNumber . toString ( ) } ) ;
133+ const fromTimestampMs = Number ( fromCheckpoint . timestampMs ) ;
134+ const toCheckpoint = await client . getCheckpoint ( { id : toCheckpointNumber . toString ( ) } ) ;
135+ const toTimestampMs = Number ( toCheckpoint . timestampMs ) ;
149136
150- return response . data . map ( ( event ) => ( {
151- blockNumber : interpolateNumber ( fromSlot , timestampFrom , toSlot , timestampTo , event . blockTime ) ,
152- txHash : event . txHash ,
153- from,
154- to,
155- token : event . token ,
156- amount : BigNumber . from ( event . amount ) ,
157- isDeposit : event . isDeposit ,
158- } ) ) ;
137+ const eventData = await getEventsFromAnalyticsApi ( 'SUI' , fromCheckpointNumber , fromTimestampMs , toCheckpointNumber , toTimestampMs ) ;
138+ return eventData . map ( ( data ) => ( { ...data , token : suiFullTokenAddressMap [ data . token ] ?? data . token } ) ) ;
159139} ;
160140
161141function constructTronParams ( ) {
@@ -225,6 +205,7 @@ const adapter: BridgeAdapter = {
225205 sonic : constructParams ( "sonic" ) ,
226206 unichain : constructParams ( "unichain" ) ,
227207 solana : getSolanaEvents ,
208+ sui : getSuiEvents ,
228209} ;
229210
230211export default adapter ;
0 commit comments