{"id":17498,"date":"2024-02-17T01:13:04","date_gmt":"2024-02-17T01:13:04","guid":{"rendered":"https:\/\/infinblock.com\/?p=17498"},"modified":"2024-04-06T01:38:40","modified_gmt":"2024-04-06T00:38:40","slug":"fundamental-concepts-of-solidity-programming-2-0","status":"publish","type":"post","link":"https:\/\/infinblock.com\/index.php\/2024\/02\/17\/fundamental-concepts-of-solidity-programming-2-0\/","title":{"rendered":"Fundamental Concepts Of Solidity Programming 2.0"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Topics: Conditions, Loops, Mappings, Nested Mappings, and Enums.<\/h2>\n\n\n\n<p>Diving deeper into the world of Solidity programming, we encounter several fundamental concepts crucial for developing robust and efficient smart contracts. This blog will explore conditional loops, mappings, nested mappings, and enums. We&#8217;ll illustrate these concepts through a smart contract example and provide detailed explanations and comments for better understanding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conditions in Solidity<\/h3>\n\n\n\n<p>Conditions are used to execute certain parts of code only if specified criteria are met. Solidity primarily uses <code>if<\/code>, <code>else if<\/code>, and <code>else<\/code> statements for conditionals.<\/p>\n\n\n\n<ul>\n<li><strong><code>if<\/code> Statement<\/strong>: Executes a block of code if the specified condition is true.<\/li>\n\n\n\n<li><strong><code>else<\/code> Statement<\/strong>: Executes a block of code if the associated <code>if<\/code> condition is false.<\/li>\n\n\n\n<li><strong><code>else if<\/code> Statement<\/strong>: Specifies a new condition to test if the first condition is false.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Loops in Solidity<\/h3>\n\n\n\n<p>Loops are used to execute a block of code multiple times. Solidity supports several types of loops, including <code>for<\/code>, <code>while<\/code>, and <code>do while<\/code>.<\/p>\n\n\n\n<ul>\n<li><strong><code>for<\/code> Loop<\/strong>: Lets you initialize a counter, check a condition, and increment\/decrement the counter all in one line. Useful for iterating a specific number of times.<\/li>\n\n\n\n<li><strong><code>while<\/code> Loop<\/strong>: Repeats a block of code as long as a specified condition is true. The condition is evaluated before the execution of the loop&#8217;s body.<\/li>\n\n\n\n<li><strong><code>do while<\/code> Loop<\/strong>: Similar to the <code>while<\/code> loop, but it guarantees the loop&#8217;s body is executed at least once since the condition is evaluated after the body&#8217;s execution.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Mapping<\/h3>\n\n\n\n<p>Mappings in Solidity are a key-value store for efficiently storing and looking up data. They are somewhat analogous to dictionaries or hash maps in other programming languages but with some unique characteristics suited to the Ethereum Virtual Machine (EVM).<\/p>\n\n\n\n<p><strong>Key Characteristics<\/strong>:<\/p>\n\n\n\n<ul>\n<li>Keys must be of an elementary type like <code>address<\/code>, <code>uint<\/code>, or <code>bytes<\/code>. Custom types or objects cannot be used as keys.<\/li>\n\n\n\n<li>Values can be any type, including another mapping or an array.<\/li>\n\n\n\n<li>Mappings are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type&#8217;s default value.<\/li>\n\n\n\n<li>They do not maintain a record of keys stored in them. As a result, you cannot iterate through keys or values directly in a mapping.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Nested Mapping<\/h3>\n\n\n\n<p>A nested mapping is a mapping that maps a key to another mapping as its value. This structure is useful for more complex data relationships, such as associating a user with specific permissions or properties that are themselves dynamically managed.<\/p>\n\n\n\n<p><strong>Use Case<\/strong>:<\/p>\n\n\n\n<ul>\n<li>Managing permissions or roles for a set of users.<\/li>\n\n\n\n<li>Tracking ownership and properties of assets in a digital marketplace.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Enums<\/h3>\n\n\n\n<p>Enums (enumerated types) are a way to create user-defined types in Solidity. They&#8217;re used to model choice and keep track of state within contracts. Enums help make contracts more readable and safer by restricting variables to a limited set of possible values.<\/p>\n\n\n\n<p><strong>Key Characteristics<\/strong>:<\/p>\n\n\n\n<ul>\n<li>Defined with the <code>enum<\/code> keyword followed by a list of options.<\/li>\n\n\n\n<li>Helpful for managing states, like stages of a contract or ownership status.<\/li>\n<\/ul>\n\n\n\n<p><strong>Changing Enum Values<\/strong>:<\/p>\n\n\n\n<ul>\n<li>Enum values can be changed by explicitly setting them to one of the defined options. This is often done within functions to update the state of a contract based on certain conditions.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example Smart Contract: Voting System<\/h3>\n\n\n\n<p>Let&#8217;s explore these concepts through a simple voting system smart contract:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract VotingSystem {\n    enum Stage {Init, Voting, Ended} \/\/ Enum to represent stages of voting\n    Stage public stage = Stage.Init; \/\/ Default stage\n\n    mapping(address =&gt; uint) public votesReceived; \/\/ Mapping to track votes\n    mapping(address =&gt; mapping(uint =&gt; bool)) public hasVoted; \/\/ Nested mapping to check if an address has voted for a particular candidate\n    \n    address[] public candidates; \/\/ Dynamic array to store candidates\n\n    \/\/ Modifier to check the current stage\n    modifier atStage(Stage _stage) {\n        require(stage == _stage, &quot;Function cannot be called at this time.&quot;);\n        _;\n    }\n\n    \/\/ Function to add candidates\n    function addCandidate(address _candidate) public atStage(Stage.Init) {\n        candidates.push(_candidate);\n    }\n\n    \/\/ Function to move to the voting stage\n    function startVoting() public atStage(Stage.Init) {\n        stage = Stage.Voting;\n    }\n\n    \/\/ Function to cast a vote\n    function vote(address _candidate, uint _candidateIndex) public atStage(Stage.Voting) {\n        require(!hasVoted[msg.sender][_candidateIndex], &quot;Already voted for this candidate.&quot;);\n        votesReceived[_candidate]++;\n        hasVoted[msg.sender][_candidateIndex] = true;\n    }\n\n    \/\/ Function to end voting\n    function endVoting() public atStage(Stage.Voting) {\n        stage = Stage.Ended;\n    }\n}\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/ SPDX-License-Identifier: MIT<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">pragma<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">solidity<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">^<\/span><span style=\"color: #B48EAD\">0.8<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">contract<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">VotingSystem<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">enum<\/span><span style=\"color: #D8DEE9FF\"> Stage <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #D8DEE9\">Init<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Voting<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Ended<\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ Enum to represent stages of voting<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">Stage<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">stage<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Stage<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Init<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ Default stage<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">mapping<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">address<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">uint<\/span><span style=\"color: #D8DEE9FF\">) <\/span><span style=\"color: #D8DEE9\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">votesReceived<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ Mapping to track votes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">mapping<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">address<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">mapping<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">uint<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">bool<\/span><span style=\"color: #D8DEE9FF\">)) <\/span><span style=\"color: #D8DEE9\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">hasVoted<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ Nested mapping to check if an address has voted for a particular candidate<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">address<\/span><span style=\"color: #D8DEE9FF\">[] <\/span><span style=\"color: #D8DEE9\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">candidates<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ Dynamic array to store candidates<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ Modifier to check the current stage<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">modifier<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">atStage<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">Stage<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">_stage<\/span><span style=\"color: #D8DEE9FF\">) <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">require<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">stage<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">==<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">_stage<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Function cannot be called at this time.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">_<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ Function to add candidates<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">function<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">addCandidate<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">address<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">_candidate<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">atStage<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">Stage.Init<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">candidates<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">push<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">_candidate<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ Function to move to the voting stage<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">function<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">startVoting<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">atStage<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">Stage.Init<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">stage<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Stage<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Voting<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ Function to cast a vote<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">function<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">vote<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">address<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">_candidate<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">uint<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">_candidateIndex<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">atStage<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">Stage.Voting<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #88C0D0\">require<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #81A1C1\">!<\/span><span style=\"color: #D8DEE9\">hasVoted<\/span><span style=\"color: #D8DEE9FF\">[<\/span><span style=\"color: #D8DEE9\">msg<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sender<\/span><span style=\"color: #D8DEE9FF\">][<\/span><span style=\"color: #D8DEE9\">_candidateIndex<\/span><span style=\"color: #D8DEE9FF\">]<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Already voted for this candidate.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">votesReceived<\/span><span style=\"color: #D8DEE9FF\">[<\/span><span style=\"color: #D8DEE9\">_candidate<\/span><span style=\"color: #D8DEE9FF\">]<\/span><span style=\"color: #81A1C1\">++;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">hasVoted<\/span><span style=\"color: #D8DEE9FF\">[<\/span><span style=\"color: #D8DEE9\">msg<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sender<\/span><span style=\"color: #D8DEE9FF\">][<\/span><span style=\"color: #D8DEE9\">_candidateIndex<\/span><span style=\"color: #D8DEE9FF\">] <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">true;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ Function to end voting<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">function<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">endVoting<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">atStage<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">Stage.Voting<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">stage<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Stage<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Ended<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Key Components Explained<\/h3>\n\n\n\n<ul>\n<li><strong>Enum <code>Stage<\/code><\/strong>: Defines stages (<code>Init<\/code>, <code>Voting<\/code>, <code>Ended<\/code>) to manage the voting process phases.<\/li>\n\n\n\n<li><strong>Mapping <code>votesReceived<\/code><\/strong>: Tracks the number of votes each candidate has received.<\/li>\n\n\n\n<li><strong>Nested Mapping <code>hasVoted<\/code><\/strong>: Checks if an address has already voted for a particular candidate to prevent multiple votes.<\/li>\n\n\n\n<li><strong>Array <code>candidates<\/code><\/strong>: Stores the list of candidates eligible for voting.<\/li>\n\n\n\n<li><strong>Modifier <code>atStage<\/code><\/strong>: Ensures functions are only called at appropriate stages, utilizing the enum to check the current stage.<\/li>\n\n\n\n<li><strong>Functions <code>addCandidate<\/code>, <code>startVoting<\/code>, <code>vote<\/code>, and <code>endVoting<\/code><\/strong>: Manage the voting process, from adding candidates to casting votes and concluding the voting stage.<\/li>\n<\/ul>\n\n\n\n<p>This smart contract showcases the use of conditional logic, state management with enums, and complex data structures with mappings and nested mappings. By understanding and applying these concepts, developers can create more dynamic and interactive smart contracts for the Ethereum blockchain.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"538\" src=\"https:\/\/mthemeus.com\/demos\/wp\/zuzu\/wp-content\/uploads\/2023\/03\/blog3-1024x538.png\" alt=\"\" class=\"wp-image-16714\" srcset=\"https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog3-1024x538.png 1024w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog3-300x158.png 300w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog3-768x404.png 768w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog3-1536x807.png 1536w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog3.png 1712w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"538\" src=\"https:\/\/mthemeus.com\/demos\/wp\/zuzu\/wp-content\/uploads\/2023\/03\/blog2-1024x538.png\" alt=\"\" class=\"wp-image-16711\" srcset=\"https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog2-1024x538.png 1024w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog2-300x158.png 300w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog2-768x404.png 768w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog2-1536x807.png 1536w, https:\/\/infinblock.com\/wp-content\/uploads\/2023\/03\/blog2.png 1712w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Mastering Smart Contracts and Solidity: A Comprehensive Guide<\/h3>\n\n\n\n<p>Dive into the world of blockchain development with our series designed for students and enthusiasts eager to master smart contract programming and Solidity. From foundational blockchain concepts to advanced Solidity development techniques, this series covers everything you need to know to become a proficient blockchain developer. Explore Ethereum&#8217;s ecosystem, understand smart contracts and the EVM, and start building decentralized applications with confidence. Join us on this journey to unlock the transformative power of blockchain technology.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Topics: Conditions, Loops, Mappings, Nested Mappings, and Enums.<\/p>\n","protected":false},"author":1,"featured_media":17499,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[3,30,1,29,7,28,34,36,54,32,5,31,6,33],"tags":[11,57,56,55,16],"acf":[],"_links":{"self":[{"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/posts\/17498"}],"collection":[{"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/comments?post=17498"}],"version-history":[{"count":2,"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/posts\/17498\/revisions"}],"predecessor-version":[{"id":17625,"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/posts\/17498\/revisions\/17625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/media\/17499"}],"wp:attachment":[{"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/media?parent=17498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/categories?post=17498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infinblock.com\/index.php\/wp-json\/wp\/v2\/tags?post=17498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}