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 { convertSolanaTxToAccts } 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,11 +29,20 @@ 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
- } ) ;
34
- res . status ( 200 ) . send ( JSON . stringify ( reobs ) ) ;
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
+ }
43
+ // Need to flatten the array of arrays before returning
44
+ const retVal = reobs . flat ( ) ;
45
+ res . status ( 200 ) . send ( JSON . stringify ( retVal ) ) ;
35
46
return ;
36
47
}
37
48
@@ -44,6 +55,7 @@ async function getAndProcessReobsVAAs(): Promise<Map<string, ReobserveInfo>> {
44
55
let current = new Map < string , ReobserveInfo > ( ) ;
45
56
let putBack : ReobserveInfo [ ] = [ ] ;
46
57
let vaas : ReobserveInfo [ ] = [ ] ;
58
+ let realVaas : ReobserveInfo [ ] = [ ] ;
47
59
48
60
try {
49
61
const res = await firestore . runTransaction ( async ( t ) => {
@@ -59,18 +71,54 @@ async function getAndProcessReobsVAAs(): Promise<Map<string, ReobserveInfo>> {
59
71
}
60
72
vaas = data . VAAs . slice ( 0 , MAX_VAAS_TO_REOBSERVE ) ;
61
73
console . log ( 'number of reobserved VAAs' , vaas . length ) ;
74
+ const MAX_SOLANA_VAAS_TO_REOBSERVE = 2 ;
75
+ // Can only process 2 Solana VAAs at a time due to rpc rate limits
76
+ // So we put the rest back in the collection
77
+ let solanaCount = 0 ;
78
+ for ( const vaa of vaas ) {
79
+ if ( vaa . chain === CHAIN_ID_SOLANA ) {
80
+ solanaCount ++ ;
81
+ if ( solanaCount > MAX_SOLANA_VAAS_TO_REOBSERVE ) {
82
+ putBack . push ( vaa ) ;
83
+ continue ;
84
+ }
85
+ }
86
+ realVaas . push ( vaa ) ;
87
+ }
88
+ console . log ( 'number of real VAAs' , realVaas . length ) ;
62
89
}
63
90
t . update ( collectionRef , { VAAs : putBack } ) ;
64
91
} ) ;
65
92
} catch ( e ) {
66
93
console . error ( 'error getting reobserved VAAs' , e ) ;
67
94
return current ;
68
95
}
69
- for ( const vaa of vaas ) {
70
- if ( ! ( await isVAASigned ( vaa . vaaKey ) ) ) {
96
+ for ( const vaa of realVaas ) {
97
+ if ( ! ( await isVAASigned ( getEnvironment ( ) , vaa . vaaKey ) ) ) {
71
98
current . set ( vaa . txhash , vaa ) ;
72
99
}
73
100
}
74
101
console . log ( 'number of reobservable VAAs that are not signed' , current . size ) ;
75
102
return current ;
76
103
}
104
+
105
+ async function processVaa ( vaa : ReobserveInfo ) : Promise < ReobserveInfo [ ] | null > {
106
+ let vaas : ReobserveInfo [ ] = [ ] ;
107
+
108
+ if ( vaa . chain === CHAIN_ID_SOLANA ) {
109
+ const origTxHash = vaa . txhash ;
110
+ const convertedTxHash : string [ ] = await convertSolanaTxToAccts ( origTxHash ) ;
111
+ console . log ( `Converted solana txHash ${ origTxHash } to account ${ convertedTxHash } ` ) ;
112
+
113
+ if ( convertedTxHash . length === 0 ) {
114
+ console . error ( `Failed to convert solana txHash ${ origTxHash } to an account.` ) ;
115
+ return null ; // Indicate failure to convert
116
+ }
117
+ for ( const account of convertedTxHash ) {
118
+ vaas . push ( { ...vaa , txhash : account } ) ; // Return a new object with the updated txhash
119
+ }
120
+ } else {
121
+ vaas . push ( vaa ) ; // Return the original object for non-Solana chains
122
+ }
123
+ return vaas ;
124
+ }
0 commit comments