balanceDifference tests and application (#1514)

* Added balanceDifference tests.

* Added balanceDifference tests.

* Now using balanceDifference in all (most) tests.

* Fixed typo.
pull/1526/head
Nicolás Venturo 6 years ago committed by GitHub
parent 5471fc808a
commit b9793abec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      test/crowdsale/AllowanceCrowdsale.test.js
  2. 16
      test/crowdsale/Crowdsale.test.js
  3. 9
      test/crowdsale/MintedCrowdsale.behavior.js
  4. 8
      test/crowdsale/RefundableCrowdsale.test.js
  5. 31
      test/examples/SampleCrowdsale.test.js
  6. 4
      test/helpers/balanceDifference.js
  7. 22
      test/helpers/test/balanceDifference.test.js
  8. 15
      test/payment/PullPayment.test.js
  9. 13
      test/payment/escrow/Escrow.behavior.js
  10. 18
      test/payment/escrow/RefundEscrow.test.js

@ -1,7 +1,7 @@
const expectEvent = require('../helpers/expectEvent');
const { ether } = require('../helpers/ether');
const shouldFail = require('../helpers/shouldFail');
const { ethGetBalance } = require('../helpers/web3');
const { balanceDifference } = require('../helpers/balanceDifference');
const { ZERO_ADDRESS } = require('../helpers/constants');
const BigNumber = web3.BigNumber;
@ -56,10 +56,9 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW
});
it('should forward funds to wallet', async function () {
const pre = await ethGetBalance(wallet);
await this.crowdsale.sendTransaction({ value, from: investor });
const post = await ethGetBalance(wallet);
post.minus(pre).should.be.bignumber.equal(value);
(await balanceDifference(wallet, () =>
this.crowdsale.sendTransaction({ value, from: investor }))
).should.be.bignumber.equal(value);
});
});

@ -1,7 +1,7 @@
const expectEvent = require('../helpers/expectEvent');
const shouldFail = require('../helpers/shouldFail');
const { balanceDifference } = require('../helpers/balanceDifference');
const { ether } = require('../helpers/ether');
const { ethGetBalance } = require('../helpers/web3');
const { ZERO_ADDRESS } = require('../helpers/constants');
const BigNumber = web3.BigNumber;
@ -97,10 +97,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
});
it('should forward funds to wallet', async function () {
const pre = await ethGetBalance(wallet);
await this.crowdsale.sendTransaction({ value, from: investor });
const post = await ethGetBalance(wallet);
post.minus(pre).should.be.bignumber.equal(value);
(await balanceDifference(wallet, () =>
this.crowdsale.sendTransaction({ value, from: investor }))
).should.be.bignumber.equal(value);
});
});
@ -121,10 +120,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
});
it('should forward funds to wallet', async function () {
const pre = await ethGetBalance(wallet);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
const post = await ethGetBalance(wallet);
post.minus(pre).should.be.bignumber.equal(value);
(await balanceDifference(wallet, () =>
this.crowdsale.buyTokens(investor, { value, from: purchaser }))
).should.be.bignumber.equal(value);
});
});
});

@ -1,5 +1,5 @@
const expectEvent = require('../helpers/expectEvent');
const { ethGetBalance } = require('../helpers/web3');
const { balanceDifference } = require('../helpers/balanceDifference');
const BigNumber = web3.BigNumber;
@ -35,10 +35,9 @@ function shouldBehaveLikeMintedCrowdsale ([_, investor, wallet, purchaser], rate
});
it('should forward funds to wallet', async function () {
const pre = await ethGetBalance(wallet);
await this.crowdsale.sendTransaction({ value, from: investor });
const post = await ethGetBalance(wallet);
post.minus(pre).should.be.bignumber.equal(value);
(await balanceDifference(wallet, () =>
this.crowdsale.sendTransaction({ value, from: investor }))
).should.be.bignumber.equal(value);
});
});
});

@ -1,5 +1,6 @@
const { ether } = require('../helpers/ether');
const { advanceBlock } = require('../helpers/advanceToBlock');
const { balanceDifference } = require('../helpers/balanceDifference');
const shouldFail = require('../helpers/shouldFail');
const time = require('../helpers/time');
const { ethGetBalance } = require('../helpers/web3');
@ -75,10 +76,9 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
});
it('refunds', async function () {
const pre = await ethGetBalance(investor);
await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
const post = await ethGetBalance(investor);
post.minus(pre).should.be.bignumber.equal(lessThanGoal);
(await balanceDifference(investor, () =>
this.crowdsale.claimRefund(investor, { gasPrice: 0 }))
).should.be.bignumber.equal(lessThanGoal);
});
});
});

