Skip to content

Commit

Permalink
fix(events): Pull the insert proposal queries into postgres adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
yousuf-haque committed Sep 26, 2023
1 parent 58b0fe0 commit 37152cf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
48 changes: 48 additions & 0 deletions server/helpers/adapters/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import db from '../postgres';
import { toVotesMessageJson } from '../utils';

/**
* Values to insert into the `events` database.
*
* [id, event, space, expire]
*/
type EventInsertValuesTuple = [string, string, string, number];

const EVENTS_INSERT_STATEMENT: string =
'INSERT INTO events (id, event, space, expire) ' +
'VALUES ($1, $2, $3, $4) ' +
'ON CONFLICT ON CONSTRAINT events_pkey DO NOTHING';

const format = (
erc712Hash: string,
Expand Down Expand Up @@ -253,3 +264,40 @@ export const deleteProcessedEvent = (event: EventsDB) => {
[event.id, event.event]
);
}

export function insertCreatedProposal(
EVENT_ID: string,
space: string,
timestamp: number
) {
return db.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
EVENT_ID,
'proposal/created',
space,
timestamp
]);
}
export function insertStartedProposal(
EVENT_ID: string,
space: string,
timestamp: number
) {
return db.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
EVENT_ID,
'proposal/start',
space,
timestamp
]);
}
export function insertProposalEnd(
EVENT_ID: string,
space: string,
timestamp: number
) {
return db.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
EVENT_ID,
'proposal/end',
space,
timestamp
]);
}
51 changes: 13 additions & 38 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,12 @@ import {
findVotesForProposals,
getAllProposalsAndVotesByAction,
saveOffchainProof,
getOffchainProof
getOffchainProof,
insertCreatedProposal,
insertStartedProposal,
insertProposalEnd
} from './helpers/adapters/postgres';
import pkg from '../package.json';
import db from './helpers/postgres';

/**
* Values to insert into the `events` database.
*
* [id, event, space, expire]
*/
type EventInsertValuesTuple = [string, string, string, number];

/**
* In order to migrate the data from snapshot-hub service to OpenLaw infra, we expose a new endpoint
Expand Down Expand Up @@ -296,7 +291,7 @@ router.post('/message', async (req, res) => {
const body = req.body;
const msg = jsonParse(body.msg);
const erc712Data = jsonParse(body.erc712Data);
const ts = (Date.now() / 1e3).toFixed();
const timestamp = (Date.now() / 1e3).toFixed();
console.log('POST /message ', msg.type);
// const minBlock = (3600 * 24) / 15;

Expand Down Expand Up @@ -333,7 +328,7 @@ router.post('/message', async (req, res) => {
if (
!msg.timestamp ||
typeof msg.timestamp !== 'string' ||
msg.timestamp > ts + 30
msg.timestamp > timestamp + 30
)
return sendError(res, 'wrong timestamp');

Expand Down Expand Up @@ -454,8 +449,8 @@ router.post('/message', async (req, res) => {
const payload = jsonParse(payloadToParse, payloadToParse);

const isNotInVotingWindow: boolean = ignoreVoteEndConstraint
? payload.start > ts
: ts > payload.end || payload.start > ts;
? payload.start > timestamp
: timestamp > payload.end || payload.start > timestamp;

if (isNotInVotingWindow) return sendError(res, 'not in voting window');

Expand Down Expand Up @@ -511,12 +506,7 @@ router.post('/message', async (req, res) => {
const EVENT_ID = `proposal/${erc712Hash}`;

// Insert `proposal/created`
await db.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
EVENT_ID,
'proposal/created',
space,
Number(ts)
]);
await insertCreatedProposal(EVENT_ID, space, Number(timestamp));

console.log(`New Draft: ${erc712Hash}`);
return res.json({
Expand Down Expand Up @@ -559,29 +549,14 @@ router.post('/message', async (req, res) => {
const EVENT_ID = `proposal/${erc712Hash}`;

// Insert `proposal/created`
await db.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
EVENT_ID,
'proposal/created',
space,
Number(ts)
]);
await insertCreatedProposal(EVENT_ID, space, Number(timestamp));

// Insert `proposal/start`
await db.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
EVENT_ID,
'proposal/start',
space,
msg.payload.start
]);
await insertStartedProposal(EVENT_ID, space, msg.payload.start);

// Insert `proposal/end`
if (msg.payload.end > ts) {
await db.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
EVENT_ID,
'proposal/end',
space,
msg.payload.end
]);
if (msg.payload.end > timestamp) {
await insertProposalEnd(EVENT_ID, space, msg.payload.end);
}

console.log(`New Proposal: ${erc712Hash}`);
Expand Down

0 comments on commit 37152cf

Please sign in to comment.