-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcast_vote.html
167 lines (149 loc) · 5.93 KB
/
cast_vote.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!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>Cast Vote</title>
</head>
<body>
<h1>Cast My Vote</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 class="card">
<label>My Address: </label> <span id="my-address"></span>
<br />
<label>My Vote Weight: </label> <span id="vote-weight"></span>
<br />
<label>Proposal: </label> <select id="proposal-selector"></select>
<br />
<label>Vote: </label>
<input type="radio" id="for" name="vote" value="for">
<label for="for">For</label>
<input type="radio" id="against" name="vote" value="against">
<label for="against">Against</label>
<input type="radio" id="abstain" name="vote" value="abstain">
<label for="abstain">Abstain</label>
<br/ >
<div id="loader" class="loader-container hidden">
<div class="loader"></div>
<span>Ethereum transactions may take a few minutes to be mined.</span>
</div>
<br />
<input id="submit" type="submit" value="Cast Vote" />
</div>
</body>
<script type="text/javascript" src="../dependencies/gov-abi.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">
window.addEventListener('load', (event) => {
let web3;
const web3Warning = document.getElementById('web3-warning');
const myAddress = document.getElementById('my-address');
const voteWeight = document.getElementById('vote-weight');
const proposalSelector = document.getElementById('proposal-selector');
const submit = document.getElementById('submit');
const loader = document.getElementById('loader');
const _for = document.getElementById('for');
const against = document.getElementById('against');
const abstain = document.getElementById('abstain');
let myAccount, governanceAddress, governanceAbi, gov;
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');
} else {
main();
}
async function main() {
const accounts = 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';
}
myAccount = accounts[0];
myAddress.innerText = myAccount;
governanceAbi = window.governanceAbi;
gov = new web3.eth.Contract(governanceAbi, governanceAddress);
// Add a list of active proposals to the UI
getProposals();
// Get my account's vote weight
getMyVoteWeight(myAccount);
submit.onclick = async () => {
// An active proposal needs to be selected in the UI
if (proposalSelector.value < 1) {
alert('Select a proposal to vote in.');
return;
}
// A vote choice needs to be selected in the UI
if (!_for.checked && !against.checked && !abstain.checked) {
alert('Select a vote option.');
return;
}
const proposal = proposalSelector.value;
let textType = 'against';
let vote = 0;
if (_for.checked) {
textType = 'for';
vote = 1;
} else if (abstain.checked) {
textType = 'abstain';
vote = 2;
}
loader.classList.remove('hidden');
try {
const tx = await gov.methods.castVote(proposal, vote).send({ from: myAccount });
console.log(tx);
alert(`Successfully voted ${textType} Proposal ${proposal}`);
window.location.reload();
} catch(e) {
console.error(e);
alert(e.message);
}
loader.classList.add('hidden');
};
function getProposals() {
const network = net === '1' ? 'mainnet' : 'ropsten';
fetch(`https://api.compound.finance/api/v2/governance/proposals?network=${network}&state=active`)
.then((response) => response.json())
.then((result) => {
let proposals = result.proposals || [];
proposalSelector.options[proposalSelector.options.length] = new Option('Select Active Proposal', 0);
for (let i in proposals) {
proposalSelector.options[proposalSelector.options.length] = new Option(proposals[i].id, proposals[i].id);
}
});
}
function getMyVoteWeight(account) {
const network = net === 1 ? 'mainnet' : 'ropsten';
fetch(`https://api.compound.finance/api/v2/governance/accounts?network=${network}&addresses=${account}`)
.then((response) => response.json())
.then((result) => {
console.log(result);
let percentOfVotes = 'ERROR';
try {
percentOfVotes = `${parseFloat(result.accounts[0].vote_weight).toFixed(4)}%`;
} catch (e) {
console.error(e, result);
percentOfVotes = 0;
}
voteWeight.innerText = percentOfVotes;
});
}
}
});
</script>
</html>