Skip to content

Instantly share code, notes, and snippets.

@ajb413
Last active August 17, 2021 22:22
Show Gist options
  • Save ajb413/e5a65991ee7ead9621643643e4835257 to your computer and use it in GitHub Desktop.
Save ajb413/e5a65991ee7ead9621643643e4835257 to your computer and use it in GitHub Desktop.
Example for casting a vote in Compound community governance. https://github.com/compound-developers/compound-governance-examples
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');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment