PullPayment.test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\'t call asyncSend externally', async function () {
  13. assert.isUndefined(this.contract.asyncSend);
  14. });
  15. it('can record an async payment correctly', async function () {
  16. const AMOUNT = 100;
  17. await this.contract.callTransfer(payee1, AMOUNT, { from: payer });
  18. const paymentsToPayee1 = await this.contract.payments(payee1);
  19. paymentsToPayee1.should.be.bignumber.equal(AMOUNT);
  20. });
  21. it('can add multiple balances on one account', async function () {
  22. await this.contract.callTransfer(payee1, 200, { from: payer });
  23. await this.contract.callTransfer(payee1, 300, { from: payer });
  24. const paymentsToPayee1 = await this.contract.payments(payee1);
  25. paymentsToPayee1.should.be.bignumber.equal(500);
  26. });
  27. it('can add balances on multiple accounts', async function () {
  28. await this.contract.callTransfer(payee1, 200, { from: payer });
  29. await this.contract.callTransfer(payee2, 300, { from: payer });
  30. const paymentsToPayee1 = await this.contract.payments(payee1);
  31. paymentsToPayee1.should.be.bignumber.equal(200);
  32. const paymentsToPayee2 = await this.contract.payments(payee2);
  33. paymentsToPayee2.should.be.bignumber.equal(300);
  34. });
  35. it('can withdraw payment', async function () {
  36. const initialBalance = await ethGetBalance(payee1);
  37. await this.contract.callTransfer(payee1, amount, { from: payer });
  38. const payment1 = await this.contract.payments(payee1);
  39. payment1.should.be.bignumber.equal(amount);
  40. await this.contract.withdrawPayments({ from: payee1 });
  41. const payment2 = await this.contract.payments(payee1);
  42. payment2.should.be.bignumber.equal(0);
  43. const balance = await ethGetBalance(payee1);
  44. Math.abs(balance - initialBalance - amount).should.be.lt(1e16);
  45. });
  46. });