PullPayment.test.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. (await this.contract.payments(payee1)).should.be.bignumber.equal(100);
  15. });
  16. it('can add multiple balances on one account', async function () {
  17. await this.contract.callTransfer(payee1, 200, { from: payer });
  18. await this.contract.callTransfer(payee1, 300, { from: payer });
  19. (await this.contract.payments(payee1)).should.be.bignumber.equal(500);
  20. });
  21. it('can add balances on multiple accounts', async function () {
  22. await this.contract.callTransfer(payee1, 200, { from: payer });
  23. await this.contract.callTransfer(payee2, 300, { from: payer });
  24. (await this.contract.payments(payee1)).should.be.bignumber.equal(200);
  25. (await this.contract.payments(payee2)).should.be.bignumber.equal(300);
  26. });
  27. it('can withdraw payment', async function () {
  28. const initialBalance = await ethGetBalance(payee1);
  29. await this.contract.callTransfer(payee1, amount, { from: payer });
  30. (await this.contract.payments(payee1)).should.be.bignumber.equal(amount);
  31. await this.contract.withdrawPayments(payee1);
  32. (await this.contract.payments(payee1)).should.be.bignumber.equal(0);
  33. const balance = await ethGetBalance(payee1);
  34. Math.abs(balance - initialBalance - amount).should.be.lt(1e16);
  35. });
  36. });