Skip to content

Commit

Permalink
feat: ignore frequent computes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekhmet committed Aug 22, 2024
1 parent afe9868 commit d07e7be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatUnits } from '@ethersproject/units';
import { register } from '@snapshot-labs/checkpoint/dist/src/register';
import snapshotjs from '@snapshot-labs/snapshot.js';
import { Mutex } from 'async-mutex';
import { SCORE_API_URL } from './constants';
import { COMPUTE_DELAY_SECONDS, SCORE_API_URL } from './constants';
import { getSpace } from './hub';
import { Delegate, Governance } from '../.checkpoint/models';

Expand All @@ -15,6 +15,7 @@ const DELEGATION_STRATEGIES = [
'delegation-with-overrides'
];

const lastCompute = new Map<string, number>();
const mutex = new Mutex();

// This function is called everytime governance information is queried from GraphQL API.
Expand All @@ -33,6 +34,14 @@ export async function compute(governances: string[]) {
for (const governance of governances) {
console.log('computing', governance);

const computedAt = lastCompute.get(governance) ?? 0;
const now = Math.floor(Date.now() / 1000);
if (now - computedAt < COMPUTE_DELAY_SECONDS) {
console.log('ignoring because of recent compute');
continue;
}
lastCompute.set(governance, now);

const space = await getSpace(governance);
const delegations = await snapshotjs.utils.getDelegatesBySpace(
space.network,
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const HUB_URL = process.env.HUB_URL ?? 'https://hub.snapshot.org';
export const SCORE_API_URL =
process.env.SCORE_API_URL ?? 'https://score.snapshot.org';

export const COMPUTE_DELAY_SECONDS = 30 * 60; // 30 minutes

0 comments on commit d07e7be

Please sign in to comment.