Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
a44303c3
Unverified
Commit
a44303c3
authored
Feb 09, 2021
by
Hadrien Croubois
Committed by
GitHub
Feb 09, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove double sload when doing math checks in tokens (#2506)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
parent
f49e9ee4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
24 deletions
+36
-24
ERC1155.sol
contracts/token/ERC1155/ERC1155.sol
+12
-8
ERC20.sol
contracts/token/ERC20/ERC20.sol
+12
-8
ERC20Burnable.sol
contracts/token/ERC20/ERC20Burnable.sol
+3
-2
ERC777.sol
contracts/token/ERC777/ERC777.sol
+9
-6
No files found.
contracts/token/ERC1155/ERC1155.sol
View file @
a44303c3
...
@@ -137,8 +137,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
...
@@ -137,8 +137,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
_beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
_beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
require(_balances[id][from] >= amount, "ERC1155: insufficient balance for transfer");
uint256 fromBalance = _balances[id][from];
_balances[id][from] -= amount;
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;
_balances[id][to] += amount;
emit TransferSingle(operator, from, to, id, amount);
emit TransferSingle(operator, from, to, id, amount);
...
@@ -175,8 +176,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
...
@@ -175,8 +176,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256 id = ids[i];
uint256 id = ids[i];
uint256 amount = amounts[i];
uint256 amount = amounts[i];
require(_balances[id][from] >= amount, "ERC1155: insufficient balance for transfer");
uint256 fromBalance = _balances[id][from];
_balances[id][from] -= amount;
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;
_balances[id][to] += amount;
}
}
...
@@ -273,8 +275,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
...
@@ -273,8 +275,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
_beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");
_beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");
require(_balances[id][account] >= amount, "ERC1155: burn amount exceeds balance");
uint256 accountBalance = _balances[id][account];
_balances[id][account] -= amount;
require(accountBalance >= amount, "ERC1155: burn amount exceeds balance");
_balances[id][account] = accountBalance - amount;
emit TransferSingle(operator, account, address(0), id, amount);
emit TransferSingle(operator, account, address(0), id, amount);
}
}
...
@@ -298,8 +301,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
...
@@ -298,8 +301,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256 id = ids[i];
uint256 id = ids[i];
uint256 amount = amounts[i];
uint256 amount = amounts[i];
require(_balances[id][account] >= amount, "ERC1155: burn amount exceeds balance");
uint256 accountBalance = _balances[id][account];
_balances[id][account] -= amount;
require(accountBalance >= amount, "ERC1155: burn amount exceeds balance");
_balances[id][account] = accountBalance - amount;
}
}
emit TransferBatch(operator, account, address(0), ids, amounts);
emit TransferBatch(operator, account, address(0), ids, amounts);
...
...
contracts/token/ERC20/ERC20.sol
View file @
a44303c3
...
@@ -147,8 +147,9 @@ contract ERC20 is Context, IERC20 {
...
@@ -147,8 +147,9 @@ contract ERC20 is Context, IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
_transfer(sender, recipient, amount);
require(_allowances[sender][_msgSender()] >= amount, "ERC20: transfer amount exceeds allowance");
uint256 currentAllowance = _allowances[sender][_msgSender()];
_approve(sender, _msgSender(), _allowances[sender][_msgSender()] - amount);
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, _msgSender(), currentAllowance - amount);
return true;
return true;
}
}
...
@@ -185,8 +186,9 @@ contract ERC20 is Context, IERC20 {
...
@@ -185,8 +186,9 @@ contract ERC20 is Context, IERC20 {
* `subtractedValue`.
* `subtractedValue`.
*/
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
require(_allowances[_msgSender()][spender] >= subtractedValue, "ERC20: decreased allowance below zero");
uint256 currentAllowance = _allowances[_msgSender()][spender];
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] - subtractedValue);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
return true;
return true;
}
}
...
@@ -211,8 +213,9 @@ contract ERC20 is Context, IERC20 {
...
@@ -211,8 +213,9 @@ contract ERC20 is Context, IERC20 {
_beforeTokenTransfer(sender, recipient, amount);
_beforeTokenTransfer(sender, recipient, amount);
require(_balances[sender] >= amount, "ERC20: transfer amount exceeds balance");
uint256 senderBalance = _balances[sender];
_balances[sender] -= amount;
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] = senderBalance - amount;
_balances[recipient] += amount;
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
emit Transfer(sender, recipient, amount);
...
@@ -253,8 +256,9 @@ contract ERC20 is Context, IERC20 {
...
@@ -253,8 +256,9 @@ contract ERC20 is Context, IERC20 {
_beforeTokenTransfer(account, address(0), amount);
_beforeTokenTransfer(account, address(0), amount);
require(_balances[account] >= amount, "ERC20: burn amount exceeds balance");
uint256 accountBalance = _balances[account];
_balances[account] -= amount;
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
_balances[account] = accountBalance - amount;
_totalSupply -= amount;
_totalSupply -= amount;
emit Transfer(account, address(0), amount);
emit Transfer(account, address(0), amount);
...
...
contracts/token/ERC20/ERC20Burnable.sol
View file @
a44303c3
...
@@ -32,8 +32,9 @@ abstract contract ERC20Burnable is Context, ERC20 {
...
@@ -32,8 +32,9 @@ abstract contract ERC20Burnable is Context, ERC20 {
* `amount`.
* `amount`.
*/
*/
function burnFrom(address account, uint256 amount) public virtual {
function burnFrom(address account, uint256 amount) public virtual {
require(allowance(account, _msgSender()) >= amount, "ERC20: burn amount exceeds allowance");
uint256 currentAllowance = allowance(account, _msgSender());
_approve(account, _msgSender(), allowance(account, _msgSender()) - amount);
require(currentAllowance >= amount, "ERC20: burn amount exceeds allowance");
_approve(account, _msgSender(), currentAllowance - amount);
_burn(account, amount);
_burn(account, amount);
}
}
}
}
contracts/token/ERC777/ERC777.sol
View file @
a44303c3
...
@@ -278,8 +278,9 @@ contract ERC777 is Context, IERC777, IERC20 {
...
@@ -278,8 +278,9 @@ contract ERC777 is Context, IERC777, IERC20 {
_move(spender, holder, recipient, amount, "", "");
_move(spender, holder, recipient, amount, "", "");
require(_allowances[holder][spender] >= amount, "ERC777: transfer amount exceeds allowance");
uint256 currentAllowance = _allowances[holder][spender];
_approve(holder, spender, _allowances[holder][spender] - amount);
require(currentAllowance >= amount, "ERC777: transfer amount exceeds allowance");
_approve(holder, spender, currentAllowance - amount);
_callTokensReceived(spender, holder, recipient, amount, "", "", false);
_callTokensReceived(spender, holder, recipient, amount, "", "", false);
...
@@ -385,8 +386,9 @@ contract ERC777 is Context, IERC777, IERC20 {
...
@@ -385,8 +386,9 @@ contract ERC777 is Context, IERC777, IERC20 {
_beforeTokenTransfer(operator, from, address(0), amount);
_beforeTokenTransfer(operator, from, address(0), amount);
// Update state variables
// Update state variables
require(_balances[from] >= amount, "ERC777: burn amount exceeds balance");
uint256 fromBalance = _balances[from];
_balances[from] -= amount;
require(fromBalance >= amount, "ERC777: burn amount exceeds balance");
_balances[from] = fromBalance - amount;
_totalSupply -= amount;
_totalSupply -= amount;
emit Burned(operator, from, amount, data, operatorData);
emit Burned(operator, from, amount, data, operatorData);
...
@@ -405,8 +407,9 @@ contract ERC777 is Context, IERC777, IERC20 {
...
@@ -405,8 +407,9 @@ contract ERC777 is Context, IERC777, IERC20 {
{
{
_beforeTokenTransfer(operator, from, to, amount);
_beforeTokenTransfer(operator, from, to, amount);
require(_balances[from] >= amount, "ERC777: transfer amount exceeds balance");
uint256 fromBalance = _balances[from];
_balances[from] -= amount;
require(fromBalance >= amount, "ERC777: transfer amount exceeds balance");
_balances[from] = fromBalance - amount;
_balances[to] += amount;
_balances[to] += amount;
emit Sent(operator, from, to, amount, userData, operatorData);
emit Sent(operator, from, to, amount, userData, operatorData);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment