Escrow.behavior.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { balance, ether, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. function shouldBehaveLikeEscrow (owner, [payee1, payee2]) {
  4. const amount = ether('42');
  5. describe('as an escrow', function () {
  6. describe('deposits', function () {
  7. it('can accept a single deposit', async function () {
  8. await this.escrow.deposit(payee1, { from: owner, value: amount });
  9. expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount);
  10. expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount);
  11. });
  12. it('can accept an empty deposit', async function () {
  13. await this.escrow.deposit(payee1, { from: owner, value: 0 });
  14. });
  15. it('only the owner can deposit', async function () {
  16. await expectRevert(this.escrow.deposit(payee1, { from: payee2 }),
  17. 'Ownable: caller is not the owner'
  18. );
  19. });
  20. it('emits a deposited event', async function () {
  21. const { logs } = await this.escrow.deposit(payee1, { from: owner, value: amount });
  22. expectEvent.inLogs(logs, 'Deposited', {
  23. payee: payee1,
  24. weiAmount: amount,
  25. });
  26. });
  27. it('can add multiple deposits on a single account', async function () {
  28. await this.escrow.deposit(payee1, { from: owner, value: amount });
  29. await this.escrow.deposit(payee1, { from: owner, value: amount.muln(2) });
  30. expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount.muln(3));
  31. expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount.muln(3));
  32. });
  33. it('can track deposits to multiple accounts', async function () {
  34. await this.escrow.deposit(payee1, { from: owner, value: amount });
  35. await this.escrow.deposit(payee2, { from: owner, value: amount.muln(2) });
  36. expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount.muln(3));
  37. expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount);
  38. expect(await this.escrow.depositsOf(payee2)).to.be.bignumber.equal(amount.muln(2));
  39. });
  40. });
  41. describe('withdrawals', async function () {
  42. it('can withdraw payments', async function () {
  43. const balanceTracker = await balance.tracker(payee1);
  44. await this.escrow.deposit(payee1, { from: owner, value: amount });
  45. await this.escrow.withdraw(payee1, { from: owner });
  46. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
  47. expect(await balance.current(this.escrow.address)).to.be.bignumber.equal('0');
  48. expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal('0');
  49. });
  50. it('can do an empty withdrawal', async function () {
  51. await this.escrow.withdraw(payee1, { from: owner });
  52. });
  53. it('only the owner can withdraw', async function () {
  54. await expectRevert(this.escrow.withdraw(payee1, { from: payee1 }),
  55. 'Ownable: caller is not the owner'
  56. );
  57. });
  58. it('emits a withdrawn event', async function () {
  59. await this.escrow.deposit(payee1, { from: owner, value: amount });
  60. const { logs } = await this.escrow.withdraw(payee1, { from: owner });
  61. expectEvent.inLogs(logs, 'Withdrawn', {
  62. payee: payee1,
  63. weiAmount: amount,
  64. });
  65. });
  66. });
  67. });
  68. }
  69. module.exports = {
  70. shouldBehaveLikeEscrow,
  71. };