Skip to content

Commit

Permalink
feat: add new options endpoint (#940)
Browse files Browse the repository at this point in the history
* feat: add new options endpoint

* fix: cache result for 2 minutes

* Update src/graphql/operations/options.ts

* fix: fix wrong interval value

---------

Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
wa0x6e and ChaituVR authored Oct 9, 2024
1 parent 04891c8 commit 2313dac
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/graphql/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import follows from './follows';
import leaderboards from './leaderboards';
import messages from './messages';
import networks from './networks';
import options from './options';
import plugins from './plugins';
import proposal from './proposal';
import proposals from './proposals';
Expand Down Expand Up @@ -36,6 +37,7 @@ export default {
subscriptions,
skins,
networks,
options,
validations,
plugins,
strategies,
Expand Down
32 changes: 32 additions & 0 deletions src/graphql/operations/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { capture } from '@snapshot-labs/snapshot-sentry';
import snapshot from '@snapshot-labs/snapshot.js';
import log from '../../helpers/log';
import db from '../../helpers/mysql';

const RUN_INTERVAL = 120e3;

let options = [];

export default async function () {
return options;
}

async function loadOptions() {
options = await db.queryAsync('SELECT s.* FROM options s');
}

async function run() {
try {
log.info('[options] Start options refresh');
await loadOptions();
log.info(`[options] ${options.length} options reloaded`);
log.info('[options] End options refresh');
} catch (e: any) {
capture(e);
log.error(`[options] failed to refresh options, ${JSON.stringify(e)}`);
}
await snapshot.utils.sleep(RUN_INTERVAL);
run();
}

run();
7 changes: 7 additions & 0 deletions src/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ type Query {
orderBy: String
orderDirection: OrderDirection
): [Leaderboard]

options: [Option]
}

input SpaceWhere {
Expand Down Expand Up @@ -641,3 +643,8 @@ type Leaderboard {
votesCount: Int
lastVote: Int
}

type Option {
name: String
value: String
}
6 changes: 6 additions & 0 deletions src/helpers/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,9 @@ CREATE TABLE leaderboard (
INDEX proposal_count (proposal_count),
INDEX last_vote (last_vote)
);

CREATE TABLE options (
name VARCHAR(100) NOT NULL,
value VARCHAR(100) NOT NULL,
PRIMARY KEY (name)
);

0 comments on commit 2313dac

Please sign in to comment.