PullPayment.test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const { ethGetBalance } = require('../helpers/web3');
  2. const BigNumber = web3.BigNumber;
  3. require('chai')
  4. .use(require('chai-bignumber')(BigNumber))
  5. .should();
  6. const PullPaymentMock = artifacts.require('PullPaymentMock');
  7. contract('PullPayment', function ([_, payer, payee1, payee2]) {
  8. const amount = web3.toWei(17.0, 'ether');
  9. beforeEach(async function () {
  10. this.contract = await PullPaymentMock.new({ value: amount });
  11. });
  12. it('can record an async payment correctly', async function () {
  13. await this.contract.callTransfer(payee1, 100, { from: payer });
  14. const paymentsToPayee1 = await this.contract.payments(payee1);
  15. paymentsToPayee1.should.be.bignumber.equal(100);
  16. });
  17. it('can add multiple balances on one account', async function () {
  18. await this.contract.callTransfer(payee1, 200, { from: payer });
  19. await this.contract.callTransfer(payee1, 300, { from: payer });
  20. const paymentsToPayee1 = await this.contract.payments(payee1);
  21. paymentsToPayee1.should.be.bignumber.equal(500);
  22. });
  23. it('can add balances on multiple accounts', async function () {
  24. await this.contract.callTransfer(payee1, 200, { from: payer });
  25. await this.contract.callTransfer(payee2, 300, { from: payer });
  26. const paymentsToPayee1 = await this.contract.payments(payee1);
  27. paymentsToPayee1.should.be.bignumber.equal(200);
  28. const paymentsToPayee2 = await this.contract.payments(payee2);
  29. paymentsToPayee2.should.be.bignumber.equal(300);
  30. });
  31. it('can withdraw payment', async function () {
  32. const initialBalance = await ethGetBalance(payee1);
  33. await this.contract.callTransfer(payee1, amount, { from: payer });
  34. const payment1 = await this.contract.payments(payee1);
  35. payment1.should.be.bignumber.equal(amount);
  36. await this.contract.withdrawPayments({ from: payee1 });
  37. const payment2 = await this.contract.payments(payee1);
  38. payment2.should.be.bignumber.equal(0);
  39. const balance = await ethGetBalance(payee1);
  40. Math.abs(balance - initialBalance - amount).should.be.lt(1e16);
  41. });
  42. });