Escrow.behavior.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const expectEvent = require('../../helpers/expectEvent');
  2. const shouldFail = require('../../helpers/shouldFail');
  3. const { ethGetBalance } = require('../../helpers/web3');
  4. const { balanceDifference } = require('../../helpers/balanceDifference');
  5. const { ether } = require('../../helpers/ether');
  6. require('../../helpers/setup');
  7. function shouldBehaveLikeEscrow (primary, [payee1, payee2]) {
  8. const amount = ether(42.0);
  9. describe('as an escrow', function () {
  10. describe('deposits', function () {
  11. it('can accept a single deposit', async function () {
  12. await this.escrow.deposit(payee1, { from: primary, value: amount });
  13. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount);
  14. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
  15. });
  16. it('can accept an empty deposit', async function () {
  17. await this.escrow.deposit(payee1, { from: primary, value: 0 });
  18. });
  19. it('only the primary account can deposit', async function () {
  20. await shouldFail.reverting(this.escrow.deposit(payee1, { from: payee2 }));
  21. });
  22. it('emits a deposited event', async function () {
  23. const { logs } = await this.escrow.deposit(payee1, { from: primary, value: amount });
  24. expectEvent.inLogs(logs, 'Deposited', {
  25. payee: payee1,
  26. weiAmount: amount,
  27. });
  28. });
  29. it('can add multiple deposits on a single account', async function () {
  30. await this.escrow.deposit(payee1, { from: primary, value: amount });
  31. await this.escrow.deposit(payee1, { from: primary, value: amount * 2 });
  32. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
  33. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount * 3);
  34. });
  35. it('can track deposits to multiple accounts', async function () {
  36. await this.escrow.deposit(payee1, { from: primary, value: amount });
  37. await this.escrow.deposit(payee2, { from: primary, value: amount * 2 });
  38. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
  39. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
  40. (await this.escrow.depositsOf(payee2)).should.be.bignumber.equal(amount * 2);
  41. });
  42. });
  43. describe('withdrawals', async function () {
  44. it('can withdraw payments', async function () {
  45. (await balanceDifference(payee1, async () => {
  46. await this.escrow.deposit(payee1, { from: primary, value: amount });
  47. await this.escrow.withdraw(payee1, { from: primary });
  48. })).should.be.bignumber.equal(amount);
  49. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0);
  50. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0);
  51. });
  52. it('can do an empty withdrawal', async function () {
  53. await this.escrow.withdraw(payee1, { from: primary });
  54. });
  55. it('only the primary account can withdraw', async function () {
  56. await shouldFail.reverting(this.escrow.withdraw(payee1, { from: payee1 }));
  57. });
  58. it('emits a withdrawn event', async function () {
  59. await this.escrow.deposit(payee1, { from: primary, value: amount });
  60. const { logs } = await this.escrow.withdraw(payee1, { from: primary });
  61. expectEvent.inLogs(logs, 'Withdrawn', {
  62. payee: payee1,
  63. weiAmount: amount,
  64. });
  65. });
  66. });
  67. });
  68. }
  69. module.exports = {
  70. shouldBehaveLikeEscrow,
  71. };