Browse Source

Add a SplitPullPayment contract that combines distribution of funds and delayed withdrawal from each party

Ariel Barmat 8 years ago
parent
commit
69e83e5086

+ 28 - 0
contracts/payment/SplitPullPayment.sol

@@ -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);
+  }
+}

+ 42 - 0
test/SplitPullPayment.js

@@ -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)
+  })
+})

+ 9 - 0
test/helpers/SplitPullPaymentMock.sol

@@ -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 {}
+}