Skip to content

Commit

Permalink
Update contracts to support Solidity 0.8.x (OpenZeppelin#2442)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Giordano <[email protected]>
  • Loading branch information
andrekorol and frangio authored Jan 14, 2021
1 parent 6be0b41 commit 974c534
Show file tree
Hide file tree
Showing 159 changed files with 338 additions and 509 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ jobs:
- run: npm ci
if: steps.cache.outputs.cache-hit != 'true'
- run: npm run coverage
env:
NODE_OPTIONS: --max_old_space_size=4096
- uses: codecov/codecov-action@v1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ OpenZeppelin Contracts features a [stable API](https://docs.openzeppelin.com/con
Once installed, you can use the contracts in the library by importing them:

```solidity
pragma solidity ^0.6.0;
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
Expand Down
2 changes: 1 addition & 1 deletion buidler.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = {
},
},
solc: {
version: '0.6.12',
version: '0.8.0',
},
};
4 changes: 2 additions & 2 deletions contracts/GSN/Context.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/*
* @dev Provides information about the current execution context, including the
Expand All @@ -13,7 +13,7 @@ pragma solidity >=0.6.0 <0.8.0;
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/GSN/GSNRecipient.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

import "./IRelayRecipient.sol";
import "./IRelayHub.sol";
Expand Down Expand Up @@ -87,7 +87,7 @@ abstract contract GSNRecipient is IRelayRecipient, Context {
*
* IMPORTANT: Contracts derived from {GSNRecipient} should never use `msg.sender`, and use {_msgSender} instead.
*/
function _msgSender() internal view virtual override returns (address payable) {
function _msgSender() internal view virtual override returns (address) {
if (msg.sender != _relayHub) {
return msg.sender;
} else {
Expand Down Expand Up @@ -192,7 +192,7 @@ abstract contract GSNRecipient is IRelayRecipient, Context {
return (gas * gasPrice * (100 + serviceFee)) / 100;
}

function _getRelayedCallSender() private pure returns (address payable result) {
function _getRelayedCallSender() private pure returns (address result) {
// We need to read 20 bytes (an address) located at array index msg.data.length - 20. In memory, the array
// is prefixed with a 32-byte length value, so we first add 32 to get the memory read index. However, doing
// so would leave the address in the upper 20 bytes of the 32-byte word, which is inconvenient and would
Expand Down
14 changes: 6 additions & 8 deletions contracts/GSN/GSNRecipientERC20Fee.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

import "./GSNRecipient.sol";
import "../math/SafeMath.sol";
import "../access/Ownable.sol";
import "../token/ERC20/SafeERC20.sol";
import "../token/ERC20/ERC20.sol";
Expand All @@ -19,7 +18,6 @@ import "../token/ERC20/ERC20.sol";
*/
contract GSNRecipientERC20Fee is GSNRecipient {
using SafeERC20 for __unstable__ERC20Owned;
using SafeMath for uint256;

enum GSNRecipientERC20FeeErrorCodes {
INSUFFICIENT_BALANCE
Expand All @@ -30,7 +28,7 @@ contract GSNRecipientERC20Fee is GSNRecipient {
/**
* @dev The arguments to the constructor are the details that the gas payment token will have: `name` and `symbol`. `decimals` is hard-coded to 18.
*/
constructor(string memory name, string memory symbol) public {
constructor(string memory name, string memory symbol) {
_token = new __unstable__ERC20Owned(name, symbol);
}

Expand Down Expand Up @@ -100,11 +98,11 @@ contract GSNRecipientERC20Fee is GSNRecipient {
// actualCharge is an _estimated_ charge, which assumes postRelayedCall will use all available gas.
// This implementation's gas cost can be roughly estimated as 10k gas, for the two SSTORE operations in an
// ERC20 transfer.
uint256 overestimation = _computeCharge(_POST_RELAYED_CALL_MAX_GAS.sub(10000), gasPrice, transactionFee);
actualCharge = actualCharge.sub(overestimation);
uint256 overestimation = _computeCharge(_POST_RELAYED_CALL_MAX_GAS - 10000, gasPrice, transactionFee);
actualCharge = actualCharge - overestimation;

// After the relayed call has been executed and the actual charge estimated, the excess pre-charge is returned
_token.safeTransfer(from, maxPossibleCharge.sub(actualCharge));
_token.safeTransfer(from, maxPossibleCharge - actualCharge);
}
}

Expand All @@ -118,7 +116,7 @@ contract GSNRecipientERC20Fee is GSNRecipient {
contract __unstable__ERC20Owned is ERC20, Ownable {
uint256 private constant _UINT256_MAX = 2**256 - 1;

constructor(string memory name, string memory symbol) public ERC20(name, symbol) { }
constructor(string memory name, string memory symbol) ERC20(name, symbol) { }

// The owner (GSNRecipientERC20Fee) can mint tokens
function mint(address account, uint256 amount) public onlyOwner {
Expand Down
4 changes: 2 additions & 2 deletions contracts/GSN/GSNRecipientSignature.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

import "./GSNRecipient.sol";
import "../cryptography/ECDSA.sol";
Expand All @@ -23,7 +23,7 @@ contract GSNRecipientSignature is GSNRecipient {
/**
* @dev Sets the trusted signer that is going to be producing signatures to approve relayed calls.
*/
constructor(address trustedSigner) public {
constructor(address trustedSigner) {
require(trustedSigner != address(0), "GSNRecipientSignature: trusted signer is the zero address");
_trustedSigner = trustedSigner;
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/GSN/IRelayHub.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Interface for `RelayHub`, the core contract of the GSN. Users should not need to interact with this contract
Expand Down
2 changes: 1 addition & 1 deletion contracts/GSN/IRelayRecipient.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Base interface for a contract that will be called via the GSN from {IRelayHub}.
Expand Down
2 changes: 1 addition & 1 deletion contracts/access/AccessControl.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

import "../utils/EnumerableSet.sol";
import "../utils/Address.sol";
Expand Down
4 changes: 2 additions & 2 deletions contracts/access/Ownable.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

import "../GSN/Context.sol";
/**
Expand All @@ -23,7 +23,7 @@ abstract contract Ownable is Context {
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () internal {
constructor () {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
Expand Down
8 changes: 3 additions & 5 deletions contracts/access/TimelockController.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.9 <0.8.0;
pragma experimental ABIEncoderV2;
pragma solidity ^0.8.0;

import "./../math/SafeMath.sol";
import "./AccessControl.sol";

/**
Expand Down Expand Up @@ -52,7 +50,7 @@ contract TimelockController is AccessControl {
/**
* @dev Initializes the contract with a given `minDelay`.
*/
constructor(uint256 minDelay, address[] memory proposers, address[] memory executors) public {
constructor(uint256 minDelay, address[] memory proposers, address[] memory executors) {
_setRoleAdmin(TIMELOCK_ADMIN_ROLE, TIMELOCK_ADMIN_ROLE);
_setRoleAdmin(PROPOSER_ROLE, TIMELOCK_ADMIN_ROLE);
_setRoleAdmin(EXECUTOR_ROLE, TIMELOCK_ADMIN_ROLE);
Expand Down Expand Up @@ -188,7 +186,7 @@ contract TimelockController is AccessControl {
require(_timestamps[id] == 0, "TimelockController: operation already scheduled");
require(delay >= _minDelay, "TimelockController: insufficient delay");
// solhint-disable-next-line not-rely-on-time
_timestamps[id] = SafeMath.add(block.timestamp, delay);
_timestamps[id] = block.timestamp + delay;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/cryptography/ECDSA.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
Expand Down
2 changes: 1 addition & 1 deletion contracts/cryptography/MerkleProof.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev These functions deal with verification of Merkle trees (hash trees),
Expand Down
17 changes: 5 additions & 12 deletions contracts/drafts/EIP712.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data.
Expand Down Expand Up @@ -43,13 +43,13 @@ abstract contract EIP712 {
* NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart
* contract upgrade].
*/
constructor(string memory name, string memory version) internal {
constructor(string memory name, string memory version) {
bytes32 hashedName = keccak256(bytes(name));
bytes32 hashedVersion = keccak256(bytes(version));
bytes32 typeHash = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
_HASHED_NAME = hashedName;
_HASHED_VERSION = hashedVersion;
_CACHED_CHAIN_ID = _getChainId();
_CACHED_CHAIN_ID = block.chainid;
_CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion);
_TYPE_HASH = typeHash;
}
Expand All @@ -58,7 +58,7 @@ abstract contract EIP712 {
* @dev Returns the domain separator for the current chain.
*/
function _domainSeparatorV4() internal view returns (bytes32) {
if (_getChainId() == _CACHED_CHAIN_ID) {
if (block.chainid == _CACHED_CHAIN_ID) {
return _CACHED_DOMAIN_SEPARATOR;
} else {
return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION);
Expand All @@ -71,7 +71,7 @@ abstract contract EIP712 {
typeHash,
name,
version,
_getChainId(),
block.chainid,
address(this)
)
);
Expand All @@ -95,11 +95,4 @@ abstract contract EIP712 {
function _hashTypedDataV4(bytes32 structHash) internal view returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", _domainSeparatorV4(), structHash));
}

function _getChainId() private pure returns (uint256 chainId) {
// solhint-disable-next-line no-inline-assembly
assembly {
chainId := chainid()
}
}
}
4 changes: 2 additions & 2 deletions contracts/drafts/ERC20Permit.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.5 <0.8.0;
pragma solidity ^0.8.0;

import "../token/ERC20/ERC20.sol";
import "./IERC20Permit.sol";
Expand Down Expand Up @@ -29,7 +29,7 @@ abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 {
*
* It's a good idea to use the same `name` that is defined as the ERC20 token name.
*/
constructor(string memory name) internal EIP712(name, "1") {
constructor(string memory name) EIP712(name, "1") {
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/drafts/IERC20Permit.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
Expand Down
4 changes: 2 additions & 2 deletions contracts/introspection/ERC165.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

import "./IERC165.sol";

Expand All @@ -21,7 +21,7 @@ abstract contract ERC165 is IERC165 {
*/
mapping(bytes4 => bool) private _supportedInterfaces;

constructor () internal {
constructor () {
// Derived contracts need only register support for their own interfaces,
// we register support for ERC165 itself here
_registerInterface(_INTERFACE_ID_ERC165);
Expand Down
2 changes: 1 addition & 1 deletion contracts/introspection/ERC165Checker.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.2 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Library used to query support of an interface declared via {IERC165}.
Expand Down
2 changes: 1 addition & 1 deletion contracts/introspection/ERC1820Implementer.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

import "./IERC1820Implementer.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/introspection/IERC165.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Interface of the ERC165 standard, as defined in the
Expand Down
2 changes: 1 addition & 1 deletion contracts/introspection/IERC1820Implementer.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Interface for an ERC1820 implementer, as defined in the
Expand Down
2 changes: 1 addition & 1 deletion contracts/introspection/IERC1820Registry.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Interface of the global ERC1820 Registry, as defined in the
Expand Down
2 changes: 1 addition & 1 deletion contracts/math/Math.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;
pragma solidity ^0.8.0;

/**
* @dev Standard math utilities missing in the Solidity language.
Expand Down
Loading

0 comments on commit 974c534

Please sign in to comment.