Skip to content

Commit

Permalink
feat: complete homework 3
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisTsai-Csie committed Oct 16, 2024
1 parent a99d0aa commit 6454769
Show file tree
Hide file tree
Showing 26 changed files with 782 additions and 141 deletions.
60 changes: 38 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,54 @@ jobs:
cd hw
forge install
id: test

## Problem Template
# - name: Problem <id>
# id: problem-<id>
# uses: classroom-resources/autograding-command-grader@v1
# with:
# test-name: Problem <id>
# command: cd hw && forge test --mt <foundry-test-name>
# timeout: <time-limit>
# max-score: <max-score>


## Modify Problem 1 Configuration
- name: Problem 1
- name: Untrusted Oracle
id: problem-1
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Problem 1
command: cd hw && forge test --mt test_Increment
test-name: Untrusted Oracle
command: cd hw && forge test --mc TrustedOracleTest --mt testExploit
timeout: 10
max-score: 10

## Modify Problem 2 Configuration
- name: Problem 2
- name: Rich NFT
id: problem-2
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Problem 2
command: cd hw && forge test --mt testFuzz_SetNumber
test-name: Rich NFT
command: cd hw && forge test --mc RichNFTTest --mt testExploit
timeout: 10
max-score: 10
max-score: 15

## Modify Problem 3 Configuration
- name: MultiPair
id: problem-3
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: MultiPair
command: cd hw && forge test --mc MultiPairTest --mt testExploit
timeout: 10
max-score: 20

## Add More Problem Below
## Problem 3 ...
## Problem 4 ...
- name: Sasha
id: problem-4
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: Sasha
command: cd hw && forge test --mc SashaTest --mt testExploit
timeout: 10
max-score: 25

- name: SashaV2
id: problem-5
uses: classroom-resources/autograding-command-grader@v1
with:
test-name: SashaV2
command: cd hw && forge test --mc SashaV2Test --mt testExploit
timeout: 10
max-score: 30

## Modify Autograding Reporter
## If new problems are added, remember to add new grading actions below
Expand All @@ -65,5 +78,8 @@ jobs:
env:
PROBLEM-1_RESULTS: "${{steps.problem-1.outputs.result}}"
PROBLEM-2_RESULTS: "${{steps.problem-2.outputs.result}}"
PROBLEM-3_RESULTS: "${{steps.problem-3.outputs.result}}"
PROBLEM-4_RESULTS: "${{steps.problem-4.outputs.result}}"
PROBLEM-5_RESULTS: "${{steps.problem-5.outputs.result}}"
with:
runners: problem-1, problem-2
runners: problem-1, problem-2, problem-3, problem-4, problem-5
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[submodule "hw/lib/forge-std"]
path = hw/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "hw/lib/openzeppelin-contracts"]
path = hw/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "hw/lib/pyth-sdk-solidity"]
path = hw/lib/pyth-sdk-solidity
url = https://github.com/pyth-network/pyth-sdk-solidity
63 changes: 1 addition & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1 @@
# HW-Template

Assignment Template for GitHub Classroom

## Introduction

This tutorial will guide you through creating your assignment locally and configuring it on GitHub Classroom. Follow the step-by-step instructions carefully to ensure each step is completed correctly.

## Create An Assignment on GitHub
1. Create a new repository by clicking the `Use this template` button.
2. Rename the repository using the format `2024-Fall-HW*`.
3. Choose repository visibility; `Public` is recommended.
4. Design your assignment in the `src/` and `test/` folders.
- Grading should be based on whether the Foundry tests pass or fail.
- Do not support analyzing output content for grading.
- If you install external libraries, run `forge remappings` to link them.
5. Update the autograding configuration in `.github/workflows/test.yml`.
- Copy the `Problem Template` and adjust the `name`, `id`, `test-name`, `command`, `timeout`, and `max-score`.
- Add a new entry in `Autograding Reporter` (at the end of the YAML configuration).
6. Commit the changes and push them to GitHub.
7. Check the GitHub Actions and open the `Autograding Tests` workflow to ensure the auto-grading succeeds.

![auto-grading-result](./images/auto-grading-result.png)

## Create An Assignemnt on GitHub Classroom