@ -2,7 +2,7 @@ const { ether } = require('../helpers/ether');
const { advanceBlock } = require('../helpers/advanceToBlock');
const shouldFail = require('../helpers/shouldFail');
const time = require('../helpers/time');
const { ethGetBalance } = require('../helpers/web3');
const { balanceDifference } = require('../helpers/balanceDifference');
const BigNumber = web3.BigNumber;
@ -82,26 +82,21 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
await time.increaseTo(this.openingTime);
await this.crowdsale.send(GOAL);
const beforeFinalization = await ethGetBalance(wallet);
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner });
const afterFinalization = await ethGetBalance(wallet);
afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL);
(await balanceDifference(wallet, async () => {
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner });
})).should.be.bignumber.equal(GOAL);
});
it('should allow refunds if the goal is not reached', async function () {
const balanceBeforeInvestment = await ethGetBalance(investor);
await time.increaseTo(this.openingTime);
await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 });
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner });
await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
const balanceAfterRefund = await ethGetBalance(investor);
balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund);
(await balanceDifference(investor, async () => {
await time.increaseTo(this.openingTime);
await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 });
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner });
await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
})).should.be.bignumber.equal(0);
});
describe('when goal > cap', function () {

@ -1,6 +1,6 @@
async function balanceDifference (account, promise) {
async function balanceDifference (account, promiseFunc) {
const balanceBefore = web3.eth.getBalance(account);
await promise();
await promiseFunc();
const balanceAfter = web3.eth.getBalance(account);
return balanceAfter.minus(balanceBefore);
}

@ -0,0 +1,22 @@
const { balanceDifference } = require('../balanceDifference');
const { sendEther } = require('../sendTransaction');
const { ether } = require('../ether');
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
contract('balanceDifference', function ([sender, receiver]) {
it('returns balance increments', async function () {
(await balanceDifference(receiver, () =>
sendEther(sender, receiver, ether(1)))
).should.be.bignumber.equal(ether(1));
});
it('returns balance decrements', async function () {
(await balanceDifference(sender, () =>
sendEther(sender, receiver, ether(1)))
).should.be.bignumber.equal(ether(-1));
});
});

@ -1,4 +1,4 @@
const { ethGetBalance } = require('../helpers/web3');
const { balanceDifference } = require('../helpers/balanceDifference');
const { ether } = require('../helpers/ether');
const BigNumber = web3.BigNumber;
@ -37,16 +37,13 @@ contract('PullPayment', function ([_, payer, payee1, payee2]) {
});
it('can withdraw payment', async function () {
const initialBalance = await ethGetBalance(payee1);
(await balanceDifference(payee1, async () => {
await this.contract.callTransfer(payee1, amount, { from: payer });
(await this.contract.payments(payee1)).should.be.bignumber.equal(amount);
await this.contract.callTransfer(payee1, amount, { from: payer });
await this.contract.withdrawPayments(payee1);
})).should.be.bignumber.equal(amount);
(await this.contract.payments(payee1)).should.be.bignumber.equal(amount);
await this.contract.withdrawPayments(payee1);
(await this.contract.payments(payee1)).should.be.bignumber.equal(0);
const balance = await ethGetBalance(payee1);
Math.abs(balance - initialBalance - amount).should.be.lt(1e16);
});
});

@ -1,6 +1,7 @@
const expectEvent = require('../../helpers/expectEvent');
const shouldFail = require('../../helpers/shouldFail');
const { ethGetBalance } = require('../../helpers/web3');
const { balanceDifference } = require('../../helpers/balanceDifference');
const { ether } = require('../../helpers/ether');
const BigNumber = web3.BigNumber;
@ -61,17 +62,13 @@ function shouldBehaveLikeEscrow (primary, [payee1, payee2]) {
describe('withdrawals', async function () {
it('can withdraw payments', async function () {
const payeeInitialBalance = await ethGetBalance(payee1);
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.withdraw(payee1, { from: primary });
(await balanceDifference(payee1, async () => {
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.withdraw(payee1, { from: primary });
})).should.be.bignumber.equal(amount);
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0);
const payeeFinalBalance = await ethGetBalance(payee1);
payeeFinalBalance.sub(payeeInitialBalance).should.be.bignumber.equal(amount);
});
it('can do an empty withdrawal', async function () {

@ -1,6 +1,6 @@
const shouldFail = require('../../helpers/shouldFail');
const expectEvent = require('../../helpers/expectEvent');
const { ethGetBalance } = require('../../helpers/web3');
const { balanceDifference } = require('../../helpers/balanceDifference');
const { ether } = require('../../helpers/ether');
const { ZERO_ADDRESS } = require('../../helpers/constants');
@ -73,11 +73,9 @@ contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee
});
it('allows beneficiary withdrawal', async function () {
const beneficiaryInitialBalance = await ethGetBalance(beneficiary);
await this.escrow.beneficiaryWithdraw();
const beneficiaryFinalBalance = await ethGetBalance(beneficiary);
beneficiaryFinalBalance.sub(beneficiaryInitialBalance).should.be.bignumber.equal(amount * refundees.length);
(await balanceDifference(beneficiary, () =>
this.escrow.beneficiaryWithdraw()
)).should.be.bignumber.equal(amount * refundees.length);
});
it('prevents entering the refund state', async function () {
@ -109,11 +107,9 @@ contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee
it('refunds refundees', async function () {
for (const refundee of [refundee1, refundee2]) {
const refundeeInitialBalance = await ethGetBalance(refundee);
await this.escrow.withdraw(refundee, { from: primary });
const refundeeFinalBalance = await ethGetBalance(refundee);
refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount);
(await balanceDifference(refundee, () =>
this.escrow.withdraw(refundee, { from: primary }))
).should.be.bignumber.equal(amount);
}
});

Loading…
Cancel
Save