RefundEscrow.test.js 5.4 KB

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