RefundEscrow.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const shouldFail = require('../../helpers/shouldFail');
  2. const expectEvent = require('../../helpers/expectEvent');
  3. const { balanceDifference } = require('../../helpers/balanceDifference');
  4. const { ether } = require('../../helpers/ether');
  5. const { ZERO_ADDRESS } = require('../../helpers/constants');
  6. require('../../helpers/setup');
  7. const RefundEscrow = artifacts.require('RefundEscrow');
  8. contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee2]) {
  9. const amount = ether(54.0);
  10. const refundees = [refundee1, refundee2];
  11. it('requires a non-null beneficiary', async function () {
  12. await shouldFail.reverting(
  13. RefundEscrow.new(ZERO_ADDRESS, { from: primary })
  14. );
  15. });
  16. context('once deployed', function () {
  17. beforeEach(async function () {
  18. this.escrow = await RefundEscrow.new(beneficiary, { from: primary });
  19. });
  20. context('active state', function () {
  21. it('has beneficiary and state', async function () {
  22. (await this.escrow.beneficiary()).should.be.equal(beneficiary);
  23. (await this.escrow.state()).should.be.bignumber.equal(0);
  24. });
  25. it('accepts deposits', async function () {
  26. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  27. (await this.escrow.depositsOf(refundee1)).should.be.bignumber.equal(amount);
  28. });
  29. it('does not refund refundees', async function () {
  30. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  31. await shouldFail.reverting(this.escrow.withdraw(refundee1));
  32. });
  33. it('does not allow beneficiary withdrawal', async function () {
  34. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  35. await shouldFail.reverting(this.escrow.beneficiaryWithdraw());
  36. });
  37. });
  38. it('only the primary account can enter closed state', async function () {
  39. await shouldFail.reverting(this.escrow.close({ from: beneficiary }));
  40. const { logs } = await this.escrow.close({ from: primary });
  41. expectEvent.inLogs(logs, 'RefundsClosed');
  42. });
  43. context('closed state', function () {
  44. beforeEach(async function () {
  45. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount })));
  46. await this.escrow.close({ from: primary });
  47. });
  48. it('rejects deposits', async function () {
  49. await shouldFail.reverting(this.escrow.deposit(refundee1, { from: primary, value: amount }));
  50. });
  51. it('does not refund refundees', async function () {
  52. await shouldFail.reverting(this.escrow.withdraw(refundee1));
  53. });
  54. it('allows beneficiary withdrawal', async function () {
  55. (await balanceDifference(beneficiary, () =>
  56. this.escrow.beneficiaryWithdraw()
  57. )).should.be.bignumber.equal(amount * refundees.length);
  58. });
  59. it('prevents entering the refund state', async function () {
  60. await shouldFail.reverting(this.escrow.enableRefunds({ from: primary }));
  61. });
  62. it('prevents re-entering the closed state', async function () {
  63. await shouldFail.reverting(this.escrow.close({ from: primary }));
  64. });
  65. });
  66. it('only the primary account can enter refund state', async function () {
  67. await shouldFail.reverting(this.escrow.enableRefunds({ from: beneficiary }));
  68. const { logs } = await this.escrow.enableRefunds({ from: primary });
  69. expectEvent.inLogs(logs, 'RefundsEnabled');
  70. });
  71. context('refund state', function () {
  72. beforeEach(async function () {
  73. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount })));
  74. await this.escrow.enableRefunds({ from: primary });
  75. });
  76. it('rejects deposits', async function () {
  77. await shouldFail.reverting(this.escrow.deposit(refundee1, { from: primary, value: amount }));
  78. });
  79. it('refunds refundees', async function () {
  80. for (const refundee of [refundee1, refundee2]) {
  81. (await balanceDifference(refundee, () =>
  82. this.escrow.withdraw(refundee, { from: primary }))
  83. ).should.be.bignumber.equal(amount);
  84. }
  85. });
  86. it('does not allow beneficiary withdrawal', async function () {
  87. await shouldFail.reverting(this.escrow.beneficiaryWithdraw());
  88. });
  89. it('prevents entering the closed state', async function () {
  90. await shouldFail.reverting(this.escrow.close({ from: primary }));
  91. });
  92. it('prevents re-entering the refund state', async function () {
  93. await shouldFail.reverting(this.escrow.enableRefunds({ from: primary }));
  94. });
  95. });
  96. });
  97. });