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
This commit is contained in:
Leo Arias
2018-09-05 13:11:29 -06:00
committed by Francisco Giordano
parent d6c7700f4c
commit 45c0c072d1
11 changed files with 127 additions and 48 deletions

View File

@ -16,8 +16,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
event Closed();
event RefundsEnabled();
State public state;
address public beneficiary;
State private state_;
address private beneficiary_;
/**
* @dev Constructor.
@ -25,8 +25,22 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
*/
constructor(address _beneficiary) public {
require(_beneficiary != address(0));
beneficiary = _beneficiary;
state = State.Active;
beneficiary_ = _beneficiary;
state_ = State.Active;
}
/**
* @return the current state of the escrow.
*/
function state() public view returns(State) {
return state_;
}
/**
* @return the beneficiary of the escrow.
*/
function beneficiary() public view returns(address) {
return beneficiary_;
}
/**
@ -34,7 +48,7 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
* @param _refundee The address funds will be sent to if a refund occurs.
*/
function deposit(address _refundee) public payable {
require(state == State.Active);
require(state_ == State.Active);
super.deposit(_refundee);
}
@ -43,8 +57,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
* further deposits.
*/
function close() public onlyOwner {
require(state == State.Active);
state = State.Closed;
require(state_ == State.Active);
state_ = State.Closed;
emit Closed();
}
@ -52,8 +66,8 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
* @dev Allows for refunds to take place, rejecting further deposits.
*/
function enableRefunds() public onlyOwner {
require(state == State.Active);
state = State.Refunding;
require(state_ == State.Active);
state_ = State.Refunding;
emit RefundsEnabled();
}
@ -61,14 +75,14 @@ contract RefundEscrow is Ownable, ConditionalEscrow {
* @dev Withdraws the beneficiary's funds.
*/
function beneficiaryWithdraw() public {
require(state == State.Closed);
beneficiary.transfer(address(this).balance);
require(state_ == State.Closed);
beneficiary_.transfer(address(this).balance);
}
/**
* @dev Returns whether refundees can withdraw their deposits (be refunded).
*/
function withdrawalAllowed(address _payee) public view returns (bool) {
return state == State.Refunding;
return state_ == State.Refunding;
}
}

View File

@ -11,12 +11,12 @@ import "../math/SafeMath.sol";
contract SplitPayment {
using SafeMath for uint256;
uint256 public totalShares = 0;
uint256 public totalReleased = 0;
uint256 private totalShares_ = 0;
uint256 private totalReleased_ = 0;
mapping(address => uint256) public shares;
mapping(address => uint256) public released;
address[] public payees;
mapping(address => uint256) private shares_;
mapping(address => uint256) private released_;
address[] private payees_;
/**
* @dev Constructor
@ -35,25 +35,60 @@ contract SplitPayment {
*/
function () external payable {}
/**
* @return the total shares of the contract.
*/
function totalShares() public view returns(uint256) {
return totalShares_;
}
/**
* @return the total amount already released.
*/
function totalReleased() public view returns(uint256) {
return totalReleased_;
}
/**
* @return the shares of an account.
*/
function shares(address _account) public view returns(uint256) {
return shares_[_account];
}
/**
* @return the amount already released to an account.
*/
function released(address _account) public view returns(uint256) {
return released_[_account];
}
/**
* @return the address of a payee.
*/
function payee(uint256 index) public view returns(address) {
return payees_[index];
}
/**
* @dev Release one of the payee's proportional payment.
* @param _payee Whose payments will be released.
*/
function release(address _payee) public {
require(shares[_payee] > 0);
require(shares_[_payee] > 0);
uint256 totalReceived = address(this).balance.add(totalReleased);
uint256 totalReceived = address(this).balance.add(totalReleased_);
uint256 payment = totalReceived.mul(
shares[_payee]).div(
totalShares).sub(
released[_payee]
shares_[_payee]).div(
totalShares_).sub(
released_[_payee]
);
require(payment != 0);
assert(address(this).balance >= payment);
released[_payee] = released[_payee].add(payment);
totalReleased = totalReleased.add(payment);
released_[_payee] = released_[_payee].add(payment);
totalReleased_ = totalReleased_.add(payment);
_payee.transfer(payment);
}
@ -66,10 +101,10 @@ contract SplitPayment {
function _addPayee(address _payee, uint256 _shares) internal {
require(_payee != address(0));
require(_shares > 0);
require(shares[_payee] == 0);
require(shares_[_payee] == 0);
payees.push(_payee);
shares[_payee] = _shares;
totalShares = totalShares.add(_shares);
payees_.push(_payee);
shares_[_payee] = _shares;
totalShares_ = totalShares_.add(_shares);
}
}