Skip to content
Merged
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
fix: updates from PR notes
  • Loading branch information
shrugs committed Jul 26, 2018
commit 0e843b2674993c1aed16a44f183e14d93b32c578
82 changes: 45 additions & 37 deletions test/Bounty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,75 +15,83 @@ const sendReward = async (from, to, value) => ethSendTransaction({
value,
});

const reward = web3.toWei(1, 'ether');
const reward = new web3.BigNumber(web3.toWei(1, 'ether'));

contract('Bounty', function ([_, owner, researcher]) {
describe('Against secure contract', function () {
context('against secure contract', function () {
beforeEach(async function () {
this.bounty = await SecureTargetBounty.new({ from: owner });
});

it('sets reward', async function () {
it('can set reward', async function () {
await sendReward(owner, this.bounty.address, reward);

const balance = await ethGetBalance(this.bounty.address);
balance.should.be.bignumber.eq(reward);
});

it('empties itself when destroyed', async function () {
await sendReward(owner, this.bounty.address, reward);

const balance = await ethGetBalance(this.bounty.address);
balance.should.be.bignumber.eq(reward);

await this.bounty.destroy({ from: owner });

const updatedBalance = await ethGetBalance(this.bounty.address);
updatedBalance.should.be.bignumber.eq(0);
});
context('with reward', function () {
beforeEach(async function () {
const result = await this.bounty.createTarget({ from: researcher });
const event = await expectEvent.inLogs(result.logs, 'TargetCreated');

it('cannot claim reward', async function () {
const result = await this.bounty.createTarget({ from: researcher });
const event = await expectEvent.inLogs(
result.logs,
'TargetCreated'
);
this.targetAddress = event.args.createdAddress;

const targetAddress = event.args.createdAddress;
await sendReward(owner, this.bounty.address, reward);
await sendReward(owner, this.bounty.address, reward);

const balance = await ethGetBalance(this.bounty.address);
balance.should.be.bignumber.eq(reward);
const balance = await ethGetBalance(this.bounty.address);
balance.should.be.bignumber.eq(reward);
});

await assertRevert(
this.bounty.claim(targetAddress, { from: researcher }),
);
it('cannot claim reward', async function () {
await assertRevert(
this.bounty.claim(this.targetAddress, { from: researcher }),
);
});
});
});

describe('Against broken contract', function () {
context('against broken contract', function () {
beforeEach(async function () {
this.bounty = await InsecureTargetBounty.new();
});

it('claims reward', async function () {
const result = await this.bounty.createTarget({ from: researcher });
const event = await expectEvent.inLogs(result.logs, 'TargetCreated');

const targetAddress = event.args.createdAddress;
this.targetAddress = event.args.createdAddress;
await sendReward(owner, this.bounty.address, reward);
});

const balance = await ethGetBalance(this.bounty.address);
balance.should.be.bignumber.eq(reward);

await this.bounty.claim(targetAddress, { from: researcher });
const claim = await this.bounty.claimed.call();
it('can claim reward', async function () {
await this.bounty.claim(this.targetAddress, { from: researcher });
const claim = await this.bounty.claimed();

claim.should.eq(true);

await this.bounty.withdrawPayments({ from: researcher });
const researcherPrevBalance = await ethGetBalance(researcher);

const gas = await this.bounty.withdrawPayments.estimateGas({ from: researcher });
const gasPrice = web3.toWei(1, 'gwei');
const gasCost = (new web3.BigNumber(gas)).times(gasPrice);

await this.bounty.withdrawPayments({ from: researcher, gasPrice: gasPrice });
const updatedBalance = await ethGetBalance(this.bounty.address);
updatedBalance.should.be.bignumber.eq(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also test:

a) that the researcher got the reward (curr - prev balances = reward)
b) that the bounty is no longer payable after claiming


const researcherCurrBalance = await ethGetBalance(researcher);
researcherCurrBalance.sub(researcherPrevBalance).should.be.bignumber.eq(reward.sub(gasCost));
});

context('reward claimed', function () {
beforeEach(async function () {
await this.bounty.claim(this.targetAddress, { from: researcher });
});

it('should no longer be payable', async function () {
await assertRevert(
sendReward(owner, this.bounty.address, reward)
);
});
});
});
});