Skip to content

Commit

Permalink
Migrate Contracts to Solidity v0.6 (OpenZeppelin#2080)
Browse files Browse the repository at this point in the history
* Initial migration to Solidity 0.6.x - v3.0 first steps (OpenZeppelin#2063)

* Initial migration, missing GSN, 721, 777 and Crowdsales.

* Add _beforeTokenOperation and _afterTokenOperation.

* Add documentation for hooks.

* Add hooks doc

* Add missing drafts

* Add back ERC721 with hooks

* Bring back ERC777

* Notes on hooks

* Bring back GSN

* Make functions virtual

* Make GSN overrides explicit

* Fix ERC20Pausable tests

* Remove virtual from some view functions

* Update linter

* Delete examples

* Remove unnecessary virtual

* Remove roles from Pausable

* Remove roles

* Remove users of roles

* Adapt ERC20 tests

* Fix ERC721 tests

* Add all ERC721 hooks

* Add ERC777 hooks

* Fix remaining tests

* Bump compiler version

* Move 721BurnableMock into mocks directory

* Remove _before hooks

* Fix tests

* Upgrade linter

* Put modifiers last

* Remove _beforeTokenApproval and _beforeOperatorApproval hooks
  • Loading branch information
nventuro authored Feb 14, 2020
1 parent 04a1b21 commit 5dfe721
Show file tree
Hide file tree
Showing 201 changed files with 1,554 additions and 4,999 deletions.
9 changes: 2 additions & 7 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
{
"extends": "solhint:recommended",
"rules": {
"indent": ["error", 4],
"func-order": "off",
"bracket-align": "off",
"compiler-fixed": "off",
"no-simple-event-func-name": "off",
"separate-by-one-line-in-contract": "off",
"two-lines-top-level-separator": "off",
"mark-callable-contracts": "off",
"compiler-version": ["error", "^0.5.0"]
"no-empty-blocks": "off",
"compiler-version": ["error", "^0.6.0"]
}
}
7 changes: 3 additions & 4 deletions contracts/GSN/Context.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.0;

/*
* @dev Provides information about the current execution context, including the
Expand All @@ -14,13 +14,12 @@ contract Context {
// Empty internal constructor, to prevent people from mistakenly deploying
// an instance of this contract, which should be used via inheritance.
constructor () internal { }
// solhint-disable-previous-line no-empty-blocks

function _msgSender() internal view returns (address payable) {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}

function _msgData() internal view returns (bytes memory) {
function _msgData() internal view virtual returns (bytes memory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
}
Expand Down
22 changes: 11 additions & 11 deletions contracts/GSN/GSNRecipient.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.0;

import "./IRelayRecipient.sol";
import "./IRelayHub.sol";
Expand All @@ -15,7 +15,7 @@ import "./Context.sol";
* information on how to use the pre-built {GSNRecipientSignature} and
* {GSNRecipientERC20Fee}, or how to write your own.
*/
contract GSNRecipient is IRelayRecipient, Context {
abstract contract GSNRecipient is IRelayRecipient, Context {
// Default RelayHub address, deployed on mainnet and all testnets at the same address
address private _relayHub = 0xD216153c06E857cD7f72665E0aF1d7D82172F494;

Expand All @@ -33,7 +33,7 @@ contract GSNRecipient is IRelayRecipient, Context {
/**
* @dev Returns the address of the {IRelayHub} contract for this recipient.
*/
function getHubAddr() public view returns (address) {
function getHubAddr() public view override returns (address) {
return _relayHub;
}

Expand All @@ -44,7 +44,7 @@ contract GSNRecipient is IRelayRecipient, Context {
* IMPORTANT: After upgrading, the {GSNRecipient} will no longer be able to receive relayed calls from the old
* {IRelayHub} instance. Additionally, all funds should be previously withdrawn via {_withdrawDeposits}.
*/
function _upgradeRelayHub(address newRelayHub) internal {
function _upgradeRelayHub(address newRelayHub) internal virtual {
address currentRelayHub = _relayHub;
require(newRelayHub != address(0), "GSNRecipient: new RelayHub is the zero address");
require(newRelayHub != currentRelayHub, "GSNRecipient: new RelayHub is the current one");
Expand All @@ -70,7 +70,7 @@ contract GSNRecipient is IRelayRecipient, Context {
*
* Derived contracts should expose this in an external interface with proper access control.
*/
function _withdrawDeposits(uint256 amount, address payable payee) internal {
function _withdrawDeposits(uint256 amount, address payable payee) internal virtual {
IRelayHub(_relayHub).withdraw(amount, payee);
}

Expand All @@ -85,7 +85,7 @@ contract GSNRecipient is IRelayRecipient, Context {
*
* IMPORTANT: Contracts derived from {GSNRecipient} should never use `msg.sender`, and use {_msgSender} instead.
*/
function _msgSender() internal view returns (address payable) {
function _msgSender() internal view virtual override returns (address payable) {
if (msg.sender != _relayHub) {
return msg.sender;
} else {
Expand All @@ -99,7 +99,7 @@ contract GSNRecipient is IRelayRecipient, Context {
*
* IMPORTANT: Contracts derived from {GSNRecipient} should never use `msg.data`, and use {_msgData} instead.
*/
function _msgData() internal view returns (bytes memory) {
function _msgData() internal view virtual override returns (bytes memory) {
if (msg.sender != _relayHub) {
return msg.data;
} else {
Expand All @@ -119,7 +119,7 @@ contract GSNRecipient is IRelayRecipient, Context {
*
* - the caller must be the `RelayHub` contract.
*/
function preRelayedCall(bytes calldata context) external returns (bytes32) {
function preRelayedCall(bytes calldata context) external virtual override returns (bytes32) {
require(msg.sender == getHubAddr(), "GSNRecipient: caller is not RelayHub");
return _preRelayedCall(context);
}
Expand All @@ -131,7 +131,7 @@ contract GSNRecipient is IRelayRecipient, Context {
* must implement this function with any relayed-call preprocessing they may wish to do.
*
*/
function _preRelayedCall(bytes memory context) internal returns (bytes32);
function _preRelayedCall(bytes memory context) internal virtual returns (bytes32);

/**
* @dev See `IRelayRecipient.postRelayedCall`.
Expand All @@ -142,7 +142,7 @@ contract GSNRecipient is IRelayRecipient, Context {
*
* - the caller must be the `RelayHub` contract.
*/
function postRelayedCall(bytes calldata context, bool success, uint256 actualCharge, bytes32 preRetVal) external {
function postRelayedCall(bytes calldata context, bool success, uint256 actualCharge, bytes32 preRetVal) external virtual override {
require(msg.sender == getHubAddr(), "GSNRecipient: caller is not RelayHub");
_postRelayedCall(context, success, actualCharge, preRetVal);
}
Expand All @@ -154,7 +154,7 @@ contract GSNRecipient is IRelayRecipient, Context {
* must implement this function with any relayed-call postprocessing they may wish to do.
*
*/
function _postRelayedCall(bytes memory context, bool success, uint256 actualCharge, bytes32 preRetVal) internal;
function _postRelayedCall(bytes memory context, bool success, uint256 actualCharge, bytes32 preRetVal) internal virtual;

/**
* @dev Return this in acceptRelayedCall to proceed with the execution of a relayed call. Note that this contract
Expand Down
20 changes: 10 additions & 10 deletions contracts/GSN/GSNRecipientERC20Fee.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.0;

import "./GSNRecipient.sol";
import "../math/SafeMath.sol";
Expand Down Expand Up @@ -43,7 +43,7 @@ contract GSNRecipientERC20Fee is GSNRecipient {
/**
* @dev Internal function that mints the gas payment token. Derived contracts should expose this function in their public API, with proper access control mechanisms.
*/
function _mint(address account, uint256 amount) internal {
function _mint(address account, uint256 amount) internal virtual {
_token.mint(account, amount);
}

Expand All @@ -63,6 +63,8 @@ contract GSNRecipientERC20Fee is GSNRecipient {
)
external
view
virtual
override
returns (uint256, bytes memory)
{
if (_token.balanceOf(from) < maxPossibleCharge) {
Expand All @@ -78,7 +80,7 @@ contract GSNRecipientERC20Fee is GSNRecipient {
* actual charge, necessary because we cannot predict how much gas the execution will actually need. The remainder
* is returned to the user in {_postRelayedCall}.
*/
function _preRelayedCall(bytes memory context) internal returns (bytes32) {
function _preRelayedCall(bytes memory context) internal virtual override returns (bytes32) {
(address from, uint256 maxPossibleCharge) = abi.decode(context, (address, uint256));

// The maximum token charge is pre-charged from the user
Expand All @@ -88,7 +90,7 @@ contract GSNRecipientERC20Fee is GSNRecipient {
/**
* @dev Returns to the user the extra amount that was previously charged, once the actual execution cost is known.
*/
function _postRelayedCall(bytes memory context, bool, uint256 actualCharge, bytes32) internal {
function _postRelayedCall(bytes memory context, bool, uint256 actualCharge, bytes32) internal virtual override {
(address from, uint256 maxPossibleCharge, uint256 transactionFee, uint256 gasPrice) =
abi.decode(context, (address, uint256, uint256, uint256));

Expand All @@ -113,17 +115,15 @@ contract GSNRecipientERC20Fee is GSNRecipient {
contract __unstable__ERC20PrimaryAdmin is ERC20, ERC20Detailed, Secondary {
uint256 private constant UINT256_MAX = 2**256 - 1;

constructor(string memory name, string memory symbol, uint8 decimals) public ERC20Detailed(name, symbol, decimals) {
// solhint-disable-previous-line no-empty-blocks
}
constructor(string memory name, string memory symbol, uint8 decimals) public ERC20Detailed(name, symbol, decimals) { }

// The primary account (GSNRecipientERC20Fee) can mint tokens
function mint(address account, uint256 amount) public onlyPrimary {
_mint(account, amount);
}

// The primary account has 'infinite' allowance for all token holders
function allowance(address owner, address spender) public view returns (uint256) {
function allowance(address owner, address spender) public view override(ERC20, IERC20) returns (uint256) {
if (spender == primary()) {
return UINT256_MAX;
} else {
Expand All @@ -132,15 +132,15 @@ contract __unstable__ERC20PrimaryAdmin is ERC20, ERC20Detailed, Secondary {
}

// Allowance for the primary account cannot be changed (it is always 'infinite')
function _approve(address owner, address spender, uint256 value) internal {
function _approve(address owner, address spender, uint256 value) internal override {
if (spender == primary()) {
return;
} else {
super._approve(owner, spender, value);
}
}

function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
function transferFrom(address sender, address recipient, uint256 amount) public override(ERC20, IERC20) returns (bool) {
if (recipient == primary()) {
_transfer(sender, recipient, amount);
return true;
Expand Down
12 changes: 5 additions & 7 deletions contracts/GSN/GSNRecipientSignature.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.0;

import "./GSNRecipient.sol";
import "../cryptography/ECDSA.sol";
Expand Down Expand Up @@ -42,6 +42,8 @@ contract GSNRecipientSignature is GSNRecipient {
)
external
view
virtual
override
returns (uint256, bytes memory)
{
bytes memory blob = abi.encodePacked(
Expand All @@ -62,11 +64,7 @@ contract GSNRecipientSignature is GSNRecipient {
}
}

function _preRelayedCall(bytes memory) internal returns (bytes32) {
// solhint-disable-previous-line no-empty-blocks
}
function _preRelayedCall(bytes memory) internal virtual override returns (bytes32) { }

function _postRelayedCall(bytes memory, bool, uint256, bytes32) internal {
// solhint-disable-previous-line no-empty-blocks
}
function _postRelayedCall(bytes memory, bool, uint256, bytes32) internal virtual override { }
}
6 changes: 3 additions & 3 deletions contracts/GSN/IRelayHub.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.0;

/**
* @dev Interface for `RelayHub`, the core contract of the GSN. Users should not need to interact with this contract
Expand Down Expand Up @@ -207,7 +207,7 @@ interface IRelayHub {
event CanRelayFailed(address indexed relay, address indexed from, address indexed to, bytes4 selector, uint256 reason);

/**
* @dev Emitted when a transaction is relayed.
* @dev Emitted when a transaction is relayed.
* Useful when monitoring a relay's operation and relayed calls to a contract
*
* Note that the actual encoded function might be reverted: this is indicated in the `status` parameter.
Expand Down Expand Up @@ -236,7 +236,7 @@ interface IRelayHub {
*/
function maxPossibleCharge(uint256 relayedCallStipend, uint256 gasPrice, uint256 transactionFee) external view returns (uint256);

// Relay penalization.
// Relay penalization.
// Any account can penalize relays, removing them from the system immediately, and rewarding the
// reporter with half of the relay's stake. The other half is burned so that, even if the relay penalizes itself, it
// still loses half of its stake.
Expand Down
2 changes: 1 addition & 1 deletion contracts/GSN/IRelayRecipient.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.0;

/**
* @dev Base interface for a contract that will be called via the GSN from {IRelayHub}.
Expand Down
14 changes: 0 additions & 14 deletions contracts/access/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,3 @@ NOTE: This page is incomplete. We're working to improve it for the next release.
== Library

{{Roles}}

== Roles

{{CapperRole}}

{{MinterRole}}

{{PauserRole}}

{{SignerRole}}

{{WhitelistAdminRole}}

{{WhitelistedRole}}
2 changes: 1 addition & 1 deletion contracts/access/Roles.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.0;

/**
* @title Roles
Expand Down
44 changes: 0 additions & 44 deletions contracts/access/roles/CapperRole.sol

This file was deleted.

44 changes: 0 additions & 44 deletions contracts/access/roles/MinterRole.sol

This file was deleted.

Loading

0 comments on commit 5dfe721

Please sign in to comment.