RefundEscrow.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import EVMRevert from '../helpers/EVMRevert';
  2. import expectEvent from '../helpers/expectEvent';
  3. const BigNumber = web3.BigNumber;
  4. require('chai')
  5. .use(require('chai-bignumber')(BigNumber))
  6. .should();
  7. const RefundEscrow = artifacts.require('RefundEscrow');
  8. contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) {
  9. const amount = web3.toWei(54.0, 'ether');
  10. const refundees = [refundee1, refundee2];
  11. beforeEach(async function () {
  12. this.escrow = await RefundEscrow.new(beneficiary, { from: owner });
  13. });
  14. context('active state', function () {
  15. it('accepts deposits', async function () {
  16. await this.escrow.deposit(refundee1, { from: owner, value: amount });
  17. const deposit = await this.escrow.depositsOf(refundee1);
  18. deposit.should.be.bignumber.equal(amount);
  19. });
  20. it('does not refund refundees', async function () {
  21. await this.escrow.deposit(refundee1, { from: owner, value: amount });
  22. await this.escrow.withdraw(refundee1).should.be.rejectedWith(EVMRevert);
  23. });
  24. it('does not allow beneficiary withdrawal', async function () {
  25. await this.escrow.deposit(refundee1, { from: owner, value: amount });
  26. await this.escrow.beneficiaryWithdraw().should.be.rejectedWith(EVMRevert);
  27. });
  28. });
  29. it('only owner can enter closed state', async function () {
  30. await this.escrow.close({ from: beneficiary }).should.be.rejectedWith(EVMRevert);
  31. const receipt = await this.escrow.close({ from: owner });
  32. await expectEvent.inLogs(receipt.logs, 'Closed');
  33. });
  34. context('closed state', function () {
  35. beforeEach(async function () {
  36. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount })));
  37. await this.escrow.close({ from: owner });
  38. });
  39. it('rejects deposits', async function () {
  40. await this.escrow.deposit(refundee1, { from: owner, value: amount }).should.be.rejectedWith(EVMRevert);
  41. });
  42. it('does not refund refundees', async function () {
  43. await this.escrow.withdraw(refundee1).should.be.rejectedWith(EVMRevert);
  44. });
  45. it('allows beneficiary withdrawal', async function () {
  46. const beneficiaryInitialBalance = await web3.eth.getBalance(beneficiary);
  47. await this.escrow.beneficiaryWithdraw();
  48. const beneficiaryFinalBalance = await web3.eth.getBalance(beneficiary);
  49. beneficiaryFinalBalance.sub(beneficiaryInitialBalance).should.be.bignumber.equal(amount * refundees.length);
  50. });
  51. });
  52. it('only owner can enter refund state', async function () {
  53. await this.escrow.enableRefunds({ from: beneficiary }).should.be.rejectedWith(EVMRevert);
  54. const receipt = await this.escrow.enableRefunds({ from: owner });
  55. await expectEvent.inLogs(receipt.logs, 'RefundsEnabled');
  56. });
  57. context('refund state', function () {
  58. beforeEach(async function () {
  59. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount })));
  60. await this.escrow.enableRefunds({ from: owner });
  61. });
  62. it('rejects deposits', async function () {
  63. await this.escrow.deposit(refundee1, { from: owner, value: amount }).should.be.rejectedWith(EVMRevert);
  64. });
  65. it('refunds refundees', async function () {
  66. for (let refundee of [refundee1, refundee2]) {
  67. const refundeeInitialBalance = await web3.eth.getBalance(refundee);
  68. await this.escrow.withdraw(refundee);
  69. const refundeeFinalBalance = await web3.eth.getBalance(refundee);
  70. refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount);
  71. }
  72. });
  73. it('does not allow beneficiary withdrawal', async function () {
  74. await this.escrow.beneficiaryWithdraw().should.be.rejectedWith(EVMRevert);
  75. });
  76. });
  77. });