Escrow.behavior.js 3.7 KB

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