-
Notifications
You must be signed in to change notification settings - Fork 24
/
get_proposals.html
121 lines (103 loc) · 4.52 KB
/
get_proposals.html
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../dependencies/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Get Proposals</title>
</head>
<body>
<h1>Proposals</h1>
<div id="web3-warning" class="hidden warning">
Make sure the example app is being served with an HTTP server. <br />
Please install MetaMask: <a href="https://metamask.io/">https://metamask.io/</a>
</div>
<div id="proposals"></div>
</body>
<script type="text/javascript" src="../dependencies/handlebars.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.4.0/dist/web3.min.js"></script>
<script type="text/javascript" src="../dependencies/gov-abi.js"></script>
<script type="text/javascript">
window.addEventListener('load', async (event) => {
let web3;
const web3Warning = document.getElementById('web3-warning');
if (typeof window.ethereum === 'undefined') {
console.error('Client does not have an active Web3 provider or the example app is not being run from an HTTP server.');
console.log('Go here to install MetaMask: https://metamask.io/');
alert(
'You need a Web3 provider to run this page. ' +
'Go here to install MetaMask:\n\n' +
'https://metamask.io/'
);
web3Warning.classList.remove('hidden');
}
await window.ethereum.request({ method: 'eth_requestAccounts' }); // enable ethereum
web3 = new Web3(window.ethereum);
const net = +window.ethereum.chainId;
// This app only works with Ropsten or Main
if (net !== 1 && net !== 3) {
alert('Please select the Main or Ropsten network.');
}
if (net === 1) {
governanceAddress = '0xc0Da02939E1441F497fd74F78cE7Decb17B66529';
} else if (net === 3) {
governanceAddress = '0x087e98b40f988e0e1d1de476b2b8d2271ac84b33';
}
const proposalListContainer = document.getElementById('proposals');
const proposalItemTemplate = Handlebars.compile(`
{{#each this}}
<div class="card">
<h4>{{ title }}</h4>
<p>{{ description }}</p>
<label>ID: </label> <span>{{ id }}</span>
<br />
<label>Votes For: </label> <span>{{ for_votes }}</span>
<br />
<label>Votes Against: </label> <span>{{ against_votes }}</span>
<br />
<label>Status: </label> <span>{{ state }}</span>
</div>
{{/each}}
`);
const enumerateProposalState = (state) => {
const proposalStates = ['Pending', 'Active', 'Canceled', 'Defeated', 'Succeeded', 'Queued', 'Expired', 'Executed'];
return proposalStates[state];
};
// Governor Bravo Contract
const governanceAbi = window.governanceAbi;
const gov = new web3.eth.Contract(governanceAbi, governanceAddress);
(async () => {
// Tell the user that we're waiting on a request to return data
proposalListContainer.innerHTML = '<p style="text-align:center;">Fetching proposals...</p>';
const initialProposalId = await gov.methods.initialProposalId().call();
const proposalCount = await gov.methods.proposalCount().call();
const proposalGets = [];
const proposalStateGets = [];
for (const i of Array.from(Array(parseInt(proposalCount)),(n,i) => i+1).slice(initialProposalId, proposalCount)) {
proposalGets.push(gov.methods.proposals(i).call());
proposalStateGets.push(gov.methods.state(i).call());
}
const proposals = await Promise.all(proposalGets);
const proposalStates = await Promise.all(proposalStateGets);
const proposalCreatedEvents = await gov.getPastEvents('ProposalCreated', {
fromBlock: 0,
toBlock: 'latest'
});
proposals.reverse();
proposalStates.reverse();
proposalCreatedEvents.reverse();
console.log('proposals', proposals)
console.log('proposalStates', proposalStates)
console.log('proposalCreatedEvents', proposalCreatedEvents)
proposals.forEach((p, i) => {
const { description } = proposalCreatedEvents[i].returnValues;
p.title = description.split(/# |\n/g)[1] || 'Untitled';
p.description = description.split(/# |\n/g)[2] || 'No description.';
p.state = enumerateProposalState(proposalStates[i]);
p.for_votes = (parseFloat(p.forVotes) / 1e18).toFixed(2);
p.against_votes = (parseFloat(p.againstVotes) / 1e18).toFixed(2);
});
proposalListContainer.innerHTML = proposalItemTemplate(proposals);
})();
});
</script>
</html>