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
5cf50367
Commit
5cf50367
authored
Sep 25, 2017
by
Francisco Giordano
Committed by
GitHub
Sep 25, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #466 from phiferd/master
Using require for Token preconditions
parents
158a7a88
38373191
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
6 deletions
+26
-6
BasicToken.sol
contracts/token/BasicToken.sol
+1
-0
BurnableToken.sol
contracts/token/BurnableToken.sol
+3
-0
StandardToken.sol
contracts/token/StandardToken.sol
+3
-6
StandardToken.js
test/StandardToken.js
+19
-0
No files found.
contracts/token/BasicToken.sol
View file @
5cf50367
...
...
@@ -21,6 +21,7 @@ contract BasicToken is ERC20Basic {
*/
function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[msg.sender]);
// SafeMath.sub will throw if there is not enough balance.
balances[msg.sender] = balances[msg.sender].sub(_value);
...
...
contracts/token/BurnableToken.sol
View file @
5cf50367
...
...
@@ -16,6 +16,9 @@ contract BurnableToken is StandardToken {
*/
function burn(uint256 _value) public {
require(_value > 0);
require(_value <= balances[msg.sender]);
// no need to require value <= totalSupply, since that would imply the
// sender's balance is greater than the totalSupply, which *should* be an assertion failure
address burner = msg.sender;
balances[burner] = balances[burner].sub(_value);
...
...
contracts/token/StandardToken.sol
View file @
5cf50367
...
...
@@ -25,15 +25,12 @@ contract StandardToken is ERC20, BasicToken {
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
uint256 _allowance = allowed[_from][msg.sender];
// Check is not needed because sub(_allowance, _value) will already throw if this condition is not met
// require (_value <= _allowance);
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]);
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] =
_allowance
.sub(_value);
allowed[_from][msg.sender] =
allowed[_from][msg.sender]
.sub(_value);
Transfer(_from, _to, _value);
return true;
}
...
...
test/StandardToken.js
View file @
5cf50367
'use strict'
;
const
assertJump
=
require
(
'./helpers/assertJump'
);
const
expectThrow
=
require
(
'./helpers/expectThrow'
);
var
StandardTokenMock
=
artifacts
.
require
(
'./helpers/StandardTokenMock.sol'
);
contract
(
'StandardToken'
,
function
(
accounts
)
{
...
...
@@ -70,6 +71,17 @@ contract('StandardToken', function(accounts) {
}
});
it
(
'should throw an error when trying to transferFrom more than _from has'
,
async
function
()
{
let
balance0
=
await
token
.
balanceOf
(
accounts
[
0
]);
await
token
.
approve
(
accounts
[
1
],
99
);
try
{
await
token
.
transferFrom
(
accounts
[
0
],
accounts
[
2
],
balance0
+
1
,
{
from
:
accounts
[
1
]});
assert
.
fail
(
'should have thrown before'
);
}
catch
(
error
)
{
assertJump
(
error
);
}
});
describe
(
'validating allowance updates to spender'
,
function
()
{
let
preApproved
;
...
...
@@ -88,6 +100,13 @@ contract('StandardToken', function(accounts) {
})
});
it
(
'should increase by 50 then set to 0 when decreasing by more than 50'
,
async
function
()
{
await
token
.
approve
(
accounts
[
1
],
50
);
await
token
.
decreaseApproval
(
accounts
[
1
],
60
);
let
postDecrease
=
await
token
.
allowance
(
accounts
[
0
],
accounts
[
1
]);
postDecrease
.
should
.
be
.
bignumber
.
equal
(
0
);
});
it
(
'should throw an error when trying to transfer to 0x0'
,
async
function
()
{
let
token
=
await
StandardTokenMock
.
new
(
accounts
[
0
],
100
);
try
{
...
...
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