From 75e5aba91c5e1734039e1312d850c099dafacccc Mon Sep 17 00:00:00 2001 From: PacificYield <173040337+PacificYield@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:23:09 +0100 Subject: [PATCH] refactor: Set functions for FHEVM/Gateway --- .solcover.js | 2 +- contracts/governance/GovernorAlphaZama.sol | 5 +---- contracts/test/DefaultFHEVMConfig.sol | 10 ++++++++++ contracts/test/DefaultGatewayConfig.sol | 10 ++++++++++ contracts/test/governance/TestComp.sol | 11 +++++++++++ .../test/governance/TestGovernorAlphaZama.sol | 17 +++++++++++++++++ .../token/ERC20/TestEncryptedERC20Mintable.sol | 15 +++++++++++++++ .../TestEncryptedERC20WithErrorsMintable.sol | 15 +++++++++++++++ contracts/token/ERC20/EncryptedERC20.sol | 1 - test/encryptedERC20/EncryptedERC20.fixture.ts | 6 +++--- .../EncryptedERC20WithErrors.fixture.ts | 8 ++++---- test/governance/Comp.fixture.ts | 12 ++++++------ test/governance/GovernorAlphaZama.fixture.ts | 8 ++++---- 13 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 contracts/test/DefaultFHEVMConfig.sol create mode 100644 contracts/test/DefaultGatewayConfig.sol create mode 100644 contracts/test/governance/TestComp.sol create mode 100644 contracts/test/governance/TestGovernorAlphaZama.sol create mode 100644 contracts/test/token/ERC20/TestEncryptedERC20Mintable.sol create mode 100644 contracts/test/token/ERC20/TestEncryptedERC20WithErrorsMintable.sol diff --git a/.solcover.js b/.solcover.js index a7a7000..1834226 100644 --- a/.solcover.js +++ b/.solcover.js @@ -2,7 +2,7 @@ export const istanbulReporter = ["html", "lcov"]; export const providerOptions = { mnemonic: process.env.MNEMONIC, }; -export const skipFiles = ["mocks", "test", "fhevmTemp"]; +export const skipFiles = ["test", "fhevmTemp"]; export const mocha = { fgrep: "[skip-on-coverage]", invert: true, diff --git a/contracts/governance/GovernorAlphaZama.sol b/contracts/governance/GovernorAlphaZama.sol index 95ba7d2..47d73c7 100644 --- a/contracts/governance/GovernorAlphaZama.sol +++ b/contracts/governance/GovernorAlphaZama.sol @@ -17,7 +17,7 @@ import { ICompoundTimelock } from "./ICompoundTimelock.sol"; * - Proposal: A new proposal is made to introduce a change. * - Voting: Users can vote on the proposal, either in favor or against it. * - Quorum: A minimum number of votes (quorum) must be reached for the proposal to pass. - * - Execution: Once a proposal passes, it is executed and takes effect on the taregt protocol. + * - Execution: Once a proposal passes, it is executed and takes effect on the protocol. */ contract GovernorAlphaZama is Ownable2Step, GatewayCaller { /// @notice Returned if proposal contains too many changes. @@ -245,9 +245,6 @@ contract GovernorAlphaZama is Ownable2Step, GatewayCaller { * 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_) { - TFHE.setFHEVM(FHEVMConfig.defaultConfig()); - Gateway.setGateway(Gateway.defaultGatewayAddress()); - TIMELOCK = ICompoundTimelock(timelock_); COMP = IComp(comp_); VOTING_PERIOD = votingPeriod_; diff --git a/contracts/test/DefaultFHEVMConfig.sol b/contracts/test/DefaultFHEVMConfig.sol new file mode 100644 index 0000000..07dc36a --- /dev/null +++ b/contracts/test/DefaultFHEVMConfig.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +pragma solidity ^0.8.24; + +import "fhevm/lib/TFHE.sol"; + +contract DefaultFHEVMConfig { + constructor() { + TFHE.setFHEVM(FHEVMConfig.defaultConfig()); + } +} diff --git a/contracts/test/DefaultGatewayConfig.sol b/contracts/test/DefaultGatewayConfig.sol new file mode 100644 index 0000000..75c87d1 --- /dev/null +++ b/contracts/test/DefaultGatewayConfig.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +pragma solidity ^0.8.24; + +import "fhevm/gateway/GatewayCaller.sol"; + +contract DefaultGatewayConfig { + constructor() { + Gateway.setGateway(Gateway.defaultGatewayAddress()); + } +} diff --git a/contracts/test/governance/TestComp.sol b/contracts/test/governance/TestComp.sol new file mode 100644 index 0000000..a4c4f11 --- /dev/null +++ b/contracts/test/governance/TestComp.sol @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +pragma solidity ^0.8.24; + +import { Comp } from "../../governance/Comp.sol"; +import { DefaultFHEVMConfig } from "../DefaultFHEVMConfig.sol"; + +contract TestComp is DefaultFHEVMConfig, Comp { + constructor(address owner_) Comp(owner_) { + // + } +} diff --git a/contracts/test/governance/TestGovernorAlphaZama.sol b/contracts/test/governance/TestGovernorAlphaZama.sol new file mode 100644 index 0000000..724ac71 --- /dev/null +++ b/contracts/test/governance/TestGovernorAlphaZama.sol @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +pragma solidity ^0.8.24; + +import { GovernorAlphaZama } from "../../governance/GovernorAlphaZama.sol"; +import { DefaultFHEVMConfig } from "../DefaultFHEVMConfig.sol"; +import { DefaultGatewayConfig } from "../DefaultGatewayConfig.sol"; + +contract TestGovernorAlphaZama is DefaultFHEVMConfig, DefaultGatewayConfig, GovernorAlphaZama { + constructor( + address owner_, + address timelock_, + address comp_, + uint256 votingPeriod_ + ) GovernorAlphaZama(owner_, timelock_, comp_, votingPeriod_) { + // + } +} diff --git a/contracts/test/token/ERC20/TestEncryptedERC20Mintable.sol b/contracts/test/token/ERC20/TestEncryptedERC20Mintable.sol new file mode 100644 index 0000000..2f0968b --- /dev/null +++ b/contracts/test/token/ERC20/TestEncryptedERC20Mintable.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +pragma solidity ^0.8.24; + +import { EncryptedERC20Mintable } from "../../../token/ERC20/extensions/EncryptedERC20Mintable.sol"; +import { DefaultFHEVMConfig } from "../../DefaultFHEVMConfig.sol"; + +contract TestEncryptedERC20Mintable is DefaultFHEVMConfig, EncryptedERC20Mintable { + constructor( + string memory name_, + string memory symbol_, + address owner_ + ) EncryptedERC20Mintable(name_, symbol_, owner_) { + // + } +} diff --git a/contracts/test/token/ERC20/TestEncryptedERC20WithErrorsMintable.sol b/contracts/test/token/ERC20/TestEncryptedERC20WithErrorsMintable.sol new file mode 100644 index 0000000..e415a20 --- /dev/null +++ b/contracts/test/token/ERC20/TestEncryptedERC20WithErrorsMintable.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +pragma solidity ^0.8.24; + +import { EncryptedERC20WithErrorsMintable } from "../../../token/ERC20/extensions/EncryptedERC20WithErrorsMintable.sol"; +import { DefaultFHEVMConfig } from "../../DefaultFHEVMConfig.sol"; + +contract TestEncryptedERC20WithErrorsMintable is DefaultFHEVMConfig, EncryptedERC20WithErrorsMintable { + constructor( + string memory name_, + string memory symbol_, + address owner_ + ) EncryptedERC20WithErrorsMintable(name_, symbol_, owner_) { + // + } +} diff --git a/contracts/token/ERC20/EncryptedERC20.sol b/contracts/token/ERC20/EncryptedERC20.sol index 417f865..a77eec3 100644 --- a/contracts/token/ERC20/EncryptedERC20.sol +++ b/contracts/token/ERC20/EncryptedERC20.sol @@ -38,7 +38,6 @@ abstract contract EncryptedERC20 is IEncryptedERC20 { * @param symbol_ Symbol. */ constructor(string memory name_, string memory symbol_) { - TFHE.setFHEVM(FHEVMConfig.defaultConfig()); _name = name_; _symbol = symbol_; } diff --git a/test/encryptedERC20/EncryptedERC20.fixture.ts b/test/encryptedERC20/EncryptedERC20.fixture.ts index 28e70dd..8e0782e 100644 --- a/test/encryptedERC20/EncryptedERC20.fixture.ts +++ b/test/encryptedERC20/EncryptedERC20.fixture.ts @@ -1,6 +1,6 @@ import { ethers } from "hardhat"; -import type { EncryptedERC20Mintable, IEncryptedERC20 } from "../../types"; +import type { IEncryptedERC20, TestEncryptedERC20Mintable } from "../../types"; import { reencryptEuint64 } from "../reencrypt"; import { Signers } from "../signers"; import { FhevmInstances } from "../types"; @@ -10,8 +10,8 @@ export async function deployEncryptedERC20Fixture( name: string, symbol: string, owner: string, -): Promise { - const contractFactory = await ethers.getContractFactory("EncryptedERC20Mintable"); +): Promise { + const contractFactory = await ethers.getContractFactory("TestEncryptedERC20Mintable"); const contract = await contractFactory .connect(signers[owner as keyof Signers]) .deploy(name, symbol, signers[owner as keyof Signers].address); diff --git a/test/encryptedERC20/EncryptedERC20WithErrors.fixture.ts b/test/encryptedERC20/EncryptedERC20WithErrors.fixture.ts index 9d2c424..d438d7e 100644 --- a/test/encryptedERC20/EncryptedERC20WithErrors.fixture.ts +++ b/test/encryptedERC20/EncryptedERC20WithErrors.fixture.ts @@ -1,6 +1,6 @@ import { ethers } from "hardhat"; -import type { EncryptedERC20WithErrorsMintable } from "../../types"; +import type { TestEncryptedERC20WithErrorsMintable } from "../../types"; import { reencryptEuint8 } from "../reencrypt"; import { Signers } from "../signers"; import { FhevmInstances } from "../types"; @@ -10,8 +10,8 @@ export async function deployEncryptedERC20WithErrorsFixture( name: string, symbol: string, owner: string, -): Promise { - const contractFactory = await ethers.getContractFactory("EncryptedERC20WithErrorsMintable"); +): Promise { + const contractFactory = await ethers.getContractFactory("TestEncryptedERC20WithErrorsMintable"); const contract = await contractFactory .connect(signers[owner as keyof Signers]) .deploy(name, symbol, signers[owner as keyof Signers].address); @@ -24,7 +24,7 @@ export async function checkErrorCode( instances: FhevmInstances, account: string, transferId: bigint, - token: EncryptedERC20WithErrorsMintable, + token: TestEncryptedERC20WithErrorsMintable, tokenAddress: string, ): Promise { const errorCodeHandle = await token.getErrorCodeForTransferId(transferId); diff --git a/test/governance/Comp.fixture.ts b/test/governance/Comp.fixture.ts index ab895c7..ef6d4c7 100644 --- a/test/governance/Comp.fixture.ts +++ b/test/governance/Comp.fixture.ts @@ -1,12 +1,12 @@ import { ethers } from "hardhat"; -import type { Comp } from "../../types"; +import type { TestComp } from "../../types"; import { reencryptEuint64 } from "../reencrypt"; import { Signers } from "../signers"; import { FhevmInstances } from "../types"; -export async function deployCompFixture(signers: Signers): Promise { - const contractFactory = await ethers.getContractFactory("Comp"); +export async function deployCompFixture(signers: Signers): Promise { + const contractFactory = await ethers.getContractFactory("TestComp"); const contract = await contractFactory.connect(signers.alice).deploy(signers.alice.address); await contract.waitForDeployment(); return contract; @@ -18,7 +18,7 @@ export async function transferTokensAndDelegate( transferAmount: bigint, account: string, delegate: string, - comp: Comp, + comp: TestComp, compAddress: string, ): Promise { const input = instances.alice.createEncryptedInput(compAddress, signers.alice.address); @@ -42,7 +42,7 @@ export async function reencryptCurrentVotes( signers: Signers, instances: FhevmInstances, account: string, - comp: Comp, + comp: TestComp, compAddress: string, ): Promise { const voteHandle = await comp.getCurrentVotes(signers[account as keyof Signers].address); @@ -55,7 +55,7 @@ export async function reencryptPriorVotes( instances: FhevmInstances, account: string, blockNumber: number, - comp: Comp, + comp: TestComp, compAddress: string, ): Promise { const voteHandle = await comp.getPriorVotes(signers[account as keyof Signers].address, blockNumber); diff --git a/test/governance/GovernorAlphaZama.fixture.ts b/test/governance/GovernorAlphaZama.fixture.ts index fa9374e..ac27117 100644 --- a/test/governance/GovernorAlphaZama.fixture.ts +++ b/test/governance/GovernorAlphaZama.fixture.ts @@ -1,6 +1,6 @@ import { ethers } from "hardhat"; -import type { CompoundTimelock, GovernorAlphaZama } from "../../types"; +import type { CompoundTimelock, TestGovernorAlphaZama } from "../../types"; import { reencryptEbool, reencryptEuint64 } from "../reencrypt"; import { Signers, getSigners } from "../signers"; import { FhevmInstances } from "../types"; @@ -17,11 +17,11 @@ export async function deployGovernorAlphaZamaFixture( signers: Signers, compAddress: string, timelockAddress: string, -): Promise { +): Promise { // @dev We use 5 only for testing purpose. // DO NOT use this value in production. const votingPeriod = 5; - const governorFactory = await ethers.getContractFactory("GovernorAlphaZama"); + const governorFactory = await ethers.getContractFactory("TestGovernorAlphaZama"); const governor = await governorFactory .connect(signers.alice) .deploy(signers.alice.address, timelockAddress, compAddress, votingPeriod); @@ -34,7 +34,7 @@ export async function reencryptVoteReceipt( instances: FhevmInstances, proposalId: bigint, account: string, - governor: GovernorAlphaZama, + governor: TestGovernorAlphaZama, governorAddress: string, ): Promise<[boolean, boolean, bigint]> { const [hasVoted, supportHandle, voteHandle] = await governor.getReceipt(