PullPayment.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { balance, ether } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const PullPaymentMock = contract.fromArtifact('PullPaymentMock');
  5. describe('PullPayment', function () {
  6. const [ payer, payee1, payee2 ] = accounts;
  7. const amount = ether('17');
  8. beforeEach(async function () {
  9. this.contract = await PullPaymentMock.new({ value: amount });
  10. });
  11. it('can record an async payment correctly', async function () {
  12. await this.contract.callTransfer(payee1, 100, { from: payer });
  13. expect(await this.contract.payments(payee1)).to.be.bignumber.equal('100');
  14. });
  15. it('can add multiple balances on one account', async function () {
  16. await this.contract.callTransfer(payee1, 200, { from: payer });
  17. await this.contract.callTransfer(payee1, 300, { from: payer });
  18. expect(await this.contract.payments(payee1)).to.be.bignumber.equal('500');
  19. });
  20. it('can add balances on multiple accounts', async function () {
  21. await this.contract.callTransfer(payee1, 200, { from: payer });
  22. await this.contract.callTransfer(payee2, 300, { from: payer });
  23. expect(await this.contract.payments(payee1)).to.be.bignumber.equal('200');
  24. expect(await this.contract.payments(payee2)).to.be.bignumber.equal('300');
  25. });
  26. it('can withdraw payment', async function () {
  27. const balanceTracker = await balance.tracker(payee1);
  28. await this.contract.callTransfer(payee1, amount, { from: payer });
  29. expect(await this.contract.payments(payee1)).to.be.bignumber.equal(amount);
  30. await this.contract.withdrawPayments(payee1);
  31. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
  32. expect(await this.contract.payments(payee1)).to.be.bignumber.equal('0');
  33. });
  34. it('can withdraw payment forwarding all gas', async function () {
  35. const balanceTracker = await balance.tracker(payee1);
  36. await this.contract.callTransfer(payee1, amount, { from: payer });
  37. expect(await this.contract.payments(payee1)).to.be.bignumber.equal(amount);
  38. await this.contract.withdrawPaymentsWithGas(payee1);
  39. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
  40. expect(await this.contract.payments(payee1)).to.be.bignumber.equal('0');
  41. });
  42. });