Skip to content

Commit d46491a

Browse files
Initial commit
0 parents  commit d46491a

File tree

8 files changed

+188
-0
lines changed

8 files changed

+188
-0
lines changed

.github/workflows/test.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
env:
9+
FOUNDRY_PROFILE: ci
10+
11+
jobs:
12+
check:
13+
strategy:
14+
fail-fast: true
15+
16+
name: Foundry project
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
submodules: recursive
22+
23+
- name: Install Foundry
24+
uses: foundry-rs/foundry-toolchain@v1
25+
with:
26+
version: nightly
27+
28+
- name: Show Forge version
29+
run: |
30+
forge --version
31+
32+
- name: Run Forge fmt
33+
run: |
34+
forge fmt --check
35+
id: fmt
36+
37+
- name: Run Forge build
38+
run: |
39+
forge build --sizes
40+
id: build
41+
42+
- name: Run Forge tests
43+
run: |
44+
forge test -vvv
45+
id: test

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Compiler files
2+
cache/
3+
out/
4+
5+
# Ignores development broadcast logs
6+
!/broadcast
7+
/broadcast/*/31337/
8+
/broadcast/**/dry-run/
9+
10+
# Docs
11+
docs/
12+
13+
# Dotenv file
14+
.env

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "lib/forge-std"]
2+
path = lib/forge-std
3+
url = https://github.com/foundry-rs/forge-std

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
# Description
3+
4+
In this assignment, you have a solidity contract. This contract has gaps and comments to guide you on what to implement.
5+
6+
7+
The assignment covers Solidity concepts. It focuses on:
8+
- Global variables
9+
- Solidity modifiers
10+
- Events and logs
11+
- Conversions and type casting
12+
- Error handling
13+
14+
15+
# Instructions
16+
17+
Open the contract file located at `src/Assignment6.sol` and follow the comments to implement the missing logic.
18+
19+
## Run Tests:
20+
21+
`forge test`
22+
23+
## Submission
24+
25+
Once all tests pass, commit all your changes to the repo.
26+
27+
Good luck and happy coding!
28+

foundry.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[profile.default]
2+
src = "src"
3+
out = "out"
4+
libs = ["lib"]
5+
6+
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

lib/forge-std

Submodule forge-std added at b93cf4b

src/Assignment6.sol

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
contract Assignment6 {
5+
// 1. Declare an event called `FundsDeposited` with parameters: `sender` and `amount`
6+
7+
// 2. Declare an event called `FundsWithdrawn` with parameters: `receiver` and `amount`
8+
9+
// 3. Create a public mapping called `balances` to tracker users balances
10+
11+
// Modifier to check if sender has enough balance
12+
modifier hasEnoughBalance(uint amount) {
13+
// Fill in the logic using require
14+
_;
15+
}
16+
17+
// Function to deposit Ether
18+
// This function should:
19+
// - Be external and payable
20+
// - Emit the `FundsDeposited` event
21+
function deposit() {
22+
// increment user balance in balances mapping
23+
24+
// emit suitable event
25+
}
26+
27+
// Function to withdraw Ether
28+
// This function should:
29+
// - Be external
30+
// - Take one parameter: `amount`
31+
// - Use the `hasEnoughBalance` modifier
32+
// - Emit the `FundsWithdrawn` event
33+
function withdraw() {
34+
// decrement user balance from balances mapping
35+
36+
// send tokens to the caller
37+
38+
// emit suitable event
39+
40+
}
41+
42+
// Function to check the contract balance
43+
// This function should:
44+
// - Be public and view
45+
// - Return the contract's balance
46+
function getContractBalance() {
47+
// return the balance of the contract
48+
49+
}
50+
}

test/Assignment6.t.sol

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
import {Test, console} from "forge-std/Test.sol";
5+
import {Assignment6} from "../src/Assignment6.sol";
6+
7+
contract Assignment6Test is Test {
8+
Assignment6 assignment;
9+
10+
function setUp() public {
11+
assignment = new Assignment6();
12+
}
13+
14+
function testDeposit() public {
15+
vm.deal(address(this), 1 ether);
16+
vm.expectEmit(true, true, false, true);
17+
emit Assignment6.FundsDeposited(address(this), 1 ether);
18+
assignment.deposit{value: 1 ether}();
19+
uint balance = assignment.balances(address(this));
20+
assertEq(balance, 1 ether, "Balance should be 1 ether");
21+
}
22+
23+
function testWithdraw() public {
24+
vm.deal(address(this), 1 ether);
25+
assignment.deposit{value: 1 ether}();
26+
vm.expectEmit(true, true, false, true); // Match receiver and amount
27+
emit Assignment6.FundsWithdrawn(address(this), 0.5 ether);
28+
assignment.withdraw(0.5 ether);
29+
uint balance = assignment.balances(address(this));
30+
assertEq(balance, 0.5 ether, "Balance should be 0.5 ether");
31+
}
32+
33+
function testContractBalance() public {
34+
vm.deal(address(this), 1 ether);
35+
assignment.deposit{value: 1 ether}();
36+
uint contractBalance = assignment.getContractBalance();
37+
assertEq(contractBalance, 1 ether, "Contract balance should be 1 ether");
38+
}
39+
40+
receive() external payable {}
41+
}

0 commit comments

Comments
 (0)