RefundEscrow.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const { balance, constants, ether, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const RefundEscrow = artifacts.require('RefundEscrow');
  5. contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee2]) {
  6. const amount = ether('54');
  7. const refundees = [refundee1, refundee2];
  8. it('requires a non-null beneficiary', async function () {
  9. await expectRevert(
  10. RefundEscrow.new(ZERO_ADDRESS, { from: primary }), 'RefundEscrow: beneficiary is the zero address'
  11. );
  12. });
  13. context('once deployed', function () {
  14. beforeEach(async function () {
  15. this.escrow = await RefundEscrow.new(beneficiary, { from: primary });
  16. });
  17. context('active state', function () {
  18. it('has beneficiary and state', async function () {
  19. expect(await this.escrow.beneficiary()).to.equal(beneficiary);
  20. expect(await this.escrow.state()).to.be.bignumber.equal('0');
  21. });
  22. it('accepts deposits', async function () {
  23. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  24. expect(await this.escrow.depositsOf(refundee1)).to.be.bignumber.equal(amount);
  25. });
  26. it('does not refund refundees', async function () {
  27. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  28. await expectRevert(this.escrow.withdraw(refundee1),
  29. 'ConditionalEscrow: payee is not allowed to withdraw'
  30. );
  31. });
  32. it('does not allow beneficiary withdrawal', async function () {
  33. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  34. await expectRevert(this.escrow.beneficiaryWithdraw(),
  35. 'RefundEscrow: beneficiary can only withdraw while closed'
  36. );
  37. });
  38. });
  39. it('only the primary account can enter closed state', async function () {
  40. await expectRevert(this.escrow.close({ from: beneficiary }),
  41. 'Secondary: caller is not the primary account'
  42. );
  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 expectRevert(this.escrow.deposit(refundee1, { from: primary, value: amount }),
  53. 'RefundEscrow: can only deposit while active'
  54. );
  55. });
  56. it('does not refund refundees', async function () {
  57. await expectRevert(this.escrow.withdraw(refundee1),
  58. 'ConditionalEscrow: payee is not allowed to withdraw'
  59. );
  60. });
  61. it('allows beneficiary withdrawal', async function () {
  62. const balanceTracker = await balance.tracker(beneficiary);
  63. await this.escrow.beneficiaryWithdraw();
  64. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount.muln(refundees.length));
  65. });
  66. it('prevents entering the refund state', async function () {
  67. await expectRevert(this.escrow.enableRefunds({ from: primary }),
  68. 'RefundEscrow: can only enable refunds while active'
  69. );
  70. });
  71. it('prevents re-entering the closed state', async function () {
  72. await expectRevert(this.escrow.close({ from: primary }),
  73. 'RefundEscrow: can only close while active'
  74. );
  75. });
  76. });
  77. it('only the primary account can enter refund state', async function () {
  78. await expectRevert(this.escrow.enableRefunds({ from: beneficiary }),
  79. 'Secondary: caller is not the primary account'
  80. );
  81. const { logs } = await this.escrow.enableRefunds({ from: primary });
  82. expectEvent.inLogs(logs, 'RefundsEnabled');
  83. });
  84. context('refund state', function () {
  85. beforeEach(async function () {
  86. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount })));
  87. await this.escrow.enableRefunds({ from: primary });
  88. });
  89. it('rejects deposits', async function () {
  90. await expectRevert(this.escrow.deposit(refundee1, { from: primary, value: amount }),
  91. 'RefundEscrow: can only deposit while active'
  92. );
  93. });
  94. it('refunds refundees', async function () {
  95. for (const refundee of [refundee1, refundee2]) {
  96. const balanceTracker = await balance.tracker(refundee);
  97. await this.escrow.withdraw(refundee, { from: primary });
  98. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
  99. }
  100. });
  101. it('does not allow beneficiary withdrawal', async function () {
  102. await expectRevert(this.escrow.beneficiaryWithdraw(),
  103. 'RefundEscrow: beneficiary can only withdraw while closed'
  104. );
  105. });
  106. it('prevents entering the closed state', async function () {
  107. await expectRevert(this.escrow.close({ from: primary }),
  108. 'RefundEscrow: can only close while active'
  109. );
  110. });
  111. it('prevents re-entering the refund state', async function () {
  112. await expectRevert(this.escrow.enableRefunds({ from: primary }),
  113. 'RefundEscrow: can only enable refunds while active'
  114. );
  115. });
  116. });
  117. });
  118. });