<?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: Godspower Eze</title>
    <description>The latest articles on Forem by Godspower Eze (@the_python_dev_).</description>
    <link>https://forem.com/the_python_dev_</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%2F481360%2Fbac5f8c5-0888-4e94-95cf-827868ccfe3a.jpg</url>
      <title>Forem: Godspower Eze</title>
      <link>https://forem.com/the_python_dev_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/the_python_dev_"/>
    <language>en</language>
    <item>
      <title>"Hello World" in Solidity</title>
      <dc:creator>Godspower Eze</dc:creator>
      <pubDate>Fri, 08 Jul 2022 07:21:20 +0000</pubDate>
      <link>https://forem.com/the_python_dev_/hello-world-in-solidity-487k</link>
      <guid>https://forem.com/the_python_dev_/hello-world-in-solidity-487k</guid>
      <description>&lt;p&gt;The "Hello World" program is most likely the first thing you would learn when trying to learn a new language.&lt;/p&gt;

&lt;p&gt;Here's what a "Hello World" smart contract in Solidity looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;
&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;HelloWorld&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is Solidity?
&lt;/h2&gt;

&lt;p&gt;Solidity is statically typed, object-oriented, and high-level language for building smart contracts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Walkthrough
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Solidity files take the extension &lt;strong&gt;.sol&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;// SPDX-License-Identifier: MIT&lt;/strong&gt; - specifies the license used by this contract. It's not mandatory but it's important since smart contracts are open source.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pragma solidity 0.8.0;&lt;/strong&gt; - specifies the version of solidity that should be used during the compilation of the contract. It's required for the code to run. Other variations like &lt;strong&gt;pragma solidity ^0.8.0;&lt;/strong&gt;, &lt;strong&gt;pragma solidity &amp;gt;=0.8.0;&lt;/strong&gt;,&lt;strong&gt;pragma solidity &amp;lt;=0.8.0;&lt;/strong&gt; and &lt;strong&gt;pragma solidity &amp;gt;=0.4.22 &amp;lt;0.8.0;&lt;/strong&gt; can also be used to specify versions greater or equal to 0.8.0, versions greater or equal to version(same as the previous), versions less than or equal to 0.8.0 and versions greater than or equal to 0.4.22 but less than 0.8.0 respectively.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;contract HelloWorld {...}&lt;/strong&gt; - creates a code block for writing the logic of the smart contract. This is similar to defining classes in object-oriented programming languages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;string public greeting = "Hello World";&lt;/strong&gt;  - as a statically-typed language the data type needs to be specified. Here, a string is used. The &lt;strong&gt;public&lt;/strong&gt; keyword is a visibility modifier. And, the &lt;strong&gt;greeting&lt;/strong&gt; is the variable name. &lt;strong&gt;public makes&lt;/strong&gt; it possible for the variable greeting to be called a getter function &lt;strong&gt;greeting()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations!! you just wrote your first &lt;a href="https://blockchaintotheworld.com/understanding-smart-contracts/"&gt;smart contract&lt;/a&gt; in solidity. Go ahead and try the code on &lt;a href="http://remix.ethereum.org/"&gt;Remix&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Just as this is your first, this is also our first post on here. First of many.&lt;/p&gt;

&lt;p&gt;Good luck on your journey.&lt;/p&gt;

&lt;p&gt;This post was originally published &lt;a href="https://blockchaintotheworld.com/hello-world-in-solidity/"&gt;here&lt;/a&gt; on Blockchaintotheworld.com&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Understanding Smart Contracts</title>
      <dc:creator>Godspower Eze</dc:creator>
      <pubDate>Fri, 08 Jul 2022 06:47:10 +0000</pubDate>
      <link>https://forem.com/the_python_dev_/understanding-smart-contracts-15j7</link>
      <guid>https://forem.com/the_python_dev_/understanding-smart-contracts-15j7</guid>
      <description>&lt;p&gt;Smart contracts are by far one of the most pivotal innovations introduced in the blockchain ecosystem. It opened a new world of possibilities. But, what exactly are smart contracts?&lt;/p&gt;

&lt;p&gt;This article would focus on explaining in the most basic sense what smart contracts are.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Smart Contracts?
&lt;/h2&gt;

