Escrow.behaviour.js 4.0 KB

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