Skip to content

Commit 0a222d4

Browse files
committed
cloud_functions: move get governed vaas into a standalone cloud function
Signed-off-by: bingyuyap <bingyu.yap.21@gmail.com>
1 parent 56682dd commit 0a222d4

File tree

2 files changed

+104
-71
lines changed

2 files changed

+104
-71
lines changed

cloud_functions/src/alarmMissingVaas.ts

+1-71
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CHAIN_ID_TO_NAME, ChainId, ChainName } from '@certusone/wormhole-sdk';
22
import { MissingVaasByChain, commonGetMissingVaas } from './getMissingVaas';
3+
import { GovernedVAAMap, getGovernedVaas } from './getGovernedVaas';
34
import { assertEnvironmentVariable, formatAndSendToSlack, isVAASigned } from './utils';
45
import { ObservedMessage, ReobserveInfo, SlackInfo } from './types';
56
import {
@@ -10,35 +11,6 @@ import {
1011
} from '@wormhole-foundation/wormhole-monitor-common';
1112
import { Firestore } from 'firebase-admin/firestore';
1213

13-
interface EnqueuedVAAResponse {
14-
sequence: string;
15-
releaseTime: number;
16-
notionalValue: string;
17-
txHash: string;
18-
}
19-
20-
interface Emitter {
21-
emitterAddress: string;
22-
enqueuedVaas: EnqueuedVAAResponse[];
23-
totalEnqueuedVaas: string;
24-
}
25-
26-
interface ChainStatus {
27-
availableNotional: string;
28-
chainId: number;
29-
emitters: Emitter[];
30-
}
31-
32-
interface GovernedVAA {
33-
chainId: number;
34-
emitterAddress: string;
35-
sequence: string;
36-
txHash: string;
37-
}
38-
39-
// The key is the vaaKey
40-
type GovernedVAAMap = Map<string, GovernedVAA>;
41-
4214
const network: Environment = getEnvironment();
4315

4416
export async function alarmMissingVaas(req: any, res: any) {
@@ -153,48 +125,6 @@ export async function alarmMissingVaas(req: any, res: any) {
153125
return;
154126
}
155127

156-
// This function gets all the enqueued VAAs from he governorStatus collection.
157-
async function getGovernedVaas(): Promise<GovernedVAAMap> {
158-
const vaas: GovernedVAAMap = new Map<string, GovernedVAA>();
159-
// Walk all the guardians and retrieve the enqueued VAAs
160-
const firestore = new Firestore();
161-
const collection = firestore.collection(
162-
assertEnvironmentVariable('FIRESTORE_GOVERNOR_STATUS_COLLECTION')
163-
);
164-
const snapshot = await collection.get();
165-
for (const doc of snapshot.docs) {
166-
const data = doc.data();
167-
if (data) {
168-
// data should be a ChainStatus[]
169-
const chains: ChainStatus[] = data.chains;
170-
chains.forEach((chain) => {
171-
// chain should be a ChainStatus
172-
const emitters: Emitter[] = chain.emitters;
173-
emitters.forEach((emitter) => {
174-
// Filter 0x off the front of the emitter address
175-
if (emitter.emitterAddress.startsWith('0x')) {
176-
emitter.emitterAddress = emitter.emitterAddress.slice(2);
177-
}
178-
// emitter should be an Emitter
179-
const enqueuedVaas: EnqueuedVAAResponse[] = emitter.enqueuedVaas;
180-
enqueuedVaas.forEach((vaa) => {
181-
// vaa should be an EnqueuedVAAResponse
182-
const governedVAA: GovernedVAA = {
183-
chainId: chain.chainId,
184-
emitterAddress: emitter.emitterAddress,
185-
sequence: vaa.sequence,
186-
txHash: vaa.txHash,
187-
};
188-
const key = `${chain.chainId}/${emitter.emitterAddress}/${vaa.sequence}`;
189-
vaas.set(key, governedVAA);
190-
});
191-
});
192-
});
193-
}
194-
}
195-
return vaas;
196-
}
197-
198128
// This function gets all the VAAs in the firestore table,
199129
// checks the timestamp (keeping any that are less than 2 hours old),
200130
// and returns a map of those VAAs.
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { ChainId } from '@certusone/wormhole-sdk/lib/cjs/utils/consts';
2+
import { Storage } from '@google-cloud/storage';
3+
import { ObservedMessage } from './types';
4+
import { assertEnvironmentVariable } from './utils';
5+
import { Firestore } from 'firebase-admin/firestore';
6+
7+
export type MissingVaasByChain = {
8+
[chain in ChainId]?: {
9+
messages: ObservedMessage[];
10+
lastRowKey: string;
11+
lastUpdated: number;
12+
};
13+
};
14+
15+
interface EnqueuedVAAResponse {
16+
sequence: string;
17+
releaseTime: number;
18+
notionalValue: string;
19+
txHash: string;
20+
}
21+
22+
interface Emitter {
23+
emitterAddress: string;
24+
enqueuedVaas: EnqueuedVAAResponse[];
25+
totalEnqueuedVaas: string;
26+
}
27+
28+
interface ChainStatus {
29+
availableNotional: string;
30+
chainId: number;
31+
emitters: Emitter[];
32+
}
33+
34+
interface GovernedVAA {
35+
chainId: number;
36+
emitterAddress: string;
37+
sequence: string;
38+
txHash: string;
39+
}
40+
41+
42+
export type GovernedVAAMap = Map<string, GovernedVAA>;
43+
44+
// This function gets all the enqueued VAAs from he governorStatus collection.
45+
export async function getGovernedVaas(): Promise<GovernedVAAMap> {
46+
const vaas: GovernedVAAMap = new Map<string, GovernedVAA>();
47+
// Walk all the guardians and retrieve the enqueued VAAs
48+
const firestore = new Firestore();
49+
const collection = firestore.collection(
50+
assertEnvironmentVariable('FIRESTORE_GOVERNOR_STATUS_COLLECTION')
51+
);
52+
const snapshot = await collection.get();
53+
for (const doc of snapshot.docs) {
54+
const data = doc.data();
55+
if (data) {
56+
// data should be a ChainStatus[]
57+
const chains: ChainStatus[] = data.chains;
58+
chains.forEach((chain) => {
59+
// chain should be a ChainStatus
60+
const emitters: Emitter[] = chain.emitters;
61+
emitters.forEach((emitter) => {
62+
// Filter 0x off the front of the emitter address
63+
if (emitter.emitterAddress.startsWith('0x')) {
64+
emitter.emitterAddress = emitter.emitterAddress.slice(2);
65+
}
66+
// emitter should be an Emitter
67+
const enqueuedVaas: EnqueuedVAAResponse[] = emitter.enqueuedVaas;
68+
enqueuedVaas.forEach((vaa) => {
69+
// vaa should be an EnqueuedVAAResponse
70+
const governedVAA: GovernedVAA = {
71+
chainId: chain.chainId,
72+
emitterAddress: emitter.emitterAddress,
73+
sequence: vaa.sequence,
74+
txHash: vaa.txHash,
75+
};
76+
const key = `${chain.chainId}/${emitter.emitterAddress}/${vaa.sequence}`;
77+
vaas.set(key, governedVAA);
78+
});
79+
});
80+
});
81+
}
82+
}
83+
return vaas;
84+
}
85+
86+
export async function getMissingVaas(req: any, res: any) {
87+
res.set('Access-Control-Allow-Origin', '*');
88+
if (req.method === 'OPTIONS') {
89+
// Send response to OPTIONS requests
90+
res.set('Access-Control-Allow-Methods', 'GET');
91+
res.set('Access-Control-Allow-Headers', 'Content-Type');
92+
res.set('Access-Control-Max-Age', '3600');
93+
res.status(204).send('');
94+
return;
95+
}
96+
97+
try {
98+
let messages: GovernedVAAMap = await getGovernedVaas();
99+
res.status(200).send(JSON.stringify(messages));
100+
} catch (e) {
101+
res.sendStatus(500);
102+
}
103+
}

0 commit comments

Comments
 (0)