Skip to content

Commit 45c0c07

Browse files
Leo Ariasfrangio
authored andcommitted
Improve encapsulation on lifecycle, ownership and payments (#1269)
* Improve encapsulation on Pausable * add the underscore * Improve encapsulation on ownership * fix rebase * fix ownership * Improve encapsulation on payments * Add missing tests * add missing test * Do not prefix getters * Fix tests. * revert pending owner reset * add missing underscore * Add missing underscore
1 parent d6c7700 commit 45c0c07

File tree

11 files changed

+127
-48
lines changed

11 files changed

+127
-48
lines changed

contracts/bounties/BreakInvariantBounty.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ contract BreakInvariantBounty is PullPayment, Ownable {
5858
* @dev Transfers the current balance to the owner and terminates the contract.
5959
*/
6060
function destroy() public onlyOwner {
61-
selfdestruct(owner);
61+
selfdestruct(owner());
6262
}
6363

6464
/**

contracts/drafts/TokenVesting.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ contract TokenVesting is Ownable {
9393

9494
revoked[_token] = true;
9595

96-
_token.safeTransfer(owner, refund);
96+
_token.safeTransfer(owner(), refund);
9797

9898
emit Revoked();
9999
}

contracts/lifecycle/Pausable.sol

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,45 @@ contract Pausable is Ownable {
1212
event Paused();
1313
event Unpaused();
1414

15-
bool public paused = false;
15+
bool private paused_ = false;
1616

1717

18+
/**
19+
* @return true if the contract is paused, false otherwise.
20+
*/
21+
function paused() public view returns(bool) {
22+
return paused_;
23+
}
24+
1825
/**
1926
* @dev Modifier to make a function callable only when the contract is not paused.
2027
*/
2128
modifier whenNotPaused() {
22-
require(!paused);
29+
require(!paused_);
2330
_;
2431
}
2532

2633
/**
2734
* @dev Modifier to make a function callable only when the contract is paused.
2835
*/
2936
modifier whenPaused() {
30-
require(paused);
37+
require(paused_);
3138
_;
3239
}
3340

3441
/**
3542
* @dev called by the owner to pause, triggers stopped state
3643
*/
3744
function pause() public onlyOwner whenNotPaused {
38-
paused = true;
45+
paused_ = true;
3946
emit Paused();
4047
}
4148

4249
/**
4350
* @dev called by the owner to unpause, returns to normal state
4451
*/
4552
function unpause() public onlyOwner whenPaused {
46-
paused = false;
53+
paused_ = false;
4754
emit Unpaused();
4855
}
4956
}

contracts/ownership/CanReclaimToken.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ contract CanReclaimToken is Ownable {
2020
*/
2121
function reclaimToken(IERC20 _token) external onlyOwner {
2222
uint256 balance = _token.balanceOf(this);
23-
_token.safeTransfer(owner, balance);
23+
_token.safeTransfer(owner(), balance);
2424
}
2525

2626
}

contracts/ownership/Ownable.sol

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pragma solidity ^0.4.24;
77
* functions, this simplifies the implementation of "user permissions".
88
*/
99
contract Ownable {
10-
address public owner;
10+
address private owner_;
1111

1212

1313
event OwnershipRenounced(address indexed previousOwner);
@@ -22,14 +22,21 @@ contract Ownable {
2222
* account.
2323
*/
2424
constructor() public {
25-
owner = msg.sender;
25+
owner_ = msg.sender;
26+
}
27+
28+
/**
29+
* @return the address of the owner.
30+
*/
31+
function owner() public view returns(address) {
32+
return owner_;
2633
}
2734

2835
/**
2936
* @dev Throws if called by any account other than the owner.
3037
*/
3138
modifier onlyOwner() {
32-
require(msg.sender == owner);
39+
require(msg.sender == owner_);
3340
_;
3441
}
3542

@@ -40,8 +47,8 @@ contract Ownable {
4047
* modifier anymore.
4148
*/
4249
function renounceOwnership() public onlyOwner {
43-
emit OwnershipRenounced(owner);
44-
owner = address(0);
50+
emit OwnershipRenounced(owner_);
51+
owner_ = address(0);
4552
}
4653

4754
/**
@@ -58,7 +65,7 @@ contract Ownable {
5865
*/
5966
function _transferOwnership(address _newOwner) internal {
6067
require(_newOwner != address(0));
61-
emit OwnershipTransferred(owner, _newOwner);
62-
owner = _newOwner;
68+
emit OwnershipTransferred(owner_, _newOwner);
69+
owner_ = _newOwner;
6370
}
6471
}

contracts/ownership/Superuser.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ contract Superuser is Ownable, RBAC {
2727
}
2828

2929
modifier onlyOwnerOrSuperuser() {
30-
require(msg.sender == owner || isSuperuser(msg.sender));
30+
require(msg.sender == owner() || isSuperuser(msg.sender));
3131
_;
3232
}
3333

contracts/payment/RefundEscrow.sol

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,39 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
1616
event Closed();
1717
event RefundsEnabled();
1818

19-
State public state;
20-
address public beneficiary;
19+
State private state_;
20+
address private beneficiary_;
2121

2222
/**
2323
* @dev Constructor.
2424
* @param _beneficiary The beneficiary of the deposits.
2525
*/
2626
constructor(address _beneficiary) public {
2727
require(_beneficiary != address(0));
28-
beneficiary = _beneficiary;
29-
state = State.Active;
28+
beneficiary_ = _beneficiary;
29+
state_ = State.Active;
30+
}
31+
32+
/**
33+
* @return the current state of the escrow.
34+
*/
35+
function state() public view returns(State) {
36+
return state_;
37+
}
38+
39+
/**
40+
* @return the beneficiary of the escrow.
41+
*/
42+
function beneficiary() public view returns(address) {
43+
return beneficiary_;
3044
}
3145

3246
/**
3347
* @dev Stores funds that may later be refunded.
3448
* @param _refundee The address funds will be sent to if a refund occurs.
3549
*/
3650
function deposit(address _refundee) public payable {
37-
require(state == State.Active);
51+
require(state_ == State.Active);
3852
super.deposit(_refundee);
3953
}
4054

@@ -43,32 +57,32 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
4357
* further deposits.
4458
*/
4559
function close() public onlyOwner {
46-
require(state == State.Active);
47-
state = State.Closed;
60+
require(state_ == State.Active);
61+
state_ = State.Closed;
4862
emit Closed();
4963
}
5064

5165
/**
5266
* @dev Allows for refunds to take place, rejecting further deposits.
5367
*/
5468
function enableRefunds() public onlyOwner {
55-
require(state == State.Active);
56-
state = State.Refunding;
69+
require(state_ == State.Active);
70+
state_ = State.Refunding;
5771
emit RefundsEnabled();
5872
}
5973

6074
/**
6175
* @dev Withdraws the beneficiary's funds.
6276
*/
6377
function beneficiaryWithdraw() public {
64-
require(state == State.Closed);
65-
beneficiary.transfer(address(this).balance);
78+
require(state_ == State.Closed);
79+
beneficiary_.transfer(address(this).balance);
6680
}
6781

6882
/**
6983
* @dev Returns whether refundees can withdraw their deposits (be refunded).
7084
*/
7185
function withdrawalAllowed(address _payee) public view returns (bool) {
72-
return state == State.Refunding;
86+
return state_ == State.Refunding;
7387
}
7488
}

contracts/payment/SplitPayment.sol

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import "../math/SafeMath.sol";
1111
contract SplitPayment {
1212
using SafeMath for uint256;
1313

14-
uint256 public totalShares = 0;
15-
uint256 public totalReleased = 0;
14+
uint256 private totalShares_ = 0;
15+
uint256 private totalReleased_ = 0;
1616

17-
mapping(address => uint256) public shares;
18-
mapping(address => uint256) public released;
19-
address[] public payees;
17+
mapping(address => uint256) private shares_;
18+
mapping(address => uint256) private released_;
19+
address[] private payees_;
2020

2121
/**
2222
* @dev Constructor
@@ -35,25 +35,60 @@ contract SplitPayment {
3535
*/
3636
function () external payable {}
3737

38+
/**
39+
* @return the total shares of the contract.
40+
*/
41+
function totalShares() public view returns(uint256) {
42+
return totalShares_;
43+
}
44+
45+
/**
46+
* @return the total amount already released.
47+
*/
48+
function totalReleased() public view returns(uint256) {
49+
return totalReleased_;
50+
}
51+
52+
/**
53+
* @return the shares of an account.
54+
*/
55+
function shares(address _account) public view returns(uint256) {
56+
return shares_[_account];
57+
}
58+
59+
/**
60+
* @return the amount already released to an account.
61+
*/
62+
function released(address _account) public view returns(uint256) {
63+
return released_[_account];
64+
}
65+
66+
/**
67+
* @return the address of a payee.
68+
*/
69+
function payee(uint256 index) public view returns(address) {
70+
return payees_[index];
71+
}
72+
3873
/**
3974
* @dev Release one of the payee's proportional payment.
4075
* @param _payee Whose payments will be released.
4176
*/
4277
function release(address _payee) public {
43-
require(shares[_payee] > 0);
78+
require(shares_[_payee] > 0);
4479

45-
uint256 totalReceived = address(this).balance.add(totalReleased);
80+
uint256 totalReceived = address(this).balance.add(totalReleased_);
4681
uint256 payment = totalReceived.mul(
47-
shares[_payee]).div(
48-
totalShares).sub(
49-
released[_payee]
82+
shares_[_payee]).div(
83+
totalShares_).sub(
84+
released_[_payee]
5085
);
5186

5287
require(payment != 0);
5388
assert(address(this).balance >= payment);
5489

55-
released[_payee] = released[_payee].add(payment);
56-
totalReleased = totalReleased.add(payment);
90+
released_[_payee] = released_[_payee].add(payment);
91+
totalReleased_ = totalReleased_.add(payment);
5792

5893
_payee.transfer(payment);
5994
}
@@ -66,10 +101,10 @@ contract SplitPayment {
66101
function _addPayee(address _payee, uint256 _shares) internal {
67102
require(_payee != address(0));
68103
require(_shares > 0);
69-
require(shares[_payee] == 0);
104+
require(shares_[_payee] == 0);
70105

71-
payees.push(_payee);
72-
shares[_payee] = _shares;
73-
totalShares = totalShares.add(_shares);
106+
payees_.push(_payee);
107+
shares_[_payee] = _shares;
108+
totalShares_ = totalShares_.add(_shares);
74109
}
75110
}

contracts/token/ERC20/ERC20Mintable.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ contract ERC20Mintable is ERC20, Ownable {
2222
}
2323

2424
modifier hasMintPermission() {
25-
require(msg.sender == owner);
25+
require(msg.sender == owner());
2626
_;
2727
}
2828

test/payment/RefundEscrow.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ contract('RefundEscrow', function ([_, owner, beneficiary, refundee1, refundee2]
2828
});
2929

3030
context('active state', function () {
31+
it('has beneficiary and state', async function () {
32+
(await this.escrow.beneficiary()).should.be.equal(beneficiary);
33+
(await this.escrow.state()).should.be.bignumber.equal(0);
34+
});
35+
3136
it('accepts deposits', async function () {
3237
await this.escrow.deposit(refundee1, { from: owner, value: amount });
3338

0 commit comments

Comments
 (0)