Skip to content

Commit c80989b

Browse files
committed
Promisify web3.eth.getBalance function calls in tests (#999)
1 parent f18c3bc commit c80989b

12 files changed

+74
-54
lines changed

test/Bounty.test.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import toPromise from './helpers/toPromise';
12

23
let sendReward = function (sender, receiver, value) {
34
web3.eth.sendTransaction({
@@ -26,7 +27,8 @@ contract('Bounty', function (accounts) {
2627
let bounty = await SecureTargetBounty.new();
2728
sendReward(owner, bounty.address, reward);
2829

29-
assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
30+
const balance = await toPromise(web3.eth.getBalance)(bounty.address);
31+
assert.equal(reward, balance.toNumber());
3032
});
3133

3234
it('empties itself when destroyed', async function () {
@@ -35,10 +37,12 @@ contract('Bounty', function (accounts) {
3537
let bounty = await SecureTargetBounty.new();
3638
sendReward(owner, bounty.address, reward);
3739

38-
assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
40+
const balance = await toPromise(web3.eth.getBalance)(bounty.address);
41+
assert.equal(reward, balance.toNumber());
3942

4043
await bounty.destroy();
41-
assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
44+
const updatedBalance = await toPromise(web3.eth.getBalance)(bounty.address);
45+
assert.equal(0, updatedBalance.toNumber());
4246
});
4347

4448
describe('Against secure contract', function () {
@@ -56,8 +60,8 @@ contract('Bounty', function (accounts) {
5660
var targetAddress = result.args.createdAddress;
5761
sendReward(owner, bounty.address, reward);
5862

59-
assert.equal(reward,
60-
web3.eth.getBalance(bounty.address).toNumber());
63+
const balance = await toPromise(web3.eth.getBalance)(bounty.address);
64+
assert.equal(reward, balance.toNumber());
6165

6266
try {
6367
await bounty.claim(targetAddress, { from: researcher });
@@ -70,8 +74,8 @@ contract('Bounty', function (accounts) {
7074
await bounty.withdrawPayments({ from: researcher });
7175
assert.isTrue(false); // should never reach here
7276
} catch (err) {
73-
assert.equal(reward,
74-
web3.eth.getBalance(bounty.address).toNumber());
77+
const updatedBalance = await toPromise(web3.eth.getBalance)(bounty.address);
78+
assert.equal(reward, updatedBalance.toNumber());
7579
}
7680
};
7781
await bounty.createTarget({ from: researcher });
@@ -93,16 +97,17 @@ contract('Bounty', function (accounts) {
9397
let targetAddress = result.args.createdAddress;
9498
sendReward(owner, bounty.address, reward);
9599

96-
assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
100+
const balance = await toPromise(web3.eth.getBalance)(bounty.address);
101+
assert.equal(reward, balance.toNumber());
97102

98103
await bounty.claim(targetAddress, { from: researcher });
99104
let claim = await bounty.claimed.call();
100105

101106
assert.isTrue(claim);
102107

103108
await bounty.withdrawPayments({ from: researcher });
104-
105-
assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
109+
const updatedBalance = await toPromise(web3.eth.getBalance)(bounty.address);
110+
assert.equal(0, updatedBalance.toNumber());
106111
};
107112
await bounty.createTarget({ from: researcher });
108113
await awaitEvent(event, watcher);

test/LimitBalance.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assertRevert from './helpers/assertRevert';
2+
import toPromise from './helpers/toPromise';
23
var LimitBalanceMock = artifacts.require('LimitBalanceMock');
34

45
contract('LimitBalance', function (accounts) {
@@ -19,7 +20,8 @@ contract('LimitBalance', function (accounts) {
1920
let amount = 1;
2021
await lb.limitedDeposit({ value: amount });
2122

22-
assert.equal(web3.eth.getBalance(lb.address), amount);
23+
const balance = await toPromise(web3.eth.getBalance)(lb.address);
24+
assert.equal(balance, amount);
2325
});
2426

2527
it('shouldnt allow sending above limit', async function () {
@@ -31,17 +33,20 @@ contract('LimitBalance', function (accounts) {
3133
let amount = 500;
3234
await lb.limitedDeposit({ value: amount });
3335

34-
assert.equal(web3.eth.getBalance(lb.address), amount);
36+
const balance = await toPromise(web3.eth.getBalance)(lb.address);
37+
assert.equal(balance, amount);
3538

3639
await lb.limitedDeposit({ value: amount });
37-
assert.equal(web3.eth.getBalance(lb.address), amount * 2);
40+
const updatedBalance = await toPromise(web3.eth.getBalance)(lb.address);
41+
assert.equal(updatedBalance, amount * 2);
3842
});
3943

4044
it('shouldnt allow multiple sends above limit', async function () {
4145
let amount = 500;
4246
await lb.limitedDeposit({ value: amount });
4347

44-
assert.equal(web3.eth.getBalance(lb.address), amount);
48+
const balance = await toPromise(web3.eth.getBalance)(lb.address);
49+
assert.equal(balance, amount);
4550
await assertRevert(lb.limitedDeposit({ value: amount + 1 }));
4651
});
4752
});

test/SimpleSavingsWallet.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import expectThrow from './helpers/expectThrow';
3+
import toPromise from './helpers/toPromise';
34

45
const SimpleSavingsWallet = artifacts.require('SimpleSavingsWallet');
56

@@ -16,7 +17,8 @@ contract('SimpleSavingsWallet', function (accounts) {
1617

1718
it('should receive funds', async function () {
1819
await web3.eth.sendTransaction({ from: owner, to: savingsWallet.address, value: paymentAmount });
19-
assert.isTrue((new web3.BigNumber(paymentAmount)).equals(web3.eth.getBalance(savingsWallet.address)));
20+
const balance = await toPromise(web3.eth.getBalance)(savingsWallet.address);
21+
assert.isTrue((new web3.BigNumber(paymentAmount)).equals(balance));
2022
});
2123

2224
it('owner can send funds', async function () {
@@ -26,8 +28,9 @@ contract('SimpleSavingsWallet', function (accounts) {
2628
await expectThrow(savingsWallet.sendTo(savingsWallet.address, paymentAmount, { from: owner }));
2729
await expectThrow(savingsWallet.sendTo(accounts[1], 0, { from: owner }));
2830

29-
const balance = web3.eth.getBalance(accounts[1]);
31+
const balance = await toPromise(web3.eth.getBalance)(accounts[1]);
3032
await savingsWallet.sendTo(accounts[1], paymentAmount, { from: owner });
31-
assert.isTrue(balance.plus(paymentAmount).equals(web3.eth.getBalance(accounts[1])));
33+
const updatedBalance = await toPromise(web3.eth.getBalance)(accounts[1]);
34+
assert.isTrue(balance.plus(paymentAmount).equals(updatedBalance));
3235
});
3336
});

test/crowdsale/AllowanceCrowdsale.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ether from '../helpers/ether';
2+
import toPromise from '../helpers/toPromise';
23
import assertRevert from '../helpers/assertRevert';
34

45
const BigNumber = web3.BigNumber;
@@ -52,9 +53,9 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW
5253
});
5354

5455
it('should forward funds to wallet', async function () {
55-
const pre = web3.eth.getBalance(wallet);
56+
const pre = await toPromise(web3.eth.getBalance)(wallet);
5657
await this.crowdsale.sendTransaction({ value, from: investor });
57-
const post = web3.eth.getBalance(wallet);
58+
const post = await toPromise(web3.eth.getBalance)(wallet);
5859
post.minus(pre).should.be.bignumber.equal(value);
5960
});
6061
});

test/crowdsale/Crowdsale.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ether from '../helpers/ether';
2+
import toPromise from '../helpers/toPromise';
23

34
const BigNumber = web3.BigNumber;
45

@@ -47,9 +48,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
4748
});
4849

4950
it('should forward funds to wallet', async function () {
50-
const pre = web3.eth.getBalance(wallet);
51+
const pre = await toPromise(web3.eth.getBalance)(wallet);
5152
await this.crowdsale.sendTransaction({ value, from: investor });
52-
const post = web3.eth.getBalance(wallet);
53+
const post = await toPromise(web3.eth.getBalance)(wallet);
5354
post.minus(pre).should.be.bignumber.equal(value);
5455
});
5556
});
@@ -72,9 +73,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
7273
});
7374

7475
it('should forward funds to wallet', async function () {
75-
const pre = web3.eth.getBalance(wallet);
76+
const pre = await toPromise(web3.eth.getBalance)(wallet);
7677
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
77-
const post = web3.eth.getBalance(wallet);
78+
const post = await toPromise(web3.eth.getBalance)(wallet);
7879
post.minus(pre).should.be.bignumber.equal(value);
7980
});
8081
});

test/crowdsale/RefundVault.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ether from '../helpers/ether';
22
import EVMRevert from '../helpers/EVMRevert';
3+
import toPromise from '../helpers/toPromise';
34

45
const BigNumber = web3.BigNumber;
56

@@ -35,9 +36,9 @@ contract('RefundVault', function ([_, owner, wallet, investor]) {
3536
await this.vault.deposit(investor, { value, from: owner });
3637
await this.vault.enableRefunds({ from: owner });
3738

38-
const pre = web3.eth.getBalance(investor);
39+
const pre = await toPromise(web3.eth.getBalance)(investor);
3940
await this.vault.refund(investor);
40-
const post = web3.eth.getBalance(investor);
41+
const post = await toPromise(web3.eth.getBalance)(investor);
4142

4243
post.minus(pre).should.be.bignumber.equal(value);
4344
});
@@ -50,9 +51,9 @@ contract('RefundVault', function ([_, owner, wallet, investor]) {
5051
it('should forward funds to wallet after closing', async function () {
5152
await this.vault.deposit(investor, { value, from: owner });
5253

53-
const pre = web3.eth.getBalance(wallet);
54+
const pre = await toPromise(web3.eth.getBalance)(wallet);
5455
await this.vault.close({ from: owner });
55-
const post = web3.eth.getBalance(wallet);
56+
const post = await toPromise(web3.eth.getBalance)(wallet);
5657

5758
post.minus(pre).should.be.bignumber.equal(value);
5859
});

test/crowdsale/RefundableCrowdsale.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { advanceBlock } from '../helpers/advanceToBlock';
33
import { increaseTimeTo, duration } from '../helpers/increaseTime';
44
import latestTime from '../helpers/latestTime';
55
import EVMRevert from '../helpers/EVMRevert';
6+
import toPromise from '../helpers/toPromise';
67

78
const BigNumber = web3.BigNumber;
89

@@ -63,20 +64,20 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser
6364
await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
6465
await increaseTimeTo(this.afterClosingTime);
6566
await this.crowdsale.finalize({ from: owner });
66-
const pre = web3.eth.getBalance(investor);
67+
const pre = await toPromise(web3.eth.getBalance)(investor);
6768
await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 })
6869
.should.be.fulfilled;
69-
const post = web3.eth.getBalance(investor);
70+
const post = await toPromise(web3.eth.getBalance)(investor);
7071
post.minus(pre).should.be.bignumber.equal(lessThanGoal);
7172
});
7273

7374
it('should forward funds to wallet after end if goal was reached', async function () {
7475
await increaseTimeTo(this.openingTime);
7576
await this.crowdsale.sendTransaction({ value: goal, from: investor });
7677
await increaseTimeTo(this.afterClosingTime);
77-
const pre = web3.eth.getBalance(wallet);
78+
const pre = await toPromise(web3.eth.getBalance)(wallet);
7879
await this.crowdsale.finalize({ from: owner });
79-
const post = web3.eth.getBalance(wallet);
80+
const post = await toPromise(web3.eth.getBalance)(wallet);
8081
post.minus(pre).should.be.bignumber.equal(goal);
8182
});
8283
});