&lt;p&gt;Smart contracts are programs that run on the blockchain. Or, deployed onto a blockchain.&lt;/p&gt;

&lt;p&gt;Every smart contract has a unique address called a contract address which is generated on deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background Story
&lt;/h2&gt;

&lt;p&gt;Blockchain technology was popularized by Satoshi Nakamoto with the creation of Bitcoin, the first decentralized digital currency.&lt;/p&gt;

&lt;p&gt;On the Bitcoin Software (also known as Bitcoin Core), a programming language called &lt;a href="https://komodoplatform.com/en/academy/bitcoin-script/"&gt;Bitcoin Scripting Language&lt;/a&gt;(or Bitcoin Script) is used for interaction. It is used to define how coins are moved in the network.&lt;/p&gt;

&lt;p&gt;Note that the Bitcoin Blockchain wasn't built with the Bitcoin Script rather the Bitcoin Script is built on top of it. The first implementation of the Bitcoin blockchain was built with C++ and other implementations have been built since then.&lt;/p&gt;

&lt;p&gt;Based on the explanation above, the Bitcoin script is a smart contract because it runs on the blockchain and it defines the rules of how bitcoins are handled. But, this has a limitation. It is &lt;a href="https://en.wikipedia.org/wiki/Turing_completeness"&gt;turing incomplete&lt;/a&gt;. It's rigid and can't be used for most use cases other than Bitcoin. It was built for Bitcoin and it alone it.&lt;/p&gt;

&lt;p&gt;Developers needed a way to build on the Blockchain without having logical limitations. This is where Ethereum comes in.&lt;/p&gt;

&lt;p&gt;In late 2013, Vitalik Buterin in a &lt;a href="https://web.archive.org/web/20131228111141/https://vbuterin.com/ethereum.html"&gt;blog post&lt;/a&gt; titled Ethereum: The Ultimate Smart Contract and Decentralized Application Platform described an idea for a Turing-complete blockchain – a decentralized computer that, given enough time and resources, could run any application.&lt;/p&gt;

&lt;p&gt;Ethereum launched in 2015 allowing developers to build smart contracts with turing-complete programming called &lt;a href="https://blockchaintotheworld.com/hello-world-in-solidity/"&gt;Solidity&lt;/a&gt;. Since then other smart contract compatible blockchains and more languages for building them have come into the space.&lt;/p&gt;

&lt;p&gt;Although Bitcoin Script can be seen as a smart contract, the terms smart contracts weren't used that much before Ethereum. Therefore, Ethereum popularized the idea of smart contracts while building an ideal version.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do Smart Contracts Work?
&lt;/h2&gt;

&lt;p&gt;They work like most computer programs would typically work. A set of instructions are defined and using those instructions you could perform certain operations if the necessary conditions are met.&lt;/p&gt;

&lt;p&gt;One distinct factor is the smart contract depends on the blockchain for storage. And most interactions are done within the confines of the blockchain. They can't easily interact with the other side world like calling a WEB API.&lt;/p&gt;

&lt;p&gt;Smart contracts are compiled to bytecode and then run by the &lt;strong&gt;Ethereum Virtual Machine (EVM)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The EVM can be seen as a processor containing Operation codes (also called &lt;strong&gt;OPCODES&lt;/strong&gt;). Every section of the bytecode has its representation in the Operation code which simply tells the EVM what to do with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Are Smart Contracts smart?
&lt;/h2&gt;

&lt;p&gt;The term word smart in the smart contract gives off the idea of something related to artificial intelligence. But, there is nothing smart about smart contracts. Until we start calling computer programs that use "if/then" statements "smart", smart contracts are not smart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Are Smart Contracts actual Legal Contracts?
&lt;/h2&gt;

&lt;p&gt;No, they are not.&lt;/p&gt;

&lt;p&gt;Smart contracts follow the fundamental idea of a legal contract which is adhering to predefined set rules. But, smart contracts are just like most computer programs and best define how they work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attributes of a Smart Contract
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;Immutability&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once smart contracts are deployed to the blockchain they cannot be changed. It is impossible to do that.&lt;/p&gt;

&lt;p&gt;This is a blessing and a curse because, before smart contracts, developers weren't used to building programs that cannot be changed but at the same time, this helps us ensure security during development and after deployment.&lt;/p&gt;

