Skip to content

Commit

Permalink
refactor: rename Comp.sol to ConfidentialERC20Votes.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Dec 2, 2024
1 parent 285eb3c commit 40b5489
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 173 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ These Solidity templates include governance-related and token-related contracts.

### Governance

- [Comp](./contracts/governance/Comp.sol)
- [ConfidentialERC20Votes](./contracts/governance/ConfidentialERC20Votes.sol)
- [GovernorAlphaZama](./contracts/governance/GovernorAlphaZama.sol)

### Utils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { Ownable2Step, Ownable } from "@openzeppelin/contracts/access/Ownable2St
import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import { SignatureChecker } from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import { ConfidentialERC20 } from "../token/ERC20/ConfidentialERC20.sol";
import { IComp } from "./IComp.sol";
import { IConfidentialERC20Votes } from "./IConfidentialERC20Votes.sol";

/**
* @title Comp
* @title ConfidentialERC20Votes
* @notice This contract inherits ConfidentialERC20, EIP712, and Ownable2Step.
* This is based on the Comp.sol contract written by Compound Labs.
* see: compound-finance/compound-protocol/blob/master/contracts/Governance/Comp.sol
* This is based on the ConfidentialERC20Votes.sol contract written by Compound Labs.
* see: compound-finance/compound-protocol/blob/master/contracts/Governance/ConfidentialERC20Votes.sol
* It is a governance token used to delegate votes, which can be used by contracts such as
* GovernorAlphaZama.sol.
* It uses encrypted votes to delegate the voting power associated
* with an account's balance.
* @dev The delegation of votes leaks information about the account's encrypted balance to the `delegatee`.
*/
abstract contract Comp is IComp, ConfidentialERC20, EIP712, Ownable2Step {
abstract contract ConfidentialERC20Votes is IConfidentialERC20Votes, ConfidentialERC20, EIP712, Ownable2Step {
/// @notice Returned if the `blockNumber` is higher or equal to the (current) `block.number`.
/// @dev It is returned in requests to access votes.
error BlockNumberEqualOrHigherThanCurrentBlock();
Expand Down Expand Up @@ -161,7 +161,7 @@ abstract contract Comp is IComp, ConfidentialERC20, EIP712, Ownable2Step {
}

/**
* @notice See {IComp-getPriorVotesForGovernor}.
* @notice See {IConfidentialERC20Votes-getPriorVotesForGovernor}.
*/
function getPriorVotesForGovernor(address account, uint256 blockNumber) public virtual returns (euint64 votes) {
if (msg.sender != governor) {
Expand Down
18 changes: 9 additions & 9 deletions contracts/governance/GovernorAlphaZama.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "fhevm/lib/TFHE.sol";
import "fhevm/gateway/GatewayCaller.sol";

import { Ownable2Step, Ownable } from "@openzeppelin/contracts/access/Ownable2Step.sol";
import { IComp } from "./IComp.sol";
import { IConfidentialERC20Votes } from "./IConfidentialERC20Votes.sol";
import { ICompoundTimelock } from "./ICompoundTimelock.sol";

/**
Expand Down Expand Up @@ -197,12 +197,12 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
uint256 public constant PROPOSAL_MAX_OPERATIONS = 10;

/// @notice The number of votes required for a voter to become a proposer.
/// @dev It is set at 100,000, which is 1% of the total supply of the Comp token.
/// @dev It is set at 100,000, which is 1% of the total supply of the ConfidentialERC20Votes token.
uint256 public constant PROPOSAL_THRESHOLD = 100000e6;

/// @notice The number of votes in support of a proposal required in order for a quorum to be reached
/// and for a vote to succeed.
/// @dev It is set at 400,000, which is 4% of the total supply of the Comp token.
/// @dev It is set at 400,000, which is 4% of the total supply of the ConfidentialERC20Votes token.
uint64 public constant QUORUM_VOTES = 400000e6;

/// @notice The delay before voting on a proposal may take place once proposed.
Expand All @@ -214,8 +214,8 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
/// (i.e 21,600 for 12-second blocks).
uint256 public immutable VOTING_PERIOD;

/// @notice Comp governance token.
IComp public immutable COMP;
/// @notice ConfidentialERC20Votes governance token.
IConfidentialERC20Votes public immutable CONFIDENTIAL_ERC20_VOTES;

/// @notice Compound Timelock.
ICompoundTimelock public immutable TIMELOCK;
Expand Down Expand Up @@ -253,15 +253,15 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
/**
* @param owner_ Owner address.
* @param timelock_ Timelock contract.
* @param comp_ Comp token.
* @param comp_ ConfidentialERC20Votes token.
* @param votingPeriod_ Voting period.
* @dev Do not use a small value in production such as 5 or 20 to avoid security issues
* unless for testing purpose. It should by at least a few days,.
* For instance, 3 days would have a votingPeriod = 21,600 blocks if 12s per block.
*/
constructor(address owner_, address timelock_, address comp_, uint256 votingPeriod_) Ownable(owner_) {
TIMELOCK = ICompoundTimelock(timelock_);
COMP = IComp(comp_);
CONFIDENTIAL_ERC20_VOTES = IConfidentialERC20Votes(comp_);
VOTING_PERIOD = votingPeriod_;

/// @dev Store these constant-like variables in the storage.
Expand Down Expand Up @@ -443,7 +443,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {

ebool canPropose = TFHE.lt(
_EUINT64_PROPOSAL_THRESHOLD,
COMP.getPriorVotesForGovernor(msg.sender, block.number - 1)
CONFIDENTIAL_ERC20_VOTES.getPriorVotesForGovernor(msg.sender, block.number - 1)
);

uint256[] memory cts = new uint256[](1);
Expand Down Expand Up @@ -645,7 +645,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
revert VoterHasAlreadyVoted();
}

euint64 votes = COMP.getPriorVotesForGovernor(voter, proposal.startBlock);
euint64 votes = CONFIDENTIAL_ERC20_VOTES.getPriorVotesForGovernor(voter, proposal.startBlock);
proposal.forVotes = TFHE.select(support, TFHE.add(proposal.forVotes, votes), proposal.forVotes);
proposal.againstVotes = TFHE.select(support, proposal.againstVotes, TFHE.add(proposal.againstVotes, votes));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ pragma solidity ^0.8.24;
import "fhevm/lib/TFHE.sol";

/**
* @title IComp
* @title IConfidentialERC20Votes
* @dev The GovernorAlphaZama relies on this interface.
*/
interface IComp {
interface IConfidentialERC20Votes {
/**
* @notice Determine the prior number of votes for an account as of a block number.
* @dev Block number must be a finalized block or else this function will revert.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;

import { Comp } from "../../governance/Comp.sol";
import { ConfidentialERC20Votes } from "../../governance/ConfidentialERC20Votes.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";

contract TestComp is MockZamaFHEVMConfig, Comp {
contract TestConfidentialERC20Votes is MockZamaFHEVMConfig, ConfidentialERC20Votes {
constructor(
address owner_,
string memory name_,
string memory symbol_,
string memory version_,
uint64 totalSupply_
) Comp(owner_, name_, symbol_, version_, totalSupply_) {
) ConfidentialERC20Votes(owner_, name_, symbol_, version_, totalSupply_) {
//
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Comp
# ConfidentialERC20Votes

This contract inherits ConfidentialERC20, EIP712, and Ownable2Step. This is based on the Comp.sol contract written by
Compound Labs. see: compound-finance/compound-protocol/blob/master/contracts/Governance/Comp.sol. It is a governance
token used to delegate votes, which can be used by contracts such as GovernorAlphaZama.sol. It uses encrypted votes to
This contract inherits ConfidentialERC20, EIP712, and Ownable2Step. This is based on the ConfidentialERC20Votes.sol
contract written by Compound Labs. see:
compound-finance/compound-protocol/blob/master/contracts/Governance/ConfidentialERC20Votes.sol. It is a governance token
used to delegate votes, which can be used by contracts such as GovernorAlphaZama.sol. It uses encrypted votes to
delegate the voting power associated with an account's balance.

_The delegation of votes leaks information about the account's encrypted balance to the `delegatee`._
Expand Down Expand Up @@ -151,7 +152,7 @@ The number of checkpoints for an `account`.
## \_checkpoints

```solidity
mapping(address => mapping(uint32 => struct Comp.Checkpoint)) _checkpoints
mapping(address => mapping(uint32 => struct ConfidentialERC20Votes.Checkpoint)) _checkpoints
```

A record of votes \_checkpoints for an `account` using incremental indices.
Expand Down Expand Up @@ -223,7 +224,7 @@ _This function enables the sender to cancel a signature._
function getPriorVotesForGovernor(address account, uint256 blockNumber) public virtual returns (euint64 votes)
```

See {IComp-getPriorVotesForGovernor}.
See {IConfidentialERC20Votes-getPriorVotesForGovernor}.

## getCurrentVotes

Expand Down
20 changes: 10 additions & 10 deletions docs/governance/GovernorAlphaZama.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ uint256 PROPOSAL_THRESHOLD

The number of votes required for a voter to become a proposer.

_It is set at 100,000, which is 1% of the total supply of the Comp token._
_It is set at 100,000, which is 1% of the total supply of the ConfidentialERC20Votes token._

## QUORUM_VOTES

Expand All @@ -311,7 +311,7 @@ uint64 QUORUM_VOTES

The number of votes in support of a proposal required in order for a quorum to be reached and for a vote to succeed.

_It is set at 400,000, which is 4% of the total supply of the Comp token._
_It is set at 400,000, which is 4% of the total supply of the ConfidentialERC20Votes token._

## VOTING_DELAY

Expand All @@ -334,10 +334,10 @@ _It is recommended to be set at 3 days in blocks (i.e 21,600 for 12-second block
## COMP

```solidity
contract IComp COMP
contract IConfidentialERC20Votes COMP
```

Comp governance token.
ConfidentialERC20Votes governance token.

## TIMELOCK

Expand Down Expand Up @@ -400,12 +400,12 @@ by at least a few days,. For instance, 3 days would have a votingPeriod = 21,600

### Parameters

| Name | Type | Description |
| -------------- | ------- | ------------------ |
| owner\_ | address | Owner address. |
| timelock\_ | address | Timelock contract. |
| comp\_ | address | Comp token. |
| votingPeriod\_ | uint256 | Voting period. |
| Name | Type | Description |
| ------------------------ | ------- | ----------------------------- |
| owner\_ | address | Owner address. |
| timelock\_ | address | Timelock contract. |
| confidentialERC20Votes\_ | address | ConfidentialERC20Votes token. |
| votingPeriod\_ | uint256 | Voting period. |

## cancel

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# IComp
# IConfidentialERC20Votes

_The GovernorAlphaZama relies on this interface._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { parseUnits } from "ethers";
import { ethers } from "hardhat";

import type { TestComp } from "../../types";
import type { TestConfidentialERC20Votes } from "../../types";
import { reencryptEuint64 } from "../reencrypt";
import { Signers } from "../signers";
import { FhevmInstances } from "../types";

export async function deployCompFixture(signers: Signers): Promise<TestComp> {
const contractFactory = await ethers.getContractFactory("TestComp");
export async function deployConfidentialERC20Votes(signers: Signers): Promise<TestConfidentialERC20Votes> {
const contractFactory = await ethers.getContractFactory("TestConfidentialERC20Votes");
const contract = await contractFactory
.connect(signers.alice)
.deploy(signers.alice.address, "CompoundZama", "COMP", "1.0", parseUnits("10000000", 6));
Expand All @@ -21,32 +21,34 @@ export async function transferTokensAndDelegate(
transferAmount: bigint,
account: string,
delegate: string,
comp: TestComp,
confidentialERC20Votes: TestConfidentialERC20Votes,
compAddress: string,
): Promise<void> {
const input = instances.alice.createEncryptedInput(compAddress, signers.alice.address);
input.add64(transferAmount);
const encryptedTransferAmount = await input.encrypt();

let tx = await comp
let tx = await confidentialERC20Votes
.connect(signers.alice)
[
"transfer(address,bytes32,bytes)"
](signers[account as keyof Signers], encryptedTransferAmount.handles[0], encryptedTransferAmount.inputProof);
await tx.wait();

tx = await comp.connect(signers[account as keyof Signers]).delegate(signers[delegate as keyof Signers].address);
tx = await confidentialERC20Votes
.connect(signers[account as keyof Signers])
.delegate(signers[delegate as keyof Signers].address);
await tx.wait();
}

export async function reencryptCurrentVotes(
signers: Signers,
instances: FhevmInstances,
account: string,
comp: TestComp,
confidentialERC20Votes: TestConfidentialERC20Votes,
compAddress: string,
): Promise<bigint> {
const voteHandle = await comp.getCurrentVotes(signers[account as keyof Signers].address);
const voteHandle = await confidentialERC20Votes.getCurrentVotes(signers[account as keyof Signers].address);
const vote = await reencryptEuint64(signers, instances, account, voteHandle, compAddress);
return vote;
}
Expand All @@ -56,10 +58,10 @@ export async function reencryptPriorVotes(
instances: FhevmInstances,
account: string,
blockNumber: number,
comp: TestComp,
confidentialERC20Votes: TestConfidentialERC20Votes,
compAddress: string,
): Promise<bigint> {
const voteHandle = await comp.getPriorVotes(signers[account as keyof Signers].address, blockNumber);
const voteHandle = await confidentialERC20Votes.getPriorVotes(signers[account as keyof Signers].address, blockNumber);
const vote = await reencryptEuint64(signers, instances, account, voteHandle, compAddress);
return vote;
}
Loading

0 comments on commit 40b5489

Please sign in to comment.