Skip to content

Commit

Permalink
Merge pull request #28 from zama-ai/feat/refactor-erc20
Browse files Browse the repository at this point in the history
feat: refactored erc20 as abstract with extensions
  • Loading branch information
jatZama authored Mar 5, 2024
2 parents 034fec8 + 42672e8 commit fe0f841
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
9 changes: 5 additions & 4 deletions contracts/DAO/Comp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pragma solidity ^0.8.20;

import "fhevm/abstracts/Reencrypt.sol";
import "fhevm/lib/TFHE.sol";
import "../EncryptedERC20.sol";
import "../token/ERC20/EncryptedERC20.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";

contract Comp is EncryptedERC20 {
contract Comp is EncryptedERC20, Ownable2Step {
/// @notice allowed smart contract
address public allowedContract;

Expand Down Expand Up @@ -44,8 +45,8 @@ contract Comp is EncryptedERC20 {
/**
* @notice Construct a new Comp token
*/
constructor() EncryptedERC20("Compound", "COMP") {
mint(1000000);
constructor() EncryptedERC20("Compound", "COMP") Ownable(msg.sender) {
_mint(1000000);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ pragma solidity ^0.8.20;

import "fhevm/lib/TFHE.sol";
import "fhevm/abstracts/Reencrypt.sol";
import "./utils/EncryptedErrors.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";
import "../../utils/EncryptedErrors.sol";

contract EncryptedERC20 is Reencrypt, Ownable2Step, EncryptedErrors {
abstract contract EncryptedERC20 is Reencrypt, EncryptedErrors {
enum ErrorCodes {
NO_ERROR,
UNSUFFICIENT_BALANCE,
Expand Down Expand Up @@ -37,10 +36,7 @@ contract EncryptedERC20 is Reencrypt, Ownable2Step, EncryptedErrors {
// A mapping of the form mapping(owner => mapping(spender => allowance)).
mapping(address => mapping(address => euint64)) internal allowances;

constructor(
string memory name_,
string memory symbol_
) Ownable(msg.sender) EncryptedErrors(uint8(type(ErrorCodes).max)) {
constructor(string memory name_, string memory symbol_) EncryptedErrors(uint8(type(ErrorCodes).max)) {
_name = name_;
_symbol = symbol_;
}
Expand All @@ -60,11 +56,6 @@ contract EncryptedERC20 is Reencrypt, Ownable2Step, EncryptedErrors {
return _totalSupply;
}

// Increase owner's balance by the given `mintedAmount`.
function mint(uint64 mintedAmount) public virtual onlyOwner {
_mint(mintedAmount);
}

// Increase sender's balance by the given `amount`.
function _mint(uint64 amount) internal virtual {
balances[msg.sender] = TFHE.add(balances[msg.sender], amount); // overflow impossible because of next line
Expand Down
19 changes: 19 additions & 0 deletions contracts/token/ERC20/extensions/EncryptedERC20Mintable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSD-3-Clause-Clear

pragma solidity ^0.8.20;

import "../EncryptedERC20.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";

contract EncryptedERC20Mintable is Ownable2Step, EncryptedERC20 {
constructor(
string memory name_,
string memory symbol_,
address owner
) Ownable(owner) EncryptedERC20(name_, symbol_) {}

// Increase owner's balance by the given `mintedAmount`.
function mint(uint64 mintedAmount) public virtual onlyOwner {
_mint(mintedAmount);
}
}
8 changes: 4 additions & 4 deletions test/encryptedERC20/EncryptedERC20.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ethers } from "hardhat";

import type { EncryptedERC20 } from "../../types";
import type { EncryptedERC20Mintable } from "../../types";
import { getSigners } from "../signers";

export async function deployEncryptedERC20Fixture(): Promise<EncryptedERC20> {
export async function deployEncryptedERC20Fixture(): Promise<EncryptedERC20Mintable> {
const signers = await getSigners();

const contractFactory = await ethers.getContractFactory("EncryptedERC20");
const contract = await contractFactory.connect(signers.alice).deploy("Naraggara", "NARA"); // City of Zama's battle
const contractFactory = await ethers.getContractFactory("EncryptedERC20Mintable");
const contract = await contractFactory.connect(signers.alice).deploy("Naraggara", "NARA", signers.alice.address); // City of Zama's battle
await contract.waitForDeployment();

return contract;
Expand Down

0 comments on commit fe0f841

Please sign in to comment.