Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions test/Bounty.test.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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 () {
Expand All @@ -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 });
Expand All @@ -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 });
Expand All @@ -91,18 +91,19 @@ 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();

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);
Expand Down
14 changes: 10 additions & 4 deletions test/LimitBalance.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import assertRevert from './helpers/assertRevert';
import { ethGetBalance } from './helpers/web3';

var LimitBalanceMock = artifacts.require('LimitBalanceMock');

contract('LimitBalance', function (accounts) {
Expand All @@ -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 () {
Expand All @@ -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 }));
});
});
13 changes: 8 additions & 5 deletions test/SimpleSavingsWallet.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import expectThrow from './helpers/expectThrow';
import { ethGetBalance, ethSendTransaction } from './helpers/web3';

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

Expand All @@ -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));
});
});
6 changes: 4 additions & 2 deletions test/crowdsale/AllowanceCrowdsale.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import ether from '../helpers/ether';
import assertRevert from '../helpers/assertRevert';
import { ethGetBalance } from '../helpers/web3';

const BigNumber = web3.BigNumber;

Expand Down Expand Up @@ -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);
});
});
Expand Down
9 changes: 5 additions & 4 deletions test/crowdsale/Crowdsale.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ether from '../helpers/ether';
import { ethGetBalance } from '../helpers/web3';

const BigNumber = web3.BigNumber;

Expand Down Expand Up @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/crowdsale/FinalizableCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/crowdsale/IncreasingPriceCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions test/crowdsale/MintedCrowdsale.behaviour.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethGetBalance } from '../helpers/web3';

const BigNumber = web3.BigNumber;

const should = require('chai')
Expand Down Expand Up @@ -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);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/crowdsale/PostDeliveryCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions test/crowdsale/RefundableCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -63,20 +64,20 @@ 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);
});

it('should forward funds to wallet after end if goal was reached', async function () {
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);
});
});
2 changes: 1 addition & 1 deletion test/crowdsale/TimedCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions test/examples/SampleCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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 });
Expand All @@ -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);
});

Expand Down
2 changes: 1 addition & 1 deletion test/examples/SimpleToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/increaseTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions test/helpers/latestTime.js
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 0 additions & 4 deletions test/helpers/toPromise.js

This file was deleted.

Loading