BreakInvariantBounty.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');
  2. const { ether } = require('./helpers/ether');
  3. const { sendEther } = require('./helpers/sendTransaction');
  4. const { balanceDifference } = require('./helpers/balanceDiff');
  5. const expectEvent = require('./helpers/expectEvent');
  6. const { assertRevert } = require('./helpers/assertRevert');
  7. const BreakInvariantBountyMock = artifacts.require('BreakInvariantBountyMock');
  8. const TargetMock = artifacts.require('TargetMock');
  9. require('chai')
  10. .use(require('chai-bignumber')(web3.BigNumber))
  11. .should();
  12. const reward = ether(1);
  13. contract('BreakInvariantBounty', function ([_, owner, researcher, anyone, nonTarget]) {
  14. beforeEach(async function () {
  15. this.bounty = await BreakInvariantBountyMock.new({ from: owner });
  16. });
  17. it('can set reward', async function () {
  18. await sendEther(owner, this.bounty.address, reward);
  19. (await ethGetBalance(this.bounty.address)).should.be.bignumber.equal(reward);
  20. });
  21. context('with reward', function () {
  22. beforeEach(async function () {
  23. await sendEther(owner, this.bounty.address, reward);
  24. });
  25. describe('claim', function () {
  26. it('is initially claimable', async function () {
  27. (await this.bounty.claimable()).should.equal(true);
  28. });
  29. it('can create claimable target', async function () {
  30. const { logs } = await this.bounty.createTarget({ from: researcher });
  31. expectEvent.inLogs(logs, 'TargetCreated');
  32. });
  33. context('with target', async function () {
  34. beforeEach(async function () {
  35. const { logs } = await this.bounty.createTarget({ from: researcher });
  36. const event = expectEvent.inLogs(logs, 'TargetCreated');
  37. this.target = TargetMock.at(event.args.createdAddress);
  38. });
  39. context('before exploiting vulnerability', async function () {
  40. it('reverts when claiming reward', async function () {
  41. await assertRevert(this.bounty.claim(this.target.address, { from: researcher }));
  42. });
  43. });
  44. context('after exploiting vulnerability', async function () {
  45. beforeEach(async function () {
  46. await this.target.exploitVulnerability({ from: researcher });
  47. });
  48. it('sends the reward to the researcher', async function () {
  49. await this.bounty.claim(this.target.address, { from: anyone });
  50. (await balanceDifference(researcher, () => this.bounty.withdrawPayments(researcher)))
  51. .should.be.bignumber.equal(reward);
  52. (await ethGetBalance(this.bounty.address)).should.be.bignumber.equal(0);
  53. });
  54. context('after claiming', async function () {
  55. beforeEach(async function () {
  56. await this.bounty.claim(this.target.address, { from: researcher });
  57. });
  58. it('is not claimable', async function () {
  59. (await this.bounty.claimable()).should.equal(false);
  60. });
  61. it('no longer accepts rewards', async function () {
  62. await assertRevert(ethSendTransaction({ from: owner, to: this.bounty.address, value: reward }));
  63. });
  64. it('reverts when reclaimed', async function () {
  65. await assertRevert(this.bounty.claim(this.target.address, { from: researcher }));
  66. });
  67. });
  68. });
  69. });
  70. context('with non-target', function () {
  71. it('reverts when claiming reward', async function () {
  72. await assertRevert(this.bounty.claim(nonTarget, { from: researcher }));
  73. });
  74. });
  75. });
  76. describe('cancelBounty', function () {
  77. context('before canceling', function () {
  78. it('is claimable', async function () {
  79. (await this.bounty.claimable()).should.equal(true);
  80. });
  81. it('can be canceled by the owner', async function () {
  82. const { logs } = await this.bounty.cancelBounty({ from: owner });
  83. expectEvent.inLogs(logs, 'BountyCanceled');
  84. (await balanceDifference(owner, () => this.bounty.withdrawPayments(owner)))
  85. .should.be.bignumber.equal(reward);
  86. });
  87. it('reverts when canceled by anyone', async function () {
  88. await assertRevert(this.bounty.cancelBounty({ from: anyone }));
  89. });
  90. });
  91. context('after canceling', async function () {
  92. beforeEach(async function () {
  93. await this.bounty.cancelBounty({ from: owner });
  94. });
  95. it('is not claimable', async function () {
  96. (await this.bounty.claimable()).should.equal(false);
  97. });
  98. it('no longer accepts rewards', async function () {
  99. await assertRevert(ethSendTransaction({ from: owner, to: this.bounty.address, value: reward }));
  100. });
  101. it('reverts when recanceled', async function () {
  102. await assertRevert(this.bounty.cancelBounty({ from: owner }));
  103. });
  104. });
  105. });
  106. });
  107. });