1
1
import { assertEnvironmentVariable , isVAASigned } from './utils' ;
2
2
import { ReobserveInfo } from './types' ;
3
3
import { Firestore } from 'firebase-admin/firestore' ;
4
- import axios from 'axios' ;
4
+ import { CHAIN_ID_SOLANA } from '@certusone/wormhole-sdk' ;
5
+ import { convertSolanaTxToAcct } from '@wormhole-foundation/wormhole-monitor-common' ;
6
+ import { getEnvironment } from '@wormhole-foundation/wormhole-monitor-common' ;
5
7
6
8
const MAX_VAAS_TO_REOBSERVE = 25 ;
7
9
@@ -27,10 +29,17 @@ export async function getReobserveVaas(req: any, res: any) {
27
29
console . log ( 'could not get missing VAAs' , e ) ;
28
30
res . sendStatus ( 500 ) ;
29
31
}
30
- let reobs : ReobserveInfo [ ] = [ ] ;
31
- reobsMap . forEach ( ( vaa ) => {
32
- reobs . push ( vaa ) ;
33
- } ) ;
32
+
33
+ let reobs : ( ReobserveInfo | null ) [ ] = [ ] ;
34
+ try {
35
+ const vaaArray = Array . from ( reobsMap . values ( ) ) ;
36
+ // Process each VAA asynchronously and filter out any null results
37
+ reobs = ( await Promise . all ( vaaArray . map ( processVaa ) ) ) . filter ( ( vaa ) => vaa !== null ) ; // Remove any VAA that failed conversion
38
+ } catch ( e ) {
39
+ console . error ( 'error processing reobservations' , e ) ;
40
+ console . error ( 'reobs' , reobs ) ;
41
+ res . sendStatus ( 500 ) ;
42
+ }
34
43
res . status ( 200 ) . send ( JSON . stringify ( reobs ) ) ;
35
44
return ;
36
45
}
@@ -44,6 +53,7 @@ async function getAndProcessReobsVAAs(): Promise<Map<string, ReobserveInfo>> {
44
53
let current = new Map < string , ReobserveInfo > ( ) ;
45
54
let putBack : ReobserveInfo [ ] = [ ] ;
46
55
let vaas : ReobserveInfo [ ] = [ ] ;
56
+ let realVaas : ReobserveInfo [ ] = [ ] ;
47
57
48
58
try {
49
59
const res = await firestore . runTransaction ( async ( t ) => {
@@ -59,18 +69,49 @@ async function getAndProcessReobsVAAs(): Promise<Map<string, ReobserveInfo>> {
59
69
}
60
70
vaas = data . VAAs . slice ( 0 , MAX_VAAS_TO_REOBSERVE ) ;
61
71
console . log ( 'number of reobserved VAAs' , vaas . length ) ;
72
+ const MAX_SOLANA_VAAS_TO_REOBSERVE = 2 ;
73
+ // Can only process 2 Solana VAAs at a time due to rpc rate limits
74
+ // So we put the rest back in the collection
75
+ let solanaCount = 0 ;
76
+ for ( const vaa of vaas ) {
77
+ if ( vaa . chain === CHAIN_ID_SOLANA ) {
78
+ solanaCount ++ ;
79
+ if ( solanaCount > MAX_SOLANA_VAAS_TO_REOBSERVE ) {
80
+ putBack . push ( vaa ) ;
81
+ continue ;
82
+ }
83
+ }
84
+ realVaas . push ( vaa ) ;
85
+ }
86
+ console . log ( 'number of real VAAs' , realVaas . length ) ;
62
87
}
63
88
t . update ( collectionRef , { VAAs : putBack } ) ;
64
89
} ) ;
65
90
} catch ( e ) {
66
91
console . error ( 'error getting reobserved VAAs' , e ) ;
67
92
return current ;
68
93
}
69
- for ( const vaa of vaas ) {
70
- if ( ! ( await isVAASigned ( vaa . vaaKey ) ) ) {
94
+ for ( const vaa of realVaas ) {
95
+ if ( ! ( await isVAASigned ( getEnvironment ( ) , vaa . vaaKey ) ) ) {
71
96
current . set ( vaa . txhash , vaa ) ;
72
97
}
73
98
}
74
99
console . log ( 'number of reobservable VAAs that are not signed' , current . size ) ;
75
100
return current ;
76
101
}
102
+
103
+ async function processVaa ( vaa : ReobserveInfo ) {
104
+ if ( vaa . chain === CHAIN_ID_SOLANA ) {
105
+ const origTxHash = vaa . txhash ;
106
+ const convertedTxHash = await convertSolanaTxToAcct ( origTxHash ) ;
107
+ console . log ( `Converted solana txHash ${ origTxHash } to account ${ convertedTxHash } ` ) ;
108
+
109
+ if ( convertedTxHash === '' ) {
110
+ console . error ( `Failed to convert solana txHash ${ origTxHash } to an account.` ) ;
111
+ return null ; // Indicate failure to convert
112
+ }
113
+
114
+ return { ...vaa , txhash : convertedTxHash } ; // Return a new object with the updated txhash
115
+ }
116
+ return vaa ; // Return the original object for non-Solana chains
117
+ }
0 commit comments