diff --git a/contracts/src/levels/index.js b/contracts/src/levels/index.js new file mode 100644 index 000000000..e9b80f7d6 --- /dev/null +++ b/contracts/src/levels/index.js @@ -0,0 +1,10 @@ +import SimpleMath from './SimpleMath'; + +export const levels = [ + ...previousLevels, + { + name: "Simple Math", + contract: SimpleMath, + description: "Overflow the multiplication to solve the contract!" + } +]; \ No newline at end of file diff --git a/contracts/test/levels/SimpleMath.sol b/contracts/test/levels/SimpleMath.sol new file mode 100644 index 000000000..8346afb99 --- /dev/null +++ b/contracts/test/levels/SimpleMath.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract SimpleMath { + uint8 public result; + + function multiply(uint8 a, uint8 b) public { + result = a * b; // players will try to overflow + } + + function isSolved() public view returns (bool) { + return result < 255; // simple check for overflow + } +} \ No newline at end of file diff --git a/contracts/test/levels/SimpleMath.test.js b/contracts/test/levels/SimpleMath.test.js new file mode 100644 index 000000000..08a1ab5c3 --- /dev/null +++ b/contracts/test/levels/SimpleMath.test.js @@ -0,0 +1,17 @@ +const { expect } = require("chai"); + +describe("SimpleMath Level", function () { + let contract; + + beforeEach(async function () { + const SimpleMath = await ethers.getContractFactory("SimpleMath"); + contract = await SimpleMath.deploy(); + await contract.deployed(); + }); + + it("should overflow the result", async function () { + await contract.multiply(20, 13); // 20*13 = 260 > uint8 max 255 + const result = await contract.result(); + expect(result).to.be.lt(260); // overflow happened + }); +}); \ No newline at end of file