RefundEscrow.test.js 5.0 KB

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