Principles of Distributed Ledgers: Coursework
Objective The objective of this coursework is to develop a Solidity contract, HumanResources, which implements
a human resources (HR) payment system and to deploy it on Optimism. This contract will enablea human resources manager to manage employee registrations,terminations, and allow employees towithdraw their salary.
Key Goals
- Implement role-based access control and enforce functions to be callable only by the HR manageror registered employees.
- Allow to register and terminate employees.
- Enable employees to withdraw salaries in USDC or ETH based on their preference.
- Use an oracle to get the current ETH price and an AMM to perform swaps from USDC to ETHfor salary disbursement in ETH.
Instructions Your task is to build the HumanResources contract that implements the given IHumanResourcesinterface and deploy it to Optimism. You are free to use any Solidity library you want, and to interactwith any other contract on Optimism. However, you must ensure that you follow the specificationsvery carefully. The correctness of the contract will be graded automatically, so failure to follow thspecifications will result in deducted marks.Project Specifications
Assumptions
- The HumanResources contract will hold USDC only. This USDC will be sent by the HR managerusing a normal transfer.
- There are two versions of USDC on Optimism (USDC and USDC.e). In this coursework, we exclusively use the USDC version deployed at this address: 0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85.
- For the purpose of this coursework, we assume 1 USD = 1 USDC
- 1 Contract Setup and Role Management
- Define the contract HumanResources that implements IHumanResources (see below). Do notmodify the given IHumanResources interface.The contract should have an hrManager. Only this address should be able to register andterminate employees. This address should be set when the contract is created and should not bemodifiable by anyone.
- Use the NotAuthorized error when an unauthorized user tries to call restricted functions.
- Registering and Managing Employees
- registerEmployee:
– Register employees with an address and a weeklyUsdSalary in USD, scaled with 18 decimals.– Emit the EmployeeRegistered event.– If the employee is already registered, revert with EmployeeAlreadyRegistered.– If an employee was terminated and is re-registered, his salary shall start accumulating againfrom the point at which he is registered again.
terminateEmployee:– Allow only the HR Manager to terminate an employee’s contract.– The employee’s salary accumulation should stop upon termination.
– Emit the EmployeeTerminated event.– If the employee is not registered, revert with EmployeeNotRegistered.
- Salary Accumulation and Withdrawal
- Accrual Logic:– Salary accrues continuously based on the weekly USD salary. For example, after 2 days,2/7ths of the weekly salary is available for withdrawal.– We do not considerweekends and non-working hours in this coursework. The salary shouldbe accumulated linearly with time.
withdrawSalary:– Allows employees to withdraw their accumulated salary in their preferred currency (USDCor ETH).– When in ETH mode and a withdrawal is initiated, swap the appropriate amount of USDCto ETH. The swap can use wrapped ETH (WETH)1 , but the salary must be paid in ETH,not in WETH. The ETH should be sent in a re-entrancy safe manner (this will be coveredin depth i代寫Principles of Distributed Ledgers n lecture 8).– Use an oracle to get the current ETH price and protect against AMM price manipulation.– Emit the SalaryWithdrawn event with isEth indicating the currency withdrawn.
1WETH is an ERC20 representation of ETH. The WETH smart contract allows anyone to lock ETH and mintWETH, an ERC20 token representation, where 1 WETH = 1 ETH. One may also redeem 1 ETH for 1 WETH token.The WETH address on Optimism is: 0x4200000000000000000000000000000000000006
2– Note: USDC has six decimals. Hence, when performing calculations between USDC andother assets (e.g., ETH), be careful to scale the values appropriately.
Currency Switching:
– Employees can call switchCurrency to toggle between receiving salary in USDC and ETH.
– The current pending salary shall be withdrawn automatically when this is called.– The default currency shall be USDC.– Emit the CurrencySwitched event whenever an employee toggles their preferred currency.
- Using AMMs and Oracles
We do not enforce any AMM or oracle for this coursework but we recommend using Uniswap as the
AMM and Chainlink as the oracle.
- Chainlink Integration (Oracle):
– The Chainlink feed for ETH/USD on Optimism can be found at the following address:0x13e3Ee699D1909E989722E753853AE30b17e08c5. The interface of this contract can befound here: AggregatorV3Interface.sol.– For the purpose of this coursework, you do not need to follow best practices such as checkingfor the time of the latest price update
- Uniswap Integration (AMM):
– We recommend using the Uniswap swap router that can be found here for Optimism:0xE592427A0AEce92De3Edee1F18E0157C05861564. The interface of this contractcan behere: IV3SwapRouter.sol.– Ensure that the swap is protected against front-running and excessive slippage. The mostcommon way to protect against front-running slippageis to pass a minimumamount out tothe swap function used. For this coursework, if the ETH returned from the swap is under2% less that what would be expected at the current price, the function should .
- Views and Employee Information
- salaryAvailable:
– Returns the available salary for an employee in the preferred currency (USDC or ETH).The amount must be scaled with the correct number of decimals for the currency.hrManager:– Returns the HR manager’s addressgetActiveEmployeeCount:– Returns thcount of currently active (non-terminated) employees.
getEmployeeInfo:– Returns the employee’s weeklyUsdSalary, employedSince timestamp (the time at which
the employee was registered, or re-registered after a termination), and terminatedAt timestamp if terminated, or zero if the employee is active (either non-terminated or re-registered).– If the employee does not exist, the function shall not revert but all returned values must bezero.
3 Deployment to Optimism
- Claim some ETH from the faucet. Make sure that you have control over the address that receivesthe ETH. NOTE: the faucet has been reset so that everyone can claim again.
- Deploy the contract to Optimism. Do NOT verify the contract.
- Register the contract address you deployed by calling the submit function of this contract:0xbe41d42e2d1373120b24dd27a5465d8ef48b9d34. IMPORTANT: use the same address you used when claiming ETH in step 1 to call the submit function, since it is what will useto identify your submission.
- You can re-submit as many times as you want. Your latest submission (until the deadline) will
be used to run the tests.
Testing Requirements Your submission should include a suite of tests covering all the functions in the interface for bothsuccess and failure cases (e.g., unauthorized user calling).To test integration with external contracts, you will need to use fork testing and use contractsdeployed on Optimism. Fork testing can be slow, so we recommend explicitly setting the block numberto make subsequent calls faster.
Submission Requirements
The submission should be a single zip file that follows the regular Foundry project structure. Please donot nest the project when zipping it (i.e., README.md and other foundry files should be at the rootof the zip file). The commands forge build and forge test MUST workwhen ran after unzippingthe submission.The submission must contain at least the following content:
- Contract Code: The code of the deployed contract. It should be in a file called src/HumanResources.sol.
- Other code: Any other code required to compile and test the application. This includes but isnot limited to foundry.toml and the content of the lib directory.Deployment Address: Provide the address of the deployed contract on Optimism. It shouldbe written in a file called deployed-address.txt, placed in the root folder, that only containsthis address.
- Transaction hash: Submit the transaction hash of calling the submit function described in 6above. It should be written in a file called submit-transaction.txt, placed in the root folder,that only contains this transaction hash.
- Test Suite: Include a full test suite for your contract (written in Solidity with Foundry). Testsshould be in the test directory.
- Documentation: Brief documentation explaining how your contract implements each functionin the IHumanResources interface and a summary of how the AMM and the oracle are integrated.The documentation should be in a README.md file placed in the rootfolder.You are free to add other relevant files to the submission but please do not include your .git folderor other files that are not relevant to it.
4Evaluation Criteria Your contract will be evaluated based on the following criteria:
- Correctness: Does the contract meet all the interface requirements and handle edge cases?
- Security: Is access control properly implemented? Are error messages used appropriately?
- Integration: Does the contract integrate correctly with the AMM and the oracle?
- Code Quality: Is the code readable, maintainable, and well-documented?
- Testing: Does the test suite thoroughly cover different scenarios and edge cases?
IMPORTANT The correctness, security, and integration parts of the grade will be given bytesting your deployed contract against our test suite. If your contract is not deployed correctly ordoes not implement the provided interface, you will not get any points for this part.5Solidity Interface