Building a Governance Interface

https://medium.com/compound-finance/building-a-governance-interface-474fc271588c?source=rss----22e4df2110f5---4

Quick Start Guide

Community governance has replaced the administrator of the Compound protocol in a major step towards full decentralization.

The primary goal of decentralization is to allow the protocol to evolve into resilient financial infrastructure, with no identifiable weak points and no reliance on any single team. In this way, the protocol can continue to scale with the growth of the entire crypto ecosystem, and last forever — or at least as long as Ethereum exists.

This guide will introduce you to Compound’s governance smart contracts, and walk you through the process of building custom functionality and interfaces that interact with the governance system.

What Is COMP?

The Compound protocol can only be upgraded and configured by COMP token holders and their delegates. All potential changes to the protocol, including the addition of new markets or the adjustment of system parameters like collateral factors or interest rate algorithms, must pass through a proposal and voting process as specified in the governance smart contracts.

COMP is a token that corresponds 1–1 with voting power in Compound governance. Holders of COMP tokens in their Ethereum wallets may delegate their voting rights either to themselves, or to any other Ethereum addresses, using a utility function in the COMP ERC-20 token contract.

The recipients of delegated voting rights, known as delegates, whether they be the COMP holders themselves or another address, may propose, vote on, and execute proposals to modify the protocol. You can see a current list of delegates on the Compound Governance Leaderboard.

More details about COMP can be found in the launch announcement of Compound community governance.

There is a Ropsten testnet COMP faucet in the Compound Discord #Governance channel — just type the phrase Please give me COMP 0x{youraddress}.

Key Concepts for Developing On Governance

Building an interface for governance or extending its functionality is straightforward once you understand the basics. For a more in-depth look at Governance, check out the full documentation. For just a quick start, continue with these key concepts.

  1. COMP — An ERC-20 token that designates the weight of a user’s voting rights. The more COMP a user has in their wallet, the more weight their delegation or vote on a proposal holds.
  2. Delegation — COMP holders cannot vote or create proposals until they delegate their voting rights to an address. Delegation can be given to one address at a time, including the COMP holder’s own address.
  3. Proposals — A proposal is executable code that modifies the protocol and how it works. In order to create a proposal, a user must have at least 1% of all COMP delegated to their address. There is a total of 10 million COMP in existence, so a user must have at least 100,000 COMP delegated to their address. Proposals are stored in the “proposals” mapping of the Governor smart contract. All proposals are subject to a 3-day voting period.
  4. Voting — Users can vote for or against single proposals once they have voting rights delegated to their address. Votes can be cast while a proposal is in the “Active” state. Votes can be submitted immediately using “castVote” or submitted later with “castVoteBySig.” If the majority of votes (and a 4% quorum of delegated COMP, i.e. 400,000 COMP) vote for a proposal, the proposal is queued in the Timelock.
  5. Timelock — All governance and other administrative actions are required to sit in the Timelock for a minimum of 2 days, after which they can be implemented into the protocol.

Over time these key components of the governance system may change, if the community decides to upgrade them in a form of meta-governance. COMP holders will be the ultimate arbiters of the future direction of every aspect of the protocol.

What Can Be Built With Compound Governance?

Application developers can build their own custom workflows and interfaces to facilitate the participation of their users and communities in Compound governance. For example, applications integrated with Compound’s interest rate markets may be interested in adding governance functionality including:

  • Encouraging users to delegate COMP voting power to the application team’s address, so the team can participate in governance on behalf of users.
  • Surfacing specific governance proposals to users so users with COMP can vote on them directly.
  • Giving users transparent insight into upcoming potential changes to Compound, including proposals for adding new markets, or other upgrades.

Such interfaces will require a combination of the following components:

  • Voting interface — Users cast their vote on an active proposal.
  • Delegation interface — Users delegate their voting rights to an address.
  • Voting weight leaderboard — List voting addresses ordered by voting weight.
  • Vote-on-my-behalf interface — Using the “castVoteBySig” function allows users to create an “unmarked ballot” which can be given to another user. This will allow another user to vote on their behalf, without needing to delegate to the other user.
  • Proposal Explorer — Browse past or present governance proposals in a streamlined user interface.
  • Proposal creation interface — Choose protocol modifications and initialize the proposal if the user has enough vote weight (>1%).

Compound Governance Code Examples

A governance interface must read and write to and from the blockchain, and we’ll walk through some basic JavaScript code examples for doing both. Reading data will be done using the Compound API and Web3. However, write operations, like delegating or voting, can only be done with Web3.

