Skip to content

Commit

Permalink
cloud_functions: add latest-tvltvm
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Sep 21, 2023
1 parent b9320f7 commit 3b35c9c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions cloud_functions/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cloud_functions/src/computeTvlTvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ async function getTokenPrices(): Promise<TokenPrice[]> {
},
});
// 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) {
Expand Down Expand Up @@ -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);
Expand Down
72 changes: 72 additions & 0 deletions cloud_functions/src/getLatestTvlTvm.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions cloud_functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down

0 comments on commit 3b35c9c

Please sign in to comment.