&lt;p&gt;During development, smart contract developers build with the mindset that the smart contract is unchangeable so they consider a lot (especially security). After deployment, as developers, we are sure that our contract cannot be changed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;Publicly Available&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All smart contracts are public. They are publicly available if a smart contract is deployed onto a public blockchain (which is mostly the case).&lt;/p&gt;

&lt;p&gt;Every public blockchain has an &lt;a href="https://etherscan.io/"&gt;explorer&lt;/a&gt; where most activities performed on the blockchain are displayed. To find a smart contract, all you need to do is get the contract address and search it on explorer.&lt;/p&gt;

&lt;p&gt;This also means that anyone can interact with a deployed smart contract. Anyone.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;Transparency&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every transaction with the smart contract from the most basic to the most complex can be viewed on the explorer.&lt;/p&gt;

&lt;p&gt;Transactions are operations that alter the state of a smart contract. Read operations are not transactions.&lt;/p&gt;

&lt;p&gt;This helps for proper auditing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applications of Smart Contracts
&lt;/h2&gt;

&lt;p&gt;Smart contracts have been used in many ways but two of the most profound use-cases are &lt;strong&gt;Fungible tokens (ERC20 Tokens)&lt;/strong&gt; and &lt;strong&gt;Non-fungible tokens(ERC721 Tokens)&lt;/strong&gt; popularly known as &lt;strong&gt;NFTs&lt;/strong&gt;. Let's see how they work briefly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;ERC20 Tokens&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Smart contracts serve as a tool for building programmable money (also digital money) and &lt;a href="https://eips.ethereum.org/EIPS/eip-20"&gt;ERC20&lt;/a&gt; is a standard that allows for easy integration of this money across platforms. The standard simply ensures that tokens must have a set of programming elements (functions, methods and events) to be interpreted accordingly.&lt;/p&gt;

&lt;p&gt;ERC20 Tokens are tokens that follow this standard. They are fungible (replaceable by one another) like native digital currencies (like ether and bitcoin) but are built using smart contracts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;ERC721 Tokens&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://eips.ethereum.org/EIPS/eip-721"&gt;ERC721&lt;/a&gt; is a standard just like ERC20 but this specifies a way of building a token that is not fungible. Fundamentally, It builds on the idea of digital rarity and uniqueness.&lt;/p&gt;

&lt;p&gt;In the past months, we have seen NFTs make mad moves with some selling for millions of dollars.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Ethereum is the blockchain of context here, but the ideas discussed can be transferred to other blockchains.&lt;/p&gt;

&lt;p&gt;Smart contracts made possible the most groundbreaking innovations in the blockchain space. One of which is Defi (Decentralized Finance). Most Defi applications like Uniswap, Compound or MakerDAO would be not in existence without smart contracts.&lt;/p&gt;

&lt;p&gt;Just like most innovations, it has had its fair share of challenges which are primarily security-related. For example, when a Decentralized Autonomous Organization (&lt;a href="https://academy.binance.com/en/glossary/decentralized-autonomous-organization"&gt;DAO&lt;/a&gt;) called "The DAO" got hacked in 2016, millions of ether (ETH) were stolen due to flaws in their code. Most of these hacks occurred as a result of human error during development.&lt;/p&gt;

&lt;p&gt;Security is a very important aspect of smart contracts and it can't be overemphasised.&lt;/p&gt;

&lt;p&gt;Building smart contracts is a skill that is valuable and becomes increasingly valuable as the space evolves. Check out this tutorial that teaches how to &lt;a href="https://blockchaintotheworld.com/hello-world-in-solidity/"&gt;build your first smart contract using solidity&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to drop a comment if you have questions and also share if you found this helpful. Bye for now.&lt;/p&gt;

&lt;p&gt;This post was originally published &lt;a href="https://blockchaintotheworld.com/understanding-smart-contracts/"&gt;here&lt;/a&gt; on Blockchaintotheworld.com&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Overflow and Underflow in Solidity: A Deep Dive</title>
      <dc:creator>Godspower Eze</dc:creator>
      <pubDate>Wed, 06 Jul 2022 07:02:54 +0000</pubDate>
      <link>https://forem.com/the_python_dev_/overflow-and-underflow-in-solidity-a-deep-dive-3o4c</link>
      <guid>https://forem.com/the_python_dev_/overflow-and-underflow-in-solidity-a-deep-dive-3o4c</guid>
      <description>&lt;p&gt;If you have ever tried to operate on numbers and gotten a counter-intuitive result then you have most likely experienced an overflow or an underflow in solidity.&lt;/p&gt;

&lt;p&gt;The concept of Overflow and Underflow in Solidity results from a fundamental concept in computer science. In this post, I intend to guide you through the fundamentals to help you understand the why behind it.&lt;/p&gt;

&lt;p&gt;I assume you understand the basics of Solidity and the basics of how binary numbers work.&lt;/p&gt;

&lt;p&gt;Copy and paste the code below on remix to replicate an Overflow and an Underflow.&lt;/p&gt;

&lt;p&gt;Deploy the code and let's walk through the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: UNLICENSED
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;OverflowAndUnderflow&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;uint8&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;underflow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Line 2&lt;/strong&gt;: I used version &lt;strong&gt;0.7.0&lt;/strong&gt; of solidity because overflow and underflow checking is implemented by default in versions &lt;strong&gt;0.8.0&lt;/strong&gt; and above. Using that, the compiler would throw an error when we try to call the functions below. Moreover, It's advised you use 0.8.0 or above when writing production code so you won't have to use libraries like SafeMath or your implementation to do the checking. This is purely experimental.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 6&lt;/strong&gt;: I declared the variable &lt;strong&gt;balance&lt;/strong&gt; which is set to &lt;strong&gt;0&lt;/strong&gt; by default.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 8 to Line 10&lt;/strong&gt;: We have the &lt;strong&gt;underflow&lt;/strong&gt; function. It uses &lt;strong&gt;line 9&lt;/strong&gt; to decrease the value of balance by 1. Call this function once and then check the new value of the balance; it's &lt;strong&gt;255&lt;/strong&gt;. i.e &lt;strong&gt;0&lt;/strong&gt; minus &lt;strong&gt;1&lt;/strong&gt; just gave us &lt;strong&gt;255&lt;/strong&gt;. That's an underflow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 12 to Line 15&lt;/strong&gt;: We have the &lt;strong&gt;overflow&lt;/strong&gt; function. It sets the value of &lt;strong&gt;balance&lt;/strong&gt; to &lt;strong&gt;255&lt;/strong&gt;. And then, increments it by &lt;strong&gt;1&lt;/strong&gt;. Intuitively, the new balance value should be &lt;strong&gt;256&lt;/strong&gt; but it's &lt;strong&gt;0&lt;/strong&gt;. That's an overflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Haven replicated an underflow and overflow situation. Let's go through the fundamentals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fundamentals of Overflow and Underflow
&lt;/h2&gt;

&lt;p&gt;Binary is at the core of how computers store and process data. An 8-bit memory would only be able to store at most 8-bit representations of any form of data at any given time.&lt;/p&gt;

&lt;p&gt;Programming languages also try to emulate this pattern to handle storage more efficiently. For example, if you wanted to store data that you know for sure won't exceed the 8-bit space why then would you create a 256-bit storage space for it?  It's a waste of storage space among other disadvantages and that's why languages like solidity give us the ability to store data in different storage spaces based on data size. For integers, we could store integers from the 8 bits up to 256 bits in steps of  8 using &lt;strong&gt;int8&lt;/strong&gt;/&lt;strong&gt;uint8&lt;/strong&gt; to &lt;strong&gt;int256&lt;/strong&gt;/&lt;strong&gt;uint256&lt;/strong&gt; respectively.&lt;/p&gt;

&lt;p&gt;Imagine declaring an unsigned integer and setting the storage to 8-bit storage as illustrated below in solidity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: UNLICENSED
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;IntegerDeclarion&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;uint8&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, at the time of assignment, an integer exceeding the 8-bit storage space is passed to the variable a. What happens then? That's what overflow and underflow are all about. Overflows and underflows are a result of data exceeding the predefined storage space created for them.&lt;/p&gt;

&lt;p&gt;So, how do you determine the minimum and maximum integer a bit size can store?&lt;/p&gt;

&lt;p&gt;Here's the simple formula:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For unsigned integers&lt;/strong&gt;: &lt;strong&gt;0&lt;/strong&gt; to &lt;strong&gt;(2 ^ n ) - 1&lt;/strong&gt; where &lt;strong&gt;n&lt;/strong&gt; is the number of bits. For example, for 8 bits: &lt;strong&gt;0&lt;/strong&gt; to &lt;strong&gt;(2 ^ 8) - 1&lt;/strong&gt; = &lt;strong&gt;0&lt;/strong&gt; to &lt;strong&gt;(256) - 1 **= **0&lt;/strong&gt; to &lt;strong&gt;255&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For signed integers&lt;/strong&gt;: &lt;strong&gt;(-2 ^ n -1)&lt;/strong&gt; to &lt;strong&gt;(2 ^ n - 1) - 1&lt;/strong&gt; where &lt;strong&gt;n&lt;/strong&gt; is the number of bits. For example, for 8 bits: &lt;strong&gt;(-2 ^ 8 -1)&lt;/strong&gt; to &lt;strong&gt;(2 ^ 8 - 1) - 1&lt;/strong&gt; = &lt;strong&gt;(-2 ^ 7)&lt;/strong&gt; to &lt;strong&gt;(2 ^ 7) - 1&lt;/strong&gt; = &lt;strong&gt;-128&lt;/strong&gt; to &lt;strong&gt;127&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or, using Solidity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: UNLICENSED
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;BitSize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// For Unsigned Integers
&lt;/span&gt;    &lt;span class="kt"&gt;uint8&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;maxForUnsigned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;uint8&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;minForUnsigned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// For Signed Integers
&lt;/span&gt;    &lt;span class="kt"&gt;int8&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;maxForSigned&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int8&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;int8&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;minForSigned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int8&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To understand this better, think of it as how modulo arithmetic works. Values of arithmetic operations are wrapped around the range of numbers they can store. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;25 + 120&lt;/strong&gt; in &lt;strong&gt;signed 8-bit(int8)&lt;/strong&gt; is &lt;strong&gt;-111&lt;/strong&gt;. &lt;strong&gt;25&lt;/strong&gt; plus &lt;strong&gt;120&lt;/strong&gt; is &lt;strong&gt;145&lt;/strong&gt; but the highest number that can be represented is &lt;strong&gt;127&lt;/strong&gt; so instead of the next number being &lt;strong&gt;128&lt;/strong&gt; it starts all over from the least value &lt;strong&gt;-128&lt;/strong&gt; and counts up to &lt;strong&gt;-127&lt;/strong&gt; then &lt;strong&gt;-126&lt;/strong&gt;, &lt;strong&gt;-125&lt;/strong&gt; up to &lt;strong&gt;-111&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;40 + 240&lt;/strong&gt; in &lt;strong&gt;unsigned 8-bit(uint8)&lt;/strong&gt; is &lt;strong&gt;24&lt;/strong&gt;. &lt;strong&gt;40&lt;/strong&gt; plus &lt;strong&gt;240&lt;/strong&gt; is &lt;strong&gt;280&lt;/strong&gt; but the highest number that can be represented is &lt;strong&gt;255&lt;/strong&gt; so instead of the next number being &lt;strong&gt;256&lt;/strong&gt; it starts all over from the least value &lt;strong&gt;0&lt;/strong&gt; then counts up to &lt;strong&gt;1&lt;/strong&gt; then &lt;strong&gt;2&lt;/strong&gt;, &lt;strong&gt;3&lt;/strong&gt; up to &lt;strong&gt;24&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0 - 1&lt;/strong&gt; in &lt;strong&gt;unsigned 8-bit(uint8)&lt;/strong&gt; is &lt;strong&gt;255&lt;/strong&gt;. &lt;strong&gt;0&lt;/strong&gt; minus &lt;strong&gt;1&lt;/strong&gt; is &lt;strong&gt;-1&lt;/strong&gt; but the least number that can be represented is &lt;strong&gt;0&lt;/strong&gt; so instead of the next number being &lt;strong&gt;-1&lt;/strong&gt; it goes over to the top at &lt;strong&gt;255&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;-128 - 5&lt;/strong&gt; in &lt;strong&gt;signed 8-bit(int8)&lt;/strong&gt; is &lt;strong&gt;123&lt;/strong&gt;. &lt;strong&gt;-128&lt;/strong&gt; minus &lt;strong&gt;5&lt;/strong&gt; is &lt;strong&gt;-133&lt;/strong&gt; but the least number that can be represented is &lt;strong&gt;-128&lt;/strong&gt; so instead of the following number being &lt;strong&gt;-129&lt;/strong&gt; it starts all over from the top at &lt;strong&gt;127&lt;/strong&gt; and counts down to &lt;strong&gt;126&lt;/strong&gt; then &lt;strong&gt;125&lt;/strong&gt;, &lt;strong&gt;124&lt;/strong&gt; down to &lt;strong&gt;123&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point, it is safe to say that the values in the range are like numbers on a clock and every number revolves around them.&lt;/p&gt;

&lt;p&gt;Let's go a bit more in-depth by using binary numbers to explain the examples above.&lt;/p&gt;

&lt;p&gt;Before we move on, here are a few things to take note of when solving in binary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;signed binary arithmetic&lt;/strong&gt;, if the addition of &lt;strong&gt;two positive numbers&lt;/strong&gt; gives a &lt;strong&gt;-ve number&lt;/strong&gt; then an overflow/underflow has occurred.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;signed binary arithmetic&lt;/strong&gt;, if the addition of &lt;strong&gt;two negative numbers&lt;/strong&gt; gives a &lt;strong&gt;+ve number&lt;/strong&gt; then an overflow/underflow has occurred.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;unsigned binary arithmetic&lt;/strong&gt;, if there is a &lt;strong&gt;carry from the MSB&lt;/strong&gt;(i.e for the &lt;strong&gt;most significant bit&lt;/strong&gt;. it's the numbers on the left-most part of the binary digit. For example, in &lt;strong&gt;00011001&lt;/strong&gt;, &lt;strong&gt;0&lt;/strong&gt; is the most significant bit and in &lt;strong&gt;10000000&lt;/strong&gt;, &lt;strong&gt;1&lt;/strong&gt; is the most significant bit) then an overflow has occurred.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;signed binary arithmetic&lt;/strong&gt;, the MSB signifies if a number is a &lt;strong&gt;-ve number&lt;/strong&gt; or &lt;strong&gt;+ve number&lt;/strong&gt;. If the &lt;strong&gt;MSB&lt;/strong&gt; is &lt;strong&gt;0&lt;/strong&gt;, it's a &lt;strong&gt;+ve number&lt;/strong&gt; while if the &lt;strong&gt;MSB&lt;/strong&gt; is &lt;strong&gt;1&lt;/strong&gt;, it's a &lt;strong&gt;-ve number&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A simple trick to know if an overflow/underflow has occurred in &lt;strong&gt;signed arithmetic&lt;/strong&gt; addition is if the &lt;strong&gt;carry into the MSB&lt;/strong&gt; and &lt;strong&gt;the carry out of the MSB&lt;/strong&gt; are different. If they are the same, an overflow/underflow has not occurred. For example, &lt;strong&gt;1&lt;/strong&gt; &amp;amp; &lt;strong&gt;1&lt;/strong&gt; and &lt;strong&gt;0&lt;/strong&gt; &amp;amp; &lt;strong&gt;0&lt;/strong&gt; means no overflow/underflow while &lt;strong&gt;0&lt;/strong&gt; &amp;amp; &lt;strong&gt;1&lt;/strong&gt; and &lt;strong&gt;1&lt;/strong&gt; &amp;amp; &lt;strong&gt;0&lt;/strong&gt; means there is an overflow/underflow. While for &lt;strong&gt;unsigned arithmetic&lt;/strong&gt;, if there is a &lt;strong&gt;carry out of the MSB&lt;/strong&gt; then there is an overflow/underflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The above is quite dense but I assure you you can understand the next part without understanding it. Feel free to go through it multiple times to understand it better but for now, you can just move to the next part.&lt;/p&gt;

&lt;p&gt;The calculations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;25 + 120&lt;/strong&gt; in &lt;strong&gt;signed 8-bit&lt;/strong&gt;: &lt;strong&gt;25&lt;/strong&gt; is &lt;strong&gt;00011001&lt;/strong&gt; and &lt;strong&gt;120&lt;/strong&gt; is &lt;strong&gt;01111000&lt;/strong&gt;. &lt;strong&gt;00011001&lt;/strong&gt; + &lt;strong&gt;01111000&lt;/strong&gt; is &lt;strong&gt;10010001&lt;/strong&gt;. &lt;strong&gt;10010001&lt;/strong&gt; is &lt;strong&gt;-111&lt;/strong&gt; in &lt;strong&gt;signed&lt;/strong&gt; binary arithmetic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;40 + 240&lt;/strong&gt; in &lt;strong&gt;unsigned 8-bit&lt;/strong&gt;: &lt;strong&gt;40&lt;/strong&gt; is &lt;strong&gt;00101000&lt;/strong&gt; and &lt;strong&gt;240&lt;/strong&gt; is &lt;strong&gt;11110000&lt;/strong&gt;. &lt;strong&gt;00101000&lt;/strong&gt; + &lt;strong&gt;11110000&lt;/strong&gt; is &lt;strong&gt;00011000&lt;/strong&gt;. &lt;strong&gt;00011000&lt;/strong&gt; is &lt;strong&gt;24&lt;/strong&gt; in &lt;strong&gt;unsigned&lt;/strong&gt; binary arithmetic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;0 - 1&lt;/strong&gt; in &lt;strong&gt;unsigned 8-bit&lt;/strong&gt;: &lt;strong&gt;0&lt;/strong&gt; is &lt;strong&gt;00000000&lt;/strong&gt; and &lt;strong&gt;1&lt;/strong&gt; is &lt;strong&gt;00000001&lt;/strong&gt;. &lt;strong&gt;00000000&lt;/strong&gt; + &lt;strong&gt;11111111&lt;/strong&gt;(i.e this is the &lt;strong&gt;2's complement&lt;/strong&gt; of &lt;strong&gt;00000001&lt;/strong&gt; which is &lt;strong&gt;1&lt;/strong&gt;. We can't perform &lt;strong&gt;0 - 1&lt;/strong&gt; rather we do &lt;strong&gt;0 + (-1)&lt;/strong&gt; and &lt;strong&gt;-1&lt;/strong&gt; is &lt;strong&gt;11111111&lt;/strong&gt;) is &lt;strong&gt;11111111&lt;/strong&gt;. &lt;strong&gt;11111111&lt;/strong&gt; is &lt;strong&gt;255&lt;/strong&gt; in &lt;strong&gt;unsigned&lt;/strong&gt; binary arithmetic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-128 - 5&lt;/strong&gt; in &lt;strong&gt;signed 8-bit&lt;/strong&gt;: &lt;strong&gt;-128&lt;/strong&gt; is &lt;strong&gt;10000000&lt;/strong&gt; and &lt;strong&gt;-5&lt;/strong&gt; is &lt;strong&gt;11111011&lt;/strong&gt;. Using &lt;strong&gt;-128 + (-5)&lt;/strong&gt; then &lt;strong&gt;10000000&lt;/strong&gt; + &lt;strong&gt;11111011&lt;/strong&gt; is &lt;strong&gt;01111011&lt;/strong&gt;. &lt;strong&gt;01111011&lt;/strong&gt; is 123 in &lt;strong&gt;signed&lt;/strong&gt; binary arithmetic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use a &lt;a href="https://www.rapidtables.com/calc/math/binary-calculator.html"&gt;binary calculator&lt;/a&gt; to try these out yourself. And, that's a wrap &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As you might have noticed, this post is not about how to prevent an overflow or underflow rather it's about why you run into them. That being said, overflows and underflows are security threats and have been the cause of hacks in the past so make sure you prevent them by using solidity version 0.8.0 and above or using the SafeMath library when writing your solidity code.&lt;/p&gt;

&lt;p&gt;This is quite complex to grasp right away so feel free to ask questions in the comment below. Please share if you found&lt;br&gt;
this helpful. Happy learning.&lt;/p&gt;

&lt;p&gt;This was originally posted &lt;a href="https://blockchaintotheworld.com/overflow-and-underflow-in-solidity/"&gt;here&lt;/a&gt; on Blockchaintotheworld.com&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>solidity</category>
    </item>
  </channel>
</rss>
