Escrow.behavior.js 3.4 KB

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