Escrow.behavior.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 receipt = await this.escrow.deposit(payee1, { from: primary, value: amount });
  27. const event = expectEvent.inLogs(receipt.logs, 'Deposited', { payee: payee1 });
  28. event.args.weiAmount.should.be.bignumber.equal(amount);
  29. });
  30. it('can add multiple deposits on a single account', async function () {
  31. await this.escrow.deposit(payee1, { from: primary, value: amount });
  32. await this.escrow.deposit(payee1, { from: primary, value: amount * 2 });
  33. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
  34. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount * 3);
  35. });
  36. it('can track deposits to multiple accounts', async function () {
  37. await this.escrow.deposit(payee1, { from: primary, value: amount });
  38. await this.escrow.deposit(payee2, { from: primary, value: amount * 2 });
  39. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
  40. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
  41. (await this.escrow.depositsOf(payee2)).should.be.bignumber.equal(amount * 2);
  42. });
  43. });
  44. describe('withdrawals', async function () {
  45. it('can withdraw payments', async function () {
  46. const payeeInitialBalance = await ethGetBalance(payee1);
  47. await this.escrow.deposit(payee1, { from: primary, value: amount });
  48. await this.escrow.withdraw(payee1, { from: primary });
  49. (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0);
  50. (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0);
  51. const payeeFinalBalance = await ethGetBalance(payee1);
  52. payeeFinalBalance.sub(payeeInitialBalance).should.be.bignumber.equal(amount);
  53. });
  54. it('can do an empty withdrawal', async function () {
  55. await this.escrow.withdraw(payee1, { from: primary });
  56. });
  57. it('only the primary account can withdraw', async function () {
  58. await expectThrow(this.escrow.withdraw(payee1, { from: payee1 }), EVMRevert);
  59. });
  60. it('emits a withdrawn event', async function () {
  61. await this.escrow.deposit(payee1, { from: primary, value: amount });
  62. const receipt = await this.escrow.withdraw(payee1, { from: primary });
  63. const event = expectEvent.inLogs(receipt.logs, 'Withdrawn', { payee: payee1 });
  64. event.args.weiAmount.should.be.bignumber.equal(amount);
  65. });
  66. });
  67. });
  68. }
  69. module.exports = {
  70. shouldBehaveLikeEscrow,
  71. };