Escrow.behavior.js 3.6 KB

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