Promisify web3 sync requests in tests (#1009)
This commit is contained in:
@ -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);
|
||||
|
||||
@ -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 }));
|
||||
});
|
||||
});
|
||||
|
||||
@ -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));
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
export default func =>
|
||||
(...args) =>
|
||||
new Promise((resolve, reject) =>
|
||||
func(...args, (error, data) => error ? reject(error) : resolve(data)));
|
||||
7
test/helpers/web3.js
Normal file
7
test/helpers/web3.js
Normal file
@ -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;
|
||||
@ -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));
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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 });
|
||||
});
|
||||
|
||||
@ -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());
|
||||
|
||||
Reference in New Issue
Block a user