-
Notifications
You must be signed in to change notification settings - Fork 7
/
Unit 5 - Solidity Basics Practical.html
86 lines (83 loc) · 5.68 KB
/
Unit 5 - Solidity Basics Practical.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome file</title>
<link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>
<body class="stackedit">
<div class="stackedit__html"><hr>
<h3 id="constructors-and-inheritance-in-solidity"><strong>1. Constructors and Inheritance in Solidity</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>Parent</code> that initializes a public state variable <code>owner</code> to the deployer’s address using a constructor. Then, write a contract <code>Child</code> that inherits from <code>Parent</code> and adds a constructor that accepts an integer <code>age</code> as a parameter and stores it in a public variable <code>age</code>.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="interacting-between-contracts-in-solidity"><strong>2. Interacting Between Contracts in Solidity</strong></h3>
<p><strong>Question</strong>:<br>
Write two contracts, <code>Caller</code> and <code>Callee</code>. In <code>Callee</code>, write a public function <code>multiply(uint x, uint y)</code> that returns the product of <code>x</code> and <code>y</code>. In <code>Caller</code>, write a function <code>callMultiply</code> that calls <code>multiply</code> from <code>Callee</code> and returns the result.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="errors-checks-and-conditions-in-solidity"><strong>3. Errors, Checks, and Conditions in Solidity</strong></h3>
<p><strong>Question</strong>:<br>
Write a function <code>withdraw(uint amount)</code> inside a contract <code>Bank</code> that allows the caller to withdraw ether only if their balance is greater than or equal to <code>amount</code>. If not, it should revert the transaction with a custom error message “Insufficient balance”.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="enums-access-control-and-function-updates-in-solidity"><strong>4. Enums, Access Control, and Function Updates in Solidity</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>Voting</code> where an enum <code>State</code> has values <code>Created</code>, <code>Voting</code>, and <code>Ended</code>. Implement a modifier <code>onlyOwner</code> that restricts access to the contract owner, and use it in the function <code>startVoting</code> which transitions the state from <code>Created</code> to <code>Voting</code>.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="events-in-solidity-emitting-logs"><strong>5. Events in Solidity: Emitting Logs</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>Token</code> that allows users to transfer tokens. Emit an event <code>Transfer</code> with parameters <code>from</code>, <code>to</code>, and <code>amount</code> whenever a transfer occurs.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="functions-in-solidity-types-and-use-cases"><strong>6. Functions in Solidity: Types and Use Cases</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>Calculator</code> with two public functions: <code>add(uint a, uint b)</code> and <code>subtract(int a, int b)</code>. The <code>add</code> function should return the sum, and the <code>subtract</code> function should return the difference.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="inheritance-in-solidity-with-function-overrides"><strong>7. Inheritance in Solidity with Function Overrides</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>Animal</code> with a virtual function <code>makeSound</code> that returns <code>"Animal sound"</code>. Then, create a contract <code>Dog</code> that overrides <code>makeSound</code> and returns <code>"Bark!"</code>.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="access-control-using-onlyowner-modifier"><strong>8. Access Control: Using <code>onlyOwner</code> Modifier</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>Store</code> where only the owner can set the price of a product using the <code>setPrice(uint _price)</code> function. Use the <code>onlyOwner</code> modifier to restrict access.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="reentrancy-guard-example"><strong>9. Reentrancy Guard Example</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>SafeBank</code> where users can deposit and withdraw Ether. Implement a reentrancy guard on the <code>withdraw</code> function to prevent reentrancy attacks.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
<h3 id="using-events-for-logging-function-calls"><strong>10. Using Events for Logging Function Calls</strong></h3>
<p><strong>Question</strong>:<br>
Write a contract <code>Counter</code> that has a public function <code>increment</code> which increases a counter by 1. Emit an event <code>Incremented</code> every time the function is called with the new counter value.</p>
<p><strong>Answer</strong>:</p>
<pre class=" language-solidity"><code class="prism language-solidity">
</code></pre>
<hr>
</div>
</body>
</html>