diff --git a/cloud_functions/scripts/deploy.sh b/cloud_functions/scripts/deploy.sh index 017a2dbf..0802cd6b 100755 --- a/cloud_functions/scripts/deploy.sh +++ b/cloud_functions/scripts/deploy.sh @@ -142,6 +142,7 @@ gcloud functions deploy messages --entry-point getMessages --runtime nodejs16 -- gcloud functions deploy message-counts --entry-point getMessageCounts --runtime nodejs16 --trigger-http --allow-unauthenticated --timeout 300 --memory 256MB --region europe-west3 gcloud functions deploy compute-message-counts --entry-point computeMessageCounts --runtime nodejs16 --trigger-http --allow-unauthenticated --timeout 300 --memory 4GB --region europe-west3 --set-env-vars BIGTABLE_TABLE_ID=$BIGTABLE_TABLE_ID,BIGTABLE_INSTANCE_ID=$BIGTABLE_INSTANCE_ID,CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL=$CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL gcloud functions deploy latest-blocks --entry-point getLatestBlocks --runtime nodejs16 --trigger-http --allow-unauthenticated --timeout 300 --memory 256MB --region europe-west3 --set-env-vars CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL=$CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL,FIRESTORE_LATEST_COLLECTION=$FIRESTORE_LATEST_COLLECTION +gcloud functions deploy latest-tvltvm --entry-point getLatestTvlTvm --runtime nodejs16 --trigger-http --allow-unauthenticated --timeout 300 --memory 256MB --region europe-west3 --set-env-vars CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL=$CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL,FIRESTORE_LATEST_TVLTVM_COLLECTION=$FIRESTORE_LATEST_TVLTVM_COLLECTION gcloud functions deploy compute-missing-vaas --entry-point computeMissingVaas --runtime nodejs16 --trigger-http --allow-unauthenticated --timeout 300 --memory 2GB --region europe-west3 --set-env-vars BIGTABLE_TABLE_ID=$BIGTABLE_TABLE_ID,BIGTABLE_INSTANCE_ID=$BIGTABLE_INSTANCE_ID,CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL=$CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL gcloud functions deploy missing-vaas --entry-point getMissingVaas --runtime nodejs16 --trigger-http --allow-unauthenticated --timeout 300 --memory 256MB --region europe-west3 gcloud functions deploy alarm-missing-vaas --entry-point alarmMissingVaas --runtime nodejs16 --trigger-http --no-allow-unauthenticated --timeout 300 --memory 256MB --region europe-west3 --set-env-vars SLACK_CHANNEL_ID=$SLACK_CHANNEL_ID,SLACK_POST_URL=$SLACK_POST_URL,SLACK_BOT_TOKEN=$SLACK_BOT_TOKEN,FIRESTORE_ALARM_MISSING_VAAS_COLLECTION=$FIRESTORE_ALARM_MISSING_VAAS_COLLECTION diff --git a/cloud_functions/src/computeTvlTvm.ts b/cloud_functions/src/computeTvlTvm.ts index 3b0d3112..5f0dfe00 100644 --- a/cloud_functions/src/computeTvlTvm.ts +++ b/cloud_functions/src/computeTvlTvm.ts @@ -78,9 +78,11 @@ async function getTokenPrices(): Promise { }, }); // get all of the known coin IDs + const today = new Date(Date.now()).toISOString().slice(0, 10); const rows = await pg(assertEnvironmentVariable('PG_TOKEN_PRICE_HISTORY_TABLE')) .select('*') .whereNotNull('coin_gecko_coin_id') + .andWhere('date', '=', today) .distinct(); return rows; } catch (e) { @@ -116,6 +118,7 @@ async function populateMaps() { // Lastly, get the price information const prices: TokenPrice[] = await getTokenPrices(); + console.log(`Got ${prices.length} token prices`); for (const p of prices) { // Check the date on the price. Only use the latest. const existingPrice = priceMap.get(p.coin_gecko_coin_id); diff --git a/cloud_functions/src/getLatestTvlTvm.ts b/cloud_functions/src/getLatestTvlTvm.ts new file mode 100644 index 00000000..d6008275 --- /dev/null +++ b/cloud_functions/src/getLatestTvlTvm.ts @@ -0,0 +1,72 @@ +import { Firestore } from 'firebase-admin/firestore'; +import { ChainId } from '@certusone/wormhole-sdk/lib/cjs/utils/consts'; +import { assertEnvironmentVariable } from './utils'; + +export type TvlTvm = { + tvl: number; + tvm: number; +}; + +export type ValuesByChain = { + [chain in ChainId]?: { tvl: number; tvm: number }; +}; + +async function getLatestTvlTvm_() { + const firestoreCollection = assertEnvironmentVariable('FIRESTORE_LATEST_TVLTVM_COLLECTION'); + let values: ValuesByChain = {}; + const firestoreDb = new Firestore({}); + try { + const collectionRef = firestoreDb.collection(firestoreCollection); + const snapshot = await collectionRef.get(); + snapshot.docs + .sort((a, b) => Number(a.id) - Number(b.id)) + .forEach((doc) => { + values[Number(doc.id) as ChainId] = { tvl: doc.data().tvl, tvm: doc.data().tvm }; + }); + } catch (e) { + console.error(e); + } + return values; +} + +let cache = { values: {} as ValuesByChain, lastUpdated: Date.now() }; +// default refresh interval = 60 min +const REFRESH_TIME_INTERVAL = + Number(process.env.CLOUD_FUNCTIONS_REFRESH_TIME_INTERVAL) || 1000 * 60 * 60; + +export async function getLatestTvlTvm(req: any, res: any) { + res.set('Access-Control-Allow-Origin', '*'); + if (req.method === 'OPTIONS') { + // Send response to OPTIONS requests + res.set('Access-Control-Allow-Methods', 'GET'); + res.set('Access-Control-Allow-Headers', 'Content-Type'); + res.set('Access-Control-Max-Age', '3600'); + res.status(204).send(''); + return; + } + let values: ValuesByChain = {}; + try { + if ( + Object.keys(cache['values']).length === 0 || + Date.now() - cache['lastUpdated'] > REFRESH_TIME_INTERVAL + ) { + if (Object.keys(cache['values']).length === 0) { + console.log(`cache is empty, setting cache[values] ${new Date()}`); + } else { + console.log(`cache is older than ${REFRESH_TIME_INTERVAL} ms, refreshing ${new Date()}`); + } + let prevDate = Date.now(); + values = await getLatestTvlTvm_(); + let timeDiff = Date.now() - prevDate; + console.log('After getLatestTvlTvm_=', timeDiff); + cache['values'] = values; + cache['lastUpdated'] = Date.now(); + } else { + console.log(`cache is still valid, not refreshing ${new Date()}`); + values = cache['values']; + } + res.status(200).send(JSON.stringify(values)); + } catch (e) { + res.sendStatus(500); + } +} diff --git a/cloud_functions/src/index.ts b/cloud_functions/src/index.ts index e3aa3ae7..e977a740 100644 --- a/cloud_functions/src/index.ts +++ b/cloud_functions/src/index.ts @@ -5,6 +5,7 @@ const functions = require('@google-cloud/functions-framework'); export const { getMessages } = require('./getMessages'); export const { getMessageCounts } = require('./getMessageCounts'); export const { getLatestBlocks } = require('./getLatestBlocks'); +export const { getLatestTvlTvm } = require('./getLatestTvlTvm'); export const { getMissingVaas } = require('./getMissingVaas'); export const { alarmMissingVaas } = require('./alarmMissingVaas'); export const { computeMissingVaas } = require('./computeMissingVaas'); @@ -27,6 +28,7 @@ functions.http('messages', getMessages); functions.http('messageCounts', getMessageCounts); functions.http('computeMessageCounts', computeMessageCounts); functions.http('latestBlocks', getLatestBlocks); +functions.http('latestTvlTvm', getLatestTvlTvm); functions.http('missingVaas', getMissingVaas); functions.http('alarmMissingVaas', alarmMissingVaas); functions.http('computeMissingVaas', computeMissingVaas);