Escrow.behavior.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 }), 'Ownable: caller is not the owner');
  17. });
  18. it('emits a deposited event', async function () {
  19. const receipt = await this.escrow.deposit(payee1, { from: owner, value: amount });
  20. expectEvent(receipt, 'Deposited', {
  21. payee: payee1,
  22. weiAmount: amount,
  23. });
  24. });
  25. it('can add multiple deposits on a single account', async function () {
  26. await this.escrow.deposit(payee1, { from: owner, value: amount });
  27. await this.escrow.deposit(payee1, { from: owner, value: amount.muln(2) });
  28. expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount.muln(3));
  29. expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount.muln(3));
  30. });
  31. it('can track deposits to multiple accounts', async function () {
  32. await this.escrow.deposit(payee1, { from: owner, value: amount });
  33. await this.escrow.deposit(payee2, { from: owner, value: amount.muln(2) });
  34. expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount.muln(3));
  35. expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount);
  36. expect(await this.escrow.depositsOf(payee2)).to.be.bignumber.equal(amount.muln(2));
  37. });
  38. });
  39. describe('withdrawals', async function () {
  40. it('can withdraw payments', async function () {
  41. const balanceTracker = await balance.tracker(payee1);
  42. await this.escrow.deposit(payee1, { from: owner, value: amount });
  43. await this.escrow.withdraw(payee1, { from: owner });
  44. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
  45. expect(await balance.current(this.escrow.address)).to.be.bignumber.equal('0');
  46. expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal('0');
  47. });
  48. it('can do an empty withdrawal', async function () {
  49. await this.escrow.withdraw(payee1, { from: owner });
  50. });
  51. it('only the owner can withdraw', async function () {
  52. await expectRevert(this.escrow.withdraw(payee1, { from: payee1 }), 'Ownable: caller is not the owner');
  53. });
  54. it('emits a withdrawn event', async function () {
  55. await this.escrow.deposit(payee1, { from: owner, value: amount });
  56. const receipt = await this.escrow.withdraw(payee1, { from: owner });
  57. expectEvent(receipt, 'Withdrawn', {
  58. payee: payee1,
  59. weiAmount: amount,
  60. });
  61. });
  62. });
  63. });
  64. }
  65. module.exports = {
  66. shouldBehaveLikeEscrow,
  67. };