Escrow.behavior.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const expectEvent = require('../helpers/expectEvent');
  2. const { expectThrow } = require('../helpers/expectThrow');
  3. const { EVMRevert } = require('../helpers/EVMRevert');
  4. const { ethGetBalance } = require('../helpers/web3');
  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 expectThrow(this.escrow.deposit(payee1, { from: payee2 }), EVMRevert);
  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. const payeeInitialBalance = await ethGetBalance(payee1);
  49. await this.escrow.deposit(payee1, { from: primary, value: amount });
  50. await this.escrow.withdraw(payee1, { from: primary });
  51. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0);
  52. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0);
  53. const payeeFinalBalance = await ethGetBalance(payee1);
  54. payeeFinalBalance.sub(payeeInitialBalance).should.be.bignumber.equal(amount);
  55. });
  56. it('can do an empty withdrawal', async function () {
  57. await this.escrow.withdraw(payee1, { from: primary });
  58. });
  59. it('only the primary account can withdraw', async function () {
  60. await expectThrow(this.escrow.withdraw(payee1, { from: payee1 }), EVMRevert);
  61. });
  62. it('emits a withdrawn event', async function () {
  63. await this.escrow.deposit(payee1, { from: primary, value: amount });
  64. const { logs } = await this.escrow.withdraw(payee1, { from: primary });
  65. expectEvent.inLogs(logs, 'Withdrawn', {
  66. payee: payee1,
  67. weiAmount: amount,
  68. });
  69. });
  70. });
  71. });
  72. }
  73. module.exports = {
  74. shouldBehaveLikeEscrow,
  75. };