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
c7636bdc
Commit
c7636bdc
authored
Aug 28, 2017
by
Francisco Giordano
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add safe ERC20 helpers
parent
af6fdae3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
0 deletions
+97
-0
SafeERC20.sol
contracts/token/SafeERC20.sol
+22
-0
SafeERC20.js
test/SafeERC20.js
+27
-0
SafeERC20Helper.sol
test/helpers/SafeERC20Helper.sol
+48
-0
No files found.
contracts/token/SafeERC20.sol
0 → 100644
View file @
c7636bdc
pragma solidity ^0.4.11;
import './ERC20Basic.sol';
import './ERC20.sol';
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure.
*/
library SafeERC20 {
function safeTransfer(ERC20Basic token, address to, uint256 value) internal {
assert(token.transfer(to, value));
}
function safeTransferFrom(ERC20 token, address from, address to, uint256 value) internal {
assert(token.transferFrom(from, to, value));
}
function safeApprove(ERC20 token, address spender, uint256 value) internal {
assert(token.approve(spender, value));
}
}
test/SafeERC20.js
0 → 100644
View file @
c7636bdc
import
EVMThrow
from
'./helpers/EVMThrow'
;
require
(
'chai'
)
.
use
(
require
(
'chai-as-promised'
))
.
should
();
const
SafeERC20Helper
=
artifacts
.
require
(
'./helpers/SafeERC20Helper.sol'
);
contract
(
'SafeERC20'
,
function
()
{
beforeEach
(
async
function
()
{
this
.
helper
=
await
SafeERC20Helper
.
new
();
});
it
(
'should throw on failed transfer'
,
async
function
()
{
await
this
.
helper
.
doFailingTransfer
().
should
.
be
.
rejectedWith
(
EVMThrow
);
});
it
(
'should throw on failed transferFrom'
,
async
function
()
{
await
this
.
helper
.
doFailingTransferFrom
().
should
.
be
.
rejectedWith
(
EVMThrow
);
});
it
(
'should throw on failed approve'
,
async
function
()
{
await
this
.
helper
.
doFailingApprove
().
should
.
be
.
rejectedWith
(
EVMThrow
);
});
});
test/helpers/SafeERC20Helper.sol
0 → 100644
View file @
c7636bdc
pragma solidity ^0.4.11;
import '../../contracts/token/ERC20.sol';
import '../../contracts/token/SafeERC20.sol';
contract ERC20FailingMock is ERC20 {
function transfer(address, uint256) returns (bool) {
return false;
}
function transferFrom(address, address, uint256) returns (bool) {
return false;
}
function approve(address, uint256) returns (bool) {
return false;
}
function balanceOf(address) constant returns (uint256) {
return 0;
}
function allowance(address, address) constant returns (uint256) {
return 0;
}
}
contract SafeERC20Helper {
using SafeERC20 for ERC20;
ERC20 token;
function SafeERC20Helper() {
token = new ERC20FailingMock();
}
function doFailingTransfer() {
token.safeTransfer(0, 0);
}
function doFailingTransferFrom() {
token.safeTransferFrom(0, 0, 0);
}
function doFailingApprove() {
token.safeApprove(0, 0);
}
}
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