Delegation and Voting with EIP-712 Signatures

Adam Bavosa
Jul 16, 2020 · 5 min read
Code examples for Compound protocol delegation and voting by signature with EIP-712

Compound’s governance system is powered by COMP token, which is distributed to users of the protocol. COMP token holders receive voting power on a 1–1 basis to the amount of COMP held; this voting power can be delegated to any address, and then can be used to vote on proposals.

There are two methods by which a user can delegate their voting rights or cast votes on proposals: either calling the relevant functions (delegate, castVote) directly; or using by-signature functionality (delegateBySig, castVotebySig).

A key benefit to users of by-signature functionality is that they can create a signed delegate or vote transaction for free, and have a trusted third-party spend ETH on gas fees and write it to the blockchain for them. In this guide, we will focus on code examples around this type of functionality.

Delegate By Signature

By using an EIP-712 “typed-structured data” signature, COMP token holders can delegate their voting rights to any Ethereum address. The COMP smart contract’s delegateBySig method is available to users that have a signed delegation transaction.

A use case for these signatures might be that a delegate wishes to recruit COMP holders to delegate their votes to the delegatee, and to enable them to do so with very low friction.

The delegatee can create a web page where users sign a delegateBySig transaction using MetaMask and their private key, which would then be posted to the delegatee’s web server. Later on, the delegatee can batch signatures into a single Ethereum transaction, and officially collect the voting rights of their constituents by executing the delegateBySig method.

Cast Vote By Signature

With the same type of signature as delegateBySig, users can enable a third party to submit a vote on their behalf in any single Compound governance proposal. The Governor smart contract’s castVoteBySig method is available to anyone that has a signed vote transaction.

The third party in this scenario, submitting the COMP-holder’s signed transaction, could be the same as in the delegateBySig example, however the voting power that they hold is for only a single proposal, instead of indefinitely. The signatory still holds the power to vote on their own behalf in the proposal if the third party has not yet published the signed transaction that was given to them.

Delegate By Signature in a Web3 Site

Using this code example, anyone can create a simple web page that enables users to delegate their voting rights, by signature, to another address. We’ll assume that all users that visit this page are using MetaMask to utilize Web3 functionality.

Create a Compound Governance delegate transaction using an Ethereum EIP-712 signature
Create a Compound Governance delegate transaction using an Ethereum EIP-712 signature

When a user visits the page, they can see their selected Web3 wallet address, and their current Compound governance delegate address. They can fill in the address of the third party that they want to delegate their voting rights to. In practice, this address can be hard-coded into the web page.

Next, the user will click “Create Delegation Signature” which will trigger a MetaMask approval of the data that is to be signed. The MetaMask documentation has an in-depth description of signing data.

MetaMask transaction signature menu
MetaMask transaction signature menu

Here is the event handler that executes when the user clicks the button.

The code utilizes the eth_signTypedData_v4 method, which is implemented within MetaMask. This is used to create typed-structured data signatures, which are described in the EIP-712 specification. In order to create a valid signature, the method needs 3 parameters.

  1. The delegatee’s Ethereum address.
  2. The nonce of the signatory account from the COMP smart contract.
  3. The transaction’s expiry time, in seconds since the Unix epoch.

The typed-structured data signature method accepts the signatory address alongside a JSON string. The EIP-712 specification defines the types, struct, and domain that make up the data that is to be signed. This is implemented in a simple method, which is called in the button-click event handler.

The delegatee, nonce, expiry, and signature can then be used by any Ethereum address to publish the delegate transaction. These are the parameters of the delegateBySig method in the COMP contract. The signature needs to be broken up into 3 parameters, known as v, r, and s.

The full web page code example for delegating with an EIP-712 signature is available in the governance examples GitHub repository.

Cast Vote By Signature in a Web3 Site

Just like in the Delegate By Signature example, a web page can be made for users to create a vote signature. If a user wishes to give away their “ballot” to a third party, that user must sign separate “for” and “against” transactions, and the third-party may choose which transaction to publish.

The structured data object for creating vote signatures is slightly different from a delegate signature. In this case, the user needs a “Ballot” definition.

Clicking “Create Vote Signatures” will prompt the user to sign a “for” and an “against” transaction. Similar to the delegateBySig method, votes can be cast by passing 2 parameters to the castVoteBySig method.

  1. The unique ID of the Compound governance proposal (auto-incrementing integer).
  2. A boolean value of the user’s support of the proposal (true or false).

Additionally, the user must pass the signature, which must be broken up into 3 parameters, known as v, r, and s (implemented in the previous section).

The full web page code example for casting a vote with an EIP-712 signature is available in the governance examples GitHub repository.

Batch and Publish Signatures with a Script or a Smart Contract

Once a user has collected signed transactions for Compound governance, they need to publish those transactions to the Ethereum blockchain. The code for implementing this could be useful for exchanges and wallet apps that manage many private keys for many different users.

A collection of signed transactions can be published to the blockchain all at once using JSON RPC or a smart contract. This example uses only Web3.js in a Node.js script. The script will create and batch-publish delegation signatures for a collection of private keys.

The web3.BatchRequest object batches the delegateBySig transactions and publishes them to the blockchain. The full Node.js example is available in the governance examples GitHub repository. For more information on the Web3.js batching, see the documentation.

The same functionality can be implemented with a Solidity smart contract. By comparison, this method uses marginally less gas than the Web3 batch approach.

The full Solidity example is available in the governance examples GitHub repository which is complete with a JSON RPC script, as well as a deploy script for the smart contract.

Thanks for reading and be sure to subscribe to the Compound Newsletter. Feel free to comment on this post, or get in touch in the #development room of the very active Compound Discord server.

Compound

Compound is an algorithmic, autonomous interest rate protocol built for developers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store