BreakInvariantBounty.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. await this.bounty.withdrawPayments(researcher, { gasPrice: 0 });
  55. const updatedBalance = await ethGetBalance(this.bounty.address);
  56. updatedBalance.should.be.bignumber.equal(0);
  57. const researcherCurrBalance = await ethGetBalance(researcher);
  58. researcherCurrBalance.sub(researcherPrevBalance).should.be.bignumber.equal(reward);
  59. });
  60. it('cannot claim reward from non-target', async function () {
  61. await assertRevert(
  62. this.bounty.claim(nonTarget, { from: researcher })
  63. );
  64. });
  65. context('reward claimed', function () {
  66. beforeEach(async function () {
  67. await this.bounty.claim(this.targetAddress, { from: researcher });
  68. });
  69. it('should no longer be payable', async function () {
  70. await assertRevert(
  71. sendReward(owner, this.bounty.address, reward)
  72. );
  73. });
  74. });
  75. });
  76. });