Escrow.behavior.js 3.3 KB

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