Skip to content

Commit

Permalink
w2-2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xyue committed Mar 18, 2023
1 parent 9f479af commit e116745
Show file tree
Hide file tree
Showing 10 changed files with 16,238 additions and 0 deletions.
11 changes: 11 additions & 0 deletions w2-2/Score/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

13 changes: 13 additions & 0 deletions w2-2/Score/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Sample Hardhat Project

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.

Try running some of the following tasks:

```shell
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.js
```
55 changes: 55 additions & 0 deletions w2-2/Score/contracts/Score.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

interface IScore {
event SetScore(address indexed student, uint256 score);

function setScore(address student, uint256 score) external;
function getScore(address student) external view returns(uint256 score);

}

contract Score is IScore {
address public teacher;
address public owner;
mapping(address => uint256) private scores;

error NotOwner();
error NotTeacher();
error InvalidScore();
error ZeroAddress();

constructor(address teacher_) {
owner = msg.sender;
teacher = teacher_;
}

modifier onlyTeacher() {
if(msg.sender != teacher) revert NotTeacher();
_;
}

modifier onlyOwner() {
if(msg.sender != owner) revert NotOwner();
_;
}

function setScore(address student, uint256 score) onlyTeacher external override {
if(msg.sender == address(0)) revert ZeroAddress();
if(score > 100) revert InvalidScore();
scores[student] = score;
emit SetScore(student,score);
}

function getScore(address student) external view override returns (uint256){
return scores[student];
}

function setTeacher(address teacher_) onlyOwner external {
teacher = teacher_;
}

function getTeacher() external view returns(address) {
return teacher;
}
}
12 changes: 12 additions & 0 deletions w2-2/Score/contracts/Teacher.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import "./Score.sol";

contract Teacher{

function callSetScore(IScore scoreContract, address student, uint256 score) external {
scoreContract.setScore(student, score);
}


}
21 changes: 21 additions & 0 deletions w2-2/Score/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-etherscan");
const dotenv = require("dotenv");

dotenv.config();

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.18",
networks: {
hardhat: {},
goerli: {
url: process.env.GOERLI_RPC_URL,
accounts: [process.env.PRIVATE_KEY]
}
},
etherscan: {
apiKey: process.env.ETHERSCAN_KEY
},

};
Loading

0 comments on commit e116745

Please sign in to comment.