Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: new governor #33

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions components/hooks/useENS.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDefaultProvider } from "@ethersproject/providers";
import { JsonRpcProvider } from "@ethersproject/providers";
import { useEffect, useState } from "react";

const useENS = (address) => {
Expand All @@ -7,7 +7,9 @@ const useENS = (address) => {
useEffect(() => {
const resolveENS = async () => {
if (address) {
const provider = getDefaultProvider();
const provider = new JsonRpcProvider(
process.env.NEXT_PUBLIC_INFURA_RPC
);
const ensName = await provider.lookupAddress(address);
setENSName(ensName);
}
Expand Down
35 changes: 25 additions & 10 deletions containers/vote.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from "axios"; // Axios requests
import { web3p } from "containers"; // Web3
import { createContainer } from "unstated-next"; // Unstated-next containerization
import { GOVERNOR_CHARLIE_ADDRESS, GOVERNOR_CHARLIE_ABI } from "helpers/abi";

// Reference implementation: https://github.com/TennisBowling/comp.vote/blob/master/bySig/vote_by_signature.html
function useVote() {
// Context
const { web3, address } = web3p.useContainer();
Expand All @@ -11,18 +11,30 @@ function useVote() {
* Generate voting message
* @param {Number} proposalId for Compound Governance proposal
* @param {boolean} support for or against
* @param {string} voter address of the voter signing the message
*/
const createVoteBySigMessage = (proposalId, support) => {
const createVoteBySigMessage = async (proposalId, support) => {
// Fetch unused nonce
const governorCharlie = new web3.eth.Contract(
GOVERNOR_CHARLIE_ABI,
GOVERNOR_CHARLIE_ADDRESS
);

const nonce = await governorCharlie.methods.nonces(address).call();

// Types
const types = {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
Ballot: [
{ name: "proposalId", type: "uint256" },
{ name: "support", type: "uint8" },
{ name: "voter", type: "address" },
{ name: "nonce", type: "uint256" },
],
};

Expand All @@ -32,14 +44,17 @@ function useVote() {
primaryType: "Ballot",
// Compound Governor contract
domain: {
name: "Compound Governor Bravo",
name: "Compound Governor",
version: "1",
chainId: 1,
verifyingContract: "0xc0Da02939E1441F497fd74F78cE7Decb17B66529",
verifyingContract: GOVERNOR_CHARLIE_ADDRESS,
},
// Message
message: {
proposalId,
support: support,
voter: address,
nonce,
},
});
};
Expand Down Expand Up @@ -77,7 +92,7 @@ function useVote() {
*/
const voteFor = async (proposalId) => {
// Generate and sign message
const msgParams = createVoteBySigMessage(proposalId, 1);
const msgParams = await createVoteBySigMessage(proposalId, 1);
const signedMsg = await signVote(msgParams);

// POST vote to server
Expand All @@ -86,11 +101,11 @@ function useVote() {

/**
* Generate an AGAINST vote for the proposalId
* @param {Number} proposalId of compund governance proposal
* @param {Number} proposalId of Compound governance proposal
*/
const voteAgainst = async (proposalId) => {
// Generate and sign message
const msgParams = createVoteBySigMessage(proposalId, 0);
const msgParams = await createVoteBySigMessage(proposalId, 0);
const signedMsg = await signVote(msgParams);

// POST vote to server
Expand All @@ -99,11 +114,11 @@ function useVote() {

/**
* Generate an ABSTAIN vote for the proposalId
* @param {Number} proposalId of compund governance proposal
* @param {Number} proposalId of Compound governance proposal
*/
const voteAbstain = async (proposalId) => {
// Generate and sign message
const msgParams = createVoteBySigMessage(proposalId, 2);
const msgParams = await createVoteBySigMessage(proposalId, 2);
const signedMsg = await signVote(msgParams);

// POST vote to server
Expand Down Expand Up @@ -147,7 +162,7 @@ function useVote() {
return {
voteFor,
voteAgainst,
voteAbstain
voteAbstain,
};
}

Expand Down
Loading