Simplify implementation using similar interface as PullPayment contract

This commit is contained in:
Ariel Barmat
2017-09-09 11:58:23 -03:00
parent 69e83e5086
commit dc1017c929
6 changed files with 65 additions and 250 deletions

View File

@ -15,8 +15,7 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa
this.payees = [payee1, payee2, payee3]
this.shares = [20, 10, 70]
this.contract = await SplitPaymentMock.new()
await this.contract.addPayeeMany(this.payees, this.shares)
this.contract = await SplitPaymentMock.new(this.payees, this.shares)
})
it('should accept payments', async function () {
@ -26,77 +25,54 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa
balance.should.be.bignumber.equal(amount)
})
it('should return if address is payee', async function () {
const isPayee = await this.contract.isPayee.call(payee1)
isPayee.should.equal(true)
it('should store shares if address is payee', async function () {
const shares = await this.contract.shares.call(payee1)
shares.should.be.bignumber.not.equal(0)
})
it('should return if address is not payee', async function () {
const isPayee = await this.contract.isPayee.call(nonpayee1)
isPayee.should.equal(false)
it('should not store shares if address is not payee', async function () {
const shares = await this.contract.shares.call(nonpayee1)
shares.should.be.bignumber.equal(0)
})
it('should return the correct payee by address', async function () {
const payeeIdx = 0
const [payee, shares] = await this.contract.getPayee.call(this.payees[payeeIdx])
payee.should.be.equal(payee1)
shares.should.be.bignumber.equal(this.shares[payeeIdx])
it('should throw if no funds to claim', async function () {
await this.contract.claim({from: payee1}).should.be.rejectedWith(EVMThrow)
})
it('should return the correct payee by index', async function () {
const payeeIdx = 1
const [payee, shares] = await this.contract.getPayeeAtIndex.call(payeeIdx)
payee.should.be.equal(payee2)
shares.should.be.bignumber.equal(this.shares[payeeIdx])
it('should throw if non-payee want to claim', async function () {
await web3.eth.sendTransaction({from: payer1, to: this.contract.address, value: amount})
await this.contract.claim({from: nonpayee1}).should.be.rejectedWith(EVMThrow)
})
it('should throw if payees and shares array have different sizes', async function () {
const payees = [payee1, payee2, payee3]
const shares = [50, 50]
await this.contract.addPayeeMany(payees, shares).should.be.rejectedWith(EVMThrow)
})
it('should throw if try to add same payee multiple times', async function () {
const payees = [payee1, payee1]
const shares = [50, 50]
await this.contract.addPayeeMany(payees, shares).should.be.rejectedWith(EVMThrow)
})
it('should throw if try to add payee with zero shares', async function () {
await this.contract.addPayee(nonpayee1, 0).should.be.rejectedWith(EVMThrow)
})
it('should throw if no funds to distribute', async function () {
await this.contract.distributeFunds({from: payee1}).should.be.rejectedWith(EVMThrow)
})
it('should distribute funds to payees', async function () {
await web3.eth.sendTransaction({from: payer1, to: this.contract.address, value: amount})
// receive funds
const initBalance = web3.eth.getBalance(this.contract.address)
initBalance.should.be.bignumber.equal(amount)
// distribute to payees
const initAmount1 = web3.eth.getBalance(payee1)
const initAmount2 = web3.eth.getBalance(payee2)
const initAmount3 = web3.eth.getBalance(payee3)
await this.contract.distributeFunds({from: payee1})
// Contract should have zero balance after distribution
const afterBalance = web3.eth.getBalance(this.contract.address)
afterBalance.should.be.bignumber.equal(0)
await this.contract.claim({from: payee1})
const profit1 = web3.eth.getBalance(payee1) - initAmount1
assert(Math.abs(profit1 - web3.toWei(0.20, 'ether')) < 1e16)
const initAmount2 = web3.eth.getBalance(payee2)
await this.contract.claim({from: payee2})
const profit2 = web3.eth.getBalance(payee2) - initAmount2
assert(Math.abs(profit2 - web3.toWei(0.10, 'ether')) < 1e16)
const initAmount3 = web3.eth.getBalance(payee3)
await this.contract.claim({from: payee3})
const profit3 = web3.eth.getBalance(payee3) - initAmount3
assert(Math.abs(profit3 - web3.toWei(0.70, 'ether')) < 1e16)
assert(Math.abs(profit1 - web3.toWei(0.20, 'ether')) < 1e16);
assert(Math.abs(profit2 - web3.toWei(0.10, 'ether')) < 1e16);
assert(Math.abs(profit3 - web3.toWei(0.70, 'ether')) < 1e16);
})
// end balance should be zero
const endBalance = web3.eth.getBalance(this.contract.address)
endBalance.should.be.bignumber.equal(0)
it('should throw if non-payee want to distribute funds', async function () {
await web3.eth.sendTransaction({from: payer1, to: this.contract.address, value: amount})
await this.contract.distributeFunds({from: nonpayee1}).should.be.rejectedWith(EVMThrow)
// check correct funds released accounting
const totalReleased = await this.contract.totalReleased.call()
totalReleased.should.be.bignumber.equal(initBalance)
})
})