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 - Viem: Transaction Explorer
In the repository you downloaded, there is a directory called /scripts. Inside, there is a file called transaction-explorer.ts, which is used to analyze a transaction on the main Ethereum network. Your job is to complete the script so that it prints the following information for each transaction:
- The transaction hash
- The block number
- The transaction sender (as an address)
- The current balance (in ETH) of the sender (as read from the blockchain)
- The transaction receiver (as an address)
- The current balance (in ETH) of the receiver (as read from the blockchain)
- The transaction amount (in ETH)
- The transaction fee, i.e. the effective gas price multiplied by the gas used (shown in both ETH and Gwei)
- The transaction status (for example, if it's successful or reverted)
You can test the script by running the following command:
npx tsx scripts/transaction-explorer.ts
Here are two example transactions (one successful and one failed), which you can use to test your script:
| Transaction Hash | Etherscan Link |
|---|---|
| 0x078a1bbbf7cb870db6fc02e7eaf843600754a6758a062cd85fe24ee065ac4e9c | View on Etherscan |
| 0xf87645aa0740109eb9bd8a59525572faf39d508681c7ffd073f763fb94ceea58 | View on Etherscan |
Hint: you can start with the getTransaction function from the Viem library, and analyze its output with console.log(). Note that you will need to use additional functions to retrieve all the required information.
Coding - Hardhat: Subscription Contract
In the repository you downloaded, there is a directory called /contracts (for storing smart contracts), and a directory called /test (for test files). In the first directory, there is a file called Subscription.sol (where you need to write a smart contract in Solidity); while in the second directory, there is a file called Subscription.ts (where you need to write tests for the contract).
The contract is meant to be used by a company to track lifetime subscriptions for their customers. It needs to implement the following functionality:
- Ability for accounts to purchase a standard subscription for 1 ETH.
- Ability for accounts who already have a subscription to upgrade to a premium subscription for +5 ETH.
- Ability for the creator of the contract to withdraw the funds from the contract.
- A view function to get the current subscription status of an address (which should return either: "standard", "premium", or "none").
- The contract should validate the inputs and payments appropriately, and return errors if something is not correct.
You can build the contracts by running the following command:
npx hardhat build
Once you start adding tests, you can run them with the following command:
npx hardhat test
Hint: you have an example contract and tests (see Counter.sol and Counter.ts) to help you get started. To optimize storage and gas, you should use an integer to store the subscription type in a mapping (or use two mappings which store a boolean).