We are going to demonstrate how to do each of the following, in order:

  • Get All COMP Token Holders
  • Get All Delegates
  • Get All Proposals (All States)
  • Get All Ballots for a Proposal
  • Delegate Voting Rights
  • Vote on an Active Proposal

The following code examples are all available in the Compound Governance Quick Start repository on GitHub. Check out the live interface examples running on GitHub pages (links are in the repository readme).

Get All COMP Token Holders

Let’s get all COMP token holders in descending order, based on their COMP balance. We can do this with the Compound API’s Governance Service.

https://medium.com/media/ce517d957e1d50ef1909686940fb2c4d/href

Full Code Example

This code uses the built in browser fetch method which returns a JavaScript promise. The repository has an identical example that does a direct blockchain query for COMP holders using Web3.js. We can find out the same information using the COMP smart contract.

The result of either of the code examples is an array of JSON objects that contain the account address, COMP token balance, and account delegate address.

https://medium.com/media/73c95d421bd5f10dc41e0b008c7cb3d0/href

Get All Delegates

We can see all of the addresses that have COMP delegated to them. Below is a blockchain query. It uses the COMP contract’s “DelegateVotesChanged” event to collect each of the current delegates.

https://medium.com/media/b6726f4f69d5a26868d453c5561b764b/href

Full Code Example

Alternatively, this information can be retrieved from the Compound API. Here is the Compound API example. These examples create an array of JSON objects that have the delegate address and the vote weight (percentage) of total COMP supply.

https://medium.com/media/be9df86539f347b7108769c2329ee7c4/href

Get All Proposals (All States)

If you’re making a governance explorer, it’s useful to fetch all of the proposals. The Compound API comes in handy here. This example will get all proposals, regardless of their state. The API is capable of filtering proposals by state, based on request parameters.

https://medium.com/media/a5ac4000f41f77978863abecbc9c24fc/href

Full Code Example

We can also fetch the same proposal data directly with Web3 by using the governance contract’s “ProposalCreated” method. The code examples create an array of JSON objects with the following proposal data.

https://medium.com/media/2826369923023bb442dc3d41d43c4a7f/href

Get All Ballots for a Proposal

Once a proposal has reached the “Active” state, voters can begin casting their ballots. The ballots are publicly stored on the blockchain, so we can retrieve them at any time. The following is an example for fetching submitted votes on proposal #1 on Ropsten.

https://medium.com/media/5782cde58e66259bf41b5c3e6c9d29b4/href

Full Code Example

Proposal IDs are in ascending order starting at 1. To see how many proposals have been made, you can fetch the “proposalCount” variable from the Governor contract. Ballots can also be fetched from the Compound API, like in this Governance service example. Here is the resulting array of JSON objects that have ballot data.

https://medium.com/media/543277e65a72f2ff61dddaad784cc322/href

Delegate Voting Rights

In order to participate in governance, a COMP token holder must delegate their voting rights. Delegation can be done to any address, including the token holder’s own address. Delegation can be made to just one address at a time.

The following is a Web3 example of setting your delegate address. If you do not have COMP tokens, you can still delegate, and future COMP tokens you receive will automatically be delegated to your selected delegate address.

https://medium.com/media/6c17146bc9657ef1f443768b2a88514e/href

Full Code Example

The code relies on the browser being Web3-enabled. Be sure to install MetaMask in your browser. Ropsten ETH can be requested from a faucet. Here’s what it looks like in action:

Vote on an Active Proposal

The most exciting part of Compound Governance is casting your vote in a proposal to change the protocol. Delegates can cast a binary vote of ‘yes’ or ‘no’ for each active proposal.

The following code example drives functionality of a voting user interface. If there are no active proposals on the Ropsten testnet, there will not be any proposals in the selector!

https://medium.com/media/c84c9702e2068ca89e2a29f45ce20e68/href

Full Code Example

Building Your Own Governance Interface

The goal of community governance is to allow the Compound protocol to become truly open and resilient financial infrastructure for all. Reaching this milestone requires the community, developers and users and institutions alike, to create their own interfaces and functionality for participating in Compound governance.

Thanks to the composable and open-access nature of Ethereum, anyone and everyone can create their own COMP and Governance projects. Here are even more resources to help get you started.

Compound Governance Resources

Share what you are working on in the #development channel of the Compound Discord server; someone is always online, and ready to answer questions. For updates, we recommend that you subscribe to the Compound Newsletter.

And if you’ve built something great, let me know at adam@compound.finance!


Building a Governance Interface was originally published in Compound on Medium, where people are continuing the conversation by highlighting and responding to this story.