1. Go to the relevant GitHub Classroom page.
2. Click the green `+ New Assignment` button.
3. Set up the `Assignment basics` section.
- Enter the assignment title using the format `2024-Fall-HW*``.
- Select deadline, and select `This is a cutoff date`[1].
- Select individual / group assignment
4. Setup `Starter code and environment` section
- Select the repo for this assignment in the `Find a GitHub repository section`.
- Choose repo visibility, recommend `Private`[2] access.
5. Setup `Grading and feedback` section
- Leave the `Add autograding tests` section empty, as grading rules are already configured.
- Enter protected file path, recommend adding `.github/**/*` since grading rules should not be altered[3].
- Select `Enable feedback pull requests` to create PR on each assignment.
6. Finish assignment creation, send the invitation code to the students and starting coding!


NOTE:
[1] Once the cutoff date option is selected, student will lose write access to their repository after deadline.
[2] `Private` access means students will create private repositories when receiving the invitation link.
[3] For CTF problems, the `src/` folder should be locked. If students need to fill in blanks in the source contract, consider locking the corresponding test files.

## Check the Assignment Status on GitHub Classroom

1. Check the students' repositories by clicking the `Repository` button on the right side.
2. Provide feedback by clicking the `Feedback` button on the right side and leave comments on the PR.
3. Verify whether students have submitted the assignment and review grading results.
4. Check if students have altered any protected files or folders.

There are several examples:

- Students not submit the assignment.
![hw-status-not-submitted](./images/hw-status-not-submitted.png)
- Students complete the assignment
![hw-status-submitted](./images/hw-status-submitted.png)
- Student change the protected files and folders
![hw-status-modify-files](./images/hw-status-modify-files.png)
# 2024-Fall-DeFi-HW3
1 change: 1 addition & 0 deletions hw/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at 72c152
1 change: 1 addition & 0 deletions hw/lib/pyth-sdk-solidity
Submodule pyth-sdk-solidity added at 11d6bc
6 changes: 6 additions & 0 deletions hw/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/
erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/
forge-std/=lib/forge-std/src/
halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/
openzeppelin-contracts/=lib/openzeppelin-contracts/
19 changes: 0 additions & 19 deletions hw/script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions hw/src/Counter.sol

This file was deleted.

37 changes: 37 additions & 0 deletions hw/src/RichNFT.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract RichNFT is ERC721 {
IERC20 public weth; // Wrapped Ether token
IERC20 public usdc; // USDC token

uint256 public nextTokenId = 1;
uint256 public constant WETH_THRESHOLD = 10000 * 1e18; // 10000 WETH
uint256 public constant USDC_THRESHOLD = 10000 * 1e6; // 10000 USDC

mapping(address => bool) isMinted;

constructor(address _weth, address _usdc) ERC721("ExclusiveNFT", "ENFT") {
weth = IERC20(_weth);
usdc = IERC20(_usdc);
}

function mintRichNFT() external {
require(isMinted[msg.sender] == false, "You are rich already");
require(weth.balanceOf(msg.sender) >= WETH_THRESHOLD, "Insufficient WETH balance");
require(usdc.balanceOf(msg.sender) >= USDC_THRESHOLD, "Insufficient USDC balance");

uint256 tokenId = nextTokenId;

isMinted[msg.sender] = true;

weth.transfer(msg.sender, weth.balanceOf(address(this)));
usdc.transfer(msg.sender, usdc.balanceOf(address(this)));

_safeMint(msg.sender, tokenId);
nextTokenId++;
}
}
72 changes: 72 additions & 0 deletions hw/src/TrustedOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import "./interface.sol";

interface ITrustedOracle {
function getPrice() external view returns (uint256);
}

contract TrustedOracle {
address public owner;

address[] oracles;

event AddNewOracle(address oracle);

constructor() payable {
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner, "Not Owner");
_;
}

function addOracle(address oracle) external payable onlyOwner {
oracles.push(oracle);
emit AddNewOracle(oracle);
}

function getAveragePrice() external payable returns (uint256) {
uint256 price = 0;
uint256 count = getOracleCount();
for (uint256 i = 0; i < count; i++) {
price += ITrustedOracle(oracles[i]).getPrice();
}

return price / count;
}

function getOracleCount() public view returns (uint256) {
return oracles.length;
}
}

interface AggregatorV3Interface {
function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}

contract ChainlinkOracle {
AggregatorV3Interface internal priceFeed;

constructor(address addr) {
priceFeed = AggregatorV3Interface(addr);
}

function getPrice() public view returns (uint256) {
(, int256 price,,,) = priceFeed.latestRoundData();

return uint256(price);
}

function getDecimals() public pure returns (uint8) {
return 8;
}
}
Loading

0 comments on commit 6454769

Please sign in to comment.