Bounty.test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');
  2. const expectEvent = require('./helpers/expectEvent');
  3. const { assertRevert } = require('./helpers/assertRevert');
  4. const SecureTargetBounty = artifacts.require('SecureTargetBounty');
  5. const InsecureTargetBounty = artifacts.require('InsecureTargetBounty');
  6. require('chai')
  7. .use(require('chai-bignumber')(web3.BigNumber))
  8. .should();
  9. const sendReward = async (from, to, value) => ethSendTransaction({
  10. from,
  11. to,
  12. value,
  13. });
  14. const reward = new web3.BigNumber(web3.toWei(1, 'ether'));
  15. contract('Bounty', function ([_, owner, researcher]) {
  16. context('against secure contract', function () {
  17. beforeEach(async function () {
  18. this.bounty = await SecureTargetBounty.new({ from: owner });
  19. });
  20. it('can set reward', async function () {
  21. await sendReward(owner, this.bounty.address, reward);
  22. const balance = await ethGetBalance(this.bounty.address);
  23. balance.should.be.bignumber.eq(reward);
  24. });
  25. context('with reward', function () {
  26. beforeEach(async function () {
  27. const result = await this.bounty.createTarget({ from: researcher });
  28. const event = await expectEvent.inLogs(result.logs, 'TargetCreated');
  29. this.targetAddress = event.args.createdAddress;
  30. await sendReward(owner, this.bounty.address, reward);
  31. const balance = await ethGetBalance(this.bounty.address);
  32. balance.should.be.bignumber.eq(reward);
  33. });
  34. it('cannot claim reward', async function () {
  35. await assertRevert(
  36. this.bounty.claim(this.targetAddress, { from: researcher }),
  37. );
  38. });
  39. });
  40. });
  41. context('against broken contract', function () {
  42. beforeEach(async function () {
  43. this.bounty = await InsecureTargetBounty.new();
  44. const result = await this.bounty.createTarget({ from: researcher });
  45. const event = await expectEvent.inLogs(result.logs, 'TargetCreated');
  46. this.targetAddress = event.args.createdAddress;
  47. await sendReward(owner, this.bounty.address, reward);
  48. });
  49. it('can claim reward', async function () {
  50. await this.bounty.claim(this.targetAddress, { from: researcher });
  51. const claim = await this.bounty.claimed();
  52. claim.should.eq(true);
  53. const researcherPrevBalance = await ethGetBalance(researcher);
  54. const gas = await this.bounty.withdrawPayments.estimateGas({ from: researcher });
  55. const gasPrice = web3.toWei(1, 'gwei');
  56. const gasCost = (new web3.BigNumber(gas)).times(gasPrice);
  57. await this.bounty.withdrawPayments({ from: researcher, gasPrice: gasPrice });
  58. const updatedBalance = await ethGetBalance(this.bounty.address);
  59. updatedBalance.should.be.bignumber.eq(0);
  60. const researcherCurrBalance = await ethGetBalance(researcher);
  61. researcherCurrBalance.sub(researcherPrevBalance).should.be.bignumber.eq(reward.sub(gasCost));
  62. });
  63. context('reward claimed', function () {
  64. beforeEach(async function () {
  65. await this.bounty.claim(this.targetAddress, { from: researcher });
  66. });
  67. it('should no longer be payable', async function () {
  68. await assertRevert(
  69. sendReward(owner, this.bounty.address, reward)
  70. );
  71. });
  72. });
  73. });
  74. });