Escrow.behaviour.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import expectEvent from '../helpers/expectEvent';
  2. import EVMRevert from '../helpers/EVMRevert';
  3. const BigNumber = web3.BigNumber;
  4. require('chai')
  5. .use(require('chai-bignumber')(BigNumber))
  6. .should();
  7. export default function (owner, [payee1, payee2]) {
  8. const amount = web3.toWei(42.0, 'ether');
  9. describe('as an escrow', function () {
  10. describe('deposits', function () {
  11. it('can accept a single deposit', async function () {
  12. await this.escrow.deposit(payee1, { from: owner, value: amount });
  13. const balance = await web3.eth.getBalance(this.escrow.address);
  14. const deposit = await this.escrow.depositsOf(payee1);
  15. balance.should.be.bignumber.equal(amount);
  16. deposit.should.be.bignumber.equal(amount);
  17. });
  18. it('can accept an empty deposit', async function () {
  19. await this.escrow.deposit(payee1, { from: owner, value: 0 });
  20. });
  21. it('only the owner can deposit', async function () {
  22. await this.escrow.deposit(payee1, { from: payee2 }).should.be.rejectedWith(EVMRevert);
  23. });
  24. it('emits a deposited event', async function () {
  25. const receipt = await this.escrow.deposit(payee1, { from: owner, value: amount });
  26. const event = await 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: owner, value: amount });
  31. await this.escrow.deposit(payee1, { from: owner, value: amount * 2 });
  32. const balance = await web3.eth.getBalance(this.escrow.address);
  33. const deposit = await this.escrow.depositsOf(payee1);
  34. balance.should.be.bignumber.equal(amount * 3);
  35. deposit.should.be.bignumber.equal(amount * 3);
  36. });
  37. it('can track deposits to multiple accounts', async function () {
  38. await this.escrow.deposit(payee1, { from: owner, value: amount });
  39. await this.escrow.deposit(payee2, { from: owner, value: amount * 2 });
  40. const balance = await web3.eth.getBalance(this.escrow.address);
  41. const depositPayee1 = await this.escrow.depositsOf(payee1);
  42. const depositPayee2 = await this.escrow.depositsOf(payee2);
  43. balance.should.be.bignumber.equal(amount * 3);
  44. depositPayee1.should.be.bignumber.equal(amount);
  45. depositPayee2.should.be.bignumber.equal(amount * 2);
  46. });
  47. });
  48. describe('withdrawals', async function () {
  49. it('can withdraw payments', async function () {
  50. const payeeInitialBalance = await web3.eth.getBalance(payee1);
  51. await this.escrow.deposit(payee1, { from: owner, value: amount });
  52. await this.escrow.withdraw(payee1, { from: owner });
  53. const escrowBalance = await web3.eth.getBalance(this.escrow.address);
  54. const finalDeposit = await this.escrow.depositsOf(payee1);
  55. const payeeFinalBalance = await web3.eth.getBalance(payee1);
  56. escrowBalance.should.be.bignumber.equal(0);
  57. finalDeposit.should.be.bignumber.equal(0);
  58. payeeFinalBalance.sub(payeeInitialBalance).should.be.bignumber.equal(amount);
  59. });
  60. it('can do an empty withdrawal', async function () {
  61. await this.escrow.withdraw(payee1, { from: owner });
  62. });
  63. it('only the owner can withdraw', async function () {
  64. await this.escrow.withdraw(payee1, { from: payee1 }).should.be.rejectedWith(EVMRevert);
  65. });
  66. it('emits a withdrawn event', async function () {
  67. await this.escrow.deposit(payee1, { from: owner, value: amount });
  68. const receipt = await this.escrow.withdraw(payee1, { from: owner });
  69. const event = await expectEvent.inLogs(receipt.logs, 'Withdrawn', { payee: payee1 });
  70. event.args.weiAmount.should.be.bignumber.equal(amount);
  71. });
  72. });
  73. });
  74. };