RefundEscrow.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 }),
  12. '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: owner });
  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: owner, 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: owner, value: amount });
  30. await expectRevert(this.escrow.withdraw(refundee1), 'ConditionalEscrow: payee is not allowed to withdraw');
  31. });
  32. it('does not allow beneficiary withdrawal', async function () {
  33. await this.escrow.deposit(refundee1, { from: owner, value: amount });
  34. await expectRevert(
  35. 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 }), 'Ownable: caller is not the owner');
  42. const receipt = await this.escrow.close({ from: owner });
  43. expectEvent(receipt, 'RefundsClosed');
  44. });
  45. context('closed state', function () {
  46. beforeEach(async function () {
  47. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount })));
  48. await this.escrow.close({ from: owner });
  49. });
  50. it('rejects deposits', async function () {
  51. await expectRevert(
  52. this.escrow.deposit(refundee1, { from: owner, 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), 'ConditionalEscrow: payee is not allowed to withdraw');
  58. });
  59. it('allows beneficiary withdrawal', async function () {
  60. const balanceTracker = await balance.tracker(beneficiary);
  61. await this.escrow.beneficiaryWithdraw();
  62. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount.muln(refundees.length));
  63. });
  64. it('prevents entering the refund state', async function () {
  65. await expectRevert(
  66. this.escrow.enableRefunds({ from: owner }),
  67. 'RefundEscrow: can only enable refunds while active',
  68. );
  69. });
  70. it('prevents re-entering the closed state', async function () {
  71. await expectRevert(this.escrow.close({ from: owner }), 'RefundEscrow: can only close while active');
  72. });
  73. });
  74. it('only the owner can enter refund state', async function () {
  75. await expectRevert(this.escrow.enableRefunds({ from: beneficiary }), 'Ownable: caller is not the owner');
  76. const receipt = await this.escrow.enableRefunds({ from: owner });
  77. expectEvent(receipt, 'RefundsEnabled');
  78. });
  79. context('refund state', function () {
  80. beforeEach(async function () {
  81. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount })));
  82. await this.escrow.enableRefunds({ from: owner });
  83. });
  84. it('rejects deposits', async function () {
  85. await expectRevert(
  86. this.escrow.deposit(refundee1, { from: owner, value: amount }),
  87. 'RefundEscrow: can only deposit while active',
  88. );
  89. });
  90. it('refunds refundees', async function () {
  91. for (const refundee of [refundee1, refundee2]) {
  92. const balanceTracker = await balance.tracker(refundee);
  93. await this.escrow.withdraw(refundee, { from: owner });
  94. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
  95. }
  96. });
  97. it('does not allow beneficiary withdrawal', async function () {
  98. await expectRevert(
  99. this.escrow.beneficiaryWithdraw(),
  100. 'RefundEscrow: beneficiary can only withdraw while closed',
  101. );
  102. });
  103. it('prevents entering the closed state', async function () {
  104. await expectRevert(this.escrow.close({ from: owner }), 'RefundEscrow: can only close while active');
  105. });
  106. it('prevents re-entering the refund state', async function () {
  107. await expectRevert(
  108. this.escrow.enableRefunds({ from: owner }),
  109. 'RefundEscrow: can only enable refunds while active',
  110. );
  111. });
  112. });
  113. });
  114. });