SplitPayment.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const BigNumber = web3.BigNumber
  2. const should = require('chai')
  3. .use(require('chai-as-promised'))
  4. .use(require('chai-bignumber')(BigNumber))
  5. .should()
  6. const EVMThrow = require('./helpers/EVMThrow.js')
  7. const SplitPaymentMock = artifacts.require('./helpers/SplitPaymentMock.sol')
  8. contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, payer1]) {
  9. const amount = web3.toWei(1.0, 'ether')
  10. beforeEach(async function () {
  11. this.payees = [payee1, payee2, payee3]
  12. this.shares = [20, 10, 70]
  13. this.contract = await SplitPaymentMock.new()
  14. await this.contract.addPayeeMany(this.payees, this.shares)
  15. })
  16. it('should accept payments', async function () {
  17. await web3.eth.sendTransaction({ from: owner, to: this.contract.address, value: amount })
  18. const balance = web3.eth.getBalance(this.contract.address)
  19. balance.should.be.bignumber.equal(amount)
  20. })
  21. it('should return if address is payee', async function () {
  22. const isPayee = await this.contract.isPayee.call(payee1)
  23. isPayee.should.equal(true)
  24. })
  25. it('should return if address is not payee', async function () {
  26. const isPayee = await this.contract.isPayee.call(nonpayee1)
  27. isPayee.should.equal(false)
  28. })
  29. it('should return the correct payee by address', async function () {
  30. const payeeIdx = 0
  31. const [payee, shares] = await this.contract.getPayee.call(this.payees[payeeIdx])
  32. payee.should.be.equal(payee1)
  33. shares.should.be.bignumber.equal(this.shares[payeeIdx])
  34. })
  35. it('should return the correct payee by index', async function () {
  36. const payeeIdx = 1
  37. const [payee, shares] = await this.contract.getPayeeAtIndex.call(payeeIdx)
  38. payee.should.be.equal(payee2)
  39. shares.should.be.bignumber.equal(this.shares[payeeIdx])
  40. })
  41. it('should throw if payees and shares array have different sizes', async function () {
  42. const payees = [payee1, payee2, payee3]
  43. const shares = [50, 50]
  44. await this.contract.addPayeeMany(payees, shares).should.be.rejectedWith(EVMThrow)
  45. })
  46. it('should throw if try to add same payee multiple times', async function () {
  47. const payees = [payee1, payee1]
  48. const shares = [50, 50]
  49. await this.contract.addPayeeMany(payees, shares).should.be.rejectedWith(EVMThrow)
  50. })
  51. it('should throw if try to add payee with zero shares', async function () {
  52. await this.contract.addPayee(nonpayee1, 0).should.be.rejectedWith(EVMThrow)
  53. })
  54. it('should throw if no funds to distribute', async function () {
  55. await this.contract.distributeFunds({from: payee1}).should.be.rejectedWith(EVMThrow)
  56. })
  57. it('should distribute funds to payees', async function () {
  58. await web3.eth.sendTransaction({from: payer1, to: this.contract.address, value: amount})
  59. const initBalance = web3.eth.getBalance(this.contract.address)
  60. initBalance.should.be.bignumber.equal(amount)
  61. const initAmount1 = web3.eth.getBalance(payee1)
  62. const initAmount2 = web3.eth.getBalance(payee2)
  63. const initAmount3 = web3.eth.getBalance(payee3)
  64. await this.contract.distributeFunds({from: payee1})
  65. // Contract should have zero balance after distribution
  66. const afterBalance = web3.eth.getBalance(this.contract.address)
  67. afterBalance.should.be.bignumber.equal(0)
  68. const profit1 = web3.eth.getBalance(payee1) - initAmount1
  69. const profit2 = web3.eth.getBalance(payee2) - initAmount2
  70. const profit3 = web3.eth.getBalance(payee3) - initAmount3
  71. assert(Math.abs(profit1 - web3.toWei(0.20, 'ether')) < 1e16);
  72. assert(Math.abs(profit2 - web3.toWei(0.10, 'ether')) < 1e16);
  73. assert(Math.abs(profit3 - web3.toWei(0.70, 'ether')) < 1e16);
  74. })
  75. it('should throw if non-payee want to distribute funds', async function () {
  76. await web3.eth.sendTransaction({from: payer1, to: this.contract.address, value: amount})
  77. await this.contract.distributeFunds({from: nonpayee1}).should.be.rejectedWith(EVMThrow)
  78. })
  79. })