-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added basic Escrow * PullPayment now uses an Escrow, removing all trust from the contract * Abstracted the Escrow tests to a behaviour * Added ConditionalEscrow * Added RefundableEscrow. * RefundableCrowdsale now uses a RefundEscrow, removed RefundVault. * Renaming after code review. * Added log test helper. * Now allowing empty deposits and withdrawals. * Style fixes. * Minor review comments. * Add Deposited and Withdrawn events, removed Refunded * The base Escrow is now Ownable, users of it (owners) must provide methods to access it.
- Loading branch information
Showing
16 changed files
with
490 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
import "../payment/ConditionalEscrow.sol"; | ||
|
||
|
||
// mock class using ConditionalEscrow | ||
contract ConditionalEscrowMock is ConditionalEscrow { | ||
mapping(address => bool) public allowed; | ||
|
||
function setAllowed(address _payee, bool _allowed) public { | ||
allowed[_payee] = _allowed; | ||
} | ||
|
||
function withdrawalAllowed(address _payee) public view returns (bool) { | ||
return allowed[_payee]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pragma solidity ^0.4.23; | ||
|
||
import "./Escrow.sol"; | ||
|
||
|
||
/** | ||
* @title ConditionalEscrow | ||
* @dev Base abstract escrow to only allow withdrawal if a condition is met. | ||
*/ | ||
contract ConditionalEscrow is Escrow { | ||
/** | ||
* @dev Returns whether an address is allowed to withdraw their funds. To be | ||
* implemented by derived contracts. | ||
* @param _payee The destination address of the funds. | ||
*/ | ||
function withdrawalAllowed(address _payee) public view returns (bool); | ||
|
||
function withdraw(address _payee) public { | ||
require(withdrawalAllowed(_payee)); | ||
super.withdraw(_payee); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
pragma solidity ^0.4.23; | ||
|
||
import "../math/SafeMath.sol"; | ||
import "../ownership/Ownable.sol"; | ||
|
||
|
||
/** | ||
* @title Escrow | ||
* @dev Base escrow contract, holds funds destinated to a payee until they | ||
* withdraw them. The contract that uses the escrow as its payment method | ||
* should be its owner, and provide public methods redirecting to the escrow's | ||
* deposit and withdraw. | ||
*/ | ||
contract Escrow is Ownable { | ||
using SafeMath for uint256; | ||
|
||
event Deposited(address indexed payee, uint256 weiAmount); | ||
event Withdrawn(address indexed payee, uint256 weiAmount); | ||
|
||
mapping(address => uint256) private deposits; | ||
|
||
function depositsOf(address _payee) public view returns (uint256) { | ||
return deposits[_payee]; | ||
} | ||
|
||
/** | ||
* @dev Stores the sent amount as credit to be withdrawn. | ||
* @param _payee The destination address of the funds. | ||
*/ | ||
function deposit(address _payee) public onlyOwner payable { | ||
uint256 amount = msg.value; | ||
deposits[_payee] = deposits[_payee].add(amount); | ||
|
||
emit Deposited(_payee, amount); | ||
} | ||
|
||
/** | ||
* @dev Withdraw accumulated balance for a payee. | ||
* @param _payee The address whose funds will be withdrawn and transferred to. | ||
*/ | ||
function withdraw(address _payee) public onlyOwner { | ||
uint256 payment = deposits[_payee]; | ||
assert(address(this).balance >= payment); | ||
|
||
deposits[_payee] = 0; | ||
|
||
_payee.transfer(payment); | ||
|
||
emit Withdrawn(_payee, payment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,42 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
import "../math/SafeMath.sol"; | ||
import "./Escrow.sol"; | ||
|
||
|
||
/** | ||
* @title PullPayment | ||
* @dev Base contract supporting async send for pull payments. Inherit from this | ||
* contract and use asyncSend instead of send or transfer. | ||
* contract and use asyncTransfer instead of send or transfer. | ||
*/ | ||
contract PullPayment { | ||
using SafeMath for uint256; | ||
Escrow private escrow; | ||
|
||
mapping(address => uint256) public payments; | ||
uint256 public totalPayments; | ||
constructor() public { | ||
escrow = new Escrow(); | ||
} | ||
|
||
/** | ||
* @dev Withdraw accumulated balance, called by payee. | ||
*/ | ||
function withdrawPayments() public { | ||
address payee = msg.sender; | ||
uint256 payment = payments[payee]; | ||
|
||
require(payment != 0); | ||
require(address(this).balance >= payment); | ||
|
||
totalPayments = totalPayments.sub(payment); | ||
payments[payee] = 0; | ||
escrow.withdraw(payee); | ||
} | ||
|
||
payee.transfer(payment); | ||
/** | ||
* @dev Returns the credit owed to an address. | ||
* @param _dest The creditor's address. | ||
*/ | ||
function payments(address _dest) public view returns (uint256) { | ||
return escrow.depositsOf(_dest); | ||
} | ||
|
||
/** | ||
* @dev Called by the payer to store the sent amount as credit to be pulled. | ||
* @param dest The destination address of the funds. | ||
* @param amount The amount to transfer. | ||
* @param _dest The destination address of the funds. | ||
* @param _amount The amount to transfer. | ||
*/ | ||
function asyncSend(address dest, uint256 amount) internal { | ||
payments[dest] = payments[dest].add(amount); | ||
totalPayments = totalPayments.add(amount); | ||
function asyncTransfer(address _dest, uint256 _amount) internal { | ||
escrow.deposit.value(_amount)(_dest); | ||
} | ||
} |
Oops, something went wrong.