Escrow.behavior.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-bignumber')(BigNumber))
  9. .should();
  10. function shouldBehaveLikeEscrow (primary, [payee1, payee2]) {
  11. const amount = ether(42.0);
  12. describe('as an escrow', function () {
  13. describe('deposits', function () {
  14. it('can accept a single deposit', async function () {
  15. await this.escrow.deposit(payee1, { from: primary, value: amount });
  16. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount);
  17. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
  18. });
  19. it('can accept an empty deposit', async function () {
  20. await this.escrow.deposit(payee1, { from: primary, value: 0 });
  21. });
  22. it('only the primary account can deposit', async function () {
  23. await shouldFail.reverting(this.escrow.deposit(payee1, { from: payee2 }));
  24. });
  25. it('emits a deposited event', async function () {
  26. const { logs } = await this.escrow.deposit(payee1, { from: primary, value: amount });
  27. expectEvent.inLogs(logs, 'Deposited', {
  28. payee: payee1,
  29. weiAmount: amount,
  30. });
  31. });
  32. it('can add multiple deposits on a single account', async function () {
  33. await this.escrow.deposit(payee1, { from: primary, value: amount });
  34. await this.escrow.deposit(payee1, { from: primary, value: amount * 2 });
  35. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
  36. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount * 3);
  37. });
  38. it('can track deposits to multiple accounts', async function () {
  39. await this.escrow.deposit(payee1, { from: primary, value: amount });
  40. await this.escrow.deposit(payee2, { from: primary, value: amount * 2 });
  41. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
  42. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
  43. (await this.escrow.depositsOf(payee2)).should.be.bignumber.equal(amount * 2);
  44. });
  45. });
  46. describe('withdrawals', async function () {
  47. it('can withdraw payments', async function () {
  48. (await balanceDifference(payee1, async () => {
  49. await this.escrow.deposit(payee1, { from: primary, value: amount });
  50. await this.escrow.withdraw(payee1, { from: primary });
  51. })).should.be.bignumber.equal(amount);
  52. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0);
  53. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0);
  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. };