forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a SplitPullPayment contract that combines distribution of funds a…
…nd delayed withdrawal from each party
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
pragma solidity ^0.4.15; | ||
|
||
import './PullPayment.sol'; | ||
import './SplitPayment.sol'; | ||
|
||
/** | ||
* @title SplitPullPayment | ||
* @dev Contract supporting the distribution of funds combined with withdrawals through PullPayment. | ||
*/ | ||
contract SplitPullPayment is SplitPayment, PullPayment { | ||
/** | ||
* @dev Return the total amount of funds available for distribution. | ||
* @dev Override from SplitPayment to take into account credited funds for pull payments. | ||
*/ | ||
function toDistribute() internal returns (uint256) { | ||
return this.balance.sub(totalPayments); | ||
} | ||
|
||
/** | ||
* @dev Perform the payment to a payee. | ||
* @dev Override from SplitPayment to do an asyncSend for later withdrawal. | ||
* @param _payee The address of the payee to be paid. | ||
* @param _amount The amount for the payment. | ||
*/ | ||
function pay(address _payee, uint256 _amount) internal { | ||
asyncSend(_payee, _amount); | ||
} | ||
} |
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,42 @@ | ||
const BigNumber = web3.BigNumber | ||
|
||
const should = require('chai') | ||
.use(require('chai-as-promised')) | ||
.use(require('chai-bignumber')(BigNumber)) | ||
.should() | ||
|
||
const EVMThrow = require('./helpers/EVMThrow.js') | ||
const SplitPullPaymentMock = artifacts.require('./helpers/SplitPullPaymentMock.sol') | ||
|
||
contract('SplitPullPayment', function ([owner, payee1, payee2, payee3, nonpayee1, payer1]) { | ||
const amount = web3.toWei(1.0, 'ether') | ||
|
||
beforeEach(async function () { | ||
this.payees = [payee1, payee2, payee3] | ||
this.shares = [20, 10, 70] | ||
|
||
this.contract = await SplitPullPaymentMock.new() | ||
await this.contract.addPayeeMany(this.payees, this.shares) | ||
}) | ||
|
||
it('should distribute funds to payees', async function () { | ||
await web3.eth.sendTransaction({from: payer1, to: this.contract.address, value: amount}) | ||
|
||
await this.contract.distributeFunds({from: payee1}) | ||
|
||
const amount1 = await this.contract.payments.call(payee1) | ||
amount1.should.be.bignumber.equal(web3.toWei(0.20, 'ether')) | ||
|
||
const amount2 = await this.contract.payments.call(payee2) | ||
amount2.should.be.bignumber.equal(web3.toWei(0.10, 'ether')) | ||
|
||
const amount3 = await this.contract.payments.call(payee3) | ||
amount3.should.be.bignumber.equal(web3.toWei(0.70, 'ether')) | ||
|
||
const balance = web3.eth.getBalance(this.contract.address) | ||
balance.should.be.bignumber.equal(amount) | ||
|
||
const totalPayments = await this.contract.totalPayments.call() | ||
balance.should.be.bignumber.equal(amount) | ||
}) | ||
}) |
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,9 @@ | ||
pragma solidity ^0.4.15; | ||
|
||
import '../../contracts/payment/SplitPullPayment.sol'; | ||
|
||
// mock class using SplitPullPaymentMock | ||
contract SplitPullPaymentMock is SplitPullPayment { | ||
function SplitPullPaymentMock() SplitPayment(0) payable { } | ||
function () payable {} | ||
} |