Skip to content

Commit

Permalink
transparent proxy model
Browse files Browse the repository at this point in the history
  • Loading branch information
hujw77 committed May 31, 2024
1 parent c57e8dc commit 4ff1701
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 149 deletions.
3 changes: 2 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@openzeppelin/contracts@5.0.2/=lib/openzeppelin-contracts/contracts/
forge-std/=lib/forge-std/src/
@openzeppelin/contracts/=lib/openzeppelin-contracts-upgradeable/lib/openzeppelin-contracts/contracts/
@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/
2 changes: 1 addition & 1 deletion src/collator/CRING.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import "@openzeppelin/contracts@5.0.2/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract CRING is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
Expand Down
12 changes: 4 additions & 8 deletions src/collator/CollatorSet.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

contract CollatorSet {
// collator count;
uint256 public count;
// ordered collators.
mapping(address => address) public collators;
// collator => score = staked_ring * (1 - commission)
mapping(address => uint256) public scoreOf;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "./CollatorStorage.sol";

abstract contract CollatorSet is Initializable, CollatorStorage {
address private constant HEAD = address(0x1);
address private constant TAIL = address(0x2);

event AddCollator(address indexed cur, uint256 score, address prev);
event RemoveCollator(address indexed cur, address prev);
event UpdateCollator(address indexed cur, uint256 score, address oldPrev, address newPrev);

constructor() {
function __CollatorSet_init() internal onlyInitializing {
collators[HEAD] = collators[TAIL];
scoreOf[HEAD] = type(uint256).max;
}
Expand Down
4 changes: 2 additions & 2 deletions src/collator/CollatorStaking.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import "@openzeppelin/contracts@5.0.2/utils/Address.sol";
import "@openzeppelin/contracts@5.0.2/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";

// Inheritance
import "./CRING.sol";
Expand Down
42 changes: 15 additions & 27 deletions src/collator/CollatorStakingHub.sol
Original file line number Diff line number Diff line change
@@ -1,43 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import "@openzeppelin/contracts@5.0.2/utils/Strings.sol";
import "@openzeppelin/contracts@5.0.2/utils/Address.sol";
import "@openzeppelin/contracts@5.0.2/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts@5.0.2/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "./interfaces/ICollatorStaking.sol";
import "./CollatorStaking.sol";
import "./CollatorSet.sol";
import "../deposit/interfaces/IDeposit.sol";

// TODO:
// 1. how to set session key.
contract CollatorStakingHub is CollatorSet, ReentrancyGuard {
contract CollatorStakingHub is Initializable, Ownable2StepUpgradeable, ReentrancyGuardUpgradeable, CollatorSet {
using Strings for uint256;
using Address for address payable;
using EnumerableSet for EnumerableSet.UintSet;

// operator => collator
mapping(address => address) public collatorOf;

// collator => commission
mapping(address => uint256) public commissionOf;

struct DepositInfo {
address account;
uint256 assets;
address collator;
}

// user => staked ring
mapping(address => uint256) public stakedRINGOf;
// user => staked depositIds
mapping(address => EnumerableSet.UintSet) private _stakedDepositsOf;
// depositId => depositInfo
mapping(uint256 => DepositInfo) public depositInfoOf;

// Deposit NFT.
IDeposit public immutable DEPOSIT;
// TODO:
Expand All @@ -60,7 +43,12 @@ contract CollatorStakingHub is CollatorSet, ReentrancyGuard {
_;
}

constructor(address dps) CollatorSet() {
function initialize(address dao) public initializer {
__CollatorSet_init();
__Ownable_init(dao);
}

constructor(address dps) {
DEPOSIT = IDeposit(dps);
}

Expand Down
33 changes: 33 additions & 0 deletions src/collator/CollatorStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

contract CollatorStorage {
// ---------------------- CollatorSetStorage ----------------------------
// collator count;
uint256 public count;
// ordered collators.
mapping(address => address) public collators;
// collator => score = staked_ring * (1 - commission)
mapping(address => uint256) public scoreOf;

// ---------------------- CollatorStakingHubStorage ---------------------
// operator => collator
mapping(address => address) public collatorOf;
// collator => commission
mapping(address => uint256) public commissionOf;
// user => staked ring
mapping(address => uint256) public stakedRINGOf;
// user => staked depositIds
mapping(address => EnumerableSet.UintSet) internal _stakedDepositsOf;

struct DepositInfo {
address account;
uint256 assets;
address collator;
}
// depositId => depositInfo

mapping(uint256 => DepositInfo) public depositInfoOf;
}
47 changes: 35 additions & 12 deletions src/deposit/Deposit.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import "@openzeppelin/contracts@5.0.2/utils/Address.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721EnumerableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "./interfaces/IKTON.sol";

contract Deposit is ERC721, ERC721Enumerable, ERC721URIStorage, ERC721Burnable {
contract Deposit is
Initializable,
ERC721Upgradeable,
ERC721EnumerableUpgradeable,
ERC721URIStorageUpgradeable,
Ownable2StepUpgradeable
{
using Address for address payable;

struct DepositInfo {
Expand Down Expand Up @@ -40,7 +46,16 @@ contract Deposit is ERC721, ERC721Enumerable, ERC721URIStorage, ERC721Burnable {
_;
}

constructor() ERC721("Deposit Token", "DPS") {}
function initialize(address dao) public initializer {
__ERC721_init("Deposit NFT", "DPS");
__ERC721Enumerable_init();
__ERC721URIStorage_init();
__Ownable_init(dao);
}

constructor() {
_disableInitializers();
}

function migrate(address account, uint64 months, uint64 startAt) external payable onlySystem {
uint256 value = msg.value;
Expand Down Expand Up @@ -140,24 +155,32 @@ contract Deposit is ERC721, ERC721Enumerable, ERC721URIStorage, ERC721Burnable {

function _update(address to, uint256 tokenId, address auth)
internal
override(ERC721, ERC721Enumerable)
override(ERC721Upgradeable, ERC721EnumerableUpgradeable)
returns (address)
{
return super._update(to, tokenId, auth);
}

function _increaseBalance(address account, uint128 value) internal override(ERC721, ERC721Enumerable) {
function _increaseBalance(address account, uint128 value)
internal
override(ERC721Upgradeable, ERC721EnumerableUpgradeable)
{
super._increaseBalance(account, value);
}

function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
function tokenURI(uint256 tokenId)
public
view
override(ERC721Upgradeable, ERC721URIStorageUpgradeable)
returns (string memory)
{
return super.tokenURI(tokenId);
}

function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable, ERC721URIStorage)
override(ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721URIStorageUpgradeable)
returns (bool)
{
return super.supportsInterface(interfaceId);
Expand Down
2 changes: 1 addition & 1 deletion src/deposit/interfaces/IDeposit.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.24;

import "@openzeppelin/contracts@5.0.2/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

interface IDeposit is IERC721 {
function assetsOf(uint256 id) external view returns (uint256);
Expand Down
2 changes: 1 addition & 1 deletion src/deposit/interfaces/IKTON.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.24;

import "@openzeppelin/contracts@5.0.2/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IKTON is IERC20 {
function burn(address from, uint256 amount) external returns (bool);
Expand Down
49 changes: 30 additions & 19 deletions src/governance/GovernanceRing.sol
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import "@openzeppelin/contracts@5.0.2/utils/Address.sol";
import "@openzeppelin/contracts@5.0.2/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC20/extensions/ERC20Permit.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC20/extensions/ERC20Votes.sol";
import "@openzeppelin/contracts@5.0.2/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts@5.0.2/access/Ownable2Step.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20PermitUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20VotesUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "../collator/interfaces/ICollatorStakingHub.sol";
import "../deposit/interfaces/IDeposit.sol";

contract GovernanceRing is ERC20, ERC20Permit, ERC20Votes, Ownable2Step {
contract GovernanceRing is
Initializable,
ERC20Upgradeable,
ERC20PermitUpgradeable,
ERC20VotesUpgradeable,
Ownable2StepUpgradeable
{
using Address for address payable;
using EnumerableSet for EnumerableSet.UintSet;

ICollatorStakingHub public HUB;
// Deposit NFT.
IDeposit public immutable DEPOSIT;
// depositId => user
mapping(uint256 => address) public depositorOf;

Expand All @@ -27,6 +30,8 @@ contract GovernanceRing is ERC20, ERC20Permit, ERC20Votes, Ownable2Step {
// user => wrap depositIds
mapping(address => EnumerableSet.UintSet) private _wrapDeposits;

ICollatorStakingHub public immutable HUB;
IDeposit public immutable DEPOSIT;
address public constant RING = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

event Wrap(address indexed account, address indexed token, uint256 assets);
Expand All @@ -39,11 +44,14 @@ contract GovernanceRing is ERC20, ERC20Permit, ERC20Votes, Ownable2Step {
_;
}

constructor(address dao, address dps, address hub)
ERC20("Governance Ring", "gRING")
ERC20Permit("Governance Ring")
Ownable(dao)
{
function initialize(address dao) public initializer {
__ERC20_init("Governance Ring", "gRING");
__ERC20Permit_init("Governance Ring");
__ERC20Votes_init();
__Ownable_init(dao);
}

constructor(address dps, address hub) {
DEPOSIT = IDeposit(dps);
HUB = ICollatorStakingHub(hub);
}
Expand Down Expand Up @@ -135,11 +143,14 @@ contract GovernanceRing is ERC20, ERC20Permit, ERC20Votes, Ownable2Step {
return "mode=timestamp";
}

function _update(address from, address to, uint256 value) internal override(ERC20, ERC20Votes) {
function _update(address from, address to, uint256 value)
internal
override(ERC20Upgradeable, ERC20VotesUpgradeable)
{
super._update(from, to, value);
}

function nonces(address owner) public view override(ERC20Permit, Nonces) returns (uint256) {
function nonces(address owner) public view override(ERC20PermitUpgradeable, NoncesUpgradeable) returns (uint256) {
return super.nonces(owner);
}
}
48 changes: 0 additions & 48 deletions src/governance/GovernorVotesQuorum.sol

This file was deleted.

Loading

0 comments on commit 4ff1701

Please sign in to comment.