Commit 40b5594f by Arun Kumar Committed by Matt Condon

Promisify web3 sync requests in tests (#1009)

parent 4575a240
import { ethGetBalance, ethSendTransaction } from './helpers/web3';
let sendReward = function (sender, receiver, value) { const sendReward = async (from, to, value) => ethSendTransaction({
web3.eth.sendTransaction({ from, to, value,
from: sender, });
to: receiver,
value: value,
});
};
var SecureTargetBounty = artifacts.require('SecureTargetBounty'); var SecureTargetBounty = artifacts.require('SecureTargetBounty');
var InsecureTargetBounty = artifacts.require('InsecureTargetBounty'); var InsecureTargetBounty = artifacts.require('InsecureTargetBounty');
...@@ -24,21 +21,24 @@ contract('Bounty', function (accounts) { ...@@ -24,21 +21,24 @@ contract('Bounty', function (accounts) {
let owner = accounts[0]; let owner = accounts[0];
let reward = web3.toWei(1, 'ether'); let reward = web3.toWei(1, 'ether');
let bounty = await SecureTargetBounty.new(); 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 () { it('empties itself when destroyed', async function () {
let owner = accounts[0]; let owner = accounts[0];
let reward = web3.toWei(1, 'ether'); let reward = web3.toWei(1, 'ether');
let bounty = await SecureTargetBounty.new(); 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(); 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 () { describe('Against secure contract', function () {
...@@ -54,10 +54,10 @@ contract('Bounty', function (accounts) { ...@@ -54,10 +54,10 @@ contract('Bounty', function (accounts) {
if (err) { throw err; } if (err) { throw err; }
var targetAddress = result.args.createdAddress; var targetAddress = result.args.createdAddress;
sendReward(owner, bounty.address, reward); await sendReward(owner, bounty.address, reward);
assert.equal(reward, const balance = await ethGetBalance(bounty.address);
web3.eth.getBalance(bounty.address).toNumber()); assert.equal(reward, balance.toNumber());
try { try {
await bounty.claim(targetAddress, { from: researcher }); await bounty.claim(targetAddress, { from: researcher });
...@@ -70,8 +70,8 @@ contract('Bounty', function (accounts) { ...@@ -70,8 +70,8 @@ contract('Bounty', function (accounts) {
await bounty.withdrawPayments({ from: researcher }); await bounty.withdrawPayments({ from: researcher });
assert.isTrue(false); // should never reach here assert.isTrue(false); // should never reach here
} catch (err) { } catch (err) {
assert.equal(reward, const updatedBalance = await ethGetBalance(bounty.address);
web3.eth.getBalance(bounty.address).toNumber()); assert.equal(reward, updatedBalance.toNumber());
} }
}; };
await bounty.createTarget({ from: researcher }); await bounty.createTarget({ from: researcher });
...@@ -91,9 +91,10 @@ contract('Bounty', function (accounts) { ...@@ -91,9 +91,10 @@ contract('Bounty', function (accounts) {
event.stopWatching(); event.stopWatching();
if (err) { throw err; } if (err) { throw err; }
let targetAddress = result.args.createdAddress; 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 }); await bounty.claim(targetAddress, { from: researcher });
let claim = await bounty.claimed.call(); let claim = await bounty.claimed.call();
...@@ -101,8 +102,8 @@ contract('Bounty', function (accounts) { ...@@ -101,8 +102,8 @@ contract('Bounty', function (accounts) {
assert.isTrue(claim); assert.isTrue(claim);
await bounty.withdrawPayments({ from: researcher }); await bounty.withdrawPayments({ from: researcher });
const updatedBalance = await ethGetBalance(bounty.address);
assert.equal(0, web3.eth.getBalance(bounty.address).toNumber()); assert.equal(0, updatedBalance.toNumber());
}; };
await bounty.createTarget({ from: researcher }); await bounty.createTarget({ from: researcher });
await awaitEvent(event, watcher); await awaitEvent(event, watcher);
......
import assertRevert from './helpers/assertRevert'; import assertRevert from './helpers/assertRevert';
import { ethGetBalance } from './helpers/web3';
var LimitBalanceMock = artifacts.require('LimitBalanceMock'); var LimitBalanceMock = artifacts.require('LimitBalanceMock');
contract('LimitBalance', function (accounts) { contract('LimitBalance', function (accounts) {
...@@ -19,7 +21,8 @@ contract('LimitBalance', function (accounts) { ...@@ -19,7 +21,8 @@ contract('LimitBalance', function (accounts) {
let amount = 1; let amount = 1;
await lb.limitedDeposit({ value: amount }); 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 () { it('shouldnt allow sending above limit', async function () {
...@@ -31,17 +34,20 @@ contract('LimitBalance', function (accounts) { ...@@ -31,17 +34,20 @@ contract('LimitBalance', function (accounts) {
let amount = 500; let amount = 500;
await lb.limitedDeposit({ value: amount }); 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 }); 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 () { it('shouldnt allow multiple sends above limit', async function () {
let amount = 500; let amount = 500;
await lb.limitedDeposit({ value: amount }); 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 })); await assertRevert(lb.limitedDeposit({ value: amount + 1 }));
}); });
}); });
import expectThrow from './helpers/expectThrow'; import expectThrow from './helpers/expectThrow';
import { ethGetBalance, ethSendTransaction } from './helpers/web3';
const SimpleSavingsWallet = artifacts.require('SimpleSavingsWallet'); const SimpleSavingsWallet = artifacts.require('SimpleSavingsWallet');
...@@ -15,19 +16,21 @@ contract('SimpleSavingsWallet', function (accounts) { ...@@ -15,19 +16,21 @@ contract('SimpleSavingsWallet', function (accounts) {
}); });
it('should receive funds', async function () { it('should receive funds', async function () {
await web3.eth.sendTransaction({ from: owner, to: savingsWallet.address, value: paymentAmount }); await ethSendTransaction({ from: owner, to: savingsWallet.address, value: paymentAmount });
assert.isTrue((new web3.BigNumber(paymentAmount)).equals(web3.eth.getBalance(savingsWallet.address))); const balance = await ethGetBalance(savingsWallet.address);
assert.isTrue((new web3.BigNumber(paymentAmount)).equals(balance));
}); });
it('owner can send funds', async function () { it('owner can send funds', async function () {
// Receive payment so we have some money to spend. // 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(0, paymentAmount, { from: owner }));
await expectThrow(savingsWallet.sendTo(savingsWallet.address, paymentAmount, { from: owner })); await expectThrow(savingsWallet.sendTo(savingsWallet.address, paymentAmount, { from: owner }));
await expectThrow(savingsWallet.sendTo(accounts[1], 0, { 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 }); 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));
}); });
}); });
import ether from '../helpers/ether'; import ether from '../helpers/ether';
import assertRevert from '../helpers/assertRevert'; import assertRevert from '../helpers/assertRevert';
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
...@@ -52,9 +54,9 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW ...@@ -52,9 +54,9 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW
}); });
it('should forward funds to wallet', async function () { 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 }); 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); post.minus(pre).should.be.bignumber.equal(value);
}); });
}); });
......
import ether from '../helpers/ether'; import ether from '../helpers/ether';
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
...@@ -47,9 +48,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -47,9 +48,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
}); });
it('should forward funds to wallet', async function () { 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 }); 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); post.minus(pre).should.be.bignumber.equal(value);
}); });
}); });
...@@ -72,9 +73,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -72,9 +73,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
}); });
it('should forward funds to wallet', async function () { 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 }); 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); post.minus(pre).should.be.bignumber.equal(value);
}); });
}); });
......
...@@ -22,7 +22,7 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) { ...@@ -22,7 +22,7 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) {
}); });
beforeEach(async function () { beforeEach(async function () {
this.openingTime = latestTime() + duration.weeks(1); this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1); this.afterClosingTime = this.closingTime + duration.seconds(1);
......
...@@ -30,7 +30,7 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) ...@@ -30,7 +30,7 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser])
beforeEach(async function () { beforeEach(async function () {
await advanceBlock(); await advanceBlock();
this.startTime = latestTime() + duration.weeks(1); this.startTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.startTime + duration.weeks(1); this.closingTime = this.startTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1); this.afterClosingTime = this.closingTime + duration.seconds(1);
this.token = await SimpleToken.new(); this.token = await SimpleToken.new();
......
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
const should = require('chai') const should = require('chai')
...@@ -34,9 +36,9 @@ export default function ([_, investor, wallet, purchaser], rate, value) { ...@@ -34,9 +36,9 @@ export default function ([_, investor, wallet, purchaser], rate, value) {
}); });
it('should forward funds to wallet', async function () { 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 }); 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); post.minus(pre).should.be.bignumber.equal(value);
}); });
}); });
......
...@@ -25,7 +25,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -25,7 +25,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
}); });
beforeEach(async function () { beforeEach(async function () {
this.openingTime = latestTime() + duration.weeks(1); this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1);
this.beforeEndTime = this.closingTime - duration.hours(1); this.beforeEndTime = this.closingTime - duration.hours(1);
this.afterClosingTime = this.closingTime + duration.seconds(1); this.afterClosingTime = this.closingTime + duration.seconds(1);
......
...@@ -3,6 +3,7 @@ import { advanceBlock } from '../helpers/advanceToBlock'; ...@@ -3,6 +3,7 @@ import { advanceBlock } from '../helpers/advanceToBlock';
import { increaseTimeTo, duration } from '../helpers/increaseTime'; import { increaseTimeTo, duration } from '../helpers/increaseTime';
import latestTime from '../helpers/latestTime'; import latestTime from '../helpers/latestTime';
import EVMRevert from '../helpers/EVMRevert'; import EVMRevert from '../helpers/EVMRevert';
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
...@@ -26,7 +27,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser ...@@ -26,7 +27,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser
}); });
beforeEach(async function () { beforeEach(async function () {
this.openingTime = latestTime() + duration.weeks(1); this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1); this.afterClosingTime = this.closingTime + duration.seconds(1);
...@@ -63,10 +64,10 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser ...@@ -63,10 +64,10 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser
await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor }); await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
await increaseTimeTo(this.afterClosingTime); await increaseTimeTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner }); 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 }) await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 })
.should.be.fulfilled; .should.be.fulfilled;
const post = web3.eth.getBalance(investor); const post = await ethGetBalance(investor);
post.minus(pre).should.be.bignumber.equal(lessThanGoal); post.minus(pre).should.be.bignumber.equal(lessThanGoal);
}); });
...@@ -74,9 +75,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser ...@@ -74,9 +75,9 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser
await increaseTimeTo(this.openingTime); await increaseTimeTo(this.openingTime);
await this.crowdsale.sendTransaction({ value: goal, from: investor }); await this.crowdsale.sendTransaction({ value: goal, from: investor });
await increaseTimeTo(this.afterClosingTime); await increaseTimeTo(this.afterClosingTime);
const pre = web3.eth.getBalance(wallet); const pre = await ethGetBalance(wallet);
await this.crowdsale.finalize({ from: owner }); 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); post.minus(pre).should.be.bignumber.equal(goal);
}); });
}); });
...@@ -25,7 +25,7 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) { ...@@ -25,7 +25,7 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
}); });
beforeEach(async function () { beforeEach(async function () {
this.openingTime = latestTime() + duration.weeks(1); this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1); this.afterClosingTime = this.closingTime + duration.seconds(1);
this.token = await SimpleToken.new(); this.token = await SimpleToken.new();
......
...@@ -4,6 +4,7 @@ import { increaseTimeTo, duration } from '../helpers/increaseTime'; ...@@ -4,6 +4,7 @@ import { increaseTimeTo, duration } from '../helpers/increaseTime';
import latestTime from '../helpers/latestTime'; import latestTime from '../helpers/latestTime';
import EVMRevert from '../helpers/EVMRevert'; import EVMRevert from '../helpers/EVMRevert';
import assertRevert from '../helpers/assertRevert'; import assertRevert from '../helpers/assertRevert';
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
...@@ -26,7 +27,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) { ...@@ -26,7 +27,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
}); });
beforeEach(async function () { beforeEach(async function () {
this.openingTime = latestTime() + duration.weeks(1); this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1); this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1); this.afterClosingTime = this.closingTime + duration.seconds(1);
...@@ -88,16 +89,16 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) { ...@@ -88,16 +89,16 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
await increaseTimeTo(this.openingTime); await increaseTimeTo(this.openingTime);
await this.crowdsale.send(GOAL); await this.crowdsale.send(GOAL);
const beforeFinalization = web3.eth.getBalance(wallet); const beforeFinalization = await ethGetBalance(wallet);
await increaseTimeTo(this.afterClosingTime); await increaseTimeTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner }); 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); afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL);
}); });
it('should allow refunds if the goal is not reached', async function () { 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 increaseTimeTo(this.openingTime);
await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 }); await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 });
...@@ -106,7 +107,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) { ...@@ -106,7 +107,7 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
await this.crowdsale.finalize({ from: owner }); await this.crowdsale.finalize({ from: owner });
await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 }).should.be.fulfilled; 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); balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund);
}); });
......
...@@ -30,7 +30,7 @@ contract('SimpleToken', accounts => { ...@@ -30,7 +30,7 @@ contract('SimpleToken', accounts => {
assert(creatorBalance.eq(totalSupply)); 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); const logs = decodeLogs(receipt.logs, SimpleToken, token.address);
assert.equal(logs.length, 1); assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Transfer'); assert.equal(logs[0].event, 'Transfer');
......
...@@ -31,8 +31,8 @@ export default function increaseTime (duration) { ...@@ -31,8 +31,8 @@ export default function increaseTime (duration) {
* *
* @param target time in seconds * @param target time in seconds
*/ */
export function increaseTimeTo (target) { export async function increaseTimeTo (target) {
let now = latestTime(); let now = (await latestTime());
if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`); if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`);
let diff = target - now; let diff = target - now;
return increaseTime(diff); return increaseTime(diff);
......
import { ethGetBlock } from './web3';
// Returns the time of the last mined block in seconds // Returns the time of the last mined block in seconds
export default function latestTime () { export default async function latestTime () {
return web3.eth.getBlock('latest').timestamp; const block = await ethGetBlock('latest');
return block.timestamp;
} }
export default func =>
(...args) =>
new Promise((resolve, reject) =>
func(...args, (error, data) => error ? reject(error) : resolve(data)));
const pify = require('pify');
const ethAsync = pify(web3.eth);
export const ethGetBalance = ethAsync.getBalance;
export const ethSendTransaction = ethAsync.sendTransaction;
export const ethGetBlock = ethAsync.getBlock;
import { ethGetBalance } from '../helpers/web3';
var Destructible = artifacts.require('Destructible'); const Destructible = artifacts.require('Destructible');
require('../helpers/transactionMined.js'); require('../helpers/transactionMined.js');
contract('Destructible', function (accounts) { contract('Destructible', function (accounts) {
it('should send balance to owner after destruction', async function () { it('should send balance to owner after destruction', async function () {
let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') }); let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') });
let owner = await destructible.owner(); let owner = await destructible.owner();
let initBalance = web3.eth.getBalance(owner); let initBalance = await ethGetBalance(owner);
await destructible.destroy({ from: owner }); await destructible.destroy({ from: owner });
let newBalance = web3.eth.getBalance(owner); let newBalance = await ethGetBalance(owner);
assert.isTrue(newBalance > initBalance); assert.isTrue(newBalance > initBalance);
}); });
it('should send balance to recepient after destruction', async function () { it('should send balance to recepient after destruction', async function () {
let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') }); let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') });
let owner = await destructible.owner(); 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 }); 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)); assert.isTrue(newBalance.greaterThan(initBalance));
}); });
}); });
import { ethGetBalance } from '../helpers/web3';
var TokenDestructible = artifacts.require('TokenDestructible'); var TokenDestructible = artifacts.require('TokenDestructible');
var StandardTokenMock = artifacts.require('StandardTokenMock'); var StandardTokenMock = artifacts.require('StandardTokenMock');
...@@ -15,9 +16,9 @@ contract('TokenDestructible', function (accounts) { ...@@ -15,9 +16,9 @@ contract('TokenDestructible', function (accounts) {
it('should send balance to owner after destruction', async function () { it('should send balance to owner after destruction', async function () {
let owner = await destructible.owner(); let owner = await destructible.owner();
let initBalance = web3.eth.getBalance(owner); let initBalance = await ethGetBalance(owner);
await destructible.destroy([], { from: owner }); await destructible.destroy([], { from: owner });
let newBalance = web3.eth.getBalance(owner); let newBalance = await ethGetBalance(owner);
assert.isTrue(newBalance > initBalance); assert.isTrue(newBalance > initBalance);
}); });
......
import { ethSendTransaction, ethGetBalance } from '../helpers/web3';
import expectThrow from '../helpers/expectThrow'; import expectThrow from '../helpers/expectThrow';
import toPromise from '../helpers/toPromise';
const HasNoEtherTest = artifacts.require('HasNoEtherTest'); const HasNoEtherTest = artifacts.require('HasNoEtherTest');
const ForceEther = artifacts.require('ForceEther'); const ForceEther = artifacts.require('ForceEther');
...@@ -19,7 +19,7 @@ contract('HasNoEther', function (accounts) { ...@@ -19,7 +19,7 @@ contract('HasNoEther', function (accounts) {
let hasNoEther = await HasNoEtherTest.new(); let hasNoEther = await HasNoEtherTest.new();
await expectThrow( await expectThrow(
toPromise(web3.eth.sendTransaction)({ ethSendTransaction({
from: accounts[1], from: accounts[1],
to: hasNoEther.address, to: hasNoEther.address,
value: amount, value: amount,
...@@ -30,20 +30,20 @@ contract('HasNoEther', function (accounts) { ...@@ -30,20 +30,20 @@ contract('HasNoEther', function (accounts) {
it('should allow owner to reclaim ether', async function () { it('should allow owner to reclaim ether', async function () {
// Create contract // Create contract
let hasNoEther = await HasNoEtherTest.new(); let hasNoEther = await HasNoEtherTest.new();
const startBalance = await web3.eth.getBalance(hasNoEther.address); const startBalance = await ethGetBalance(hasNoEther.address);
assert.equal(startBalance, 0); assert.equal(startBalance, 0);
// Force ether into it // Force ether into it
let forceEther = await ForceEther.new({ value: amount }); let forceEther = await ForceEther.new({ value: amount });
await forceEther.destroyAndSend(hasNoEther.address); await forceEther.destroyAndSend(hasNoEther.address);
const forcedBalance = await web3.eth.getBalance(hasNoEther.address); const forcedBalance = await ethGetBalance(hasNoEther.address);
assert.equal(forcedBalance, amount); assert.equal(forcedBalance, amount);
// Reclaim // Reclaim
const ownerStartBalance = await web3.eth.getBalance(accounts[0]); const ownerStartBalance = await ethGetBalance(accounts[0]);
await hasNoEther.reclaimEther(); await hasNoEther.reclaimEther();
const ownerFinalBalance = await web3.eth.getBalance(accounts[0]); const ownerFinalBalance = await ethGetBalance(accounts[0]);
const finalBalance = await web3.eth.getBalance(hasNoEther.address); const finalBalance = await ethGetBalance(hasNoEther.address);
assert.equal(finalBalance, 0); assert.equal(finalBalance, 0);
assert.isAbove(ownerFinalBalance, ownerStartBalance); assert.isAbove(ownerFinalBalance, ownerStartBalance);
}); });
...@@ -55,7 +55,7 @@ contract('HasNoEther', function (accounts) { ...@@ -55,7 +55,7 @@ contract('HasNoEther', function (accounts) {
// Force ether into it // Force ether into it
let forceEther = await ForceEther.new({ value: amount }); let forceEther = await ForceEther.new({ value: amount });
await forceEther.destroyAndSend(hasNoEther.address); await forceEther.destroyAndSend(hasNoEther.address);
const forcedBalance = await web3.eth.getBalance(hasNoEther.address); const forcedBalance = await ethGetBalance(hasNoEther.address);
assert.equal(forcedBalance, amount); assert.equal(forcedBalance, amount);
// Reclaim // Reclaim
......
import expectEvent from '../helpers/expectEvent'; import expectEvent from '../helpers/expectEvent';
import EVMRevert from '../helpers/EVMRevert'; import EVMRevert from '../helpers/EVMRevert';
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
...@@ -15,7 +16,7 @@ export default function (owner, [payee1, payee2]) { ...@@ -15,7 +16,7 @@ export default function (owner, [payee1, payee2]) {
it('can accept a single deposit', async function () { it('can accept a single deposit', async function () {
await this.escrow.deposit(payee1, { from: owner, value: amount }); 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); const deposit = await this.escrow.depositsOf(payee1);
balance.should.be.bignumber.equal(amount); balance.should.be.bignumber.equal(amount);
...@@ -41,7 +42,7 @@ export default function (owner, [payee1, payee2]) { ...@@ -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 });
await this.escrow.deposit(payee1, { from: owner, value: amount * 2 }); 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); const deposit = await this.escrow.depositsOf(payee1);
balance.should.be.bignumber.equal(amount * 3); balance.should.be.bignumber.equal(amount * 3);
...@@ -52,7 +53,7 @@ export default function (owner, [payee1, payee2]) { ...@@ -52,7 +53,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 });
await this.escrow.deposit(payee2, { from: owner, value: amount * 2 }); 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 depositPayee1 = await this.escrow.depositsOf(payee1);
const depositPayee2 = await this.escrow.depositsOf(payee2); const depositPayee2 = await this.escrow.depositsOf(payee2);
...@@ -64,14 +65,14 @@ export default function (owner, [payee1, payee2]) { ...@@ -64,14 +65,14 @@ export default function (owner, [payee1, payee2]) {
describe('withdrawals', async function () { describe('withdrawals', async function () {
it('can withdraw payments', 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.deposit(payee1, { from: owner, value: amount });
await this.escrow.withdraw(payee1, { from: owner }); 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 finalDeposit = await this.escrow.depositsOf(payee1);
const payeeFinalBalance = await web3.eth.getBalance(payee1); const payeeFinalBalance = await ethGetBalance(payee1);
escrowBalance.should.be.bignumber.equal(0); escrowBalance.should.be.bignumber.equal(0);
finalDeposit.should.be.bignumber.equal(0); finalDeposit.should.be.bignumber.equal(0);
......
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
require('chai') require('chai')
...@@ -45,7 +47,7 @@ contract('PullPayment', function (accounts) { ...@@ -45,7 +47,7 @@ contract('PullPayment', function (accounts) {
it('can withdraw payment', async function () { it('can withdraw payment', async function () {
const payee = accounts[1]; const payee = accounts[1];
const initialBalance = web3.eth.getBalance(payee); const initialBalance = await ethGetBalance(payee);
await this.contract.callTransfer(payee, amount); await this.contract.callTransfer(payee, amount);
...@@ -56,7 +58,7 @@ contract('PullPayment', function (accounts) { ...@@ -56,7 +58,7 @@ contract('PullPayment', function (accounts) {
const payment2 = await this.contract.payments(payee); const payment2 = await this.contract.payments(payee);
payment2.should.be.bignumber.equal(0); 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); Math.abs(balance - initialBalance - amount).should.be.lt(1e16);
}); });
}); });
import EVMRevert from '../helpers/EVMRevert'; import EVMRevert from '../helpers/EVMRevert';
import expectEvent from '../helpers/expectEvent'; import expectEvent from '../helpers/expectEvent';
import { ethGetBalance } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
...@@ -60,9 +61,9 @@ contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) { ...@@ -60,9 +61,9 @@ contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) {
}); });
it('allows beneficiary withdrawal', async function () { it('allows beneficiary withdrawal', async function () {
const beneficiaryInitialBalance = await web3.eth.getBalance(beneficiary); const beneficiaryInitialBalance = await ethGetBalance(beneficiary);
await this.escrow.beneficiaryWithdraw(); 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); beneficiaryFinalBalance.sub(beneficiaryInitialBalance).should.be.bignumber.equal(amount * refundees.length);
}); });
...@@ -89,9 +90,9 @@ contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) { ...@@ -89,9 +90,9 @@ contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) {
it('refunds refundees', async function () { it('refunds refundees', async function () {
for (let refundee of [refundee1, refundee2]) { for (let refundee of [refundee1, refundee2]) {
const refundeeInitialBalance = await web3.eth.getBalance(refundee); const refundeeInitialBalance = await ethGetBalance(refundee);
await this.escrow.withdraw(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); refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount);
} }
......
import { ethGetBalance, ethSendTransaction } from '../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
require('chai') require('chai')
...@@ -19,9 +21,9 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa ...@@ -19,9 +21,9 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa
}); });
it('should accept payments', async function () { 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); balance.should.be.bignumber.equal(amount);
}); });
...@@ -40,35 +42,35 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa ...@@ -40,35 +42,35 @@ contract('SplitPayment', function ([owner, payee1, payee2, payee3, nonpayee1, pa
}); });
it('should throw if non-payee want to claim', async function () { 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); await this.contract.claim({ from: nonpayee1 }).should.be.rejectedWith(EVMThrow);
}); });
it('should distribute funds to payees', async function () { 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 // receive funds
const initBalance = web3.eth.getBalance(this.contract.address); const initBalance = await ethGetBalance(this.contract.address);
initBalance.should.be.bignumber.equal(amount); initBalance.should.be.bignumber.equal(amount);
// distribute to payees // distribute to payees
const initAmount1 = web3.eth.getBalance(payee1); const initAmount1 = await ethGetBalance(payee1);
await this.contract.claim({ from: 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); 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 }); 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); 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 }); 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); assert(Math.abs(profit3 - web3.toWei(0.70, 'ether')) < 1e16);
// end balance should be zero // 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); endBalance.should.be.bignumber.equal(0);
// check correct funds released accounting // check correct funds released accounting
......
...@@ -16,7 +16,7 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) { ...@@ -16,7 +16,7 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) {
beforeEach(async function () { beforeEach(async function () {
this.token = await MintableToken.new({ from: owner }); 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); this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime);
await this.token.mint(this.timelock.address, amount, { from: owner }); await this.token.mint(this.timelock.address, amount, { from: owner });
}); });
......
import EVMRevert from '../../helpers/EVMRevert'; import EVMRevert from '../../helpers/EVMRevert';
import latestTime from '../../helpers/latestTime'; import latestTime from '../../helpers/latestTime';
import { increaseTimeTo, duration } from '../../helpers/increaseTime'; import { increaseTimeTo, duration } from '../../helpers/increaseTime';
import { ethGetBlock } from '../../helpers/web3';
const BigNumber = web3.BigNumber; const BigNumber = web3.BigNumber;
...@@ -18,7 +19,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { ...@@ -18,7 +19,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
beforeEach(async function () { beforeEach(async function () {
this.token = await MintableToken.new({ from: owner }); 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.cliff = duration.years(1);
this.duration = duration.years(2); this.duration = duration.years(2);
...@@ -40,7 +41,8 @@ contract('TokenVesting', function ([_, owner, beneficiary]) { ...@@ -40,7 +41,8 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
await increaseTimeTo(this.start + this.cliff); await increaseTimeTo(this.start + this.cliff);
const { receipt } = await this.vesting.release(this.token.address); 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); const balance = await this.token.balanceOf(beneficiary);
balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.duration).floor()); balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.duration).floor());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment