RefundEscrow.test.js 5.0 KB

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