BreakInvariantBounty.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');
  2. const expectEvent = require('./helpers/expectEvent');
  3. const { assertRevert } = require('./helpers/assertRevert');
  4. const SecureInvariantTargetBounty = artifacts.require('SecureInvariantTargetBounty');
  5. const InsecureInvariantTargetBounty = artifacts.require('InsecureInvariantTargetBounty');
  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('BreakInvariantBounty', function ([_, owner, researcher, nonTarget]) {
  16. context('against secure contract', function () {
  17. beforeEach(async function () {
  18. this.bounty = await SecureInvariantTargetBounty.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.equal(reward);
  24. });
  25. context('with reward', function () {
  26. beforeEach(async function () {
  27. const result = await this.bounty.createTarget({ from: researcher });
  28. const event = 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.equal(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 InsecureInvariantTargetBounty.new();
  44. const result = await this.bounty.createTarget({ from: researcher });
  45. const event = 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.equal(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.equal(0);
  60. const researcherCurrBalance = await ethGetBalance(researcher);
  61. researcherCurrBalance.sub(researcherPrevBalance).should.be.bignumber.equal(reward.sub(gasCost));
  62. });
  63. it('cannot claim reward from non-target', async function () {
  64. await assertRevert(
  65. this.bounty.claim(nonTarget, { from: researcher })
  66. );
  67. });
  68. context('reward claimed', function () {
  69. beforeEach(async function () {
  70. await this.bounty.claim(this.targetAddress, { from: researcher });
  71. });
  72. it('should no longer be payable', async function () {
  73. await assertRevert(
  74. sendReward(owner, this.bounty.address, reward)
  75. );
  76. });
  77. });
  78. });
  79. });