test/examples/SampleCrowdsale.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { increaseTimeTo, duration } from '../helpers/increaseTime';
44
import latestTime from '../helpers/latestTime';
55
import EVMRevert from '../helpers/EVMRevert';
66
import assertRevert from '../helpers/assertRevert';
7+
import toPromise from '../helpers/toPromise';
78

89
const BigNumber = web3.BigNumber;
910

@@ -91,16 +92,16 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
9192
await increaseTimeTo(this.openingTime);
9293
await this.crowdsale.send(GOAL);
9394

94-
const beforeFinalization = web3.eth.getBalance(wallet);
95+
const beforeFinalization = await toPromise(web3.eth.getBalance)(wallet);
9596
await increaseTimeTo(this.afterClosingTime);
9697
await this.crowdsale.finalize({ from: owner });
97-
const afterFinalization = web3.eth.getBalance(wallet);
98+
const afterFinalization = await toPromise(web3.eth.getBalance)(wallet);
9899

99100
afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL);
100101
});
101102

102103
it('should allow refunds if the goal is not reached', async function () {
103-
const balanceBeforeInvestment = web3.eth.getBalance(investor);
104+
const balanceBeforeInvestment = await toPromise(web3.eth.getBalance)(investor);
104105

105106
await increaseTimeTo(this.openingTime);
106107
await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 });
@@ -109,7 +110,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
109110
await this.crowdsale.finalize({ from: owner });
110111
await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 }).should.be.fulfilled;
111112

