Skip to content

Commit c3c5d76

Browse files
committed
added examples
1 parent a781955 commit c3c5d76

File tree

7 files changed

+132
-166
lines changed

7 files changed

+132
-166
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pragma solidity ^0.4.24;
2+
3+
import "../token/ERC20/ERC20Mintable.sol";
4+
import "../token/ERC20/ERC20Detailed.sol";
5+
import "../token/ERC20/TokenTimelock.sol";
6+
7+
/**
8+
* @title SampleTimelockToken
9+
* @dev Very simple ERC20 Token that can be minted.
10+
* It is meant to be used in a tokentimelock contract.
11+
*/
12+
contract SampleTimelockToken is ERC20Mintable, ERC20Detailed {
13+
constructor() public ERC20Detailed("Sample Timelock Token", "STT", 18) {}
14+
}
15+
16+
17+
/**
18+
* @title SampleTokenTimelock
19+
* @dev This is an example of a token lock for certain time.
20+
*/
21+
22+
contract SampleTokenTimelock is TokenTimelock{
23+
24+
constructor(
25+
ERC20Mintable token,
26+
address beneficiary,
27+
uint256 releaseTime
28+
)
29+
public
30+
TokenTimelock(token, beneficiary, releaseTime){}
31+
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pragma solidity ^0.4.24;
2+
3+
import "../drafts/TokenVesting.sol";
4+
5+
/**
6+
* @title SampleTokenVesting
7+
* @dev This is an example of a token vesting for defined time period.
8+
* Tokens to be vested will be sent directly to this contract.
9+
*/
10+
11+
contract SampleTokenVesting is TokenVesting{
12+
13+
constructor(
14+
address beneficiary,
15+
uint256 start,
16+
uint256 cliffDuration,
17+
uint256 duration,
18+
bool revocable
19+
)
20+
public
21+
TokenVesting(beneficiary, start, cliffDuration, duration, revocable){}
22+
23+
}

contracts/mocks/SafeERC20Helper.sol

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,28 @@ pragma solidity ^0.4.24;
33
import "../token/ERC20/IERC20.sol";
44
import "../token/ERC20/SafeERC20.sol";
55

6-
contract ERC20FailingMock is IERC20 {
7-
uint256 private _allowance;
8-
function totalSupply() public view returns (uint256) {
9-
return 0;
10-
}
11-
12-
function transfer(address, uint256) public returns (bool) {
13-
return false;
14-
}
15-
16-
function transferFrom(address, address, uint256) public returns (bool) {
17-
return false;
18-
}
19-
20-
function approve(address, uint256) public returns (bool) {
21-
return false;
22-
}
23-
24-
function increaseAllowance(address, uint256) public returns (bool){
25-
return false;
26-
}
27-
28-
function decreaseAllowance(address, uint256) public returns (bool){
29-
return false;
30-
}
31-
32-
function balanceOf(address) public view returns (uint256) {
33-
return 0;
34-
}
35-
36-
function allowance(address, address) public view returns (uint256) {
37-
return 0;
38-
}
39-
}
6+
contract ERC20FailingMock {
7+
uint256 private _allowance;
8+
9+
function transfer(address, uint256) public returns (bool) {
10+
return false;
11+
}
12+
13+
function transferFrom(address, address, uint256) public returns (bool) {
14+
return false;
15+
}
16+
17+
function approve(address, uint256) public returns (bool) {
18+
return false;
19+
}
4020

41-
contract ERC20SucceedingMock is IERC20 {
42-
uint256 private _allowance;
21+
function allowance(address, address) public view returns (uint256) {
22+
return 0;
23+
}
24+
}
4325

44-
function totalSupply() public view returns (uint256) {
45-
return 0;
46-
}
26+
contract ERC20SucceedingMock {
27+
uint256 private _allowance;
4728

4829
function transfer(address, uint256) public returns (bool) {
4930
return true;
@@ -57,33 +38,20 @@ contract ERC20SucceedingMock is IERC20 {
5738
return true;
5839
}
5940

60-
function increaseAllowance(address, uint256) public returns (bool){
61-
return true;
62-
}
63-
64-
function decreaseAllowance(address, uint256) public returns (bool){
65-
return true;
66-
}
67-
68-
function balanceOf(address) public view returns (uint256) {
69-
return 0;
70-
}
71-
72-
function allowance(address, address) public view returns (uint256) {
73-
return _allowance;
74-
}
41+
function setAllowance(uint256 allowance_) public {
42+
_allowance = allowance_;
43+
}
7544

76-
function setAllowance(uint256 value) public {
77-
_allowance = value;
78-
}
45+
function allowance(address, address) public view returns (uint256) {
46+
return _allowance;
47+
}
7948
}
8049

8150
contract SafeERC20Helper {
51+
using SafeERC20 for IERC20;
8252

83-
using SafeERC20 for IERC20;
84-
85-
IERC20 private _failing;
86-
IERC20 private _succeeding;
53+
IERC20 private _failing;
54+
IERC20 private _succeeding;
8755

8856
constructor () public {
8957
_failing = IERC20(new ERC20FailingMock());
@@ -130,23 +98,15 @@ contract SafeERC20Helper {
13098
_succeeding.safeIncreaseAllowance(address(0), amount);
13199
}
132100

133-
function doFailingIncreaseAllowance() public {
134-
_failing.safeIncreaseAllowance(address(0), 0);
135-
}
136-
137-
function doFailingDecreaseAllowance() public {
138-
_failing.safeDecreaseAllowance(address(0), 0);
139-
}
140-
141-
function doSucceedingTransfer() public {
142-
_succeeding.safeTransfer(address(0), 0);
143-
}
101+
function doSucceedingDecreaseAllowance(uint256 amount) public {
102+
_succeeding.safeDecreaseAllowance(address(0), amount);
103+
}
144104

145105
function setAllowance(uint256 allowance_) public {
146106
ERC20SucceedingMock(_succeeding).setAllowance(allowance_);
147107
}
148108

149-
function doSucceedingApprove() public {
150-
_succeeding.safeApprove(address(0), 0);
151-
}
152-
}
109+
function allowance() public view returns (uint256) {
110+
return _succeeding.allowance(address(0), address(0));
111+
}
112+
}

contracts/token/ERC20/SafeERC20.sol

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pragma solidity ^0.4.24;
22

3-
import "./ERC20.sol";
43
import "./IERC20.sol";
4+
import "../../math/SafeMath.sol";
55

66
/**
77
* @title SafeERC20
@@ -10,58 +10,31 @@ import "./IERC20.sol";
1010
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
1111
*/
1212
library SafeERC20 {
13-
function safeTransfer(
14-
IERC20 token,
15-
address to,
16-
uint256 value
17-
)
18-
internal
19-
{
20-
require(token.transfer(to, value));
21-
}
13+
using SafeMath for uint256;
2214

23-
function safeTransferFrom(
24-
IERC20 token,
25-
address from,
26-
address to,
27-
uint256 value
28-
)
29-
internal
30-
{
31-
require(token.transferFrom(from, to, value));
32-
}
15+
function safeTransfer(IERC20 token, address to, uint256 value) internal {
16+
require(token.transfer(to, value));
17+
}
3318

34-
function safeApprove(
35-
IERC20 token,
36-
address spender,
37-
uint256 value
38-
)
39-
internal
40-
{
41-
// safeApprove should only be called when setting an initial allowance,
42-
// or when resetting it to zero. To increase and decrease it, use
43-
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
44-
require((value == 0) || (token.allowance(msg.sender, spender) == 0));
45-
require(token.approve(spender, value));
46-
}
19+
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
20+
require(token.transferFrom(from, to, value));
21+
}
4722

48-
function safeIncreaseAllowance(
49-
IERC20 token,
50-
address spender,
51-
uint256 addedValue
52-
)
53-
internal
54-
{
55-
require(token.increaseAllowance(spender, addedValue));
56-
}
23+
function safeApprove(IERC20 token, address spender, uint256 value) internal {
24+
// safeApprove should only be called when setting an initial allowance,
25+
// or when resetting it to zero. To increase and decrease it, use
26+
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
27+
require((value == 0) || (token.allowance(msg.sender, spender) == 0));
28+
require(token.approve(spender, value));
29+
}
5730

58-
function safeDecreaseAllowance(
59-
IERC20 token,
60-
address spender,
61-
uint256 subtractedValue
62-
)
63-
internal
64-
{
65-
require(token.decreaseAllowance(spender, subtractedValue));
66-
}
67-
}
31+
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
32+
uint256 newAllowance = token.allowance(address(this), spender).add(value);
33+
require(token.approve(spender, newAllowance));
34+
}
35+
36+
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
37+
uint256 newAllowance = token.allowance(address(this), spender).sub(value);
38+
require(token.approve(spender, newAllowance));
39+
}
40+
}

0 commit comments

Comments
 (0)