RefundEscrow.test.js 4.7 KB

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