112-
const balanceAfterRefund = web3.eth.getBalance(investor);
113+
const balanceAfterRefund = await toPromise(web3.eth.getBalance)(investor);
113114
balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund);
114115
});
115116

test/lifecycle/Destructible.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
1+
import toPromise from '../helpers/toPromise';
22
var Destructible = artifacts.require('Destructible');
33
require('../helpers/transactionMined.js');
44

55
contract('Destructible', function (accounts) {
66
it('should send balance to owner after destruction', async function () {
77
let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') });
88
let owner = await destructible.owner();
9-
let initBalance = web3.eth.getBalance(owner);
9+
let initBalance = await toPromise(web3.eth.getBalance)(owner);
1010
await destructible.destroy({ from: owner });
11-
let newBalance = web3.eth.getBalance(owner);
11+
let newBalance = await toPromise(web3.eth.getBalance)(owner);
1212
assert.isTrue(newBalance > initBalance);
1313
});
1414

1515
it('should send balance to recepient after destruction', async function () {
1616
let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') });
1717
let owner = await destructible.owner();
18-
let initBalance = web3.eth.getBalance(accounts[1]);
18+
let initBalance = await toPromise(web3.eth.getBalance)(accounts[1]);
1919
await destructible.destroyAndSend(accounts[1], { from: owner });
20-
let newBalance = web3.eth.getBalance(accounts[1]);
20+
let newBalance = await toPromise(web3.eth.getBalance)(accounts[1]);
2121
assert.isTrue(newBalance.greaterThan(initBalance));
2222
});
2323
});

test/lifecycle/TokenDestructible.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import toPromise from '../helpers/toPromise';
22
var TokenDestructible = artifacts.require('TokenDestructible');
33
var StandardTokenMock = artifacts.require('StandardTokenMock');
44
require('../helpers/transactionMined.js');
@@ -15,9 +15,9 @@ contract('TokenDestructible', function (accounts) {
1515

1616
it('should send balance to owner after destruction', async function () {
1717
let owner = await destructible.owner();
18-
let initBalance = web3.eth.getBalance(owner);
18+
let initBalance = await toPromise(web3.eth.getBalance)(owner);
1919
await destructible.destroy([], { from: owner });
20-
let newBalance = web3.eth.getBalance(owner);
20+
let newBalance = await toPromise(web3.eth.getBalance)(owner);
2121
assert.isTrue(newBalance > initBalance);
2222
});
2323

0 commit comments

Comments
 (0)