Skip to content

Commit

Permalink
Merge branch 'master' into refactor/virtualERC165
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Feb 12, 2021
2 parents 592e8c3 + ee6348a commit d963cf8
Show file tree
Hide file tree
Showing 15 changed files with 1,222 additions and 550 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ jobs:
if: steps.cache.outputs.cache-hit != 'true'
- run: npm run lint
- run: npm run test
env:
FORCE_COLOR: 1
ENABLE_GAS_REPORT: 1
- name: Print gas report
run: cat gas-report.txt

coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: actions/setup-node@v2
with:
node-version: 10.x
Expand Down
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## Unreleased

* Now targeting the 0.8.x line of Solidity compilers. For 0.6.x (resp 0.7.x) support, use version 3.4.0 (resp 3.4.0-solc-0.7) of OpenZeppelin.
* `Context`: making `_msgData` return `bytes calldata` instead of `bytes memory` ([#2492](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2492))
* Now targeting the 0.8.x line of Solidity compilers. For 0.6.x (resp 0.7.x) support, use version 3.4.0 (resp 3.4.0-solc-0.7) of OpenZeppelin.
* `Context`: making `_msgData` return `bytes calldata` instead of `bytes memory` ([#2492](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2492))
* `ERC20`: Removed the `_setDecimals` function and the storage slot associated to decimals. ([#2502](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2502))
* `Strings`: addition of a `toHexString` function. ([#2504](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2504))

## 3.4.0 (2021-02-02)

Expand Down
10 changes: 8 additions & 2 deletions contracts/mocks/ERC20DecimalsMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ pragma solidity ^0.8.0;
import "../token/ERC20/ERC20.sol";

contract ERC20DecimalsMock is ERC20 {
constructor (string memory name, string memory symbol, uint8 decimals) ERC20(name, symbol) {
_setupDecimals(decimals);
uint8 immutable private _decimals;

constructor (string memory name_, string memory symbol_, uint8 decimals_) ERC20(name_, symbol_) {
_decimals = decimals_;
}

function decimals() public view virtual override returns (uint8) {
return _decimals;
}
}
6 changes: 6 additions & 0 deletions contracts/mocks/StringsMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ contract StringsMock {
function fromUint256(uint256 value) public pure returns (string memory) {
return Strings.toString(value);
}
function fromUint256Hex(uint256 value) public pure returns (string memory) {
return Strings.toHexString(value);
}
function fromUint256HexFixed(uint256 value, uint256 length) public pure returns (string memory) {
return Strings.toHexString(value, length);
}
}
20 changes: 12 additions & 8 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {

_beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);

require(_balances[id][from] >= amount, "ERC1155: insufficient balance for transfer");
_balances[id][from] -= amount;
uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;

emit TransferSingle(operator, from, to, id, amount);
Expand Down Expand Up @@ -178,8 +179,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256 id = ids[i];
uint256 amount = amounts[i];

require(_balances[id][from] >= amount, "ERC1155: insufficient balance for transfer");
_balances[id][from] -= amount;
uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;
}

Expand Down Expand Up @@ -276,8 +278,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {

_beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");

require(_balances[id][account] >= amount, "ERC1155: burn amount exceeds balance");
_balances[id][account] -= amount;
uint256 accountBalance = _balances[id][account];
require(accountBalance >= amount, "ERC1155: burn amount exceeds balance");
_balances[id][account] = accountBalance - amount;

emit TransferSingle(operator, account, address(0), id, amount);
}
Expand All @@ -301,8 +304,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256 id = ids[i];
uint256 amount = amounts[i];

require(_balances[id][account] >= amount, "ERC1155: burn amount exceeds balance");
_balances[id][account] -= amount;
uint256 accountBalance = _balances[id][account];
require(accountBalance >= amount, "ERC1155: burn amount exceeds balance");
_balances[id][account] = accountBalance - amount;
}

emit TransferBatch(operator, account, address(0), ids, amounts);
Expand Down
45 changes: 18 additions & 27 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ contract ERC20 is Context, IERC20 {

string private _name;
string private _symbol;
uint8 private _decimals;

/**
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with
* a default value of 18.
* @dev Sets the values for {name} and {symbol}.
*
* To select a different value for {decimals}, use {_setupDecimals}.
* The defaut value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All three of these values are immutable: they can only be set once during
* construction.
*/
constructor (string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}

/**
Expand All @@ -76,15 +74,15 @@ contract ERC20 is Context, IERC20 {
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
* called.
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overloaded;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual returns (uint8) {
return _decimals;
return 18;
}

/**
Expand Down Expand Up @@ -149,8 +147,9 @@ contract ERC20 is Context, IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);

require(_allowances[sender][_msgSender()] >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, _msgSender(), _allowances[sender][_msgSender()] - amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, _msgSender(), currentAllowance - amount);

return true;
}
Expand Down Expand Up @@ -187,8 +186,9 @@ contract ERC20 is Context, IERC20 {
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
require(_allowances[_msgSender()][spender] >= subtractedValue, "ERC20: decreased allowance below zero");
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] - subtractedValue);
uint256 currentAllowance = _allowances[_msgSender()][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
_approve(_msgSender(), spender, currentAllowance - subtractedValue);

return true;
}
Expand All @@ -213,8 +213,9 @@ contract ERC20 is Context, IERC20 {

_beforeTokenTransfer(sender, recipient, amount);

require(_balances[sender] >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] -= amount;
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] = senderBalance - amount;
_balances[recipient] += amount;

emit Transfer(sender, recipient, amount);
Expand Down Expand Up @@ -255,8 +256,9 @@ contract ERC20 is Context, IERC20 {

_beforeTokenTransfer(account, address(0), amount);

require(_balances[account] >= amount, "ERC20: burn amount exceeds balance");
_balances[account] -= amount;
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
_balances[account] = accountBalance - amount;
_totalSupply -= amount;

emit Transfer(account, address(0), amount);
Expand All @@ -283,17 +285,6 @@ contract ERC20 is Context, IERC20 {
emit Approval(owner, spender, amount);
}

/**
* @dev Sets {decimals} to a value other than the default one of 18.
*
* WARNING: This function should only be called from the constructor. Most
* applications that interact with token contracts will not expect
* {decimals} to ever change, and may work incorrectly if it does.
*/
function _setupDecimals(uint8 decimals_) internal virtual {
_decimals = decimals_;
}

/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
Expand Down
5 changes: 3 additions & 2 deletions contracts/token/ERC20/ERC20Burnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ abstract contract ERC20Burnable is Context, ERC20 {
* `amount`.
*/
function burnFrom(address account, uint256 amount) public virtual {
require(allowance(account, _msgSender()) >= amount, "ERC20: burn amount exceeds allowance");
_approve(account, _msgSender(), allowance(account, _msgSender()) - amount);
uint256 currentAllowance = allowance(account, _msgSender());
require(currentAllowance >= amount, "ERC20: burn amount exceeds allowance");
_approve(account, _msgSender(), currentAllowance - amount);
_burn(account, amount);
}
}
15 changes: 9 additions & 6 deletions contracts/token/ERC777/ERC777.sol
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ contract ERC777 is Context, IERC777, IERC20 {

_move(spender, holder, recipient, amount, "", "");

require(_allowances[holder][spender] >= amount, "ERC777: transfer amount exceeds allowance");
_approve(holder, spender, _allowances[holder][spender] - amount);
uint256 currentAllowance = _allowances[holder][spender];
require(currentAllowance >= amount, "ERC777: transfer amount exceeds allowance");
_approve(holder, spender, currentAllowance - amount);

_callTokensReceived(spender, holder, recipient, amount, "", "", false);

Expand Down Expand Up @@ -385,8 +386,9 @@ contract ERC777 is Context, IERC777, IERC20 {
_beforeTokenTransfer(operator, from, address(0), amount);

// Update state variables
require(_balances[from] >= amount, "ERC777: burn amount exceeds balance");
_balances[from] -= amount;
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC777: burn amount exceeds balance");
_balances[from] = fromBalance - amount;
_totalSupply -= amount;

emit Burned(operator, from, amount, data, operatorData);
Expand All @@ -405,8 +407,9 @@ contract ERC777 is Context, IERC777, IERC20 {
{
_beforeTokenTransfer(operator, from, to, amount);

require(_balances[from] >= amount, "ERC777: transfer amount exceeds balance");
_balances[from] -= amount;
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC777: transfer amount exceeds balance");
_balances[from] = fromBalance - amount;
_balances[to] += amount;

emit Sent(operator, from, to, amount, userData, operatorData);
Expand Down
10 changes: 8 additions & 2 deletions contracts/utils/Counters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ library Counters {
}

function increment(Counter storage counter) internal {
counter._value += 1;
unchecked {
counter._value += 1;
}
}

function decrement(Counter storage counter) internal {
counter._value = counter._value - 1;
uint256 value = counter._value;
require(value > 0, "Counter: decrement overflow");
unchecked {
counter._value = value - 1;
}
}
}
43 changes: 38 additions & 5 deletions contracts/utils/Strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ pragma solidity ^0.8.0;
* @dev String operations.
*/
library Strings {
bytes16 private constant alphabet = "0123456789abcdef";

/**
* @dev Converts a `uint256` to its ASCII `string` representation.
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
// Inspired by OraclizeAPI's implementation - MIT licence
Expand All @@ -23,12 +25,43 @@ library Strings {
temp /= 10;
}
bytes memory buffer = new bytes(digits);
uint256 index = digits;
temp = value;
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}

/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
if (value == 0) {
return "0x00";
}
uint256 temp = value;
uint256 length = 0;
while (temp != 0) {
buffer[--index] = bytes1(uint8(48 + uint256(temp % 10)));
temp /= 10;
length++;
temp >>= 8;
}
return toHexString(value, length);
}

/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = alphabet[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}

}
20 changes: 15 additions & 5 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@ const path = require('path');
require('@nomiclabs/hardhat-truffle5');
require('@nomiclabs/hardhat-solhint');
require('solidity-coverage');
require('hardhat-gas-reporter');

for (const f of fs.readdirSync(path.join(__dirname, 'hardhat'))) {
require(path.join(__dirname, 'hardhat', f));
}

const enableGasReport = !!process.env.ENABLE_GAS_REPORT;

/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: '0.8.0',
settings: {
optimizer: {
enabled: false,
runs: 200,
solidity: {
version: '0.8.0',
settings: {
optimizer: {
enabled: enableGasReport,
runs: 200,
},
},
},
networks: {
hardhat: {
blockGasLimit: 10000000,
},
},
gasReporter: {
enable: enableGasReport,
currency: 'USD',
outputFile: process.env.CI ? 'gas-report.txt' : undefined,
},
};
Loading

0 comments on commit d963cf8

Please sign in to comment.