This exam layout needs a larger screen. Please open it on a desktop computer.
Theory
Answer the following questions to the best of your ability. Each question has exactly one correct answer. There are no negative marks for incorrect answers.
Coding - Solidity: Powers of Two
The repository you downloaded contains a Hardhat starter project. Open the file in hardhat/contracts/PowersOfTwo.sol (that's where you will write your solution for this task). The file already has the boilerplate code written for you.
Your job is to write a contract for playing a game called "Powers of Two". The user should guess successive powers of two (2, 4, 8, 16, 32, etc), and in each step, if they answer correctly they get an extra 1 point, and if they answer incorrectly they lose a point (but if they already have 0 points, they can't go negative, i.e. the score will still remain 0 for that user). The contract should use a mapping, so that multiple addresses can play individually (i.e. we should keep track of each address's points and last correct guess).
For example, if we know the last correct guess for address X was the number 8 and that they have 3 points, then if they answer 16 we can save that number as their last correct guess and increase their points to 4. We keep those details for everyone that plays our game in the same contract.
To be more specific, the contract should have the following functions:
- points(addr) - which should return the number of points for the account with address addr
- last(addr) - which should return the last correctly guessed power of two by address addr (it should start with 1, if the user hasn't guessed anything yet)
- answer(num) - for sending a number, which is the user's guess for the next power of two (this function should also update the points we store for the user)
You can build the contract by going into the hardhat directory, and running the following command:
npx hardhat build
Hint: you have an example contract (see Documentation links on the top) to help you get started.
Coding - React: Powers of Two
The repository you downloaded contains a React+Wagmi starter project. You can start the development server by running npm run dev. Your job is to create a simple page that shows the balance of the connected wallet account, and use a contract called "Powers of Two" which allows the account to play a simple game. The user interface should show the number of points, the last power of two provided correctly by the user, and to allow the user to submit an answer for the next power of two. A designer on our team has already created the design for the web app. Your job is to create the frontend in React.
The page (design)
The page should retrieve the necessary information from the blockchain, including the wallet balance in the first card, the number of points in the second card, and the last correct number (which is shown in the last card as "X*2="). It should also enable the user to answer the question for the next power of two (the answer to the question X*2). If the wallet is not connected, the page should just redirect the user to the Status tab, so the user can connect their wallet.
Keep in mind that you shouldn't recreate the whole design, as the layout is already part of the starter template (this includes the header, the footer, and the sidebar). You should only add the missing component(s) from the main section (in the HTML source code, this will be available under the "main-content-container" element). You can create the page in "pages/home".
If you solved the previous task by writing a valid contract in Solidity, you can use that contract for testing. However, it's recommended that you use the contract deployed at address 0x5B495d0e92A8bC50b72D8502c5EfDaBea104E4a0 instead (since it's already verified and tested), and to use the ABI available below (which you can save in a file and use it by importing it whenever you need to access the contract functionality).
The contract has the following functions:
- points(addr) - which returns the number of points for the account with address addr
- last(addr) - which returns the last correctly guessed power of two by address addr (and starts with 1)
- answer(num) - for sending a number, which is the guess for the next power of two
Important links: