<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Decipher with Zaryab</title>
    <description>The latest articles on Forem by Decipher with Zaryab (@zaryab2000).</description>
    <link>https://forem.com/zaryab2000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F412254%2Fb78c5250-b013-48dd-a5dc-a6677e1d3a13.png</url>
      <title>Forem: Decipher with Zaryab</title>
      <link>https://forem.com/zaryab2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zaryab2000"/>
    <language>en</language>
    <item>
      <title>EVM Part1: The ABCs of Ethereum Virtual Machine</title>
      <dc:creator>Decipher with Zaryab</dc:creator>
      <pubDate>Fri, 03 Mar 2023 04:40:18 +0000</pubDate>
      <link>https://forem.com/zaryab2000/evm-part1-the-abcs-of-ethereum-virtual-machine-265j</link>
      <guid>https://forem.com/zaryab2000/evm-part1-the-abcs-of-ethereum-virtual-machine-265j</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;This is going to be a Multi-Part Series with all extensive and imperative details about the Ethereum Virtual Machine, Opocdes, Bytecodes etc.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The next parts of this series shall only be published on the official website of Decipher with Zaryab.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.zaryabs.com/#/portal/" rel="noopener noreferrer"&gt;&lt;strong&gt;Subscribe&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;for Free now&lt;/strong&gt;.&lt;br&gt;
&lt;strong&gt;JOIN&lt;/strong&gt; &lt;a href="https://www.zaryabs.com/#/portal/" rel="noopener noreferrer"&gt;zaryabs.com&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  EVM in 100 words
&lt;/h1&gt;

&lt;p&gt;The Ethereum Virtual Machine can simply be understood as an isolated environment specifically designed to process smart contract transactions as well as, most importantly, determine the overall state of the Ethereum blockchain with every new block that gets mined.&lt;/p&gt;

&lt;p&gt;Imagine EVM as a computer that specializes in handling smart contract transactions as well as defining the rules for state changes from block to block in the Ethereum blockchain.&lt;/p&gt;

&lt;p&gt;However, unlike physical computers, EVM is a virtual machine that is not bound to a single operating system or location, thus allowing global accessibility for anyone willing to run an Ethereum node.&lt;/p&gt;

&lt;p&gt;Alright, there you go. EVM is now explained in precisely 100 words. &lt;em&gt;Go ahead, count it&lt;/em&gt; 😃&lt;/p&gt;

&lt;h1&gt;
  
  
  Let's start Deciphering EVM
&lt;/h1&gt;

&lt;p&gt;Now it's time to dive in just a bit deeper into EVMs.&lt;/p&gt;

&lt;p&gt;If you have ever tried to understand and read about EVM before, you might have come across different ways in which EVM is defined. For instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;EVM is a Virtual Machine ...&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;EVM is a State Machine ...&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;EVM is a Quasi-Turing complete machine...&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I believe the best way to understand EVM effectively is to decipher each of these statements about EVM.&lt;/p&gt;

&lt;p&gt;So, let's get started&lt;/p&gt;

&lt;h1&gt;
  
  
  EVM as a Virtual Machine...
&lt;/h1&gt;

&lt;p&gt;To begin with, let's start with the VM(virtual machine) part of the EVM.&lt;/p&gt;

&lt;p&gt;Let's roll back and forget about Ethereum and even virtual machines.&lt;/p&gt;

&lt;p&gt;Let's talk about Physical machines.&lt;/p&gt;

&lt;p&gt;Physical machines can simply be understood as the hardware that runs on your computer like the x86, ARM, or, perhaps your favorite, the new Apple M1. It's important to note that the lowest level of instruction, for a physical machine, is Binary. &lt;em&gt;Basically just Zeros and Ones.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The physical machines perform actions based on their interpretation of these binary opcode instructions themselves.&lt;/p&gt;

&lt;p&gt;Now, &lt;a href="https://en.wikipedia.org/wiki/Virtual_machine" rel="noopener noreferrer"&gt;Virtual machines&lt;/a&gt; are very similar to physical machines as they too have opcodes to perform specific tasks. Some virtual machines you might know are JVM, LLVM, etc.&lt;/p&gt;

&lt;p&gt;However, a crucial difference is a fact that these virtual machines, although they are quite similar to physical machines, aren't tied to any physical hardware.&lt;/p&gt;

&lt;p&gt;This means that virtual machines provide a runtime environment but are completely platform agnostic. It allows compatibility across a wide variety of systems, irrespective of the underlying hardware.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Alright, now that we understand VMs, it won't be hard to grasp the Ethereum virtual machine.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ethereum.org/en/developers/docs/evm/" rel="noopener noreferrer"&gt;EVM&lt;/a&gt; is a virtual machine which means it can run on multiple hardware irrespective of the underlying platform/OS. This is quite crucial as it plays a major role in enabling decentralization since we can run EVM on as many platforms as we want.&lt;/p&gt;

&lt;p&gt;In fact, the EVM is what makes Ethereum so interesting and &lt;em&gt;sort of&lt;/em&gt; revolutionary because we now have the power to execute any arbitrary code on a blockchain.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the pre-EVM era, this would have been a nightmare as a new blockchain with custom logic had to be created every time a new app with specific use case was to be built.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thus, EVM is a virtual machine that provides us with a completely virtual runtime execution environment for processing smart contract transactions and has global accessibility where anyone with any hardware can easily run this virtual machine.&lt;/p&gt;

&lt;h1&gt;
  
  
  EVM as a State Machine...
&lt;/h1&gt;

&lt;p&gt;As previously discussed, &lt;em&gt;in the 100-word EVM intro,&lt;/em&gt; one of the essential goals of EVM is to determine the state of the Ethereum blockchain, hence the &lt;em&gt;state machine.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But what exactly are State machines?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="noopener noreferrer"&gt;State machine&lt;/a&gt;, in very simpler terms, is a device that specializes in storing state as well as defining the rules of state changes of a particular system.&lt;/p&gt;

&lt;p&gt;It can change(&lt;em&gt;transition&lt;/em&gt;) its state from one to another depending on the inputs provided to it. See&lt;/p&gt;

&lt;p&gt;Now, considering the world of Ethereum, the state refers to an extensive data structure that keeps all the accounts, i.e., Externally-owned accounts (EOAs) or Contract Accounts(&lt;em&gt;more on this later&lt;/em&gt;), linked together by hashes.&lt;/p&gt;

&lt;p&gt;In other words, every detail on the Ethereum blockchain, i.e., &lt;em&gt;the amount of ERC20 tokens you hold, governance proposals and their vote counts, NFT ownership&lt;/em&gt;, etc, are technically different states on the network. These states are reducible to a single root hash or, as we call it, &lt;em&gt;the&lt;/em&gt; &lt;em&gt;World State.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This world state of Ethereum basically represents the current state of the entire blockchain which comprises all the historical states that together form this current state hash.&lt;/p&gt;

&lt;p&gt;Now it's important to note that even the slightest change in any of the accounts (EOA or Contract Accounts), leads to a change/transition in the world state. This means with every single transaction that is mined, &lt;em&gt;be it your NFT ownership change or a governance proposal being executed, *leads to a change in the overall state of the chain. *Read more about Ethereum's world state &lt;/em&gt;&lt;a href="https://medium.com/cybermiles/diving-into-ethereums-world-state-c893102030ed" rel="noopener noreferrer"&gt;&lt;em&gt;in this article&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell, every successfully executed transaction, irrespective of how big or small, leads to a change in the Ethereum state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdypyn5odolerc0k606w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdypyn5odolerc0k606w.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fig.1a-Source: ethereum.org&lt;/p&gt;

&lt;p&gt;And this is all possible because EVM acts as a state machine.&lt;/p&gt;

&lt;h1&gt;
  
  
  Well, why is this important?
&lt;/h1&gt;

&lt;p&gt;EVM being a state machine plays a significant role in making the Ethereum blockchain so effective.&lt;/p&gt;

&lt;p&gt;If we take a quick look at the &lt;a href="https://bitcoin.org/en/how-it-works" rel="noopener noreferrer"&gt;Bitcoin blockchain&lt;/a&gt;, we will find that it's a distributed ledger that is quite strict in its state transitions. Transactions on the bitcoin blockchain lead to global state transitions that mainly track or record the ownership of the native currency(BTC).&lt;/p&gt;

&lt;p&gt;While on the other hand, Ethereum being a distributed state machine provides much more than that.&lt;/p&gt;

&lt;p&gt;Instead of simply recording the &lt;em&gt;currency ownership states&lt;/em&gt;, &lt;em&gt;it has the power to transition to a new state in response to an arbitrary smart contract input data as well.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is incredibly significant because such flexibility in the state transitions is what allows us to create custom currencies(&lt;em&gt;ERC-20 tokens&lt;/em&gt;), NFTs, complicated Defi protocols, or even a complete DAO using smart contracts.&lt;/p&gt;

&lt;h1&gt;
  
  
  EVM as a Quasi-Turing Complete Machine...
&lt;/h1&gt;

&lt;p&gt;Alright, so per our usual approach, let's forget about Ethereum or EVM and only focus on the &lt;a href="https://www.merriam-webster.com/dictionary/quasi" rel="noopener noreferrer"&gt;Quasi&lt;/a&gt;-Turing Complete part.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Actually, just focus on the &lt;/em&gt;&lt;em&gt;Turing Complete&lt;/em&gt;&lt;em&gt; part.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Turing_completeness" rel="noopener noreferrer"&gt;Turing Complete&lt;/a&gt; refers to the kind of machine that enables running a program that can answer/solve any computational problem, irrespective of the time or memory it takes to do so.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In other words 👇&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftokcoilbyfmi7rh96wa9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftokcoilbyfmi7rh96wa9.png" width="800" height="1067"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes, that's Alan Turing who coined the idea of "Turing Completeness" 😃&lt;/p&gt;

&lt;p&gt;For instance, languages like C, C++, or Java are turing complete systems.&lt;/p&gt;

&lt;p&gt;Well, now comes the obvious question --- Can the Ethereum blockchain be considered a Turing Complete system?&lt;/p&gt;

&lt;p&gt;Well, yeah sure. (&lt;em&gt;But read the whole thing, there's a catch&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;It's turing complete as it can execute any program similar to a turing machine and can read as well as write data to memory. In fact, turning complete plays a significant role in allowing Ethereum to run arbitrary logic via smart contracts, which other blockchains are incapable of.&lt;/p&gt;

&lt;p&gt;However, there lies a caveat with the turning-completeness of Ethereum that cannot be overlooked. &lt;em&gt;Here comes the catch.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you recall the nature of Turing machine systems, they can basically &lt;em&gt;run forever&lt;/em&gt; and* use infinite memory.*&lt;/p&gt;

&lt;p&gt;In the world of Ethereum, though, we cannot really have programs that run forever or uses infinite memory.&lt;/p&gt;

&lt;p&gt;While regular computers can simply &lt;em&gt;halt&lt;/em&gt; such infinitely running programs, we cannot do the same with Ethereum as it's a global computer(&lt;em&gt;and a distributed ledger&lt;/em&gt;) that must be accessible/online at all times.&lt;/p&gt;

&lt;p&gt;Now, in order to avoid the halting issue in Ethereum, it includes a specific type of limitation in the computational steps for any smart contract execution. It imposes this limitation using the concept of &lt;em&gt;Gas. &lt;/em&gt;&lt;em&gt;Check out &lt;/em&gt;&lt;a href="https://consensys.net/blog/blockchain-explained/a-guide-to-gas/" rel="noopener noreferrer"&gt;&lt;em&gt;this article&lt;/em&gt;&lt;/a&gt;&lt;em&gt; by Consensys to learn more about the fundamentals of Gas in Ethereum.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This means the EVM keeps track of every instruction that is to be executed and charges a particular amount of &lt;em&gt;Gas fees &lt;/em&gt;(&lt;em&gt;in ETH&lt;/em&gt;) for every computation or storage being used in the transaction. In other words, if you run out of gas (&lt;em&gt;basically money&lt;/em&gt;) you can no more execute the transaction, thus resolving the issue of transactions/computations that run infinitely.&lt;/p&gt;

&lt;p&gt;In simpler terms, Ethereum is indeed a Turing complete system. However, it does impose limitations using gas which doesn't really allow this system to include forever running computations or those that use infinite storage.&lt;/p&gt;

&lt;p&gt;Therefore, while EVM doesn't belong completely in the category of a Turing-Complete system, we call it a Quasi-Turing Complete System.&lt;/p&gt;

&lt;h1&gt;
  
  
  Understanding the crucial components of EVM &amp;amp; Ethereum
&lt;/h1&gt;

&lt;p&gt;EVM can be considered a machine with a simple stack-based architecture.&lt;/p&gt;

&lt;p&gt;In order to effectively understand the working mechanism of EVM, we must understand some of its most crucial components and their responsibilities.&lt;/p&gt;

&lt;p&gt;However, before diving into EVM components, I feel it's important to touch base on some fundamental concepts of the Ethereum blockchain.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: These fundamental concepts will help us understand EVM components better. However, feel free to skip them if you are already aware of them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Quick Review of the basics
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. Accounts in Ethereum
&lt;/h2&gt;

&lt;p&gt;An &lt;a href="https://ethereum.org/en/developers/docs/accounts/" rel="noopener noreferrer"&gt;account in Ethereum&lt;/a&gt; blockchain can simply be defined as a component that represents either an individual user or smart contracts on the blockchain and allows them to initiate transactions.&lt;/p&gt;

&lt;p&gt;Ethereum includes 2 different types of accounts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Externally Owned Accounts --- also called &lt;em&gt;EOA&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  Contract Accounts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's quickly understand each of them:&lt;/p&gt;

&lt;p&gt;a. Externally Owned Accounts are controlled by their private key. In simpler terms, a specific EOA is controlled by a user who owns the private key of that account.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  These accounts are capable of initiating transactions in the blockchain. However, transactions between two EOAs can only be limited to ether or token transfers.&lt;/li&gt;
&lt;li&gt;  Since these accounts are owned by private keys, the owner of the key must sign the transaction to initiate any activity in their account.&lt;/li&gt;
&lt;li&gt;  For instance, this is an Externally Owned Account --- &lt;a href="https://etherscan.io/address/0x3f03d6eb11659e1e006cc8522552ac4b1d5f6420" rel="noopener noreferrer"&gt;HERE&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;b. Contract Accounts, on the other hand, are not controlled by a private key but by their own smart contract code logic.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;Have you ever deployed on any EVM chain?&lt;/em&gt; The address you get after contract deployment is basically a contract account.&lt;/li&gt;
&lt;li&gt;  These are the accounts that do not just hold your ether or token balance but are also capable of storing smart contract codes within themselves.&lt;/li&gt;
&lt;li&gt;  While contract accounts are also capable of sending transactions, they do so only in response to receiving a particular transaction. In other words, they won't send a transaction unless their logic allows them to.&lt;/li&gt;
&lt;li&gt;  It must also be noted, that, unlike externally owned accounts, creating a contract has a cost attached to it since we are deploying data on the chain.&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://etherscan.io/address/0x66329Fdd4042928BfCAB60b179e1538D56eeeeeE" rel="noopener noreferrer"&gt;This&lt;/a&gt;, for instance, is a Contract Account&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The Concept of GAS
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Remember when we discussed EVM being a quasi-turing complete system?&lt;/em&gt; &lt;em&gt;Well, the concept of &lt;/em&gt;&lt;em&gt;Gas&lt;/em&gt;&lt;em&gt; plays a significant role in making it one.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Gas, in laymen's terms, can be understood as a unit that describes the amount of computation power required to execute specific transactions on the blockchain.&lt;/p&gt;

&lt;p&gt;For a transaction to be executed and affect the world state of the Ethereum chain, one must pay a certain amount of gas. Additionally, this amount of gas relies on how heavy a transaction is. &lt;em&gt;The heavier the transaction, the more gas one must pay to execute it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For instance, transferring 1 ETH to your friend can cost lesser gas than executing a Swap function on Uniswap smart contracts.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Transactions in Ethereum
&lt;/h2&gt;

&lt;p&gt;Alright, we discussed briefly how EOAs are capable of initiating and sending a transaction, but.... What exactly are transactions in the context of EVM?&lt;/p&gt;

&lt;p&gt;A one-liner answer would be:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A transaction in the Ethereum chain is basically an action that can be triggered by any externally owned account to perform a state-changing interaction on the blockchain.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It means, &lt;em&gt;whether you use your ethereum account to transfer an NFT to your friend or you interact with the &lt;/em&gt;&lt;a href="https://etherscan.io/address/0x66329Fdd4042928BfCAB60b179e1538D56eeeeeE" rel="noopener noreferrer"&gt;&lt;em&gt;PushCore smart contract&lt;/em&gt;&lt;/a&gt;&lt;em&gt; to create a channel on the Push Protocol or you deploy a new smart contract&lt;/em&gt;, everything is a transaction on the blockchain that contains cryptographically signed data messages defining the task to perform.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ethereum.org/en/developers/docs/transactions/" rel="noopener noreferrer"&gt;Transactions&lt;/a&gt; in the Ethereum blockchain can broadly be categorized into 2 specific types:&lt;/p&gt;

&lt;p&gt;a. Message Call transactions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Message calls refer to the transactions that are triggered by an EOA in order to interact with a contract account or yet another EOA.&lt;/li&gt;
&lt;li&gt;  You sending USDT to my address or interacting with Uniswap are all examples of message call transactions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;b. Contract Creation transactions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  As the name suggests, these are transactions that aim to create a new contract account on the chain.&lt;/li&gt;
&lt;li&gt;  For instance, deploying a new smart contract for your next web3 project is a type of contract creation transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Architecture of a transaction in Ethereum&lt;/p&gt;

&lt;p&gt;Let's quickly understand some of the crucial fields that an &lt;a href="https://www.quicknode.com/guides/ethereum-development/what-are-ethereum-transactions" rel="noopener noreferrer"&gt;ethereum transaction&lt;/a&gt; holds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;nonce&lt;/em&gt;: A counter that keeps increasing every time you initiate a transaction from a particular EOA.&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;recipient or to&lt;/em&gt;: the address of the recipient of this transaction. (it can be an EOA or a Contract Account).&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;value&lt;/em&gt;: the amount of ether to be transferred from the sender to the recipient address. &lt;em&gt;It can be left zero if no eth transfer is supposed to happen in your transaction.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;data&lt;/em&gt;: an optional field to include any arbitrary data. This field can contain code in a specific transaction for a Contract account&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;gaslimit&lt;/em&gt;: which refers to the amount of gas that can use in a particular transaction.&lt;/li&gt;
&lt;li&gt;  maxPriorityFeePerGas as well as maxFeePerGas&lt;/li&gt;
&lt;li&gt;  and, signature&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alright, now that we understand the fundamental topics of the Ethereum blockchain, we are all set to dive in deep to get the gist of some crucial EVM components.&lt;/p&gt;

&lt;h1&gt;
  
  
  EVM Architecture and its Components
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm3mie7b692p6smbogb49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm3mie7b692p6smbogb49.png" width="800" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fig.1b-&lt;a href="https://github.com/takenobu-hs/ethereum-evm-illustrated" rel="noopener noreferrer"&gt;takenobu-hs github&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Storage
&lt;/h1&gt;

&lt;p&gt;To begin with, Storage can be seen as the Hard drive of the computer that stores data permanently.&lt;/p&gt;

&lt;p&gt;Storage can simply be understood as the permanent storage of the EVM. Any value that is written to the storage is retained even after the execution is completed.&lt;/p&gt;

&lt;p&gt;In simpler terms, Storage is basically a key-value storage system that maps 32 bytes slots to 32-byte values.&lt;/p&gt;

&lt;p&gt;It's imperative to note that since storage holds data permanently, it consumes more gas and is therefore costlier to use in your smart contracts.&lt;/p&gt;

&lt;p&gt;💡 &lt;em&gt;Considering the fact that storage operations are costs more gas, (= more money), it's always recommended to optimize your contracts to only use storage operations where necessary and remove wipe out the rest.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Memory
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;While EVM storage resembles a Hard drive, Memory is more of a RAM in the computer, a temporary data holder.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;EVM memory is not really persistent and is wiped out as soon as the function execution is completed.&lt;/p&gt;

&lt;p&gt;Unlike storage, memory usage in smart contracts is comparatively a cheap operation that costs less gas. However, it must be kept in mind that the cost of using memory can increase as memory usage increases in a function.&lt;/p&gt;

&lt;h1&gt;
  
  
  Calldata:
&lt;/h1&gt;

&lt;p&gt;Calldata is an interesting section of the EVM and equally imperative.&lt;/p&gt;

&lt;p&gt;It is a quite special data location of the EVM that refers to the location that stores the input parameters or arguments of the function that is supposed to be called.&lt;/p&gt;

&lt;p&gt;Quite similar to memory, calldata is also a non-persistent data holder that is cleared after the execution of the transaction.&lt;/p&gt;

&lt;p&gt;Calldata is quite cheaper and costs very less gas.&lt;/p&gt;

&lt;p&gt;It should also be noted that it can never be written as it's read-only.&lt;/p&gt;

&lt;h1&gt;
  
  
  Stack
&lt;/h1&gt;

&lt;p&gt;The stack is the most important component of the EVM.&lt;/p&gt;

&lt;p&gt;EVM is technically a stack machine as it plays a significant role in every computation or operation performed in the EVM. With a maximum size of a total of 1024 values, the EVM stack can contain words of 256 bits.&lt;/p&gt;

&lt;p&gt;It is mainly used to store smart contract instruction inputs as well as outputs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Program Counter
&lt;/h1&gt;

&lt;p&gt;The Program counter in an EVM indicates the specific instruction from the code, that should be read and executed next by the EVM.&lt;/p&gt;

&lt;p&gt;This counter basically allows the EVM to step through the entire contract code and interpret as well as execute the specific instructions to reach the desired outcome/state.&lt;/p&gt;

&lt;p&gt;At the very beginning of any transaction execution, the Program counter is set to ZERO.&lt;/p&gt;

&lt;h1&gt;
  
  
  How does EVM work?
&lt;/h1&gt;

&lt;p&gt;Alright, now that we grasped the very core fundamentals of the Ethereum Virtual Machine as well as its components, it's time to combine all of it together and get the gist of how an EVM works.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: This is an eagle-eye perspective of how EVM works. We will dive into a lot of technical, in-depth and fun details about EVM in next articles of this series.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before diving into EVM's working mechanism...&lt;/p&gt;

&lt;p&gt;If you remember the very first line of the &lt;em&gt;EVM in 100 words&lt;/em&gt; section of the article, it said --- &lt;em&gt;Ethereum virtual machine is specifically designed to process smart contract transactions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While it's true, it should also be kept in mind that the EVM cannot really execute solidity smart contracts directly. This is because the EVM doesn't really understand a high-level language like Solidity and therefore cannot interpret or execute it directly.&lt;/p&gt;

&lt;p&gt;Every Solidity smart contract is compiled down to low-level machine instructions called Opcodes, which are understandable by the EVM.&lt;/p&gt;

&lt;p&gt;In order to execute any tasks, &lt;em&gt;storing data in state variables or executing a swap of tokens&lt;/em&gt;&lt;em&gt;,&lt;/em&gt; the EVM uses these opcodes.&lt;/p&gt;

&lt;p&gt;There are around 140 total unique opcodes at the moment and each one of them has a specific functionality attached to it. &lt;em&gt;We shall read about opcodes extensively in the next article of this series.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  EVM working mechanism
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;To begin with, it all starts with a transaction.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is what a dummy Ethereum transaction might look like. 👇&lt;/p&gt;

&lt;p&gt;{\&lt;br&gt;
  from: "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",\&lt;br&gt;
  to: "0xac03bb73b6a9e108530aff4df5077c2b3d481e5a",\&lt;br&gt;
  gasLimit: "21000",\&lt;br&gt;
  maxFeePerGas: "300",\&lt;br&gt;
  maxPriorityFeePerGas: "10",\&lt;br&gt;
  nonce: "0",\&lt;br&gt;
  value: "10000000000",\&lt;br&gt;
  data: "0xabcd",\&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Let's consider this transaction as the one that the EVM is now about to execute.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; EVM Instantiation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whenever a transaction triggers the execution of smart contract code, an Ethereum Virtual Machine (EVM) is instantiated with all the necessary information. These details are related to the current block being created.&lt;/p&gt;

&lt;p&gt;The address in the '&lt;em&gt;to' *field is the address of the contract that is being targeted. Additionally, the transaction includes all other crucial details like *gas limit&lt;/em&gt;, &lt;em&gt;ether value, *as well as the *data&lt;/em&gt; that is to be executed on the target contract.&lt;/p&gt;

&lt;p&gt;As soon as an EVM is instantiated, it basically prepares itself to start the processing of a new smart contract transaction.&lt;/p&gt;

&lt;p&gt;This preparation, for instance, involves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;the program counter of the EVM being set to zero,&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Storage being loaded from the contract account,&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Environment variables being set as well as memory initialized to zero, etc.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The EVM then pulls up the contract and its code associated with the given address in the transaction.\&lt;br&gt;
This is where the significant role of the &lt;em&gt;Program Counter&lt;/em&gt; starts. The program counter now helps the machine to step through the entire code which allows the EVM to interpret and execute the given instructions one-by-one.&lt;/p&gt;

&lt;p&gt;Once all such preparations are done, the EVM technically is then ready to start executing the instructions (&lt;em&gt;opcodes&lt;/em&gt;) and resulting in the intended state changes of the contract.&lt;/p&gt;

&lt;p&gt;2. Gas Supply &amp;amp; Adjustments&lt;/p&gt;

&lt;p&gt;Although the EVM is now all set to start processing these opcodes, we cannot keep the concept of *Gas *out of this equation.&lt;/p&gt;

&lt;p&gt;As previously discussed in the *EVM as a quasi-turing complete machine *section, for every smart contract operation to be executed, a transaction fee must be paid in the form of gas.&lt;/p&gt;

&lt;p&gt;When initiating a smart contract transaction, the sender of the transaction must provide &lt;em&gt;a certain value of gas fees&lt;/em&gt;(&lt;em&gt;in the form of ether&lt;/em&gt;) that he is willing to pay for this transaction to be executed.&lt;/p&gt;

&lt;p&gt;Interestingly, as the smart contract operations are executed, the gas supply provided with the transaction reduces.&lt;/p&gt;

&lt;p&gt;In other words, if the sender didn't provide an adequate gas supply required for the transaction to be completed, the supply eventually gets to zero and the EVM throws an &lt;em&gt;Out of Gas &lt;/em&gt;&lt;em&gt;exception error&lt;/em&gt;. This immediately stops further execution of the transaction.&lt;/p&gt;

&lt;p&gt;If such an event happens, the entire transaction is rolled back and no changes are included in Ethereum's state.&lt;/p&gt;

&lt;p&gt;The sender's ether balance, however, does decrease since he/she had to pay for the gas that was consumed so far by the transaction.&lt;/p&gt;

&lt;p&gt;Gas can also be Refunded&lt;/p&gt;

&lt;p&gt;Some transactions can also include a higher gas limit, i.e., the amount of gas that the sender is allowing to be consumed in his/her transaction.&lt;/p&gt;

&lt;p&gt;However, if the provided gas limit is way higher than the required gas in the transaction, EVM simply refunds the unused gas back to the sender.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6ecqznb9bdjox1yy1sg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6ecqznb9bdjox1yy1sg.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fig.1c-Source: Ethereum.org&lt;/p&gt;

&lt;p&gt;3. Processing transactions and Changing World State&lt;/p&gt;

&lt;p&gt;Alright, let's consider that the above-mentioned transaction neither throws any *Out of gas *error nor has any logic error that might lead to its rejection.&lt;/p&gt;

&lt;p&gt;This technically means this transaction can be successfully processed by the EVM, consume the required amount of gas, and lead to the expected changes in not just the target contract's state but also the entire world state of the chain.&lt;/p&gt;

&lt;p&gt;Contracts calling Contracts&lt;/p&gt;

&lt;p&gt;An interesting point to keep in mind is smart contracts can initiate transactions or calls to other smart contracts well.&lt;/p&gt;

&lt;p&gt;A transaction initiated by an EOA to some &lt;em&gt;Contract X *can also lead to a new transaction initiated by the targeted *Contract X *to some other *Contract Y&lt;/em&gt; if need be.&lt;/p&gt;

&lt;p&gt;Every &lt;em&gt;new call to a new contract creates a similar cycle of EVM instantiation&lt;/em&gt; and transaction execution which ultimately leads to desired state changes.&lt;/p&gt;

&lt;p&gt;It should, however, be noted that every new smart contract transaction within the original transaction requires gas to adequately execute the transaction.&lt;/p&gt;

&lt;p&gt;Therefore, a new EVM instantiation highly relies on the amount of gas remaining for the execution of the new transaction. In case the gas passed along with the transaction, isn't sufficient enough to make new calls to new contracts, the transaction shall completely revert.&lt;/p&gt;

&lt;p&gt;The diagram below represents such transactions quite clearly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5suu45crbgrm1k2nxify.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5suu45crbgrm1k2nxify.png" width="800" height="812"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fig.1d-Source: &lt;a href="https://www.researchgate.net/figure/Ethereum-transaction-workflow-diagram_fig1_342323668" rel="noopener noreferrer"&gt;ResearchGate&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrapping it up
&lt;/h1&gt;

&lt;p&gt;That brings us to the end of the very first part of the EVM series.&lt;/p&gt;

&lt;p&gt;Remember, the core objective of this part was to set a strong foundation of the basics and important details about the Ethereum virtual machines. Thus, preparing ourselves to dive much deeper into its technicalities in the next series.&lt;/p&gt;

&lt;p&gt;We now have a very clear idea of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;Basics of EVM.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Why is EVM a state machine and what exactly are virtual machines?&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Why is EVM called a Quasi-Turing complete instead of a Turing complete machine?&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Core components of EVM&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;An eagle-eye perspective of EVM's working mechanism.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next part, we shall start exploring the entire journey of smart contracts --- from development to its on-chain execution. Stay tuned.&lt;/p&gt;

&lt;h1&gt;
  
  
  Further Reading
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://ethereum.org/en/developers/docs/evm/" rel="noopener noreferrer"&gt;&lt;em&gt;Intro to Ethereum Virtual Machine: Ethereum.org&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://medium.com/cybermiles/diving-into-ethereums-world-state-c893102030ed" rel="noopener noreferrer"&gt;&lt;em&gt;Diving into Ethereum's world state&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Ethereum&lt;/em&gt; &lt;a href="https://ethereum.org/en/developers/docs/accounts/" rel="noopener noreferrer"&gt;Accounts&lt;/a&gt;, &lt;a href="https://ethereum.org/en/developers/docs/smart-contracts/" rel="noopener noreferrer"&gt;&lt;em&gt;Smart Contracts&lt;/em&gt;&lt;/a&gt;&lt;em&gt;,&lt;/em&gt; and &lt;a href="https://ethereum.org/en/developers/docs/transactions/" rel="noopener noreferrer"&gt;Transactions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;E&lt;/em&gt;&lt;a href="https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html#index-6" rel="noopener noreferrer"&gt;&lt;em&gt;VM from Solidity Documentation&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Subscribe to Decipher with Zaryab 👇
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.zaryabs.com/" rel="noopener noreferrer"&gt;https://www.zaryabs.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>𝐃𝐞𝐜𝐢𝐩𝐡𝐞𝐫 𝐰𝐢𝐭𝐡 𝐙𝐚𝐫𝐲𝐚𝐛 𝐧𝐨𝐰 𝐡𝐚𝐬 𝐢𝐭𝐬 𝐨𝐰𝐧 𝐇𝐎𝐌𝐄. 🏡</title>
      <dc:creator>Decipher with Zaryab</dc:creator>
      <pubDate>Mon, 20 Feb 2023 06:30:26 +0000</pubDate>
      <link>https://forem.com/zaryab2000/-31p4</link>
      <guid>https://forem.com/zaryab2000/-31p4</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C6sAdEUx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kp4m3rikroarg2y4axxe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C6sAdEUx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kp4m3rikroarg2y4axxe.png" alt="Image description" width="880" height="293"&gt;&lt;/a&gt;&lt;em&gt;Attention Developers, Smart Contract Ninjas, and Web3 Enthusiasts.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Excited to share with you all about the launch of my independent publication, Decipher with Zaryab.&lt;br&gt;
Started on 20th June 2022, with a family of 2000+ subscribers on LinkedIn, Decipher with Zaryab has now found its own home at 𝘇𝗮𝗿𝘆𝗮𝗯𝘀.𝗰𝗼𝗺&lt;/p&gt;

&lt;p&gt;Decipher with Zaryab is aimed at helping Web3 builders become better Smart Contract Developers with strong mental models around Web3 &amp;amp; Smart Contract Security.&lt;br&gt;
The publication provides simplified and curated technical (and sometimes not-so-technical) content for some of the most complicated but interesting topics in Web3.&lt;br&gt;
Additionally, this publication is designed to help Web3 enthusiasts become valuable Web3 contributors.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bigger Picture
&lt;/h2&gt;

&lt;p&gt;With its curated and simplified technical (and sometimes non-technical) content, this publication aims to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Help software developers easily start out with smart contract development.&lt;/li&gt;
&lt;li&gt;Provide simplified content for some of the most complicated technical or non-technical topics in Web3.&lt;/li&gt;
&lt;li&gt;Emphasize the significance and need for Smart Contract Security while developing smart contracts&lt;/li&gt;
&lt;li&gt;Share well-evaluated mental models for writing better, optimized, and secure smart contracts.&lt;/li&gt;
&lt;li&gt;Guide Web3 enthusiasts to become valuable Web3 contributors&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you are someone who:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strives to be a better smart contract developer&lt;/li&gt;
&lt;li&gt;Become a valuable contributor to the Web3 space&lt;/li&gt;
&lt;li&gt;Understand the significance of having mental models for being productive as a Developer&lt;/li&gt;
&lt;li&gt;Loves simplified, curated, and well-researched content that gives you the 'AHA' moments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;𝐓𝐡𝐢𝐬 𝐩𝐮𝐛𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧 𝐢𝐬 𝐟𝐨𝐫 𝐘𝐎𝐔.&lt;/p&gt;

&lt;p&gt;𝗔𝗹𝗽𝗵𝗮: 𝙰 𝚗𝚎𝚠 𝚊𝚛𝚝𝚒𝚌𝚕𝚎 𝚜𝚎𝚛𝚒𝚎𝚜 𝚒𝚜 𝚝𝚘 𝚋𝚎 𝚕𝚊𝚞𝚗𝚌𝚑𝚎𝚍 𝚝𝚘𝚖𝚘𝚛𝚛𝚘𝚠. 𝚂𝚝𝚊𝚢 𝚃𝚞𝚗𝚎𝚍 👀&lt;br&gt;
𝐒𝐮𝐛𝐬𝐜𝐫𝐢𝐛𝐞 𝐓𝐨𝐝𝐚𝐲 to get access to the full archive of everything that's been published before and everything that's still to come. Your very own private library is at &lt;a href="https://www.zaryabs.com/"&gt;https://www.zaryabs.com/&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>programming</category>
    </item>
    <item>
      <title>Yet another Re-entrancy attack - What's keeping us from being a better Smart Contract Developer?</title>
      <dc:creator>Decipher with Zaryab</dc:creator>
      <pubDate>Mon, 10 Oct 2022 05:34:17 +0000</pubDate>
      <link>https://forem.com/zaryab2000/yet-another-re-entrancy-attack-whats-keeping-us-from-being-a-better-smart-contract-developer-4543</link>
      <guid>https://forem.com/zaryab2000/yet-another-re-entrancy-attack-whats-keeping-us-from-being-a-better-smart-contract-developer-4543</guid>
      <description>&lt;p&gt;It's been &lt;strong&gt;&lt;em&gt;6 years, 3months, and 25 days&lt;/em&gt;&lt;/strong&gt; since the infamous DAO hack that shook the entire web3 world.&lt;/p&gt;

&lt;p&gt;The one where we witnessed more than 3.5 million Ether being stolen away due to a bug in the smart contract.&lt;/p&gt;

&lt;p&gt;This hack introduced us to one of the most dangerous attacks, &lt;strong&gt;&lt;em&gt;The Re-Entrancy attack,&lt;/em&gt;&lt;/strong&gt; possible in a smart contract.&lt;/p&gt;

&lt;p&gt;Since then there have been enormous improvements and attempts to mitigate the risk of introducing such attack vectors in smart contracts.&lt;/p&gt;

&lt;p&gt;Automated testing tools started including warnings about re-entrancy bugs, smart contract auditors became particularly cautious with codes dealing with external calls, and loads of blogs as well as video content were created (&lt;em&gt;and are still being made&lt;/em&gt;) explaining the re-entrancy attack.&lt;/p&gt;

&lt;p&gt;However, if you have observed the exploits in the past few years, along with all other new attack vectors, re-entrancy exploits are still a major concern and can be seen as a considerable bug behind many smart contract exploits.&lt;/p&gt;

&lt;p&gt;In fact, the latest re-entrancy exploit that I read about was just a few weeks ago when &lt;a href="https://twitter.com/BeosinAlert/status/1559760874184646657"&gt;Stader’s NearX smart contract was exploited&lt;/a&gt; with the exact same very well-known re-entrancy bug &amp;amp; $830,000 were lost.&lt;/p&gt;

&lt;p&gt;Well, this isn’t just about re-entrancy bugs in smart contracts and neither is the blog going to explain re-entrancy bugs ( &lt;em&gt;not again&lt;/em&gt; ).&lt;/p&gt;

&lt;p&gt;Re-entrancy attacks are just one of the many well-known attacks that the smart contract dev community is well aware of but we find traces of such bugs in smart contracts even today, which leads to incredibly massive exploits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now the question is — &lt;strong&gt;WHY?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;==============================&lt;/p&gt;

&lt;p&gt;Despite having enormous tools, libraries, and educational content around such common bugs and attack vectors in solidity, why do we still witness such well-known attacks time and again?&lt;/p&gt;

&lt;p&gt;This is undoubtedly one of those broad or open-ended questions that might have multiple narratives or answers.&lt;/p&gt;

&lt;p&gt;However, after being involved in multiple smart contract security audits now, interacting with quite a few smart contract devs, and being part of some really active smart contract developer communities, I came across 2 extremely simple but imperative reasons behind this.&lt;/p&gt;

&lt;p&gt;Most importantly, these reasons revolve around the very basics of how we approach Smart Contract development and learn Solidity, especially in the initial stages.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. The “&lt;em&gt;Security-is-NOT-my-job&lt;/em&gt;”&lt;/strong&gt; mindset
&lt;/h2&gt;

&lt;p&gt;==============================================&lt;/p&gt;

&lt;p&gt;It can unquestionably be stated that the majority of the developers in the web3 space believe contract development and smart contract security to be two different things, which is true, to some extent.&lt;/p&gt;

&lt;p&gt;However, completely denying any correlation between these two things is probably a mistake.&lt;/p&gt;

&lt;p&gt;The rationale behind this is the fact that, at the very heart of it, the process of smart contract development broadly includes 3 most significant steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Design &amp;amp; Development&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Optimizations&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Security Validations of the Contract&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While we are quite well aware of the first two steps, we most often forget, or even worse, do not consider &lt;strong&gt;&lt;em&gt;Security Validations&lt;/em&gt;&lt;/strong&gt; to be part of the contract development procedure at all.&lt;/p&gt;

&lt;p&gt;It’s concerning because even if we consider a basic fact about smart contracts, i.e., their immutable nature after on-chain deployment, we can quite clearly see the significance of adding &lt;strong&gt;security checks and validations&lt;/strong&gt; as a mandatory step in the smart contract development process.&lt;/p&gt;

&lt;p&gt;The fact that you cannot change a line of code in your contract even if you found a major bug just seconds after deployment, is daunting in itself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Well, just in case you think you can always upgrade your smart contracts or upgradeable contracts are extremely secure, &lt;strong&gt;let me stop you right there. &lt;em&gt;🛑&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Read about the&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://medium.com/immunefi/wormhole-uninitialized-proxy-bugfix-review-90250c41a43a"&gt;&lt;strong&gt;&lt;em&gt;Wormhole Proxy Bug&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; and think again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Upgradeable contracts can have bugs too. &lt;em&gt;🪲&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What should you do?&lt;/strong&gt;
&lt;/h3&gt;




&lt;p&gt;In very simpler terms, consider &lt;strong&gt;&lt;em&gt;security checks &amp;amp; validations&lt;/em&gt;&lt;/strong&gt; as an imperative part of any smart contract you develop, and learn at least the basics of smart contract security if you haven’t already.&lt;/p&gt;

&lt;p&gt;Once you consider this, you can no more rely on just writing test cases for your smart contracts.&lt;/p&gt;

&lt;p&gt;Test scripts are undoubtedly helpful, however, they are more inclined towards ensuring the contract functions execute as intended while the security of a contract is much more than that.&lt;/p&gt;

&lt;p&gt;Even as a smart contract developer, you should definitely be good with at least one of the following (&lt;em&gt;actually more than just 1&lt;/em&gt;) security tools to validate the security of your contract:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;&lt;em&gt;Static analysis tools like&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://github.com/crytic/slither"&gt;&lt;strong&gt;&lt;em&gt;Slither&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;,&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://github.com/ConsenSys/mythril"&gt;&lt;strong&gt;&lt;em&gt;Mythril&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;,&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://mythx.io/"&gt;&lt;strong&gt;&lt;em&gt;Mythx&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt; These tools effectively help you identify any well-known smart contract bugs that you might have in your contract.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;em&gt;Fuzz Testing tools like&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://github.com/crytic/echidna"&gt;&lt;strong&gt;&lt;em&gt;Echidna&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;or&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://mariachris.github.io/Pubs/FSE-2020-Harvey.pdf"&gt;&lt;strong&gt;&lt;em&gt;Harvey&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;  help identify potential exploit scenarios or contract execution failures by throwing random &amp;amp; unexpected data into your contract.&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://github.com/consensys/scribble"&gt;&lt;strong&gt;Scribble&lt;/strong&gt;&lt;/a&gt;, which is an amazing runtime verification tool by &lt;a href="https://medium.com/u/6c7078bf7b01?source=post_page-----4b3decc52b46--------------------------------"&gt;ConsenSys&lt;/a&gt; that allows you to annotate a solidity smart contract with crucial properties.&lt;/li&gt;
&lt;li&gt;  While auditing complex &amp;amp; bulky smart contracts, the one thing you need the most is a &lt;em&gt;visualization tool&lt;/em&gt;, and that’s exactly where &lt;a href="https://github.com/ConsenSys/surya"&gt;&lt;strong&gt;Surya&lt;/strong&gt;&lt;/a&gt; comes in.
It provides an incredibly simplified version of all crucial details about a contract’s structure including call graphs, inheritance graphs, etc.&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://marketplace.visualstudio.com/items?itemName=tintinweb.solidity-visual-auditor"&gt;&lt;strong&gt;VS Code visual auditor&lt;/strong&gt;&lt;/a&gt; extension is an extremely helpful tool that provides security-oriented syntax as well as semantic highlighting and quite a few other tools that make the secure development of contracts easier.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;My favorites → &lt;strong&gt;Slither, Surya, VS Code visual auditor, and Echindna&lt;/strong&gt;. &lt;em&gt;😎&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why should you do it (&lt;em&gt;If you can get contracts Audited&lt;/em&gt;)?&lt;/strong&gt;
&lt;/h3&gt;




&lt;p&gt;Well, to begin with, no one will ever care about the security of your smart contracts more than you (&lt;em&gt;and your team&lt;/em&gt;). The very first person responsible for the security of your code should be &lt;strong&gt;YOU&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Secondly, the smart contract audit industry has been (&lt;em&gt;and still is&lt;/em&gt;) in a “ &lt;strong&gt;&lt;em&gt;High Demand &amp;amp; Low Supply of good security auditors&lt;/em&gt; “&lt;/strong&gt; phase*&lt;em&gt;.&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;This basically means there are just a handful of extremely experienced auditors and relying on just a few of them won’t really be scalable enough for the web3 world to expand its boundaries exponentially.&lt;/p&gt;

&lt;p&gt;Additionally, there is a huge line of projects waiting for their contracts to get audited, which is why, when it's your turn you need to be ready with the right set of contracts, adequate test cases, coverage reports, and most importantly, the crucial discussion points you might want to have with your security auditor.&lt;/p&gt;

&lt;p&gt;However, you won’t really be capable of having any effectual discussion with smart contract security experts if you yourself don’t understand, at least, the basics of smart contract security.&lt;/p&gt;

&lt;p&gt;The inclusion of the &lt;strong&gt;&lt;em&gt;security validations&lt;/em&gt;&lt;/strong&gt; step in your smart contract development journey allows you to filter out all the well-known bugs beforehand with the help of static analysis tools.&lt;/p&gt;

&lt;p&gt;This allows security auditors to specifically concentrate on the more important potential threats of the contracts instead of identifying and reporting the ones that could be easily found with the tools mentioned above.&lt;/p&gt;

&lt;p&gt;Therefore, performing your own security checks before an audit doesn’t just shorten the lengthy audit durations, it helps achieve an adequate &amp;amp; better result out of the whole procedure.&lt;/p&gt;

&lt;p&gt;This is a concept that the &lt;a href="https://secureum.substack.com/"&gt;&lt;em&gt;Secureum community&lt;/em&gt;&lt;/a&gt; brilliantly defines as &lt;a href="https://github.com/secureum/CARE"&gt;CARE (&lt;em&gt;Comprehensive Audit Readiness Evaluation&lt;/em&gt;)&lt;/a&gt; which intends to prepare your contracts before an audit to ensure that the outcome from the security audit is comparatively better and effective.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s important to note that i*&lt;em&gt;&lt;em&gt;ncluding security as a part of your smart contract development process isn’t a substitution for an audit of your contract, but a preparation for an adequate security audit.&lt;/em&gt;&lt;/em&gt;*&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. Avoiding Mistakes &amp;amp; Experimentations with Solidity (&lt;em&gt;and the lessons that come from them&lt;/em&gt;)
&lt;/h2&gt;

&lt;p&gt;==============================================================================================&lt;/p&gt;

&lt;p&gt;Alright, while the first point was for developers already familiar with solidity(&lt;em&gt;and basics of smart contract security&lt;/em&gt;), this part is more inclined towards those starting out with learning solidity or the ones in the very early phases of smart contract development.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here is a quick tip for you:&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Along with learning and developing simple solidity smart contracts, don’t be scared of experimenting and making mistakes while learning solidity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since its inception &lt;em&gt;Solidity&lt;/em&gt; or smart contract development, in general, has been incredibly interesting but daunting as well as difficult for many developers at the early stage because of some very obvious reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;em&gt;The&lt;/em&gt; &lt;strong&gt;&lt;em&gt;immutable nature of Smart Contracts&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;and the idea of getting almost everything right in one single shot before on-chain deployment.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;The open-source nature of smart contracts and the fact that every single line of code is&lt;/em&gt; &lt;strong&gt;&lt;em&gt;accessible/readable by anyone.&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;The whole concept of&lt;/em&gt; &lt;strong&gt;&lt;em&gt;programming and storing money within Smart Contracts&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;and the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;security risks&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;that come with it.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;em&gt;Solidity still being at a very&lt;/em&gt; &lt;strong&gt;&lt;em&gt;nascent stage&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;and the difficulties in keeping up with the rapidly changing/evolving language.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i1Dc4cop--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phgubedtawmuk5n93e5t.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i1Dc4cop--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phgubedtawmuk5n93e5t.jpeg" alt="" width="620" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the concerns and risks associated with making mistakes while developing a smart contract can’t be overlooked. However, these have an adverse effect on the journey toward learning smart contracts effectively.&lt;/p&gt;

&lt;p&gt;With so much at stake, it becomes harder for developers to &lt;strong&gt;&lt;em&gt;experiment with Solidity or make mistakes on their own.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The popular idea of “&lt;strong&gt;&lt;em&gt;Not reinventing the wheel or trying something new, to avoid introducing new bugs in the contract”&lt;/em&gt;&lt;/strong&gt; stops developers from exploring different concepts of solidity and thus limits their potential with smart contract development, as a whole.&lt;/p&gt;

&lt;p&gt;In fact, it is now considered a lot safer to simply fork an already existing audited contract instead of writing one from scratch. While this definitely does minimize the risk of contracts having bugs in production, it does stop developers from dealing with all the complications one might(&lt;em&gt;and should&lt;/em&gt;) go through while developing the same. And, therefore, keeps us from deep-diving into solidity concepts more often than not.&lt;/p&gt;

&lt;p&gt;While simply following a blockchain development course or consuming online content on smart contracts might help you get started with any specific topic, it won’t let you dive in deep and learn the fundamentals. &lt;em&gt;Not unless you get your hands dirty with the same.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most importantly, not experimenting with Solidity smart contracts also keeps us from learning about the various kinds of security vulnerabilities a contract might have and the plethora of ways a contract can be broken.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For instance, going back to the re-entrancy example:&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every smart contract developer might have heard, read, or discussed the reentrancy attack vector, but how many of us actually went ahead and tried to experiment with the same on some dummy contract, just to understand what happens behind the scenes?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is precisely where smart contract security-based war games or CTFs like &lt;a href="https://ethernaut.openzeppelin.com/"&gt;&lt;em&gt;Ethernaut&lt;/em&gt;&lt;/a&gt; &lt;em&gt;or&lt;/em&gt; &lt;a href="https://www.damnvulnerabledefi.xyz/"&gt;&lt;em&gt;Damn Vulnerable Defi&lt;/em&gt;&lt;/a&gt; also play such a significant role in learning smart contract development and security. They allow you to directly interact with a contract directly, make mistakes and eventually figure out the vulnerabilities.&lt;/p&gt;

&lt;p&gt;Now, there are definitely some smart contract developers who experiment with Solidity and smart contracts a lot and therefore are becoming the best in the industry (&lt;em&gt;check the&lt;/em&gt; &lt;a href="https://underhanded.soliditylang.org/"&gt;&lt;em&gt;Underhand Solidity Contest winners&lt;/em&gt;&lt;/a&gt; &lt;em&gt;or their&lt;/em&gt; &lt;a href="https://github.com/ethereum/solidity-underhanded-contest/tree/master/2022/submissions_2022"&gt;&lt;em&gt;Challange Submissions&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, for instance&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T7u35DVe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk7w3p8mcr6k5zy0rhv0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T7u35DVe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk7w3p8mcr6k5zy0rhv0.png" alt="Winners of Underhanded Solidity Contest 2022&amp;lt;br&amp;gt;
" width="880" height="777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, as I previously mentioned, &lt;strong&gt;&lt;em&gt;for the web3 world to expand its boundaries and scale with utmost security measures, we cannot simply rely on a handful of experts in the space.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, every smart contract developer should aspire to learn more and dive in deep by not simply learning the basics but experimenting, making mistakes, learning from them, and then sharing them with the community.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Because, quite similar to life in general, the best learnings in software development also comes from mistakes.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Mistakes or Learning Experiences&lt;/em&gt;&lt;/strong&gt; every smart contract dev should have at least once:
&lt;/h2&gt;

&lt;p&gt;If you are just starting out with Solidity, there are a bunch of mistakes/experiments you can (&lt;em&gt;and should&lt;/em&gt;) do while learning.&lt;/p&gt;

&lt;p&gt;Dropping off some of them from the top of my head.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solidity 101 mistakes/learnings:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Using &lt;strong&gt;&lt;em&gt;memory&lt;/em&gt;&lt;/strong&gt; keyword instead of &lt;strong&gt;&lt;em&gt;storage&lt;/em&gt;&lt;/strong&gt; and then realizing you never really stored the crucial contract states permanently on the contract.&lt;/li&gt;
&lt;li&gt; Relying on blog time stamps for random calculation and realizing how they can be manipulated by the miners is therefore a bad practice. &lt;em&gt;More details are&lt;/em&gt; &lt;a href="https://swcregistry.io/docs/SWC-116"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt; Storing secret information on smart contract state variables with a private visibility modifier only to find out that nothing is private on a smart contract.
Every state variable value can be seen by anyone. &lt;em&gt;Read more about this in&lt;/em&gt; &lt;a href="https://swcregistry.io/docs/SWC-136"&gt;&lt;em&gt;SWC-136&lt;/em&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; You will always believe that a contract with no payable function (&lt;em&gt;or payable fallback function&lt;/em&gt;) will never receive any ether unless you learn how &lt;a href="https://swcregistry.io/docs/SWC-106"&gt;&lt;strong&gt;&lt;em&gt;selfdestruct&lt;/em&gt;&lt;/strong&gt; can forcefully fund such a contract with ETH.&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; Failing to provide required &lt;em&gt;input validation&lt;/em&gt; in functions &amp;amp; realizing how this can lead to unwanted scenarios any invalid arguments can be passed without proper validation. ( &lt;a href="https://swcregistry.io/docs/SWC-123"&gt;&lt;em&gt;SWC-123&lt;/em&gt;&lt;/a&gt;  )&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Gas Optimization &amp;amp; Security mistakes/learnings :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;5. Writing expensive for loops only to find out how this could lead to a &lt;a href="https://swcregistry.io/docs/SWC-128"&gt;&lt;strong&gt;&lt;em&gt;block gas limit&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; issue and fail the complete transaction&lt;/p&gt;

&lt;p&gt;6. Violating &lt;a href="https://fravoll.github.io/solidity-patterns/checks_effects_interactions.html"&gt;&lt;strong&gt;&lt;em&gt;check effects interaction pattern&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;s and realizing how this could lead to the classic Rentrancy attack.&lt;/p&gt;

&lt;p&gt;7. Using delegate calls between two contracts with different storage layouts and realizing why storage layouts must be exactly similar for delegate calls to work.&lt;br&gt;&lt;br&gt;
It's extremely important to ensure storage layout between two contracts involved in a &lt;em&gt;delegatecall()&lt;/em&gt; is exactly the same. Otherwise, this leads to storage collisions. Read more about storage collisions &lt;a href="https://docs.openzeppelin.com/upgrades-plugins/1.x/proxies#storage-collisions-between-implementation-versions"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;8. Including inadequate access controls for imperative functions of the contract and realizing how this could lead to any bad actor executing transactions without approval. &lt;a href="https://swcregistry.io/docs/SWC-105"&gt;&lt;em&gt;SWC-105&lt;/em&gt;&lt;/a&gt;  covers this topic quite adequately.&lt;/p&gt;

&lt;p&gt;9. Trying to send ether from one contract to another using the &lt;strong&gt;&lt;em&gt;transfer&lt;/em&gt;&lt;/strong&gt; function only to find out that it always sends a hard-coded &lt;strong&gt;&lt;em&gt;2100&lt;/em&gt;&lt;/strong&gt; gas only. This will help you realize the significance of the &lt;strong&gt;&lt;em&gt;.call.value(…)(“ ”)&lt;/em&gt;&lt;/strong&gt; function which allows adjusting the gas to be sent with a transaction. Read more about why &lt;a href="https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/"&gt;.&lt;em&gt;call() might be better than transfer()&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;10. Realising how tricky it is to delete mappings values in struct or in general and learning the right way to do it.&lt;/p&gt;

&lt;p&gt;11. Trying to swap &lt;em&gt;Token A&lt;/em&gt; with &lt;em&gt;Token B&lt;/em&gt; using Uniswap and being vulnerable to sandwich attacks. This might help you realize the dark side of transaction order dependencies and MEVs and the  &lt;a href="https://www.paradigm.xyz/2020/08/ethereum-is-a-dark-forest"&gt;&lt;em&gt;DARK Forest of Ethereum&lt;/em&gt;&lt;/a&gt; &lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Mr. Walter White experimenting with Solidity&lt;/p&gt;

&lt;p&gt;While these are just some examples, there are way more interesting topics in smart contract development, from basic solidity to advanced deep dives in EVM Opcodes, that one can learn and experiment with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping it UP 😊
&lt;/h2&gt;

&lt;p&gt;=================&lt;/p&gt;

&lt;p&gt;Web3 has come a long way in a very short span of time, however, the security of smart contracts is still a major concern.&lt;/p&gt;

&lt;p&gt;The two ideas discussed above aren’t gonna make it all sunshine and rainbows at one go. There is undoubtedly a lot more we need to do to make Web3 a safer place.&lt;/p&gt;

&lt;p&gt;However, imagine a whole generation of developers that includes the two ideas mentioned above while learning or developing smart contracts, prioritizes learning the basics of security, experiments with Solidity, learn from their mistakes, and then shares those learnings with the community.&lt;/p&gt;

&lt;p&gt;That will, slowly but surely, lead us to a much safer and more secure web3 world than what we have now.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;All it takes is some commitment be to consistent, the will to learn more from mistakes and experiments, and the aspiration to be the better version of the smart contract developer that you already are.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  About myself
&lt;/h2&gt;

&lt;p&gt;============&lt;/p&gt;

&lt;h2&gt;
  
  
  Who am I? 🙋🏻‍♂️
&lt;/h2&gt;

&lt;p&gt;𝙃𝙞, 𝙄 𝙖𝙢 𝙕𝙖𝙧𝙮𝙖𝙗 👋🏻&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A Smart Contract Engineer and Dev Team Lead at &lt;a href="https://medium.com/u/7e1cbe74a9a4?source=post_page-----4b3decc52b46--------------------------------"&gt;Push Protocol&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;  I work on smart contracts development as well as security audits of smart contracts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Drop a ‘HI’ and Get in Touch 🤝
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://linktr.ee/zaryab2000"&gt;&lt;strong&gt;&lt;em&gt;My Socials |Follow my Weekly Newsletter |My Web3 Speech &amp;amp; Presentations&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>security</category>
      <category>solidity</category>
      <category>ethereum</category>
    </item>
    <item>
      <title>Does adding a PAYABLE keyword in Solidity actually save GAS?</title>
      <dc:creator>Decipher with Zaryab</dc:creator>
      <pubDate>Mon, 20 Jun 2022 06:49:48 +0000</pubDate>
      <link>https://forem.com/zaryab2000/does-adding-a-payable-keyword-in-solidity-actually-save-gas-24bm</link>
      <guid>https://forem.com/zaryab2000/does-adding-a-payable-keyword-in-solidity-actually-save-gas-24bm</guid>
      <description>&lt;p&gt;If you have been developing smart contracts using Solidity lately, chances are you might have come across the payable keyword. &lt;/p&gt;

&lt;p&gt;This blog is specifically about the same where we decipher all its interesting and weird secrets. 😃&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick Intro: Basics of the Payable Keyword
&lt;/h2&gt;

&lt;p&gt;Out of all the wonderful things that a smart contract can do, storing your money(ETH) is one of them. Now in order to take receive ETH in a smart contract, Solidity language has got a specific keyword called payable.&lt;/p&gt;

&lt;p&gt;A payable keyword, in very simpler terms, is a modifier in the solidity language that can be attached to any function. Once attached, this keyword allows the function to receive ether. In other words, while triggering a function with a payable keyword, you can send ether (msg.value) along with that transaction.&lt;/p&gt;

&lt;p&gt;While this is all good, I came across an interesting caveat around the payable keyword while scrolling through Twitter a couple of months ago. It grabbed all my attention and I figured that an interesting (but really wired) scenario takes place whenever a payable modifier is attached to any function 👀.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let’s take a quick look at this interestingly wired scenario:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6tsm7yfw4gbmawoz77p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6tsm7yfw4gbmawoz77p.png" alt="A setter function with NO Payable Keyword"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image attached above, we have a very simple setter function that sets the uint256 variable state to 100. If you trigger this function, you will find that the transaction gas cost is somewhere around 43300.&lt;/p&gt;

&lt;p&gt;Alright, now let’s look at a 2nd condition.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjnn51smbcg7tt2xa6lc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjnn51smbcg7tt2xa6lc.png" alt="A setter function WITH Payable Keyword"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the 2nd case, we have the exact same function that executes an exactly similar transaction of setting a state variable. However, the only difference here is an additional payable modifier attached to the function.&lt;/p&gt;

&lt;p&gt;Quite interestingly if you look at the transaction gas cost for calling this function, it's around 43276 which is lower than the function with no payable keyword mentioned above.&lt;/p&gt;

&lt;p&gt;Yes, you got that right. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Adding a simple payable keyword just reduced the amount of gas consumption in the function. 😃&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The AHA Moment 💡💡
&lt;/h2&gt;

&lt;p&gt;Alright, now it's time to understand — Why exactly does the payable modifier lower the Gas consumption?&lt;/p&gt;

&lt;p&gt;A very simple answer to that question is:&lt;/p&gt;

&lt;p&gt;Adding a payable keyword lowers the number of opcodes being executed, thus lowering the amount of gas consumption.&lt;/p&gt;

&lt;p&gt;That’s weird, isn’t it? How does adding an extra modifier to a function lower down the number of opcodes instead of increasing it???&lt;/p&gt;

&lt;p&gt;Well here’s some technical (and logical) explanation for it. 😃&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As we already know, for a function to be capable of receiving ether, a payable modifier must be attached to it. While a function without any payable modifier shall never be able to receive any ether.&lt;/li&gt;
&lt;li&gt;It must be noted that this is a strict rule in Solidity and therefore if you try to pass Ether while calling a non-payable function, it will simply revert.&lt;/li&gt;
&lt;li&gt;Therefore in the case of Non-Payable functions, there are additional opcodes that are executed while calling a non-payable function which ensures that the function shall only be executed if the ether (msg.value) is sent along with the transaction is exactly equal to ZERO.&lt;/li&gt;
&lt;li&gt;However, the same is not true for Payable function. Payable functions allow users to pass in both non-zero or zero ether values while calling the function.&lt;/li&gt;
&lt;li&gt;This basically means that even if zero ether (msg.value == 0) is sent while calling a payable function, the transaction is not reverted. Hence, there is no need to explicitly check the msg.value in the case of Payable functions.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  In a Nutshell 🥜
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F055wnzcunyqo1jkqll4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F055wnzcunyqo1jkqll4a.png" alt="GAS Difference between PAYABLE and NON-PAYABLE Functions in Solidity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;In case of Non-Payable Functions:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;a. Additional checks are included to ensure that no ether value is passed while calling the function.&lt;/p&gt;

&lt;p&gt;b. These checks increase the number of opcodes being executed.&lt;/p&gt;

&lt;p&gt;c. The increased number of opcodes ultimately results in higher gas usage.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;In the case of Payable functions:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;a. No additional checks are required since the function can accept both zero or non-zero values of ether.&lt;/p&gt;

&lt;p&gt;b. No additional checks means no additional opcodes being executed.&lt;/p&gt;

&lt;p&gt;c. Lower opcodes in execution means lower consumption of gas.&lt;/p&gt;

&lt;p&gt;My TWO CENTS 🪙🪙&lt;br&gt;
Does all of the above-mentioned details, mean we should use PAYABLE Functions to save gas?&lt;/p&gt;

&lt;p&gt;Well, that could be a discussion.&lt;/p&gt;

&lt;p&gt;Gas optimization is undoubtedly something that every smart contract wizard dreams of in their contracts.&lt;/p&gt;

&lt;p&gt;However, an imperative question is:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fascy65jcf15dlemqbvkw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fascy65jcf15dlemqbvkw.png" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;While saving gas is important, it’s not really a good idea to compromise on the intended behavior of the function, minimizing the use of necessary state changes, or using inadequate tactics just to save a few extra amounts of gas. In other words, if a function has nothing to do with receiving ether, then it should not really have any payable keyword attached to it, even if that saves you some gas.&lt;/p&gt;

&lt;p&gt;Therefore, I firmly believe that adding an unnecessary payable keyword in a function just to save gas is probably a bad decision. The above-mentioned scenario of reduction in gas while adding the payable keyword is just a wired solidity language design that is not at all effective.&lt;/p&gt;

&lt;h2&gt;
  
  
  About myself
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Who am I? 🙋🏻‍♂️&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
𝙃𝙞, 𝙄 𝙖𝙢 𝙕𝙖𝙧𝙮𝙖𝙗 👋🏻 &lt;br&gt;
I am a proficient Blockchain and Smart Contract Engineer with a vision of Decentralizing and Securing the traditional Web with Web3. Mostly work on Smart Contracts with significant experience in both Development and Smart Contract Security.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I Do 🧑🏼‍💻&lt;/strong&gt;&lt;br&gt;
I write secure and optimized Smart Contracts&lt;br&gt;
I perform security audits on smart contracts and enhance the overall security of smart contracts on EVM chains&lt;br&gt;
I write and speak about Web3 and Smart Contracts &amp;amp; contribute my part towards expanding the boundaries for Web3.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drop a ‘HI’ and Get in Touch 🤝&lt;/em&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/zaryab-afser-97085b157/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt;. | &lt;a href="https://twitter.com/zaryab_eth" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;. | &lt;a href="https://github.com/zaryab2000" rel="noopener noreferrer"&gt;Github&lt;/a&gt; | &lt;a href="https://www.linkedin.com/newsletters/decipher-with-zaryab-6944516958839021568/" rel="noopener noreferrer"&gt;Subscribe to My Weekly Newsletter&lt;/a&gt; | &lt;a href="https://zaryab2000.notion.site/78bcd204b866426687e1afbcdc61c5c7" rel="noopener noreferrer"&gt;Invite me for Web3 Events&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>web3</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Solidity Guide: Behind the Scenes of PUBLIC and EXTERNAL Visibility of Solidity</title>
      <dc:creator>Decipher with Zaryab</dc:creator>
      <pubDate>Tue, 27 Jul 2021 14:42:56 +0000</pubDate>
      <link>https://forem.com/zaryab2000/behind-the-scenes-of-public-and-external-visibility-of-solidity-44b6</link>
      <guid>https://forem.com/zaryab2000/behind-the-scenes-of-public-and-external-visibility-of-solidity-44b6</guid>
      <description>&lt;p&gt;✩ 🎀 ---𝑨𝒏 𝑰𝒏𝒕𝒆𝒓𝒆𝒔𝒕𝒊𝒏𝒈 𝑭𝑨𝑪𝑻 𝒆𝒗𝒆𝒓𝒚 𝑺𝒎𝒂𝒓𝒕 𝑪𝒐𝒏𝒕𝒓𝒂𝒄𝒕 𝑫𝒆𝒗𝒆𝒍𝒐𝒑𝒆𝒓 𝑴𝑼𝑺𝑻 𝑲𝑵𝑶𝑾--- 🎀 ✩&lt;/p&gt;

&lt;p&gt;When it comes to 𝐏𝐮𝐛𝐥𝐢𝐜 &amp;amp; 𝐄𝐱𝐭𝐞𝐫𝐧𝐚𝐥 visibility keywords,&lt;/p&gt;

&lt;p&gt;𝘏𝘦𝘳𝘦 𝘪𝘴 𝘸𝘩𝘢𝘵 M𝘰𝘴𝘵 𝘰𝘧 𝘶𝘴 A𝘭𝘳𝘦𝘢𝘥𝘺 K𝘯𝘰𝘸:&lt;br&gt;
𝐏𝐮𝐛𝐥𝐢𝐜 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧: Can be called from everywhere, internally as well as from outside the Contract&lt;/p&gt;

&lt;p&gt;𝐄𝐱𝐭𝐞𝐫𝐧𝐚𝐥 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧: Can only be called from outside the contract and not accessible from within the contract.&lt;/p&gt;

&lt;p&gt;𝘏𝘦𝘳𝘦 𝘪𝘴 𝘸𝘩𝘢𝘵 S𝘰𝘮𝘦 𝘰𝘧 𝘶𝘴 M𝘪𝘨𝘩𝘵 K𝘯𝘰𝘸:&lt;/p&gt;

&lt;p&gt;𝐏𝐮𝐛𝐥𝐢𝐜 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧: Leads to Higher GAS COSTS&lt;/p&gt;

&lt;p&gt;𝐄𝐱𝐭𝐞𝐫𝐧𝐚𝐥 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧: Costs Comparatively Lower GAS&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j4Vn4tPQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zc5yg3t6zexwtnkq3il.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j4Vn4tPQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zc5yg3t6zexwtnkq3il.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
𝑵𝒐𝒘, 𝑯𝒆𝒓𝒆 𝒊𝒔 𝒘𝒉𝒂𝒕 𝑴𝒐𝒔𝒕 𝒐𝒇 𝒖𝒔 𝑴𝒊𝒈𝒉𝒕 𝑵𝑶𝑻 𝑲𝑵𝑶𝑾:&lt;/p&gt;

&lt;p&gt;𝐖𝐡𝐲 𝐄𝐱𝐚𝐜𝐭𝐥𝐲 𝐝𝐨𝐞𝐬 𝐏𝐔𝐁𝐋𝐈𝐂 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬 𝐂𝐎𝐒𝐓𝐒 𝐦𝐨𝐫𝐞 𝐆𝐚𝐬 𝐭𝐡𝐚𝐧 𝐄𝐗𝐓𝐄𝐑𝐍𝐀𝐋 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬?&lt;br&gt;
In the case of PUBLIC Functions, arguments of the Functions are copied to 𝐌𝐄𝐌𝐎𝐑𝐘.&lt;br&gt;
While on the other hand, Functions with External visibility can directly read arguments from 𝐂𝐀𝐋𝐋𝐃𝐀𝐓𝐀.&lt;/p&gt;

&lt;p&gt;𝙎𝙞𝙣𝙘𝙚 𝘾𝘼𝙇𝙇𝘿𝘼𝙏𝘼 𝙞𝙨 𝘾𝙃𝙀𝘼𝙋𝙀𝙍 𝙩𝙝𝙖𝙣 𝙈𝙀𝙈𝙊𝙍𝙔, 𝙀𝙭𝙩𝙚𝙧𝙣𝙖𝙡 𝙁𝙪𝙣𝙘𝙩𝙞𝙤𝙣𝙨 𝙧𝙚𝙨𝙪𝙡𝙩 𝙞𝙣 𝙖 𝙡𝙤𝙬𝙚𝙧 𝙀𝙭𝙚𝙘𝙪𝙩𝙞𝙤𝙣 𝘾𝙤𝙨𝙩(𝙂𝙖𝙨) 𝙩𝙝𝙖𝙣 𝙋𝙐𝘽𝙇𝙄𝘾 𝙁𝙪𝙣𝙘𝙩𝙞𝙤𝙣𝙨.&lt;/p&gt;

&lt;p&gt;𝘈𝘳𝘦 𝘺𝘰𝘶 𝘤𝘶𝘳𝘪𝘰𝘶𝘴 𝘢𝘣𝘰𝘶𝘵 𝘸𝘩𝘺 𝘗𝘜𝘉𝘓𝘐𝘊 𝘧𝘶𝘯𝘤𝘵𝘪𝘰𝘯𝘴 𝘤𝘰𝘱𝘺 𝘢𝘳𝘨𝘶𝘮𝘦𝘯𝘵𝘴 𝘵𝘰 𝘔𝘌𝘔𝘖𝘙𝘠 𝘸𝘩𝘪𝘭𝘦 𝘌𝘹𝘵𝘦𝘳𝘯𝘢𝘭 𝘍𝘶𝘯𝘤𝘵𝘪𝘰𝘯𝘴 𝘥𝘰𝘯'𝘵?&lt;/p&gt;

&lt;p&gt;PUBLIC Functions can be called from outside as well as within the contract(Internal Calls). And internal calls are executed via code JUMP as array arguments are passed internally by Pointers to Memory.&lt;br&gt;
Hence, when the compiler generates OPCODES for an internal function, the function expects its arguments to be located in Memory itself.&lt;/p&gt;

&lt;p&gt;However, this is not at all the case in EXTERNAL functions. They don't care about Internal Calls at all and thus end up saving some gas.&lt;br&gt;
Don't forget to COMMENT and Share your IDEAS. 𝗟𝗲𝘁'𝘀 𝗟𝗲𝗮𝗿𝗻 𝗮𝗻𝗱 𝗚𝗿𝗼𝘄 𝗧𝗼𝗴𝗲𝘁𝗵𝗲𝗿.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
      <category>smartcontracts</category>
      <category>ethereum</category>
    </item>
    <item>
      <title>Solidity Security: The Significance of CHECK-EFFECTS-INTERACTION Pattern in Smart Contracts</title>
      <dc:creator>Decipher with Zaryab</dc:creator>
      <pubDate>Tue, 27 Jul 2021 14:30:05 +0000</pubDate>
      <link>https://forem.com/zaryab2000/the-significance-of-check-effects-interaction-pattern-5hn6</link>
      <guid>https://forem.com/zaryab2000/the-significance-of-check-effects-interaction-pattern-5hn6</guid>
      <description>&lt;p&gt;𝘛𝘰 𝘢𝘭𝘭 𝘵𝘩𝘦 𝘚𝘔𝘈𝘙𝘛 𝘊𝘰𝘯𝘵𝘳𝘢𝘤𝘵 𝘋𝘦𝘷𝘴 𝘰𝘶𝘵 𝘵𝘩𝘦𝘳𝘦, 📢&lt;br&gt;
⚠️ 𝙉𝙀𝙑𝙀𝙍 𝙑𝙄𝙊𝙇𝘼𝙏𝙀 𝙏𝙃𝙀 𝘾𝙃𝙀𝘾𝙆-𝙀𝙁𝙁𝙀𝘾𝙏𝙎-𝙄𝙉𝙏𝙀𝙍𝘼𝘾𝙏𝙄𝙊𝙉 𝙋𝘼𝙏𝙏𝙀𝙍𝙉 ⚠️&lt;/p&gt;

&lt;p&gt;Building a secure smart contract does require adhering to the Best Practices.&lt;br&gt;
And one of the most crucial practices to keep in mind is the 𝐂𝐇𝐄𝐂𝐊 𝐄𝐅𝐅𝐄𝐂𝐓𝐒 𝐈𝐍𝐓𝐄𝐑𝐀𝐂𝐓𝐈𝐎𝐍 𝐏𝐚𝐭𝐭𝐞𝐫𝐧 while making External Calls.&lt;/p&gt;

&lt;p&gt;𝐖𝐇𝐀𝐓 𝐞𝐱𝐚𝐜𝐭𝐥𝐲 𝐝𝐨𝐞𝐬 𝐢𝐭 𝐦𝐞𝐚𝐧?&lt;br&gt;
In simple terms, it means that while designing a function in solidity, any state modification in the function must happen before an external call is made.&lt;/p&gt;

&lt;p&gt;𝐖𝐇𝐘 𝐔𝐬𝐞 𝐭𝐡𝐢𝐬 𝐏𝐚𝐭𝐭𝐞𝐫𝐧?&lt;br&gt;
Remember the DAO Hack of 2016 where the attacker drained 3.6 million ETH?&lt;br&gt;
Well, one of the Imperative reasons behind that hack was the Violation of Check-Effects-Interaction patterns in function code.&lt;/p&gt;

&lt;p&gt;𝙒𝙝𝙮 𝙗𝙚 𝙘𝙖𝙧𝙚𝙛𝙪𝙡 𝙬𝙝𝙚𝙣 𝙀𝙭𝙚𝙘𝙪𝙩𝙞𝙣𝙜 𝙀𝙭𝙩𝙚𝙧𝙣𝙖𝙡 𝘾𝙖𝙡𝙡𝙨?&lt;br&gt;
An external call technically shifts the control over execution to another contract or a Third Party. This allows the Third-party contract to leverage from the fact that the Contract State didn't change before the external call.&lt;/p&gt;

&lt;p&gt;It leads to an extremely undesirable scenario where a malicious actor can re-enter the contract and disturb the expected flow. Thus, leading to a potential Re-entrancy Scenario.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ezwkfn6xx69ong7620w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ezwkfn6xx69ong7620w.png" alt="Check-Effects-Pattern"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;𝐇𝐎𝐖 𝐝𝐨𝐞𝐬 𝐭𝐡𝐢𝐬 𝐏𝐚𝐭𝐭𝐞𝐫𝐧 𝐒𝐞𝐜𝐮𝐫𝐞 𝐂𝐨𝐧𝐭𝐫𝐚𝐜𝐭𝐬?&lt;br&gt;
Let's understand this by breaking down the 3 imperative steps in this pattern. (𝘔𝘰𝘴𝘵 𝘪𝘮𝘱𝘰𝘳𝘵𝘢𝘯𝘵𝘭𝘺, 𝘐𝘯 𝘵𝘩𝘦 𝘦𝘹𝘢𝘤𝘵 𝘰𝘳𝘥𝘦𝘳)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;𝗖𝗛𝗘𝗖𝗞&lt;br&gt;
The first part is to implement a 𝘾𝙃𝙀𝘾𝙆 or input validations(𝘸𝘪𝘵𝘩 𝘳𝘦𝘲𝘶𝘪𝘳𝘦 𝘰𝘳 𝘢𝘴𝘴𝘦𝘳𝘵 𝘴𝘵𝘢𝘵𝘦𝘮𝘦𝘯𝘵𝘴) to ensure that arguments passed are valid and the function is ready to be executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;𝗘𝗙𝗙𝗘𝗖𝗧𝗦&lt;br&gt;
Resolve all the 𝙀𝙁𝙁𝙀𝘾𝙏𝙎 to the State of the Contract. This part involves optimistically modifying the State Variables to a valid state in the protocol.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;𝗜𝗡𝗧𝗘𝗥𝗔𝗖𝗧𝗜𝗢𝗡&lt;br&gt;
The final step should include any 𝙄𝙉𝙏𝙀𝙍𝘼𝘾𝙏𝙄𝙊𝙉 with other external contracts. This is the step that should include any external call that is being made from the function, at the very end of the function.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;✧ 🎀 𝗜𝗻 𝗮 𝗡𝗨𝗧𝗦𝗛𝗘𝗟𝗟 🎀 ✧&lt;br&gt;
External calls must be the very last thing that you should do in a function. 𝘼𝙣𝙮 𝙨𝙩𝙖𝙩𝙚 𝙫𝙖𝙧𝙞𝙖𝙗𝙡𝙚 𝙢𝙤𝙙𝙞𝙛𝙞𝙘𝙖𝙩𝙞𝙤𝙣 𝙢𝙪𝙨𝙩 𝙝𝙖𝙥𝙥𝙚𝙣 𝙗𝙚𝙛𝙤𝙧𝙚 𝙖𝙣 𝙚𝙭𝙩𝙚𝙧𝙣𝙖𝙡 𝙘𝙖𝙡𝙡 𝙞𝙨 𝙚𝙭𝙚𝙘𝙪𝙩𝙚𝙙 𝙞𝙣 𝙤𝙧𝙙𝙚𝙧 𝙩𝙤 𝙖𝙫𝙤𝙞𝙙 𝙖 𝙧𝙚-𝙚𝙣𝙩𝙧𝙖𝙣𝙘𝙮 𝙨𝙘𝙚𝙣𝙖𝙧𝙞𝙤.&lt;/p&gt;

&lt;p&gt;Moreover, even if attackers try to re-enter a function that follows the CHECK-EFFECTS-INTERACTION pattern, they cannot really abuse the State of the contract as it has been already modified before the external call is made.&lt;/p&gt;

</description>
      <category>security</category>
      <category>smartcontracts</category>
      <category>blockchain</category>
      <category>ethereum</category>
    </item>
  </channel>
</rss>
