Skip to content

Commit

Permalink
fetch all proposals using snapshot (#1169)
Browse files Browse the repository at this point in the history
* fetch all proposals using snapshot

* add date time fixes

Co-authored-by: dillchen <dillon@commonwealth.im>
  • Loading branch information
fstar1129 and dillchen authored May 10, 2021
1 parent ae4ea96 commit b15917e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
2 changes: 2 additions & 0 deletions client/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ export async function selectNode(n?: NodeInfo, deferred = false): Promise<boolea
return false;
} else {
app.chain = newChain;
const snapshotId = app.chain?.meta.chain.snapshot;
app.snapshot.fetchSnapshotProposals(snapshotId);
}
if (initApi) {
app.chain.initApi(); // required for loading NearAccounts
Expand Down
25 changes: 20 additions & 5 deletions client/scripts/controllers/server/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@ import $ from 'jquery';
import app from 'state';
import m from 'mithril';
import { SnapshotProposalStore } from 'stores';
import { SnapshotProposal } from '../../models';

class SnapshotController {
private _proposalStore: SnapshotProposalStore = new SnapshotProposalStore();
// private _votes = new Store<SnapshotVote>();
public get proposalStore() { return this._proposalStore; }

public async fetchSnapshotProposals(snapshot: string) {
return new Promise((resolve, reject) => {
this._proposalStore.clear();
resolve('');
m.redraw();
});
const response = await $.get(`https://hub.snapshot.page/api/${snapshot}/proposals`);
// if (response.status !== 'Success') {
// throw new Error(`Cannot fetch snapshot proposals: ${response.status}`);
// }
this._proposalStore.clear();
for (const key in response) {
this._proposalStore.add(new SnapshotProposal(
key,
response[key].address,
+response[key].msg.timestamp,
response[key].msg.payload.start,
response[key].msg.payload.end,
response[key].msg.payload.name,
response[key].msg.payload.body,
response[key].sig,
response[key].authorIpfsHash,
response[key].relayerIpfsHash
))
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions client/scripts/models/SnapshotProposal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SnapshotProposal {
public readonly address: string;
public readonly ipfsHash: string;
public readonly authorAddress: string;
public readonly timestamp: string;
public readonly start: string;
public readonly end: string;
Expand All @@ -9,8 +10,9 @@ class SnapshotProposal {
public readonly authorIpfsHash: string;
public readonly relayerIpfsHash: string;

constructor(address, timestamp, start, end, name, body, sig, authorIpfsHash, relayerIpfsHash) {
this.address = address;
constructor(ipfsHash, authorAddress, timestamp, start, end, name, body, sig, authorIpfsHash, relayerIpfsHash) {
this.ipfsHash = ipfsHash;
this.authorAddress = authorAddress;
this.timestamp = timestamp;
this.start = start;
this.end = end;
Expand Down
27 changes: 14 additions & 13 deletions client/scripts/views/components/sidebar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export const OffchainNavigationModule: m.Component<{}, { dragulaInitialized: tru
const onSearchPage = (p) => p.startsWith(`/${app.activeId()}/search`);
const onMembersPage = (p) => p.startsWith(`/${app.activeId()}/members`)
|| p.startsWith(`/${app.activeId()}/account/`);
const onSnapshotProposal = (p) => p.startsWith(`/${app.activeId()}/snapshot-proposals`);
const onChatPage = (p) => p === `/${app.activeId()}/chat`;

return m('.OffchainNavigationModule.SidebarModule', [
Expand Down Expand Up @@ -129,17 +128,6 @@ export const OffchainNavigationModule: m.Component<{}, { dragulaInitialized: tru
m.route.set(`/${app.activeId()}/members`);
},
}),
app.chain?.meta.chain.snapshot !== null
&& m(Button, {
rounded: true,
fluid: true,
active: onSnapshotProposal(m.route.get()),
label: 'Snapshot proposal',
onclick: (e) => {
e.preventDefault();
m.route.set(`/${app.activeId()}/snapshot-proposals`);
},
}),
// m(Button, {
// rounded: true,
// fluid: true,
Expand Down Expand Up @@ -174,7 +162,8 @@ export const OnchainNavigationModule: m.Component<{}, {}> = {
|| app.chain.class === ChainClass.Moloch
|| app.chain.network === ChainNetwork.Marlin
|| app.chain.network === ChainNetwork.MarlinTestnet
|| app.chain.class === ChainClass.Commonwealth);
|| app.chain.class === ChainClass.Commonwealth
|| app.chain?.meta.chain.snapshot)
if (!hasProposals) return;

const showMolochMenuOptions = app.user.activeAccount && app.chain?.class === ChainClass.Moloch;
Expand All @@ -183,6 +172,7 @@ export const OnchainNavigationModule: m.Component<{}, {}> = {

const showMarlinOptions = app.user.activeAccount && app.chain?.network === ChainNetwork.Marlin;

const onSnapshotProposal = (p) => p.startsWith(`/${app.activeId()}/snapshot-proposals`);
const onProposalPage = (p) => (
p.startsWith(`/${app.activeChainId()}/proposals`)
|| p.startsWith(`/${app.activeChainId()}/proposal/councilmotion`)
Expand Down Expand Up @@ -344,6 +334,17 @@ export const OnchainNavigationModule: m.Component<{}, {}> = {
},
label: 'Approve tokens',
}),
app.chain?.meta.chain.snapshot !== null
&& m(Button, {
rounded: true,
fluid: true,
active: onSnapshotProposal(m.route.get()),
label: 'Snapshot Proposals',
onclick: (e) => {
e.preventDefault();
m.route.set(`/${app.activeId()}/snapshot-proposals`);
},
}),
showCommonwealthMenuOptions && m(Button, {
fluid: true,
rounded: true,
Expand Down
17 changes: 9 additions & 8 deletions client/scripts/views/pages/snapshot_proposals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const SnapshotProposalsPage: m.Component<{ topic?: string }, {
lastSubpage: string;
lastVisitedUpdated?: boolean;
onscroll: any;
allProposals: any;
}> = {
oncreate: (vnode) => {
mixpanel.track('PageVisit', {
Expand All @@ -113,27 +114,27 @@ const SnapshotProposalsPage: m.Component<{ topic?: string }, {
},

oninit: (vnode) => {
const snapshotId = app.chain?.meta.chain.snapshot;
app.snapshot.fetchSnapshotProposals(snapshotId);
},

view: (vnode) => {
let listing = [];
const allProposals = app.snapshot.proposalStore.getAll();

listing.push(m('.discussion-group-wrap', allProposals
.map((proposal) => m(ProposalRow, { proposal }))));
listing.push(m('.discussion-group-wrap', app.snapshot.proposalStore.getAll()
.map((proposal) => m(ProposalRow, { proposal }))));


return m(Sublayout, {
class: 'SnapshotProposalsPage',
title: '',
class: 'DiscussionsPage',
title: 'Snapshot Proposals',
description: '',
showNewProposalButton: true,
}, [
(app.chain || app.community) && [
m('.discussions-main', [
m(SnapshotProposalStagesBar, {}),
m(Listing, { content: listing }),
listing.length === 0
? m('.topic-loading-spinner-wrap', [ m(Spinner, { active: true, size: 'lg' }) ])
: m(Listing, { content: listing }),
])
]
]);
Expand Down
13 changes: 9 additions & 4 deletions client/scripts/views/pages/snapshot_proposals/proposal_row.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'pages/discussions/discussion_row.scss';

import m from 'mithril';
import moment from 'moment-twitter';
import _ from 'lodash';

import app from 'state';
Expand All @@ -12,17 +13,21 @@ import ProposalListingRow from 'views/components/proposal_listing_row';
const ProposalRow: m.Component<{ proposal: SnapshotProposal }, { expanded: boolean }> = {
view: (vnode) => {
const { proposal } = vnode.attrs;

if (!proposal) return;
const proposalLink = `/${app.activeId()}/snapshot-proposals/${proposal.address}`;
const proposalLink = `/${app.activeId()}/snapshot-proposals/${proposal.ipfsHash}`;

const time = moment(+proposal.end * 1000);
const now = moment();
const rowHeader: any = [
link('a', proposalLink, proposal.name),
];
const rowSubheader = [
proposal.address && link('a.proposal-topic', proposalLink, [
m('span.proposal-topic-name', `${proposal.address}`),
proposal.ipfsHash && link('a.proposal-topic', proposalLink, [
m('span.proposal-topic-name', `${proposal.ipfsHash}`),
]),
m('.created-at', link('a', proposalLink, `Ended ${formatLastUpdated(proposal.end)}`)),
m('.created-at', link('a', proposalLink, (now > time) ? `Ended ${formatLastUpdated(time)}`
: `Ending ${formatLastUpdated(moment(+proposal.end * 1000))}`)),
];

return m(ProposalListingRow, {
Expand Down

0 comments on commit b15917e

Please sign in to comment.