Skip to content

Commit

Permalink
fix(persistence-interface): Introduce message with votes and proposal…
Browse files Browse the repository at this point in the history
… with votes types
  • Loading branch information
yousuf-haque committed Oct 6, 2023
1 parent d45a66a commit 7c0a1b0
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 63 deletions.
21 changes: 12 additions & 9 deletions server/helpers/adapters/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { MessagesRepository } from '../../repositories/messages-repository.js';
import { OffchainProofsRepository } from '../../repositories/offchain-proofs-repository.js';
import { Event } from '../../models/event.js';
import { Message } from '../../models/message.js';
import { Message, MessageWithVotes, VoteMessage } from '../../models/message.js';

/**
* Values to insert into the `events` database.
Expand Down Expand Up @@ -188,28 +188,28 @@ const getProposalByDraft = async (space: string, id: string) => {
return result.rows;
};

const getProposalVotes = async (space: string, id: string) => {
const getProposalVotes = async (space: string, id: string): Promise<Message[]> => {
const query = `SELECT * FROM messages WHERE type = 'vote' AND space = $1 AND payload ->> 'proposalId' = $2 ORDER BY timestamp ASC`;
const result = await db.query(query, [space, id]);
console.log(result.rows.length);
return result.rows;
};

const findVotesForProposals = (space, proposals) =>
const findVotesForProposals: (space: string, proposals: Message[]) => Promise<MessageWithVotes[]> = (space, proposals) =>
Promise.all(
proposals.map(p =>
getProposalVotes(space, p.id)
.then(votes =>
votes && votes.length > 0 ? toVotesMessageJson(votes) : []
)
.then(votes => {
p['votes'] = votes;
return p;
})
.then(votes => ({
...p,
votes
}))
)
);

const getAllProposalsAndVotes = async (space: string) => {
const getAllProposalsAndVotes = async (space: string): Promise<MessageWithVotes[]> => {
const queryProposals = `SELECT * FROM messages WHERE type = 'proposal' AND space = $1`;
const proposalsResult = await db.query(queryProposals, [space]);
console.log(proposalsResult.rows.length);
Expand All @@ -219,7 +219,7 @@ const getAllProposalsAndVotes = async (space: string) => {
const getAllProposalsAndVotesByAction = async (
space: string,
actionId: string
) => {
) : Promise<MessageWithVotes[]>=> {
const queryProposals = `SELECT * FROM messages WHERE type = 'proposal' AND space = $1 AND "actionId" = $2 ORDER BY timestamp DESC`;
const proposalsResult = await db.query(queryProposals, [space, actionId]);
console.log(proposalsResult.rows.length);
Expand Down Expand Up @@ -282,6 +282,7 @@ function insertCreatedProposal(
])
.then(() => undefined);
}

function insertStartedProposal(
EVENT_ID: string,
space: string,
Expand All @@ -296,6 +297,7 @@ function insertStartedProposal(
])
.then(() => undefined);
}

function insertProposalEnd(EVENT_ID: string, space: string, timestamp: number) {
return db
.query<any, EventInsertValuesTuple>(EVENTS_INSERT_STATEMENT, [
Expand All @@ -306,6 +308,7 @@ function insertProposalEnd(EVENT_ID: string, space: string, timestamp: number) {
])
.then(() => undefined);
}

export const postgresEventsRepository: EventsRepository = {
deleteProcessedEvent,
getExpiredEvents,
Expand Down
108 changes: 59 additions & 49 deletions server/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { providers } from 'ethers';
import { convertUtf8ToHex } from '@walletconnect/utils';
import * as ethUtil from 'ethereumjs-util';
import { isValidSignature } from './eip1271';
import {
Message,
MessageWithVotes,
ProposalWithVotesMessage,
VoteMessage
} from '../models/message.js';

export const jsonParse = (input, fallback?) => {
try {
Expand Down Expand Up @@ -88,31 +94,31 @@ export const hashPersonalMessage = (msg: string): string => {
return ethUtil.bufferToHex(hash);
};

export const toMessageJson = (messages: any): any =>
Object.fromEntries(
messages.map(message => {
return [
message.type === 'vote' ? message.address : message.id,
{
address: message.address,
data: message.data,
msg: {
version: message.version,
timestamp: message.timestamp.toString(),
token: message.token,
type: message.type,
payload: message.payload
},
sig: message.sig,
authorIpfsHash: message.id,
relayerIpfsHash: message.metadata.relayerIpfsHash,
actionId: message.actionId
export const toMessageJson = (messages: Message[]): ProposalWithVotesMessage =>
messages.reduce((proposal, message) => {
return {
...proposal,
[message.type === 'vote' ? message.address : message.id]: {
address: message.address,
data: message.data,
msg: {
version: message.version,
timestamp: message.timestamp.toString(),
token: message.token,
type: message.type,
payload: message.payload
},
];
})
);
sig: message.sig,
authorIpfsHash: message.id,
relayerIpfsHash: message.metadata
? message.metadata.relayerIpfsHash
: undefined,
actionId: message.actionId
}
};
}, {});

export const toVoteMessageJson = (message: any): any => {
export const toVoteMessageJson = (message: Message): VoteMessage => {
return {
[message.address]: {
id: message.id,
Expand All @@ -127,36 +133,40 @@ export const toVoteMessageJson = (message: any): any => {
},
sig: message.sig,
authorIpfsHash: message.id,
relayerIpfsHash: message.metadata.relayerIpfsHash,
relayerIpfsHash: message.metadata
? message.metadata.relayerIpfsHash
: undefined,
actionId: message.actionId
}
};
};

export const toVotesMessageJson = (messages: any): any =>
export const toVotesMessageJson = (messages: Message[]): VoteMessage[] =>
messages.map(m => toVoteMessageJson(m));

export const toProposalWithVotesMessageJson = (messages: any): any =>
Object.fromEntries(
messages.map(message => {
return [
message.id,
{
address: message.address,
data: message.data,
msg: {
version: message.version,
timestamp: message.timestamp.toString(),
token: message.token,
type: message.type,
payload: message.payload
},
votes: message.votes,
sig: message.sig,
authorIpfsHash: message.id,
relayerIpfsHash: message.metadata.relayerIpfsHash,
actionId: message.actionId
}
];
})
);
export const toProposalWithVotesMessageJson = (
messages: MessageWithVotes[]
): ProposalWithVotesMessage =>
messages.reduce((proposal, message) => {
return {
...proposal,
[message.id]: {
address: message.address,
data: message.data,
msg: {
version: message.version,
timestamp: message.timestamp.toString(),
token: message.token,
type: message.type,
payload: message.payload
},
votes: message.votes,
sig: message.sig,
authorIpfsHash: message.id,
relayerIpfsHash: message.metadata
? message.metadata.relayerIpfsHash
: undefined,
actionId: message.actionId
}
};
}, {});
26 changes: 25 additions & 1 deletion server/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,31 @@ export type Message = {
type: string;
payload?: string;
sig: string;
metadata?: string;
metadata?: {
relayerIpfsHash: string;
};
actionId: string;
data?: string;
};
export type VoteMessage = {
[address: string]: {
id: string;
address: string;
data?: string;
msg: {
version: string;
timestamp: string;
token?: string;
type: string;
payload?: string;
};
sig: string;
authorIpfsHash: string;
relayerIpfsHash?: string;
actionId: string;
};
};
export type MessageWithVotes = Message & { votes: VoteMessage[] };
export type ProposalWithVotesMessage = {
[id: string]: MessageWithVotes;
};
11 changes: 7 additions & 4 deletions server/repositories/messages-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { postgresMessagesRepository } from '../helpers/adapters/postgres';
import { Message } from '../models/message.js';
import { Message, MessageWithVotes } from '../models/message.js';

export type MessagesRepository = {
getMessages: (spaces: string, msgType: string) => Promise<Message[]>;
Expand All @@ -20,11 +20,11 @@ export type MessagesRepository = {
) => Promise<Message[]>;
getProposalByDraft: (space: string, id: string) => Promise<Message[]>;
getProposalVotes: (space: string, id: string) => Promise<Message[]>;
getAllProposalsAndVotes: (space: string) => Promise<any[]>;
getAllProposalsAndVotes: (space: string) => Promise<MessageWithVotes[]>;
getAllProposalsAndVotesByAction: (
space: string,
actionId: string
) => Promise<any[]>;
) => Promise<MessageWithVotes[]>;
getAllDraftsExceptSponsored: (space: string) => Promise<Message[]>;
storeDraft: (
space,
Expand Down Expand Up @@ -55,6 +55,9 @@ export type MessagesRepository = {
actionId
) => Promise<void>
sponsorDraftIfAny: (space, erc712DraftHash) => Promise<number>,
findVotesForProposals: (space, proposals) => Promise<any[]>
findVotesForProposals: (space, proposals) => Promise<MessageWithVotes[]>
};



export const messagesRepository: MessagesRepository = postgresMessagesRepository;

0 comments on commit 7c0a1b0

Please sign in to comment.