-
Notifications
You must be signed in to change notification settings - Fork 194
/
helpers.ts
103 lines (96 loc) · 3.56 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { jsonParse } from '../helpers/utils';
const network = process.env.NETWORK || 'testnet';
export function formatSpace(id, settings) {
const space = jsonParse(settings, {});
space.id = id;
space.private = space.private || false;
space.avatar =
space.avatar ||
`https://raw.githubusercontent.com/snapshot-labs/snapshot-spaces/master/spaces/${id}/space.png`;
space.about = space.about || '';
space.categories = space.categories || [];
space.admins = space.admins || [];
space.members = space.members || [];
space.plugins = space.plugins || {};
space.filters = space.filters || {};
space.filters.minScore = space.filters.minScore || 0;
space.filters.onlyMembers = space.filters.onlyMembers || false;
space.voting = space.voting || {};
space.voting.delay = space.voting.delay || null;
space.voting.period = space.voting.period || null;
space.voting.type = space.voting.type || null;
space.voting.quorum = space.voting.quorum || null;
space.voting.blind = space.voting.blind || false;
space.voting.hideAbstain = space.voting.hideAbstain || false;
space.validation = space.validation || { name: 'basic', params: {} };
return space;
}
export function formatProposal(proposal) {
proposal.choices = jsonParse(proposal.choices, []);
proposal.strategies = jsonParse(proposal.strategies, []);
proposal.plugins = jsonParse(proposal.plugins, {});
proposal.scores = jsonParse(proposal.scores, []);
proposal.scores_by_strategy = jsonParse(proposal.scores_by_strategy, []);
let proposalState = 'pending';
const ts = parseInt((Date.now() / 1e3).toFixed());
if (ts > proposal.start) proposalState = 'active';
if (ts > proposal.end) proposalState = 'closed';
proposal.state = proposalState;
proposal.space = formatSpace(proposal.space, proposal.settings);
const networkStr = network === 'testnet' ? 'demo.' : '';
proposal.link = `https://${networkStr}snapshot.org/#/${proposal.space.id}/proposal/${proposal.id}`;
return proposal;
}
export function formatVote(vote) {
vote.choice = jsonParse(vote.choice);
vote.metadata = jsonParse(vote.metadata, {});
vote.vp_by_strategy = jsonParse(vote.vp_by_strategy, []);
vote.space = formatSpace(vote.space, vote.settings);
return vote;
}
export function formatFollow(follow) {
follow.space = formatSpace(follow.space, follow.settings);
return follow;
}
export function formatSubscription(subscription) {
subscription.space = formatSpace(subscription.space, subscription.settings);
return subscription;
}
export function buildWhereQuery(fields, alias, where) {
let query: any = '';
const params: any[] = [];
Object.entries(fields).forEach(([field, type]) => {
if (where[field]) {
query += `AND ${alias}.${field} = ? `;
params.push(where[field]);
}
const fieldIn = where[`${field}_in`] || [];
if (fieldIn.length > 0) {
query += `AND ${alias}.${field} IN (?) `;
params.push(fieldIn);
}
if (type === 'number') {
const fieldGt = where[`${field}_gt`];
const fieldGte = where[`${field}_gte`];
const fieldLt = where[`${field}_lt`];
const fieldLte = where[`${field}_lte`];
if (fieldGt) {
query += `AND ${alias}.${field} > ? `;
params.push(fieldGt);
}
if (fieldGte) {
query += `AND ${alias}.${field} >= ? `;
params.push(fieldGte);
}
if (fieldLt) {
query += `AND ${alias}.${field} < ? `;
params.push(fieldLt);
}
if (fieldLte) {
query += `AND ${alias}.${field} <= ? `;
params.push(fieldLte);
}
}
});
return { query, params };
}