Skip to content

Commit

Permalink
Merge pull request #37 from DemocracyEarth/openzeppelin
Browse files Browse the repository at this point in the history
Implementation of Upgradeable Contracts
  • Loading branch information
santisiri authored Jan 8, 2021
2 parents ffacb5d + 4d39ba8 commit 81bba6b
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ allFiredEvents
coverageEnv/
scTopics
.DS_Store
.openzeppelin

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ Built in collaboration with [Kleros](https://github.com/kleros) and the [Proof o
- Token has `burn` function —a. k. a. deflation— that can be triggered by users or a DAO.
- `Snapshot` event emitted to ease use of token for governance purposes.
- `ProofOfHumanity` registry can be updated with governance mechanism.
- Implements `ERC20Upgradeable` contracts with [OpenZeppelin](https://github.com/openzeppelin) proxy libraries.

Built with [Hardhat](https://github.com/nomiclabs/hardhat) and [OpenZeppelin](https://github.com/openzeppelin) contracts.
Built with [Hardhat](https://github.com/nomiclabs/hardhat).

Latest release is [`version 0.1.3`](https://github.com/DemocracyEarth/ubi/releases)
Latest release is [`version 0.1.4`](https://github.com/DemocracyEarth/ubi/releases)

## Setup

Expand Down
64 changes: 64 additions & 0 deletions contracts/Initializable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.4.24 <=0.7.3;


/**
* @title Initializable
*
* @dev Helper contract to support initializer functions. To use it, replace
* the constructor with a function that has the `initializer` modifier.
* WARNING: Unlike constructors, initializer functions must be manually
* invoked. This applies both to deploying an Initializable contract, as well
* as extending an Initializable contract via inheritance.
* WARNING: When used with inheritance, manual care must be taken to not invoke
* a parent initializer twice, or ensure that all initializers are idempotent,
* because this is not dealt with automatically as with constructors.
*/
contract Initializable {

/**
* @dev Indicates that the contract has been initialized.
*/
bool private initialized;

/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool private initializing;

/**
* @dev Modifier to use in the initializer function of a contract.
*/
modifier initializer() {
require(initializing || isConstructor() || !initialized, "Contract instance has already been initialized");

bool isTopLevelCall = !initializing;
if (isTopLevelCall) {
initializing = true;
initialized = true;
}

_;

if (isTopLevelCall) {
initializing = false;
}
}

/// @dev Returns true if and only if the function is running in the constructor
function isConstructor() private view returns (bool) {
// extcodesize checks the size of the code stored in an address, and
// address returns the current address. Since the code is still not
// deployed when running a constructor, any checks on its code size will
// yield zero, making it an effective way to detect if a contract is
// under construction or not.
address self = address(this);
uint256 cs;
assembly { cs := extcodesize(self) }
return cs == 0;
}

// Reserved storage space to allow for layout changes in the future.
uint256[50] private ______gap;
}
23 changes: 13 additions & 10 deletions contracts/UBI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.3;

import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Snapshot.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20SnapshotUpgradeable.sol";

/**
* @title ProofOfHumanity Interface
Expand All @@ -33,7 +33,7 @@ interface IProofOfHumanity {
}


contract UBI is ERC20Burnable, ERC20Snapshot {
contract UBI is Initializable, ERC20BurnableUpgradeable, ERC20SnapshotUpgradeable {
/* Events */

/** @dev Emitted when UBI is minted or taken by a reporter.
Expand All @@ -47,21 +47,19 @@ contract UBI is ERC20Burnable, ERC20Snapshot {
uint256 _value
);

/* Governable Storage */
/* Storage */

/// @dev How many tokens per second will be minted for every valid human proof per second.
uint256 public accruedPerSecond;

/// @dev To prevent intrinsic risks of flash loan attacks it will restrict key functions to one per block.
mapping(address => uint256) public lastBlock;

/* Constructor Storage */

/// @dev The Proof Of Humanity registry to reference.
IProofOfHumanity public proofOfHumanity;

/// @dev The contract's governor.
address public governor = msg.sender;
address public governor;

/// @dev Persists time of last minted tokens for any given address.
mapping(address => uint256) public lastMintedSecond;
Expand Down Expand Up @@ -119,9 +117,14 @@ contract UBI is ERC20Burnable, ERC20Snapshot {
* @param _accruedPerSecond How much of the token is accrued per block.
* @param _proofOfHumanity The Proof Of Humanity registry to reference.
*/
constructor(uint256 _initialSupply, string memory _name, string memory _symbol, uint256 _accruedPerSecond, IProofOfHumanity _proofOfHumanity) public ERC20(_name, _symbol) {
function initialize(uint256 _initialSupply, string memory _name, string memory _symbol, uint256 _accruedPerSecond, IProofOfHumanity _proofOfHumanity) public initializer {
__Context_init_unchained();
__ERC20_init_unchained(_name, _symbol);

accruedPerSecond = _accruedPerSecond;
proofOfHumanity = _proofOfHumanity;
governor = msg.sender;

_mint(msg.sender, _initialSupply);
}

Expand Down Expand Up @@ -197,7 +200,7 @@ contract UBI is ERC20Burnable, ERC20Snapshot {

/** @dev Overrides with Snapshot mechanisms _beforeTokenTransfer functions.
*/
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC20, ERC20Snapshot) {
ERC20Snapshot._beforeTokenTransfer(from, to, amount);
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC20Upgradeable, ERC20SnapshotUpgradeable) {
ERC20SnapshotUpgradeable._beforeTokenTransfer(from, to, amount);
}
}
4 changes: 2 additions & 2 deletions deployment-params.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Deployment params
module.exports.INITIAL_SUPPLY = '10000000000000000000000000';
module.exports.TOKEN_NAME = "Universal Basic Income";
module.exports.TOKEN_SYMBOL = "EARTH";
module.exports.TOKEN_NAME = "Democracy Earth";
module.exports.TOKEN_SYMBOL = "UBI";
module.exports.ACCRUED_PER_SECOND = '100000000000000';
module.exports.PROOF_OF_HUMANITY_KOVAN = '0x413752ADd5ff51CC1928FA73BEFA65b37dEB8730';
11 changes: 8 additions & 3 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-truffle5");
require("@nomiclabs/hardhat-solhint");
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");
require('@openzeppelin/hardhat-upgrades');
require("hardhat-gas-reporter");
require("solidity-coverage");

Expand All @@ -23,10 +25,13 @@ module.exports = {
develop: {
url: "http://localhost:8545",
},
/* kovan: {
/*
kovan: {
url: `https://kovan.infura.io/v3/${INFURA_API_KEY}`,
accounts: [`0x${KOVAN_PRIVATE_KEY}`]
}, */
accounts: [`0x${KOVAN_PRIVATE_KEY}`],
gasMultiplier: 3
},
*/
coverage: {
url: "http://localhost:8555"
}
Expand Down
145 changes: 145 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@openzeppelin/contracts": "^3.3.0",
"@openzeppelin/contracts-upgradeable": "^3.3.0",
"@openzeppelin/hardhat-upgrades": "^1.4.3",
"bignumber.js": "^9.0.1",
"chai": "^4.2.0",
"chai-as-promised": "7.1.1",
Expand Down
Loading

0 comments on commit 81bba6b

Please sign in to comment.