From 40b5594f52fce22f6c9190e8e45ccb3cab624783 Mon Sep 17 00:00:00 2001 From: Arun Kumar Date: Tue, 10 Jul 2018 16:01:51 -0500 Subject: [PATCH] Promisify web3 sync requests in tests (#1009) --- test/Bounty.test.js | 43 ++++++++++--------- test/LimitBalance.test.js | 14 ++++-- test/SimpleSavingsWallet.test.js | 13 +++--- test/crowdsale/AllowanceCrowdsale.test.js | 6 ++- test/crowdsale/Crowdsale.test.js | 9 ++-- test/crowdsale/FinalizableCrowdsale.test.js | 2 +- .../IncreasingPriceCrowdsale.test.js | 2 +- test/crowdsale/MintedCrowdsale.behaviour.js | 6 ++- test/crowdsale/PostDeliveryCrowdsale.test.js | 2 +- test/crowdsale/RefundableCrowdsale.test.js | 11 ++--- test/crowdsale/TimedCrowdsale.test.js | 2 +- test/examples/SampleCrowdsale.test.js | 11 ++--- test/examples/SimpleToken.test.js | 2 +- test/helpers/increaseTime.js | 4 +- test/helpers/latestTime.js | 7 ++- test/helpers/toPromise.js | 4 -- test/helpers/web3.js | 7 +++ test/lifecycle/Destructible.test.js | 11 ++--- test/lifecycle/TokenDestructible.test.js | 5 ++- test/ownership/HasNoEther.test.js | 18 ++++---- test/payment/Escrow.behaviour.js | 13 +++--- test/payment/PullPayment.test.js | 6 ++- test/payment/RefundEscrow.test.js | 9 ++-- test/payment/SplitPayment.test.js | 26 +++++------ test/token/ERC20/TokenTimelock.test.js | 2 +- test/token/ERC20/TokenVesting.test.js | 6 ++- 26 files changed, 137 insertions(+), 104 deletions(-) delete mode 100644 test/helpers/toPromise.js create mode 100644 test/helpers/web3.js diff --git a/test/Bounty.test.js b/test/Bounty.test.js index b440f399c..0fd768be4 100644 --- a/test/Bounty.test.js +++ b/test/Bounty.test.js @@ -1,11 +1,8 @@ +import { ethGetBalance, ethSendTransaction } from './helpers/web3'; -let sendReward = function (sender, receiver, value) { - web3.eth.sendTransaction({ - from: sender, - to: receiver, - value: value, - }); -}; +const sendReward = async (from, to, value) => ethSendTransaction({ + from, to, value, +}); var SecureTargetBounty = artifacts.require('SecureTargetBounty'); var InsecureTargetBounty = artifacts.require('InsecureTargetBounty'); @@ -24,21 +21,24 @@ contract('Bounty', function (accounts) { let owner = accounts[0]; let reward = web3.toWei(1, 'ether'); let bounty = await SecureTargetBounty.new(); - sendReward(owner, bounty.address, reward); + await sendReward(owner, bounty.address, reward); - assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber()); + const balance = await ethGetBalance(bounty.address); + assert.equal(reward, balance.toNumber()); }); it('empties itself when destroyed', async function () { let owner = accounts[0]; let reward = web3.toWei(1, 'ether'); let bounty = await SecureTargetBounty.new(); - sendReward(owner, bounty.address, reward); + await sendReward(owner, bounty.address, reward); - assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber()); + const balance = await ethGetBalance(bounty.address); + assert.equal(reward, balance.toNumber()); await bounty.destroy(); - assert.equal(0, web3.eth.getBalance(bounty.address).toNumber()); + const updatedBalance = await ethGetBalance(bounty.address); + assert.equal(0, updatedBalance.toNumber()); }); describe('Against secure contract', function () { @@ -54,10 +54,10 @@ contract('Bounty', function (accounts) { if (err) { throw err; } var targetAddress = result.args.createdAddress; - sendReward(owner, bounty.address, reward); + await sendReward(owner, bounty.address, reward); - assert.equal(reward, - web3.eth.getBalance(bounty.address).toNumber()); + const balance = await ethGetBalance(bounty.address); + assert.equal(reward, balance.toNumber()); try { await bounty.claim(targetAddress, { from: researcher }); @@ -70,8 +70,8 @@ contract('Bounty', function (accounts) { await bounty.withdrawPayments({ from: researcher }); assert.isTrue(false); // should never reach here } catch (err) { - assert.equal(reward, - web3.eth.getBalance(bounty.address).toNumber()); + const updatedBalance = await ethGetBalance(bounty.address); + assert.equal(reward, updatedBalance.toNumber()); } }; await bounty.createTarget({ from: researcher }); @@ -91,9 +91,10 @@ contract('Bounty', function (accounts) { event.stopWatching(); if (err) { throw err; } let targetAddress = result.args.createdAddress; - sendReward(owner, bounty.address, reward); + await sendReward(owner, bounty.address, reward); - assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber()); + const balance = await ethGetBalance(bounty.address); + assert.equal(reward, balance.toNumber()); await bounty.claim(targetAddress, { from: researcher }); let claim = await bounty.claimed.call(); @@ -101,8 +102,8 @@ contract('Bounty', function (accounts) { assert.isTrue(claim); await bounty.withdrawPayments({ from: researcher }); - - assert.equal(0, web3.eth.getBalance(bounty.address).toNumber()); + const updatedBalance = await ethGetBalance(bounty.address); + assert.equal(0, updatedBalance.toNumber()); }; await bounty.createTarget({ from: researcher }); await awaitEvent(event, watcher); diff --git a/test/LimitBalance.test.js b/test/LimitBalance.test.js index 9df8b0dc8..6c91f7555 100644 --- a/test/LimitBalance.test.js +++ b/test/LimitBalance.test.js @@ -1,4 +1,6 @@ import assertRevert from './helpers/assertRevert'; +import { ethGetBalance } from './helpers/web3'; + var LimitBalanceMock = artifacts.require('LimitBalanceMock'); contract('LimitBalance', function (accounts) { @@ -19,7 +21,8 @@ contract('LimitBalance', function (accounts) { let amount = 1; await lb.limitedDeposit({ value: amount }); - assert.equal(web3.eth.getBalance(lb.address), amount); + const balance = await ethGetBalance(lb.address); + assert.equal(balance, amount); }); it('shouldnt allow sending above limit', async function () { @@ -31,17 +34,20 @@ contract('LimitBalance', function (accounts) { let amount = 500; await lb.limitedDeposit({ value: amount }); - assert.equal(web3.eth.getBalance(lb.address), amount); + const balance = await ethGetBalance(lb.address); + assert.equal(balance, amount); await lb.limitedDeposit({ value: amount }); - assert.equal(web3.eth.getBalance(lb.address), amount * 2); + const updatedBalance = await ethGetBalance(lb.address); + assert.equal(updatedBalance, amount * 2); }); it('shouldnt allow multiple sends above limit', async function () { let amount = 500; await lb.limitedDeposit({ value: amount }); - assert.equal(web3.eth.getBalance(lb.address), amount); + const balance = await ethGetBalance(lb.address); + assert.equal(balance, amount); await assertRevert(lb.limitedDeposit({ value: amount + 1 })); }); }); diff --git a/test/SimpleSavingsWallet.test.js b/test/SimpleSavingsWallet.test.js index 07787c2de..20a6a4cae 100644 --- a/test/SimpleSavingsWallet.test.js +++ b/test/SimpleSavingsWallet.test.js @@ -1,5 +1,6 @@ import expectThrow from './helpers/expectThrow'; +import { ethGetBalance, ethSendTransaction } from './helpers/web3'; const SimpleSavingsWallet = artifacts.require('SimpleSavingsWallet'); @@ -15,19 +16,21 @@ contract('SimpleSavingsWallet', function (accounts) { }); it('should receive funds', async function () { - await web3.eth.sendTransaction({ from: owner, to: savingsWallet.address, value: paymentAmount }); - assert.isTrue((new web3.BigNumber(paymentAmount)).equals(web3.eth.getBalance(savingsWallet.address))); + await ethSendTransaction({ from: owner, to: savingsWallet.address, value: paymentAmount }); + const balance = await ethGetBalance(savingsWallet.address); + assert.isTrue((new web3.BigNumber(paymentAmount)).equals(balance)); }); it('owner can send funds', async function () { // Receive payment so we have some money to spend. - await web3.eth.sendTransaction({ from: accounts[9], to: savingsWallet.address, value: 1000000 }); + await ethSendTransaction({ from: accounts[9], to: savingsWallet.address, value: 1000000 }); await expectThrow(savingsWallet.sendTo(0, paymentAmount, { from: owner })); await expectThrow(savingsWallet.sendTo(savingsWallet.address, paymentAmount, { from: owner })); await expectThrow(savingsWallet.sendTo(accounts[1], 0, { from: owner })); - const balance = web3.eth.getBalance(accounts[1]); + const balance = await ethGetBalance(accounts[1]); await savingsWallet.sendTo(accounts[1], paymentAmount, { from: owner }); - assert.isTrue(balance.plus(paymentAmount).equals(web3.eth.getBalance(accounts[1]))); + const updatedBalance = await ethGetBalance(accounts[1]); + assert.isTrue(balance.plus(paymentAmount).equals(updatedBalance)); }); }); diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index 44799010a..e785b2ca5 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -1,5 +1,7 @@ + import ether from '../helpers/ether'; import assertRevert from '../helpers/assertRevert'; +import { ethGetBalance } from '../helpers/web3'; const BigNumber = web3.BigNumber; @@ -52,9 +54,9 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW }); it('should forward funds to wallet', async function () { - const pre = web3.eth.getBalance(wallet); + const pre = await ethGetBalance(wallet); await this.crowdsale.sendTransaction({ value, from: investor }); - const post = web3.eth.getBalance(wallet); + const post = await ethGetBalance(wallet); post.minus(pre).should.be.bignumber.equal(value); }); }); diff --git a/test/crowdsale/Crowdsale.test.js b/test/crowdsale/Crowdsale.test.js index 4053dca37..d2b3af46b 100644 --- a/test/crowdsale/Crowdsale.test.js +++ b/test/crowdsale/Crowdsale.test.js @@ -1,4 +1,5 @@ import ether from '../helpers/ether'; +import { ethGetBalance } from '../helpers/web3'; const BigNumber = web3.BigNumber; @@ -47,9 +48,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }); it('should forward funds to wallet', async function () { - const pre = web3.eth.getBalance(wallet); + const pre = await ethGetBalance(wallet); await this.crowdsale.sendTransaction({ value, from: investor }); - const post = web3.eth.getBalance(wallet); + const post = await ethGetBalance(wallet); post.minus(pre).should.be.bignumber.equal(value); }); }); @@ -72,9 +73,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }); it('should forward funds to wallet', async function () { - const pre = web3.eth.getBalance(wallet); + const pre = await ethGetBalance(wallet); await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - const post = web3.eth.getBalance(wallet); + const post = await ethGetBalance(wallet); post.minus(pre).should.be.bignumber.equal(value); }); }); diff --git a/test/crowdsale/FinalizableCrowdsale.test.js b/test/crowdsale/FinalizableCrowdsale.test.js index 9a6c16a6f..1a8b058a6 100644 --- a/test/crowdsale/FinalizableCrowdsale.test.js +++ b/test/crowdsale/FinalizableCrowdsale.test.js @@ -22,7 +22,7 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { }); beforeEach(async function () { - this.openingTime = latestTime() + duration.weeks(1); + this.openingTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); diff --git a/test/crowdsale/IncreasingPriceCrowdsale.test.js b/test/crowdsale/IncreasingPriceCrowdsale.test.js index 76f82ab02..680564b50 100644 --- a/test/crowdsale/IncreasingPriceCrowdsale.test.js +++ b/test/crowdsale/IncreasingPriceCrowdsale.test.js @@ -30,7 +30,7 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) beforeEach(async function () { await advanceBlock(); - this.startTime = latestTime() + duration.weeks(1); + this.startTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.startTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); this.token = await SimpleToken.new(); diff --git a/test/crowdsale/MintedCrowdsale.behaviour.js b/test/crowdsale/MintedCrowdsale.behaviour.js index 91861364c..42bd95417 100644 --- a/test/crowdsale/MintedCrowdsale.behaviour.js +++ b/test/crowdsale/MintedCrowdsale.behaviour.js @@ -1,3 +1,5 @@ +import { ethGetBalance } from '../helpers/web3'; + const BigNumber = web3.BigNumber; const should = require('chai') @@ -34,9 +36,9 @@ export default function ([_, investor, wallet, purchaser], rate, value) { }); it('should forward funds to wallet', async function () { - const pre = web3.eth.getBalance(wallet); + const pre = await ethGetBalance(wallet); await this.crowdsale.sendTransaction({ value, from: investor }); - const post = web3.eth.getBalance(wallet); + const post = await ethGetBalance(wallet); post.minus(pre).should.be.bignumber.equal(value); }); }); diff --git a/test/crowdsale/PostDeliveryCrowdsale.test.js b/test/crowdsale/PostDeliveryCrowdsale.test.js index 1860a8dea..c0d5a5436 100644 --- a/test/crowdsale/PostDeliveryCrowdsale.test.js +++ b/test/crowdsale/PostDeliveryCrowdsale.test.js @@ -25,7 +25,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { }); beforeEach(async function () { - this.openingTime = latestTime() + duration.weeks(1); + this.openingTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1); this.beforeEndTime = this.closingTime - duration.hours(1); this.afterClosingTime = this.closingTime + duration.seconds(1); diff --git a/test/crowdsale/RefundableCrowdsale.test.js b/test/crowdsale/RefundableCrowdsale.test.js index 551e65c98..6b0f5855d 100644 --- a/test/crowdsale/RefundableCrowdsale.test.js +++ b/test/crowdsale/RefundableCrowdsale.test.js @@ -3,6 +3,7 @@ import { advanceBlock } from '../helpers/advanceToBlock'; import { increaseTimeTo, duration } from '../helpers/increaseTime'; import latestTime from '../helpers/latestTime'; import EVMRevert from '../helpers/EVMRevert'; +import { ethGetBalance } from '../helpers/web3'; const BigNumber = web3.BigNumber; @@ -26,7 +27,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser }); beforeEach(async function () { - this.openingTime = latestTime() + duration.weeks(1); + this.openingTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); @@ -63,10 +64,10 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor }); await increaseTimeTo(this.afterClosingTime); await this.crowdsale.finalize({ from: owner }); - const pre = web3.eth.getBalance(investor); + const pre = await ethGetBalance(investor); await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 }) .should.be.fulfilled; - const post = web3.eth.getBalance(investor); + const post = await ethGetBalance(investor); post.minus(pre).should.be.bignumber.equal(lessThanGoal); }); @@ -74,9 +75,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser await increaseTimeTo(this.openingTime); await this.crowdsale.sendTransaction({ value: goal, from: investor }); await increaseTimeTo(this.afterClosingTime); - const pre = web3.eth.getBalance(wallet); + const pre = await ethGetBalance(wallet); await this.crowdsale.finalize({ from: owner }); - const post = web3.eth.getBalance(wallet); + const post = await ethGetBalance(wallet); post.minus(pre).should.be.bignumber.equal(goal); }); }); diff --git a/test/crowdsale/TimedCrowdsale.test.js b/test/crowdsale/TimedCrowdsale.test.js index ad54792a4..c3062a73b 100644 --- a/test/crowdsale/TimedCrowdsale.test.js +++ b/test/crowdsale/TimedCrowdsale.test.js @@ -25,7 +25,7 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) { }); beforeEach(async function () { - this.openingTime = latestTime() + duration.weeks(1); + this.openingTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); this.token = await SimpleToken.new(); diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index 7daa48688..2e56f07b5 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -4,6 +4,7 @@ import { increaseTimeTo, duration } from '../helpers/increaseTime'; import latestTime from '../helpers/latestTime'; import EVMRevert from '../helpers/EVMRevert'; import assertRevert from '../helpers/assertRevert'; +import { ethGetBalance } from '../helpers/web3'; const BigNumber = web3.BigNumber; @@ -26,7 +27,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) { }); beforeEach(async function () { - this.openingTime = latestTime() + duration.weeks(1); + this.openingTime = (await latestTime()) + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1); this.afterClosingTime = this.closingTime + duration.seconds(1); @@ -88,16 +89,16 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) { await increaseTimeTo(this.openingTime); await this.crowdsale.send(GOAL); - const beforeFinalization = web3.eth.getBalance(wallet); + const beforeFinalization = await ethGetBalance(wallet); await increaseTimeTo(this.afterClosingTime); await this.crowdsale.finalize({ from: owner }); - const afterFinalization = web3.eth.getBalance(wallet); + const afterFinalization = await ethGetBalance(wallet); afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL); }); it('should allow refunds if the goal is not reached', async function () { - const balanceBeforeInvestment = web3.eth.getBalance(investor); + const balanceBeforeInvestment = await ethGetBalance(investor); await increaseTimeTo(this.openingTime); await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 }); @@ -106,7 +107,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) { await this.crowdsale.finalize({ from: owner }); await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 }).should.be.fulfilled; - const balanceAfterRefund = web3.eth.getBalance(investor); + const balanceAfterRefund = await ethGetBalance(investor); balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund); }); diff --git a/test/examples/SimpleToken.test.js b/test/examples/SimpleToken.test.js index 3d173ca35..7f81c438a 100644 --- a/test/examples/SimpleToken.test.js +++ b/test/examples/SimpleToken.test.js @@ -30,7 +30,7 @@ contract('SimpleToken', accounts => { assert(creatorBalance.eq(totalSupply)); - const receipt = web3.eth.getTransactionReceipt(token.transactionHash); + const receipt = await web3.eth.getTransactionReceipt(token.transactionHash); const logs = decodeLogs(receipt.logs, SimpleToken, token.address); assert.equal(logs.length, 1); assert.equal(logs[0].event, 'Transfer'); diff --git a/test/helpers/increaseTime.js b/test/helpers/increaseTime.js index 93b880523..8eb39459a 100644 --- a/test/helpers/increaseTime.js +++ b/test/helpers/increaseTime.js @@ -31,8 +31,8 @@ export default function increaseTime (duration) { * * @param target time in seconds */ -export function increaseTimeTo (target) { - let now = latestTime(); +export async function increaseTimeTo (target) { + let now = (await latestTime()); if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`); let diff = target - now; return increaseTime(diff); diff --git a/test/helpers/latestTime.js b/test/helpers/latestTime.js index 219b54c74..1b975f9ce 100644 --- a/test/helpers/latestTime.js +++ b/test/helpers/latestTime.js @@ -1,4 +1,7 @@ +import { ethGetBlock } from './web3'; + // Returns the time of the last mined block in seconds -export default function latestTime () { - return web3.eth.getBlock('latest').timestamp; +export default async function latestTime () { + const block = await ethGetBlock('latest'); + return block.timestamp; } diff --git a/test/helpers/toPromise.js b/test/helpers/toPromise.js deleted file mode 100644 index 2e9bcfbd3..000000000 --- a/test/helpers/toPromise.js +++ /dev/null @@ -1,4 +0,0 @@ -export default func => - (...args) => - new Promise((resolve, reject) => - func(...args, (error, data) => error ? reject(error) : resolve(data))); diff --git a/test/helpers/web3.js b/test/helpers/web3.js new file mode 100644 index 000000000..b98f94817 --- /dev/null +++ b/test/helpers/web3.js @@ -0,0 +1,7 @@ +const pify = require('pify'); + +const ethAsync = pify(web3.eth); + +export const ethGetBalance = ethAsync.getBalance; +export const ethSendTransaction = ethAsync.sendTransaction; +export const ethGetBlock = ethAsync.getBlock; diff --git a/test/lifecycle/Destructible.test.js b/test/lifecycle/Destructible.test.js index 2757d76cd..c70bf0094 100644 --- a/test/lifecycle/Destructible.test.js +++ b/test/lifecycle/Destructible.test.js @@ -1,23 +1,24 @@ +import { ethGetBalance } from '../helpers/web3'; -var Destructible = artifacts.require('Destructible'); +const Destructible = artifacts.require('Destructible'); require('../helpers/transactionMined.js'); contract('Destructible', function (accounts) { it('should send balance to owner after destruction', async function () { let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') }); let owner = await destructible.owner(); - let initBalance = web3.eth.getBalance(owner); + let initBalance = await ethGetBalance(owner); await destructible.destroy({ from: owner }); - let newBalance = web3.eth.getBalance(owner); + let newBalance = await ethGetBalance(owner); assert.isTrue(newBalance > initBalance); }); it('should send balance to recepient after destruction', async function () { let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') }); let owner = await destructible.owner(); - let initBalance = web3.eth.getBalance(accounts[1]); + let initBalance = await ethGetBalance(accounts[1]); await destructible.destroyAndSend(accounts[1], { from: owner }); - let newBalance = web3.eth.getBalance(accounts[1]); + let newBalance = await ethGetBalance(accounts[1]); assert.isTrue(newBalance.greaterThan(initBalance)); }); }); diff --git a/test/lifecycle/TokenDestructible.test.js b/test/lifecycle/TokenDestructible.test.js index 8566bf190..509c06c09 100644 --- a/test/lifecycle/TokenDestructible.test.js +++ b/test/lifecycle/TokenDestructible.test.js @@ -1,3 +1,4 @@ +import { ethGetBalance } from '../helpers/web3'; var TokenDestructible = artifacts.require('TokenDestructible'); var StandardTokenMock = artifacts.require('StandardTokenMock'); @@ -15,9 +16,9 @@ contract('TokenDestructible', function (accounts) { it('should send balance to owner after destruction', async function () { let owner = await destructible.owner(); - let initBalance = web3.eth.getBalance(owner); + let initBalance = await ethGetBalance(owner); await destructible.destroy([], { from: owner }); - let newBalance = web3.eth.getBalance(owner); + let newBalance = await ethGetBalance(owner); assert.isTrue(newBalance > initBalance); }); diff --git a/test/ownership/HasNoEther.test.js b/test/ownership/HasNoEther.test.js index ff95e998f..a586051d7 100644 --- a/test/ownership/HasNoEther.test.js +++ b/test/ownership/HasNoEther.test.js @@ -1,6 +1,6 @@ - +import { ethSendTransaction, ethGetBalance } from '../helpers/web3'; import expectThrow from '../helpers/expectThrow'; -import toPromise from '../helpers/toPromise'; + const HasNoEtherTest = artifacts.require('HasNoEtherTest'); const ForceEther = artifacts.require('ForceEther'); @@ -19,7 +19,7 @@ contract('HasNoEther', function (accounts) { let hasNoEther = await HasNoEtherTest.new(); await expectThrow( - toPromise(web3.eth.sendTransaction)({ + ethSendTransaction({ from: accounts[1], to: hasNoEther.address, value: amount, @@ -30,20 +30,20 @@ contract('HasNoEther', function (accounts) { it('should allow owner to reclaim ether', async function () { // Create contract let hasNoEther = await HasNoEtherTest.new(); - const startBalance = await web3.eth.getBalance(hasNoEther.address); + const startBalance = await ethGetBalance(hasNoEther.address); assert.equal(startBalance, 0); // Force ether into it let forceEther = await ForceEther.new({ value: amount }); await forceEther.destroyAndSend(hasNoEther.address); - const forcedBalance = await web3.eth.getBalance(hasNoEther.address); + const forcedBalance = await ethGetBalance(hasNoEther.address); assert.equal(forcedBalance, amount); // Reclaim - const ownerStartBalance = await web3.eth.getBalance(accounts[0]); + const ownerStartBalance = await ethGetBalance(accounts[0]); await hasNoEther.reclaimEther(); - const ownerFinalBalance = await web3.eth.getBalance(accounts[0]); - const finalBalance = await web3.eth.getBalance(hasNoEther.address); + const ownerFinalBalance = await ethGetBalance(accounts[0]); + const finalBalance = await ethGetBalance(hasNoEther.address); assert.equal(finalBalance, 0); assert.isAbove(ownerFinalBalance, ownerStartBalance); }); @@ -55,7 +55,7 @@ contract('HasNoEther', function (accounts) { // Force ether into it let forceEther = await ForceEther.new({ value: amount }); await forceEther.destroyAndSend(hasNoEther.address); - const forcedBalance = await web3.eth.getBalance(hasNoEther.address); + const forcedBalance = await ethGetBalance(hasNoEther.address); assert.equal(forcedBalance, amount); // Reclaim diff --git a/test/payment/Escrow.behaviour.js b/test/payment/Escrow.behaviour.js index ab3947578..eb9d11909 100644 --- a/test/payment/Escrow.behaviour.js +++ b/test/payment/Escrow.behaviour.js @@ -1,5 +1,6 @@ import expectEvent from '../helpers/expectEvent'; import EVMRevert from '../helpers/EVMRevert'; +import { ethGetBalance } from '../helpers/web3'; const BigNumber = web3.BigNumber; @@ -15,7 +16,7 @@ export default function (owner, [payee1, payee2]) { it('can accept a single deposit', async function () { await this.escrow.deposit(payee1, { from: owner, value: amount }); - const balance = await web3.eth.getBalance(this.escrow.address); + const balance = await ethGetBalance(this.escrow.address); const deposit = await this.escrow.depositsOf(payee1); balance.should.be.bignumber.equal(amount); @@ -41,7 +42,7 @@ export default function (owner, [payee1, payee2]) { await this.escrow.deposit(payee1, { from: owner, value: amount }); await this.escrow.deposit(payee1, { from: owner, value: amount * 2 }); - const balance = await web3.eth.getBalance(this.escrow.address); + const balance = await ethGetBalance(this.escrow.address); const deposit = await this.escrow.depositsOf(payee1); balance.should.be.bignumber.equal(amount * 3); @@ -52,7 +53,7 @@ export default function (owner, [payee1, payee2]) { await this.escrow.deposit(payee1, { from: owner, value: amount }); await this.escrow.deposit(payee2, { from: owner, value: amount * 2 }); - const balance = await web3.eth.getBalance(this.escrow.address); + const balance = await ethGetBalance(this.escrow.address); const depositPayee1 = await this.escrow.depositsOf(payee1); const depositPayee2 = await this.escrow.depositsOf(payee2); @@ -64,14 +65,14 @@ export default function (owner, [payee1, payee2]) { describe('withdrawals', async function () { it('can withdraw payments', async function () { - const payeeInitialBalance = await web3.eth.getBalance(payee1); + const payeeInitialBalance = await ethGetBalance(payee1); await this.escrow.deposit(payee1, { from: owner, value: amount }); await this.escrow.withdraw(payee1, { from: owner }); - const escrowBalance = await web3.eth.getBalance(this.escrow.address); + const escrowBalance = await ethGetBalance(this.escrow.address); const finalDeposit = await this.escrow.depositsOf(payee1); - const payeeFinalBalance = await web3.eth.getBalance(payee1); + const payeeFinalBalance = await ethGetBalance(payee1); escrowBalance.should.be.bignumber.equal(0); finalDeposit.should.be.bignumber.equal(0); diff --git a/test/payment/PullPayment.test.js b/test/payment/PullPayment.test.js index 68b440ed8..c2dc421d7 100644 --- a/test/payment/PullPayment.test.js +++ b/test/payment/PullPayment.test.js @@ -1,3 +1,5 @@ +import { ethGetBalance } from '../helpers/web3'; + const BigNumber = web3.BigNumber; require('chai') @@ -45,7 +47,7 @@ contract('PullPayment', function (accounts) { it('can withdraw payment', async function () { const payee = accounts[1]; - const initialBalance = web3.eth.getBalance(payee); + const initialBalance = await ethGetBalance(payee); await this.contract.callTransfer(payee, amount); @@ -56,7 +58,7 @@ contract('PullPayment', function (accounts) { const payment2 = await this.contract.payments(payee); payment2.should.be.bignumber.equal(0); - const balance = web3.eth.getBalance(payee); + const balance = await ethGetBalance(payee); Math.abs(balance - initialBalance - amount).should.be.lt(1e16); }); }); diff --git a/test/payment/RefundEscrow.test.js b/test/payment/RefundEscrow.test.js index fadabc0b0..34971dfa3 100644 --- a/test/payment/RefundEscrow.test.js +++ b/test/payment/RefundEscrow.test.js @@ -1,5 +1,6 @@ import EVMRevert from '../helpers/EVMRevert'; import expectEvent from '../helpers/expectEvent'; +import { ethGetBalance } from '../helpers/web3'; const BigNumber = web3.BigNumber; @@ -60,9 +61,9 @@ contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) { }); it('allows beneficiary withdrawal', async function () { - const beneficiaryInitialBalance = await web3.eth.getBalance(beneficiary); + const beneficiaryInitialBalance = await ethGetBalance(beneficiary); await this.escrow.beneficiaryWithdraw(); - const beneficiaryFinalBalance = await web3.eth.getBalance(beneficiary); + const beneficiaryFinalBalance = await ethGetBalance(beneficiary); beneficiaryFinalBalance.sub(beneficiaryInitialBalance).should.be.bignumber.equal(amount * refundees.length); }); @@ -89,9 +90,9 @@ contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) { it('refunds refundees', async function () { for (let refundee of [refundee1, refundee2]) { - const refundeeInitialBalance = await web3.eth.getBalance(refundee); + const refundeeInitialBalance = await ethGetBalance(refundee); await this.escrow.withdraw(refundee); - const refundeeFinalBalance = await web3.eth.getBalance(refundee); + const refundeeFinalBalance = await ethGetBalance(refundee); refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount); } diff --git a/test/payment/SplitPayment.test.js b/test/payment/SplitPayment.test.js index dd18eaeac..92b682aa9 100644 --- a/test/payment/SplitPayment.test.js +++ b/test/payment/SplitPayment.test.js @@ -1,3 +1,5 @@ +import { ethGetBalance, ethSendTransaction } from '../helpers/web3'; + const BigNumber = web3.BigNumber; require('chai') @@ -19,9 +21,9 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa }); it('should accept payments', async function () { - await web3.eth.sendTransaction({ from: owner, to: this.contract.address, value: amount }); + await ethSendTransaction({ from: owner, to: this.contract.address, value: amount }); - const balance = web3.eth.getBalance(this.contract.address); + const balance = await ethGetBalance(this.contract.address); balance.should.be.bignumber.equal(amount); }); @@ -40,35 +42,35 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa }); it('should throw if non-payee want to claim', async function () { - await web3.eth.sendTransaction({ from: payer1, to: this.contract.address, value: amount }); + await ethSendTransaction({ from: payer1, to: this.contract.address, value: amount }); await this.contract.claim({ from: nonpayee1 }).should.be.rejectedWith(EVMThrow); }); it('should distribute funds to payees', async function () { - await web3.eth.sendTransaction({ from: payer1, to: this.contract.address, value: amount }); + await ethSendTransaction({ from: payer1, to: this.contract.address, value: amount }); // receive funds - const initBalance = web3.eth.getBalance(this.contract.address); + const initBalance = await ethGetBalance(this.contract.address); initBalance.should.be.bignumber.equal(amount); // distribute to payees - const initAmount1 = web3.eth.getBalance(payee1); + const initAmount1 = await ethGetBalance(payee1); await this.contract.claim({ from: payee1 }); - const profit1 = web3.eth.getBalance(payee1) - initAmount1; + const profit1 = await ethGetBalance(payee1) - initAmount1; assert(Math.abs(profit1 - web3.toWei(0.20, 'ether')) < 1e16); - const initAmount2 = web3.eth.getBalance(payee2); + const initAmount2 = await ethGetBalance(payee2); await this.contract.claim({ from: payee2 }); - const profit2 = web3.eth.getBalance(payee2) - initAmount2; + const profit2 = await ethGetBalance(payee2) - initAmount2; assert(Math.abs(profit2 - web3.toWei(0.10, 'ether')) < 1e16); - const initAmount3 = web3.eth.getBalance(payee3); + const initAmount3 = await ethGetBalance(payee3); await this.contract.claim({ from: payee3 }); - const profit3 = web3.eth.getBalance(payee3) - initAmount3; + const profit3 = await ethGetBalance(payee3) - initAmount3; assert(Math.abs(profit3 - web3.toWei(0.70, 'ether')) < 1e16); // end balance should be zero - const endBalance = web3.eth.getBalance(this.contract.address); + const endBalance = await ethGetBalance(this.contract.address); endBalance.should.be.bignumber.equal(0); // check correct funds released accounting diff --git a/test/token/ERC20/TokenTimelock.test.js b/test/token/ERC20/TokenTimelock.test.js index d99c4b247..0c5ad744b 100644 --- a/test/token/ERC20/TokenTimelock.test.js +++ b/test/token/ERC20/TokenTimelock.test.js @@ -16,7 +16,7 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) { beforeEach(async function () { this.token = await MintableToken.new({ from: owner }); - this.releaseTime = latestTime() + duration.years(1); + this.releaseTime = (await latestTime()) + duration.years(1); this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime); await this.token.mint(this.timelock.address, amount, { from: owner }); }); diff --git a/test/token/ERC20/TokenVesting.test.js b/test/token/ERC20/TokenVesting.test.js index e6ce5c177..8c33250ae 100644 --- a/test/token/ERC20/TokenVesting.test.js +++ b/test/token/ERC20/TokenVesting.test.js @@ -1,6 +1,7 @@ import EVMRevert from '../../helpers/EVMRevert'; import latestTime from '../../helpers/latestTime'; import { increaseTimeTo, duration } from '../../helpers/increaseTime'; +import { ethGetBlock } from '../../helpers/web3'; const BigNumber = web3.BigNumber; @@ -18,7 +19,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { beforeEach(async function () { this.token = await MintableToken.new({ from: owner }); - this.start = latestTime() + duration.minutes(1); // +1 minute so it starts after contract instantiation + this.start = (await latestTime()) + duration.minutes(1); // +1 minute so it starts after contract instantiation this.cliff = duration.years(1); this.duration = duration.years(2); @@ -40,7 +41,8 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { await increaseTimeTo(this.start + this.cliff); const { receipt } = await this.vesting.release(this.token.address); - const releaseTime = web3.eth.getBlock(receipt.blockNumber).timestamp; + const block = await ethGetBlock(receipt.blockNumber); + const releaseTime = block.timestamp; const balance = await this.token.balanceOf(beneficiary); balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.duration).floor());