Escrow.behaviour.js 3.9 KB

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