Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' into remove-babel
  • Loading branch information
nventuro committed Jul 13, 2018
commit 5ce1e5bfab6dc5459f230fd554732662c21a3b75
45 changes: 24 additions & 21 deletions test/Bounty.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
let sendReward = function (sender, receiver, value) {
web3.eth.sendTransaction({
from: sender,
to: receiver,
value: value,
});
};
const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');

var SecureTargetBounty = artifacts.require('SecureTargetBounty');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use const for these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we're only removing Babel in this PR, that's coming on a separate one to keep everything tidy.

Copy link
Contributor Author

@nventuro nventuro Jul 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #1091.

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

const sendReward = async (from, to, value) => ethSendTransaction({
from, to, value,
});

function awaitEvent (event, handler) {
return new Promise((resolve, reject) => {
function wrappedHandler (...args) {
Expand All @@ -23,21 +22,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 @@ -53,10 +55,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 @@ -69,8 +71,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 @@ -90,18 +92,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 @@
const { assertRevert } = require('./helpers/assertRevert');
const { ethGetBalance } = require('./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,4 +1,5 @@
const { expectThrow } = require('./helpers/expectThrow');
const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');

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

Expand All @@ -14,19 +15,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));
});
});
5 changes: 3 additions & 2 deletions test/crowdsale/AllowanceCrowdsale.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { ether } = require('../helpers/ether');
const { assertRevert } = require('../helpers/assertRevert');
const { ethGetBalance } = require('../helpers/web3');

const BigNumber = web3.BigNumber;

Expand Down Expand Up @@ -52,9 +53,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 @@
const { ether } = require('../helpers/ether');
const { ethGetBalance } = require('../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 @@
const { ethGetBalance } = require('../helpers/web3');

const BigNumber = web3.BigNumber;

const should = require('chai')
Expand Down Expand Up @@ -34,9 +36,9 @@ function shouldBehaveLikeMintedCrowdsale ([_, investor, wallet, purchaser], rate
});

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 @@ const { advanceBlock } = require('../helpers/advanceToBlock');
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const { EVMRevert } = require('../helpers/EVMRevert');
const { ethGetBalance } = require('../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 @@ const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const { EVMRevert } = require('../helpers/EVMRevert');
const { assertRevert } = require('../helpers/assertRevert');
const { ethGetBalance } = require('../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
5 changes: 3 additions & 2 deletions test/helpers/increaseTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ function increaseTime (duration) {
*
* @param target time in seconds
*/
function increaseTimeTo (target) {
let now = latestTime();
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,6 +1,9 @@
const { ethGetBlock } = require('./web3');

// Returns the time of the last mined block in seconds
function latestTime () {
return web3.eth.getBlock('latest').timestamp;
async function latestTime () {
const block = await ethGetBlock('latest');
return block.timestamp;
}

module.exports = {
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.