Skip to content

Commit 7bb8723

Browse files
Leo Ariasfrangio
authored andcommitted
Improve encapsulation on ERCs (#1270)
* Improve encapsulation on ERC165 * Improve encapsulation on ERC20 * Improve encapsulation on ERC721 * Add tests, use standard getters * fix tests * Fix lint * move interface ids to implementation contracts * Do not prefix getters
1 parent 45c0c07 commit 7bb8723

File tree

12 files changed

+268
-145
lines changed

12 files changed

+268
-145
lines changed

contracts/drafts/TokenVesting.sol

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,93 @@ contract TokenVesting is Ownable {
2121
event Revoked();
2222

2323
// beneficiary of tokens after they are released
24-
address public beneficiary;
24+
address private beneficiary_;
2525

26-
uint256 public cliff;
27-
uint256 public start;
28-
uint256 public duration;
26+
uint256 private cliff_;
27+
uint256 private start_;
28+
uint256 private duration_;
2929

30-
bool public revocable;
30+
bool private revocable_;
3131

32-
mapping (address => uint256) public released;
33-
mapping (address => bool) public revoked;
32+
mapping (address => uint256) private released_;
33+
mapping (address => bool) private revoked_;
3434

3535
/**
3636
* @dev Creates a vesting contract that vests its balance of any ERC20 token to the
3737
* _beneficiary, gradually in a linear fashion until _start + _duration. By then all
3838
* of the balance will have vested.
3939
* @param _beneficiary address of the beneficiary to whom vested tokens are transferred
40-
* @param _cliff duration in seconds of the cliff in which tokens will begin to vest
40+
* @param _cliffDuration duration in seconds of the cliff in which tokens will begin to vest
4141
* @param _start the time (as Unix time) at which point vesting starts
4242
* @param _duration duration in seconds of the period in which the tokens will vest
4343
* @param _revocable whether the vesting is revocable or not
4444
*/
4545
constructor(
4646
address _beneficiary,
4747
uint256 _start,
48-
uint256 _cliff,
48+
uint256 _cliffDuration,
4949
uint256 _duration,
5050
bool _revocable
5151
)
5252
public
5353
{
5454
require(_beneficiary != address(0));
55-
require(_cliff <= _duration);
55+
require(_cliffDuration <= _duration);
5656

57-
beneficiary = _beneficiary;
58-
revocable = _revocable;
59-
duration = _duration;
60-
cliff = _start.add(_cliff);
61-
start = _start;
57+
beneficiary_ = _beneficiary;
58+
revocable_ = _revocable;
59+
duration_ = _duration;
60+
cliff_ = _start.add(_cliffDuration);
61+
start_ = _start;
62+
}
63+
64+
/**
65+
* @return the beneficiary of the tokens.
66+
*/
67+
function beneficiary() public view returns(address) {
68+
return beneficiary_;
69+
}
70+
71+
/**
72+
* @return the cliff time of the token vesting.
73+
*/
74+
function cliff() public view returns(uint256) {
75+
return cliff_;
76+
}
77+
78+
/**
79+
* @return the start time of the token vesting.
80+
*/
81+
function start() public view returns(uint256) {
82+
return start_;
83+
}
84+
85+
/**
86+
* @return the duration of the token vesting.
87+
*/
88+
function duration() public view returns(uint256) {
89+
return duration_;
90+
}
91+
92+
/**
93+
* @return true if the vesting is revocable.
94+
*/
95+
function revocable() public view returns(bool) {
96+
return revocable_;
97+
}
98+
99+
/**
100+
* @return the amount of the token released.
101+
*/
102+
function released(address _token) public view returns(uint256) {
103+
return released_[_token];
104+
}
105+
106+
/**
107+
* @return true if the token is revoked.
108+
*/
109+
function revoked(address _token) public view returns(bool) {
110+
return revoked_[_token];
62111
}
63112

64113
/**
@@ -70,9 +119,9 @@ contract TokenVesting is Ownable {
70119

71120
require(unreleased > 0);
72121

73-
released[_token] = released[_token].add(unreleased);
122+
released_[_token] = released_[_token].add(unreleased);
74123

75-
_token.safeTransfer(beneficiary, unreleased);
124+
_token.safeTransfer(beneficiary_, unreleased);
76125

77126
emit Released(unreleased);
78127
}
@@ -83,15 +132,15 @@ contract TokenVesting is Ownable {
83132
* @param _token ERC20 token which is being vested
84133
*/
85134
function revoke(IERC20 _token) public onlyOwner {
86-
require(revocable);
87-
require(!revoked[_token]);
135+
require(revocable_);
136+
require(!revoked_[_token]);
88137

89138
uint256 balance = _token.balanceOf(address(this));
90139

91140
uint256 unreleased = releasableAmount(_token);
92141
uint256 refund = balance.sub(unreleased);
93142

94-
revoked[_token] = true;
143+
revoked_[_token] = true;
95144

96145
_token.safeTransfer(owner(), refund);
97146

@@ -103,7 +152,7 @@ contract TokenVesting is Ownable {
103152
* @param _token ERC20 token which is being vested
104153
*/
105154
function releasableAmount(IERC20 _token) public view returns (uint256) {
106-
return vestedAmount(_token).sub(released[_token]);
155+
return vestedAmount(_token).sub(released_[_token]);
107156
}
108157

109158
/**
@@ -112,14 +161,14 @@ contract TokenVesting is Ownable {
112161
*/
113162
function vestedAmount(IERC20 _token) public view returns (uint256) {
114163
uint256 currentBalance = _token.balanceOf(this);
115-
uint256 totalBalance = currentBalance.add(released[_token]);
164+
uint256 totalBalance = currentBalance.add(released_[_token]);
116165

117-
if (block.timestamp < cliff) {
166+
if (block.timestamp < cliff_) {
118167
return 0;
119-
} else if (block.timestamp >= start.add(duration) || revoked[_token]) {
168+
} else if (block.timestamp >= start_.add(duration_) || revoked_[_token]) {
120169
return totalBalance;
121170
} else {
122-
return totalBalance.mul(block.timestamp.sub(start)).div(duration);
171+
return totalBalance.mul(block.timestamp.sub(start_)).div(duration_);
123172
}
124173
}
125174
}

contracts/introspection/SupportsInterfaceWithLookup.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "./IERC165.sol";
1010
*/
1111
contract SupportsInterfaceWithLookup is IERC165 {
1212

13-
bytes4 public constant InterfaceId_ERC165 = 0x01ffc9a7;
13+
bytes4 private constant InterfaceId_ERC165 = 0x01ffc9a7;
1414
/**
1515
* 0x01ffc9a7 ===
1616
* bytes4(keccak256('supportsInterface(bytes4)'))

contracts/token/ERC20/ERC20Capped.sol

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ import "./ERC20Mintable.sol";
99
*/
1010
contract ERC20Capped is ERC20Mintable {
1111

12-
uint256 public cap;
12+
uint256 private cap_;
1313

1414
constructor(uint256 _cap) public {
1515
require(_cap > 0);
16-
cap = _cap;
16+
cap_ = _cap;
17+
}
18+
19+
/**
20+
* @return the cap for the token minting.
21+
*/
22+
function cap() public view returns(uint256) {
23+
return cap_;
1724
}
1825

1926
/**
@@ -29,7 +36,7 @@ contract ERC20Capped is ERC20Mintable {
2936
public
3037
returns (bool)
3138
{
32-
require(totalSupply().add(_amount) <= cap);
39+
require(totalSupply().add(_amount) <= cap_);
3340

3441
return super.mint(_to, _amount);
3542
}

contracts/token/ERC20/ERC20Detailed.sol

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,34 @@ import "./IERC20.sol";
1010
* just as on Ethereum all the operations are done in wei.
1111
*/
1212
contract ERC20Detailed is IERC20 {
13-
string public name;
14-
string public symbol;
15-
uint8 public decimals;
13+
string private name_;
14+
string private symbol_;
15+
uint8 private decimals_;
1616

1717
constructor(string _name, string _symbol, uint8 _decimals) public {
18-
name = _name;
19-
symbol = _symbol;
20-
decimals = _decimals;
18+
name_ = _name;
19+
symbol_ = _symbol;
20+
decimals_ = _decimals;
21+
}
22+
23+
/**
24+
* @return the name of the token.
25+
*/
26+
function name() public view returns(string) {
27+
return name_;
28+
}
29+
30+
/**
31+
* @return the symbol of the token.
32+
*/
33+
function symbol() public view returns(string) {
34+
return symbol_;
35+
}
36+
37+
/**
38+
* @return the number of decimals of the token.
39+
*/
40+
function decimals() public view returns(uint8) {
41+
return decimals_;
2142
}
2243
}

contracts/token/ERC20/ERC20Mintable.sol

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ contract ERC20Mintable is ERC20, Ownable {
1313
event Mint(address indexed to, uint256 amount);
1414
event MintFinished();
1515

16-
bool public mintingFinished = false;
16+
bool private mintingFinished_ = false;
1717

1818

1919
modifier canMint() {
20-
require(!mintingFinished);
20+
require(!mintingFinished_);
2121
_;
2222
}
2323

@@ -26,6 +26,13 @@ contract ERC20Mintable is ERC20, Ownable {
2626
_;
2727
}
2828

29+
/**
30+
* @return true if the minting is finished.
31+
*/
32+
function mintingFinished() public view returns(bool) {
33+
return mintingFinished_;
34+
}
35+
2936
/**
3037
* @dev Function to mint tokens
3138
* @param _to The address that will receive the minted tokens.
@@ -51,7 +58,7 @@ contract ERC20Mintable is ERC20, Ownable {
5158
* @return True if the operation was successful.
5259
*/
5360
function finishMinting() public onlyOwner canMint returns (bool) {
54-
mintingFinished = true;
61+
mintingFinished_ = true;
5562
emit MintFinished();
5663
return true;
5764
}

contracts/token/ERC20/TokenTimelock.sol

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ contract TokenTimelock {
1212
using SafeERC20 for IERC20;
1313

1414
// ERC20 basic token contract being held
15-
IERC20 public token;
15+
IERC20 private token_;
1616

1717
// beneficiary of tokens after they are released
18-
address public beneficiary;
18+
address private beneficiary_;
1919

2020
// timestamp when token release is enabled
21-
uint256 public releaseTime;
21+
uint256 private releaseTime_;
2222

2323
constructor(
2424
IERC20 _token,
@@ -29,21 +29,42 @@ contract TokenTimelock {
2929
{
3030
// solium-disable-next-line security/no-block-members
3131
require(_releaseTime > block.timestamp);
32-
token = _token;
33-
beneficiary = _beneficiary;
34-
releaseTime = _releaseTime;
32+
token_ = _token;
33+
beneficiary_ = _beneficiary;
34+
releaseTime_ = _releaseTime;
35+
}
36+
37+
/**
38+
* @return the token being held.
39+
*/
40+
function token() public view returns(IERC20) {
41+
return token_;
42+
}
43+
44+
/**
45+
* @return the beneficiary of the tokens.
46+
*/
47+
function beneficiary() public view returns(address) {
48+
return beneficiary_;
49+
}
50+
51+
/**
52+
* @return the time when the tokens are released.
53+
*/
54+
function releaseTime() public view returns(uint256) {
55+
return releaseTime_;
3556
}
3657

3758
/**
3859
* @notice Transfers tokens held by timelock to beneficiary.
3960
*/
4061
function release() public {
4162
// solium-disable-next-line security/no-block-members
42-
require(block.timestamp >= releaseTime);
63+
require(block.timestamp >= releaseTime_);
4364

44-
uint256 amount = token.balanceOf(address(this));
65+
uint256 amount = token_.balanceOf(address(this));
4566
require(amount > 0);
4667

47-
token.safeTransfer(beneficiary, amount);
68+
token_.safeTransfer(beneficiary_, amount);
4869
}
4970
}

0 commit comments

Comments
 (0)