Skip to content

Commit

Permalink
fix: ERC20 inheritance layout, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
seinmyung25 committed Mar 14, 2024
1 parent 7c1f68d commit 3e7a23d
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 83 deletions.
45 changes: 19 additions & 26 deletions contracts/access/EcoOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,60 @@ import { Ownable2StepUpgradeable } from "@openzeppelin/contracts-upgradeable/acc

interface IOwnable {
function owner() external view returns (address);
function renounceOwnership() external ;
function transferOwnership(address newOwner) external ;

function renounceOwnership() external;

function transferOwnership(address newOwner) external;
}

interface IOwnable2Step is IOwnable {
function pendingOwner() external view returns (address);
function acceptOwnership() external ;

function acceptOwnership() external;
}

interface IEcoOwnable is IOwnable2Step {
function initEcoOwnable(address initialOwner) external ;
function _initEcoOwnable(address initialOwner) external;

function registerPendingOwner(address pendingOwner) external ;
function registerPendingOwner(address pendingOwner) external;
}

abstract contract EcoOwnable is
IEcoOwnable,
Initializable,
Ownable2StepUpgradeable
{
function initEcoOwnable(address initialOwner) public initializer {
abstract contract EcoOwnable is IEcoOwnable, Initializable, Ownable2StepUpgradeable {
function _initEcoOwnable(address initialOwner) public onlyInitializing {
__Ownable_init(initialOwner);
}

function owner() public view virtual
override(IOwnable, OwnableUpgradeable) returns (address) {
function owner() public view virtual override(IOwnable, OwnableUpgradeable) returns (address) {
return super.owner();
}

function renounceOwnership() public virtual
override(IOwnable, OwnableUpgradeable) {
function renounceOwnership() public virtual override(IOwnable, OwnableUpgradeable) {
return super.renounceOwnership();
}

function transferOwnership(address newOwner) public virtual
override(IOwnable, Ownable2StepUpgradeable) {
function transferOwnership(address newOwner) public virtual override(IOwnable, Ownable2StepUpgradeable) {
return OwnableUpgradeable.transferOwnership(newOwner);
}

function pendingOwner() public view virtual
override(IOwnable2Step, Ownable2StepUpgradeable) returns (address) {
function pendingOwner() public view virtual override(IOwnable2Step, Ownable2StepUpgradeable) returns (address) {
return super.pendingOwner();
}

function registerPendingOwner(address nextPendingOwner) public override {
return Ownable2StepUpgradeable.transferOwnership(nextPendingOwner);
}

function acceptOwnership() public virtual
override(IOwnable2Step, Ownable2StepUpgradeable) {
function acceptOwnership() public virtual override(IOwnable2Step, Ownable2StepUpgradeable) {
return super.acceptOwnership();
}

function _transferOwnership(address newOwner) internal virtual
override(Ownable2StepUpgradeable) {
function _transferOwnership(address newOwner) internal virtual override(Ownable2StepUpgradeable) {
return super._transferOwnership(newOwner);
}
}

contract TestEcoOwnable is EcoOwnable {
constructor() {
initEcoOwnable(_msgSender());
constructor() initializer {
_initEcoOwnable(_msgSender());
}
}
}
4 changes: 4 additions & 0 deletions contracts/access/SelectorRoleControlUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ contract SelectorRoleControlUpgradeable is
PausableUpgradeable,
AccessControlEnumerableUpgradeable
{
function initSelectorRoleControl(address initialOnwer) public initializer {
_initEcoOwnable(initialOnwer);
}

modifier onlyAdmin() {
_onlyAdmin(_msgSender());
_;
Expand Down
2 changes: 1 addition & 1 deletion contracts/proxy/admin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract EcoProxyAdmin is IEcoProxyAdmin, EcoOwnable {
}

function initEcoProxyAdmin(address initialOwner) public override initializer {
initEcoOwnable(initialOwner);
_initEcoOwnable(initialOwner);
}

/**
Expand Down
21 changes: 8 additions & 13 deletions contracts/token/ERC20/ERC20L2BridgedUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
pragma solidity ^0.8.0;

import { IERC20Burnable, IERC20Mintable, IEcoERC20 } from "./IERC20.sol";
import { ERC20MintableUpgradeable, ERC20MintableUpgradeableWithDecimal } from "./ERC20MintableUpgradeable.sol";
import { ERC20MintableUpgradeable } from "./ERC20MintableUpgradeable.sol";
import { EcoERC20Upgradeable } from "./EcoERC20Upgradeable.sol";

import { IERC165 } from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import { AccessControlEnumerableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/extensions/AccessControlEnumerableUpgradeable.sol";
Expand All @@ -27,13 +28,7 @@ interface IKromaBridgedERC20 {
function burn(address _from, uint256 _amount) external;
}

contract ERC20L2BridgedUpgradeable is IL2BridgeERC20, ERC20MintableUpgradeableWithDecimal {
constructor(
string memory name,
string memory symbol,
uint8 decimals
) ERC20MintableUpgradeableWithDecimal(name, symbol, decimals) {}

contract ERC20L2BridgedUpgradeable is IL2BridgeERC20, EcoERC20Upgradeable {
// keccak256(abi.encode(uint256(keccak256("eco.storage.ERC20L2Bridged")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant ERC20L2BridgedStorageLocation =
0x077ffeaa7eec0cfdcda90af8784697c3c22a1b5cfbafe2f9887cbd76cdb47300;
Expand All @@ -53,12 +48,12 @@ contract ERC20L2BridgedUpgradeable is IL2BridgeERC20, ERC20MintableUpgradeableWi
address initialOwner,
string memory name,
string memory symbol,
uint8 _decimals,
uint8 decimals,
address remoteToken,
address bridge
) public initializer {
require(_decimals == decimals(), "decimal");
initEcoERC20Mintable(initialOwner, name, symbol);
initEcoERC20(initialOwner, name, symbol, decimals);

ERC20L2BridgedStorage storage $ = _getERC20L2BridgedStorage();
$.REMOTE_TOKEN = remoteToken;
$.BRIDGE = bridge;
Expand All @@ -72,11 +67,11 @@ contract ERC20L2BridgedUpgradeable is IL2BridgeERC20, ERC20MintableUpgradeableWi
return interfaceId == type(IKromaBridgedERC20).interfaceId || super.supportsInterface(interfaceId);
}

function burn(uint256 amount) public override(ERC20MintableUpgradeable, IERC20Burnable) {
function burn(uint256 amount) public override(EcoERC20Upgradeable, IERC20Burnable) {
super.burn(amount);
}

function burnFrom(address account, uint256 amount) public override(ERC20MintableUpgradeable, IERC20Burnable) {
function burnFrom(address account, uint256 amount) public override(EcoERC20Upgradeable, IERC20Burnable) {
super.burnFrom(account, amount);
}

Expand Down
49 changes: 49 additions & 0 deletions contracts/token/ERC20/ERC20MetadataUpgradeable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/ERC721.sol)

pragma solidity ^0.8.0;

import { ERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";

import { IEcoERC20Metadata, IERC20Metadata } from "./IERC20.sol";

abstract contract ERC20MetadataUpgradeable is IEcoERC20Metadata, ERC20Upgradeable {
// keccak256(abi.encode(uint256(keccak256("eco.storage.ERC20MetadataUpgradeable")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant ERC20MetadataUpgradeableStorageLocation =
0x5bacbea0cd534f9867e3a5c99fe0e401e8856261242468f24aa4869ec40ac300;

struct ERC20MetadataUpgradeableStorage {
string name;
string symbol;
uint8 decimals;
}

function _getERC20MetadataUpgradeableStorage() private pure returns (ERC20MetadataUpgradeableStorage storage $) {
assembly {
$.slot := ERC20MetadataUpgradeableStorageLocation
}
}

function _initEcoERC20Metadata(
string memory _name,
string memory _symbol,
uint8 _decimals
) public override onlyInitializing {
ERC20MetadataUpgradeableStorage storage $ = _getERC20MetadataUpgradeableStorage();
$.name = _name;
$.symbol = _symbol;
$.decimals = _decimals;
}

function name() public view virtual override(ERC20Upgradeable, IERC20Metadata) returns (string memory) {
return _getERC20MetadataUpgradeableStorage().name;
}

function symbol() public view virtual override(ERC20Upgradeable, IERC20Metadata) returns (string memory) {
return _getERC20MetadataUpgradeableStorage().symbol;
}

function decimals() public view virtual override(ERC20Upgradeable, IERC20Metadata) returns (uint8) {
return _getERC20MetadataUpgradeableStorage().decimals;
}
}
43 changes: 4 additions & 39 deletions contracts/token/ERC20/ERC20MintableUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,16 @@ import { ISelectorRoleControl, IPausable, IEcoOwnable, SelectorRoleControlUpgrad
import { ERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import { ERC20BurnableUpgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";

import { IERC20Metadata, IERC20Base, IERC20Burnable, IERC20Mintable, IEcoERC20Mintable, IEcoERC20 } from "./IERC20.sol";
import { IERC20Metadata, IERC20Burnable, IEcoERC20Mintable } from "./IERC20.sol";

abstract contract ERC20Decimal is ERC20Upgradeable {
uint8 private immutable _decimals_;
import { ERC20MintableUpgradeable } from "./ERC20MintableUpgradeable.sol";

constructor(uint8 _decimals) {
_decimals_ = _decimals;
}

function decimals() public view virtual override returns (uint8) {
return _decimals_;
}
}

contract ERC20MintableUpgradeable is
IEcoERC20,
abstract contract ERC20MintableUpgradeable is
IEcoERC20Mintable,
SelectorRoleControlUpgradeable,
ERC20Upgradeable,
ERC20BurnableUpgradeable
{
constructor(string memory name, string memory symbol) initializer {
initEcoERC20Mintable(_msgSender(), name, symbol);
}

function initEcoERC20Mintable(
address initialOwner,
string memory name,
string memory symbol
) public override initializer {
__Ownable_init(initialOwner);
__ERC20_init(name, symbol);
}

function mint(address to, uint256 amount) public virtual override onlyAdmin {
_mint(to, amount);
}
Expand All @@ -56,15 +33,3 @@ contract ERC20MintableUpgradeable is
return super.burnFrom(account, amount);
}
}

contract ERC20MintableUpgradeableWithDecimal is ERC20MintableUpgradeable, ERC20Decimal {
constructor(
string memory name,
string memory symbol,
uint8 _decimals
) initializer ERC20MintableUpgradeable(name, symbol) ERC20Decimal(_decimals) {}

function decimals() public view virtual override(IERC20Metadata, ERC20Upgradeable, ERC20Decimal) returns (uint8) {
return super.decimals();
}
}
95 changes: 95 additions & 0 deletions contracts/token/ERC20/EcoERC20Upgradeable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/ERC721.sol)

pragma solidity ^0.8.0;

import { ISelectorRoleControl, IPausable, IEcoOwnable, SelectorRoleControlUpgradeable } from "../../access/SelectorRoleControlUpgradeable.sol";

import { ERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import { ERC20BurnableUpgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";

import { IERC20, IERC20Metadata, IERC20Burnable, IEcoERC20 } from "./IERC20.sol";

import { ERC20MetadataUpgradeable } from "./ERC20MetadataUpgradeable.sol";
import { ERC20MintableUpgradeable } from "./ERC20MintableUpgradeable.sol";

contract EcoERC20Upgradeable is IEcoERC20, ERC20MetadataUpgradeable, ERC20MintableUpgradeable {
function initEcoERC20(
address initialOwner,
string memory _name,
string memory _symbol,
uint8 _decimals
) public initializer {
_initEcoOwnable(initialOwner);
_initEcoERC20Metadata(_name, _symbol, _decimals);
}

function name()
public
view
override(ERC20MetadataUpgradeable, ERC20Upgradeable, IERC20Metadata)
returns (string memory)
{
return super.name();
}

function symbol()
public
view
override(ERC20MetadataUpgradeable, ERC20Upgradeable, IERC20Metadata)
returns (string memory)
{
return super.symbol();
}

function decimals()
public
view
override(ERC20MetadataUpgradeable, ERC20Upgradeable, IERC20Metadata)
returns (uint8)
{
return super.decimals();
}

function burn(uint256 amount) public virtual override(ERC20MintableUpgradeable, IERC20Burnable) {
super.burn(amount);
}

function burnFrom(
address account,
uint256 amount
) public virtual override(ERC20MintableUpgradeable, IERC20Burnable) {
super.burnFrom(account, amount);
}

function totalSupply() public view override(ERC20Upgradeable, IERC20) returns (uint256) {
return super.totalSupply();
}

function balanceOf(address account) public view override(ERC20Upgradeable, IERC20) returns (uint256) {
return super.balanceOf(account);
}

function transfer(address to, uint256 value) public virtual override(ERC20Upgradeable, IERC20) returns (bool) {
return super.transfer(to, value);
}

function allowance(
address owner,
address spender
) public view override(ERC20Upgradeable, IERC20) returns (uint256) {
return super.allowance(owner, spender);
}

function approve(address spender, uint256 value) public virtual override(ERC20Upgradeable, IERC20) returns (bool) {
return super.approve(spender, value);
}

function transferFrom(
address from,
address to,
uint256 value
) public virtual override(ERC20Upgradeable, IERC20) returns (bool) {
return super.transferFrom(from, to, value);
}
}
10 changes: 6 additions & 4 deletions contracts/token/ERC20/IERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { ISelectorRoleControl } from "../../access/SelectorRoleControlUpgradeabl

interface IERC20Base is IERC20, IERC20Metadata, IERC20Errors {}

interface IEcoERC20Metadata is IERC20Base {
function _initEcoERC20Metadata(string memory _name, string memory _symbol, uint8 _decimals) external;
}

interface IWETH is IERC20, IERC20Metadata {
event Deposit(address indexed acc, uint256 amount);
event Withdrawal(address indexed acc, uint256 amount);
Expand All @@ -32,8 +36,6 @@ interface IERC20Mintable is IERC20Burnable {
function mint(address to, uint256 amount) external;
}

interface IEcoERC20Mintable is IERC20Mintable {
function initEcoERC20Mintable(address initialOwner, string memory name, string memory symbol) external;
}
interface IEcoERC20Mintable is IERC20Mintable {}

interface IEcoERC20 is ISelectorRoleControl, IEcoERC20Mintable {}
interface IEcoERC20 is IEcoERC20Mintable, ISelectorRoleControl {}

0 comments on commit 3e7a23d

Please sign in to comment.