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