Skip to content

Commit bcc442e

Browse files
author
Leo Arias
committed
Improve encapsulation on payments
1 parent 72da588 commit bcc442e

File tree

5 files changed

+85
-32
lines changed

5 files changed

+85
-32
lines changed

contracts/ownership/Heritable.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ contract Heritable is Ownable {
108108
// solium-disable-next-line security/no-block-members
109109
require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_);
110110
emit HeirOwnershipClaimed(getOwner(), heir_);
111-
transferOwnership(heir_);
111+
_transferOwnership(heir_);
112112
timeOfDeath_ = 0;
113113
}
114114

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 getState() public view returns(State) {
36+
return state_;
37+
}
38+
39+
/**
40+
* @return the beneficiary of the escrow.
41+
*/
42+
function getBeneficiary() 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,26 +35,61 @@ contract SplitPayment {
3535
*/
3636
function () external payable {}
3737

38+
/**
39+
* @return the total shares of the contract.
40+
*/
41+
function getTotalShares() public view returns(uint256) {
42+
return totalShares_;
43+
}
44+
45+
/**
46+
* @return the total amount already released.
47+
*/
48+
function getTotalReleased() public view returns(uint256) {
49+
return totalReleased_;
50+
}
51+
52+
/**
53+
* @return the shares of an account.
54+
*/
55+
function getShares(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 getReleased(address _account) public view returns(uint256) {
63+
return released_[_account];
64+
}
65+
66+
/**
67+
* @return the address of a payee.
68+
*/
69+
function getPayee(uint256 index) public view returns(address) {
70+
return payees_[index];
71+
}
72+
3873
/**
3974
* @dev Claim your share of the balance.
4075
*/
4176
function claim() public {
4277
address payee = msg.sender;
4378

44-
require(shares[payee] > 0);
79+
require(shares_[payee] > 0);
4580

46-
uint256 totalReceived = address(this).balance.add(totalReleased);
81+
uint256 totalReceived = address(this).balance.add(totalReleased_);
4782
uint256 payment = totalReceived.mul(
48-
shares[payee]).div(
49-
totalShares).sub(
50-
released[payee]
83+
shares_[payee]).div(
84+
totalShares_).sub(
85+
released_[payee]
5186
);
5287

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

56-
released[payee] = released[payee].add(payment);
57-
totalReleased = totalReleased.add(payment);
91+
released_[payee] = released_[payee].add(payment);
92+
totalReleased_ = totalReleased_.add(payment);
5893

5994
payee.transfer(payment);
6095
}
@@ -67,10 +102,10 @@ contract SplitPayment {
67102
function addPayee(address _payee, uint256 _shares) internal {
68103
require(_payee != address(0));
69104
require(_shares > 0);
70-
require(shares[_payee] == 0);
105+
require(shares_[_payee] == 0);
71106

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

test/payment/RefundEscrow.test.js

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

3030
context('active state', function () {
31+
it('has beneficiary', async function () {
32+
(await this.escrow.getBeneficiary()).should.be.equal(beneficiary);
33+
});
34+
3135
it('accepts deposits', async function () {
3236
await this.escrow.deposit(refundee1, { from: owner, value: amount });
3337

test/payment/SplitPayment.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1,
5353
});
5454

5555
it('should store shares if address is payee', async function () {
56-
(await this.contract.shares.call(payee1)).should.be.bignumber.not.equal(0);
56+
(await this.contract.getShares(payee1)).should.be.bignumber.not.equal(0);
5757
});
5858

5959
it('should not store shares if address is not payee', async function () {
60-
(await this.contract.shares.call(nonpayee1)).should.be.bignumber.equal(0);
60+
(await this.contract.getShares(nonpayee1)).should.be.bignumber.equal(0);
6161
});
6262

6363
it('should throw if no funds to claim', async function () {
@@ -96,7 +96,7 @@ contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1,
9696
(await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0);
9797

9898
// check correct funds released accounting
99-
(await this.contract.totalReleased.call()).should.be.bignumber.equal(initBalance);
99+
(await this.contract.getTotalReleased()).should.be.bignumber.equal(initBalance);
100100
});
101101
});
102102
});

0 commit comments

Comments
 (0)