RefundEscrow.test.js 5.3 KB

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