<?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>DEV Community: Ebuka Ukatu </title>
    <description>The latest articles on DEV Community by Ebuka Ukatu  (@tallnerd).</description>
    <link>https://dev.to/tallnerd</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%2F1115926%2Fcc30ad37-d002-4b12-a18c-72bbdb7ce477.jpg</url>
      <title>DEV Community: Ebuka Ukatu </title>
      <link>https://dev.to/tallnerd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tallnerd"/>
    <language>en</language>
    <item>
      <title>Security Best Practices for Smart Contracts in Django Projects</title>
      <dc:creator>Ebuka Ukatu </dc:creator>
      <pubDate>Wed, 09 Aug 2023 14:01:35 +0000</pubDate>
      <link>https://dev.to/tallnerd/security-best-practices-for-smart-contracts-in-django-projects-4861</link>
      <guid>https://dev.to/tallnerd/security-best-practices-for-smart-contracts-in-django-projects-4861</guid>
      <description>&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;You should have an intermediate understanding of the following to understand the topic and follow along with the case studies and analysis in this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need an understanding of &lt;a href="https://docs.soliditylang.org/en/v0.8.21/" rel="noopener noreferrer"&gt;solidity&lt;/a&gt; programming language, and if you don’t, preview this &lt;a href="https://www.tutorialspoint.com/solidity/index.htm" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Intermediate understanding of &lt;a href="https://docs.python.org/3/tutorial/index.html" rel="noopener noreferrer"&gt;Python&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;You need an understanding of &lt;a href="https://www.djangoproject.com/start/" rel="noopener noreferrer"&gt;Django&lt;/a&gt; and how it works with &lt;a href="https://dev.to/tallnerd/building-smart-contracts-for-django-applications-369c"&gt;smart contracts&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;An understanding of &lt;a href="https://github.com/git-guides" rel="noopener noreferrer"&gt;Git&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;An understanding of &lt;a href="https://hardhat.org/" rel="noopener noreferrer"&gt;hardhat&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Smart contracts are programmable codes that execute a predicted outcome when the conditions of an agreement are met. Unlike traditional contracts that need the enforcement of third parties, with smart contracts, there's no need. &lt;/p&gt;

&lt;p&gt;Django is a web development framework that works with Python, and there are several uses of smart contracts in Django:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can use smart contract to save the data of your Django application on a blockchain.&lt;/li&gt;
&lt;li&gt;You can use smart contracts for crypto payments on your Django application.&lt;/li&gt;
&lt;li&gt;Smarts are used in Django to explore and interact with already existing external contracts. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Security is essential for smart contracts. Like any software application deployed for public use, developers consider security. Here are a few crucial benefits of security in smart contracts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security in smart contracts prevents exploits by malicious actors. A DAO hack is one instance. There are others. &lt;/li&gt;
&lt;li&gt;Better security means increased trust in smart contracts because there are few incidents where users could lose any asset. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are best practices developers can use to bolster the security of their smart contracts in Django applications. Using these practices helps developers achieve optimal contract operation. &lt;/p&gt;

&lt;h2&gt;
  
  
  Smart Contract Security Recommended Practices
&lt;/h2&gt;

&lt;p&gt;Smart contracts are immutable once deployed, and developers must carry out practices that improve smart contracts. Understanding these practices is a prerequisite for smart contract development. Let's delve into them. &lt;/p&gt;

&lt;h3&gt;
  
  
  A. Use a Linter to Detect Bugs and Vulnerabilities
&lt;/h3&gt;

&lt;p&gt;Linters are programming tools that analyze program source codes and give feedback on bugs and syntax errors. Developers then use this information to better their contracts by fixing the issues before they deploy. &lt;/p&gt;

&lt;p&gt;Developers use this information to optimize their smart contract code by fixing the issues before deploying. &lt;/p&gt;

&lt;p&gt;There are a few linters for smart contracts written for solidity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Slither&lt;/strong&gt;: Built using Python. Slither is a security framework that allows you to analyze your contract code, check for errors and vulnerabilities, and then output detailed visual information about these errors if there are any and has an Application Programming Interface (API) that lets anyone write custom analyses. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mythril&lt;/strong&gt;: Built for EVM chains, this is another security analysis tool for smart contracts. Developers use it to detect common vulnerabilities in contracts like integer overflows. However, it doesn’t apply to the business logic of a function in a contract.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Case Study Using Slither
&lt;/h4&gt;

&lt;p&gt;Here, using a contract example, you will analyze the contract and figure out where you can use the information returned by Slither to optimize your code. Using Remix Integrated Development Environment (IDE), you need to have the following installed: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Solidity.&lt;/li&gt;
&lt;li&gt;Solidity compiler. &lt;/li&gt;
&lt;li&gt;Solidity compiler version.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can install each of these independently. However, since you are using Remix for this case study, you don’t. Get into your terminal and write the command and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g @remix-project/remixd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to have it situated in a directory of your choice, then run the command without &lt;code&gt;-g&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @remix-project/remixd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you have successfully run the command, you should have this:&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%2Ftq4xuqlng4veehf3enkx.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%2Ftq4xuqlng4veehf3enkx.png" alt="Security Best Practices for Smart Contracts in Django Projects" width="800" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you have it installed, you need to run &lt;code&gt;remixid&lt;/code&gt; in your terminal using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;remixd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will have this in your terminal:&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%2Fax3osjknsjepwn0udr1y.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%2Fax3osjknsjepwn0udr1y.png" alt="Security Best Practices for Smart Contracts in Django Projects" width="800" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you have done so create a folder for your smart contract, get into your terminal and run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir [new-folder-name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your terminal, in place of the &lt;code&gt;new-folder-name&lt;/code&gt;, put the name you wish to call your folder. &lt;/p&gt;

&lt;p&gt;Afterward, head over to Remix on the homepage, and follow the steps to access the folder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on the area shown and select Connect to Localhost:&lt;/li&gt;
&lt;/ul&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%2F0hukepv51jcbgmujv56t.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%2F0hukepv51jcbgmujv56t.png" alt="Security Best Practices for Smart Contracts in Django Projects" width="367" height="623"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After the folder loads create a new file for your smart contract. &lt;/li&gt;
&lt;li&gt;After you have input this solidity contract code in, save, and compile it:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.4.4;


// ERC20 token interface is implemented only partially.
// hence some functions are left undefined:
//  - transfer, transferFrom,
//  - approve, allowance.

contract PresaleToken {

    /// @dev Constructor
    /// @param _tokenManager Token manager address.
    function PresaleToken(address _tokenManager, address _escrow) {
        tokenManager = _tokenManager;
        escrow = _escrow;
    }


    /*/
     *  Constants
    /*/

    string public constant name = "TEST Presale Token";
    string public constant symbol = "TST";
    uint   public constant decimals = 18;

    uint public constant PRICE = 606; // 606 SPT per Ether

    //  price
    // Cup is 10 000 ETH
    // 1 eth = 606 presale tokens
    // ETH price ~50$ for 28.03.2017
    // Cup in $ is ~ 500 000$

    uint public constant TOKEN_SUPPLY_LIMIT = 606 * 10000 * (1 ether / 1 wei);



    /*/
     *  Token state
    /*/

    enum Phase {
        Created,
        Running,
        Paused,
        Migrating,
        Migrated
    }

    Phase public currentPhase = Phase.Created;
    uint public totalSupply = 0; // amount of tokens already sold

    // Token manager has exclusive priveleges to call administrative
    // functions on this contract.
    address public tokenManager;

    // Gathered funds can be withdrawn only to escrow's address.
    address public escrow;

    // Crowdsale manager has exclusive priveleges to burn presale tokens.
    address public crowdsaleManager;

    mapping (address =&amp;gt; uint256) private balance;


    modifier onlyTokenManager()     { if(msg.sender != tokenManager) throw; _; }
    modifier onlyCrowdsaleManager() { if(msg.sender != crowdsaleManager) throw; _; }


    /*/
     *  Events
    /*/

    event LogBuy(address indexed owner, uint value);
    event LogBurn(address indexed owner, uint value);
    event LogPhaseSwitch(Phase newPhase);


    /*/
     *  Public functions
    /*/

    function() payable {
        buyTokens(msg.sender);
    }

    /// @dev Lets buy you some tokens.
    function buyTokens(address _buyer) public payable {
        // Available only if presale is running.
        if(currentPhase != Phase.Running) throw;

        if(msg.value == 0) throw;
        uint newTokens = msg.value * PRICE;
        if (totalSupply + newTokens &amp;gt; TOKEN_SUPPLY_LIMIT) throw;
        balance[_buyer] += newTokens;
        totalSupply += newTokens;
        LogBuy(_buyer, newTokens);
    }


    /// @dev Returns number of tokens owned by given address.
    /// @param _owner Address of token owner.
    function burnTokens(address _owner) public
        onlyCrowdsaleManager
    {
        // Available only during migration phase
        if(currentPhase != Phase.Migrating) throw;

        uint tokens = balance[_owner];
        if(tokens == 0) throw;
        balance[_owner] = 0;
        totalSupply -= tokens;
        LogBurn(_owner, tokens);

        // Automatically switch phase when migration is done.
        if(totalSupply == 0) {
            currentPhase = Phase.Migrated;
            LogPhaseSwitch(Phase.Migrated);
        }
    }


    /// @dev Returns number of tokens owned by given address.
    /// @param _owner Address of token owner.
    function balanceOf(address _owner) constant returns (uint256) {
        return balance[_owner];
    }


    /*/
     *  Administrative functions
    /*/

    function setPresalePhase(Phase _nextPhase) public
        onlyTokenManager
    {
        bool canSwitchPhase
            =  (currentPhase == Phase.Created &amp;amp;&amp;amp; _nextPhase == Phase.Running)
            || (currentPhase == Phase.Running &amp;amp;&amp;amp; _nextPhase == Phase.Paused)
                // switch to migration phase only if crowdsale manager is set
            || ((currentPhase == Phase.Running || currentPhase == Phase.Paused)
                &amp;amp;&amp;amp; _nextPhase == Phase.Migrating
                &amp;amp;&amp;amp; crowdsaleManager != 0x0)
            || (currentPhase == Phase.Paused &amp;amp;&amp;amp; _nextPhase == Phase.Running)
                // switch to migrated only if everyting is migrated
            || (currentPhase == Phase.Migrating &amp;amp;&amp;amp; _nextPhase == Phase.Migrated
                &amp;amp;&amp;amp; totalSupply == 0);

        if(!canSwitchPhase) throw;
        currentPhase = _nextPhase;
        LogPhaseSwitch(_nextPhase);
    }


    function withdrawEther() public
        onlyTokenManager
    {
        // Available at any phase.
        if(this.balance &amp;gt; 0) {
            if(!escrow.send(this.balance)) throw;
        }
    }


    function setCrowdsaleManager(address _mgr) public
        onlyTokenManager
    {
        // You can't change crowdsale contract when migration is in progress.
        if(currentPhase == Phase.Migrating) throw;
        crowdsaleManager = _mgr;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now that you complied it, head over to this part of your Remix interface, click on analyze, then the Slither section.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the errors, you can then work on fixing the smart contract presale code using the error as pointers. &lt;/p&gt;

&lt;h3&gt;
  
  
  B. Perform Thorough Testing on your Smart Contract
&lt;/h3&gt;

&lt;p&gt;Testing your smart contract helps you identify possible exploitable errors you wouldn't have ordinarily noticed even after going over the code yourself or using any linter. &lt;/p&gt;

&lt;p&gt;Given that smart contracts are immutable when deployed to the mainnet. Since they usually deal with massive valued assets, any exploitation would lead to user losses. &lt;/p&gt;

&lt;p&gt;Since Linters don't work on the logic of codes, you should use an advanced technique to determine if your code can be deployed and integrated with your Django application. To do this, you could perform the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unit testing&lt;/strong&gt;: Performing thorough unit tests on your smart contract code is necessary because each function gets tested individually to see if the function's logic works. This development process divides all testable parts into units. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Doing this helps you as a developer to know if parts of your &lt;br&gt;
code work as you intend it to and meets the minimum requirement &lt;br&gt;
for security, usability, and reliability. If it doesn't meet  these requirements, you can fix the bugs and save those &lt;br&gt;
interacting with it from future exploitation.&lt;/p&gt;

&lt;p&gt;Unit tests are an automated type of testing because you use &lt;br&gt;
scripts to carry it out. And to properly carry out a unit test, &lt;br&gt;
you have to consider the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You should comprehensively understand your smart contract's business logic and workflow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Know the assumptions associated with your smart contract. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check and measure the code coverage of your smart contract.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use a well-developed testing suite like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Foundry&lt;/li&gt;
&lt;li&gt;Truffle&lt;/li&gt;
&lt;li&gt;Brownie&lt;/li&gt;
&lt;li&gt;Remix&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using these suites allows you to perform unit testing faster. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fuzz testing&lt;/strong&gt;: Your unit tests could be good enough to let you figure out vulnerabilities in your code. However, it's good to run more than one type of test. Fuzz testing enables you to input millions of unexpected data against your smart contract to cause surprising behavioral logic. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is fast, efficient, and easy to test scenarios or assumptions you might have yet to think about with your smart contract code. &lt;/p&gt;

&lt;p&gt;Most programmers use Diligence fuzzing for their smart contracts. It's also good to note that fuzzing used the scribble standard to identify complex vulnerabilities better. &lt;/p&gt;
&lt;h4&gt;
  
  
  Case Study using Consensys Diligence Fuzzing
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://consensys.io/" rel="noopener noreferrer"&gt;Consensys&lt;/a&gt;, for a while, had launched the beta version of their &lt;a href="https://consensys.io/diligence/fuzzing/" rel="noopener noreferrer"&gt;diligence fuzzing&lt;/a&gt;. However, earlier this year, they launched the tool for use within the developer community. &lt;/p&gt;

&lt;p&gt;This case study will use the Consensys diligence fuzzing tool to test a staking smart contract. To run fuzz testing, follow the tutorial:&lt;/p&gt;
&lt;h5&gt;
  
  
  Step 1: Create an account
&lt;/h5&gt;

&lt;p&gt;You must create a &lt;a href="https://fuzzing.diligence.tools" rel="noopener noreferrer"&gt;diligence account&lt;/a&gt; to carry out the fuzzing test. When you signup, you get 10 hours of free fuzzing time. Use the Google sign-in option to sign up or input your email and create a password:&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%2Fx73qgo6o35h7c6zvsej9.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%2Fx73qgo6o35h7c6zvsej9.png" alt="Security Best Practices for Smart Contracts in Django Projects" width="800" height="385"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;h4&gt;
  
  
  Step 2: Get an Application Programming Interface (API) key
&lt;/h4&gt;

&lt;p&gt;Head over to this part of the website:&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%2Fkqz9effv2w24kvb0cyci.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%2Fkqz9effv2w24kvb0cyci.png" alt="Security Best Practices for Smart Contracts in Django Projects" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on Create a new APT Key:&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%2Fzsth5czkqw1qf5vnh48o.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%2Fzsth5czkqw1qf5vnh48o.png" alt="Security Best Practices for Smart Contracts in Django Projects" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give it a name, create, and copy the API:&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%2Fnkk6c3pcgrdjzehfahtn.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%2Fnkk6c3pcgrdjzehfahtn.png" alt="Security Best Practices for Smart Contracts in Django Projects" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 3: Create a Working Hardhat working Folder
&lt;/h4&gt;

&lt;p&gt;Here you need to create a working Hardhat project folder. After you have created it, head over to this GitHub repository: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Consensys/scribble-exercise-1/blob/master/contracts/vulnerableERC20.sol" rel="noopener noreferrer"&gt;https://github.com/Consensys/scribble-exercise-1/blob/master/contracts/vulnerableERC20.sol&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the code into your Hardhat project, and create a new folder for your &lt;code&gt;contracts&lt;/code&gt;. Afterward, create a new file for the solidity code, and paste the code.&lt;/p&gt;

&lt;p&gt;After you have, compile it by running this command in the project &lt;code&gt;contracts&lt;/code&gt; directory on your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx hardhat compile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 4: Install dependencies
&lt;/h4&gt;

&lt;p&gt;Navigate to the root folder you had created for the Hardhat project in the previous step on your terminal and install project-related dependencies by running these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install diligence-fuzzing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the command, next, run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -g eth-scribble ganache truffle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And after you do, get on with the next step.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 5: Add your Created API Key
&lt;/h4&gt;

&lt;p&gt;Now that you have installed the dependencies, get into the Hardhat root folder you created and create an &lt;code&gt;env&lt;/code&gt; where you can add your API key by running this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo FUZZ_API_KEY='your API key here' &amp;gt; .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In place of &lt;code&gt;your API key here&lt;/code&gt; input the key you created initially and run the command. &lt;/p&gt;

&lt;h4&gt;
  
  
  Step 6: Annotate the Contract
&lt;/h4&gt;

&lt;p&gt;Get into the root directory, and find the contract, &lt;br&gt;
&lt;code&gt;contracts/'the name you saved it with'.sol&lt;/code&gt;. After you have, add this to the top of the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// #invariant "balances are in sync" unchecked_sum(_balances) == _totalSupply;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And above the transfer function, add this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// #if_succeeds msg.sender != _to ==&amp;gt; _balances[_to] == old(_balances[_to]) + _value; /// #if_succeeds msg.sender != _to ==&amp;gt; _balances[msg.sender] == old(_balances[msg.sender]) - _value; /// #if_succeeds msg.sender == _to ==&amp;gt; _balances[msg.sender] == old(_balances[_to]); /// #if_succeeds old(_balances[msg.sender]) &amp;gt;= _value;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you have, run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fuzz arm

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then &lt;code&gt;deploy&lt;/code&gt; the contract to your chosen network.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 7: Run Fuzz Test
&lt;/h4&gt;

&lt;p&gt;To run the fuzz test from the root folder in your terminal, run the command:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;make fuzz&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Head over to your dashboard at Diligence and wait a few minutes. The failure or errors in the smart contract will show.&lt;/p&gt;

&lt;p&gt;You can further click through the properties to see what’s wrong with the contract and how you could fix its logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  C. Write Simple Smart Contracts
&lt;/h3&gt;

&lt;p&gt;Smart contracts can be complex depending on what objective you aim for with them. However, simple rules can help you write simple smart contracts. &lt;/p&gt;

&lt;p&gt;Simple, smart contracts make your code easier to audit and run tests on, saving you time and making your code more effective. The following are simple rules you can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Depending on the complexity, break your whole code into more minor contracts focused on a functionality.&lt;/li&gt;
&lt;li&gt;Apply &lt;a href="https://en.wikipedia.org/wiki/Modular_programming" rel="noopener noreferrer"&gt;modular programming&lt;/a&gt; principles when writing your contracts to make them reusable across projects. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  D. Apply Safe Maths Operations in Contracts
&lt;/h3&gt;

&lt;p&gt;During arithmetic operations, overflow tends to happen, and most times this isn’t the programmer's fault because it is assumed that a data type will hold the data assigned to it. Also, underflows happen, and this is due to the result of an arithmetic calculation being in an absolute value that gets represented. &lt;/p&gt;

&lt;p&gt;In solidity, however, unlike most high-level programming languages where an error is shown for an overflow or underflow, this isn’t the case. And since solidity doesn’t show this error, safe maths is used to revert the mathematical operation or transaction when an overflow or underflow happens. &lt;/p&gt;

&lt;p&gt;Importing the safe math library when writing a contract helps to validate if a logic will cause an overflow or underflow. However, this library is only available and works for versions of solidity from 0.8 and lower. So, if you use a lower version of solidity when writing your smart contract, importing this operation is a good security measure to prevent the loss of assets or funds. &lt;/p&gt;

&lt;p&gt;To import safe maths operations in your solidity code, use this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.0.0/contracts/math/SafeMath.sol";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  E. Use Reentrancy Security Strategies in your Contract
&lt;/h3&gt;

&lt;p&gt;Reentrancy attacks have seen contracts drained of their fund. In solidity, the attack happens when a malicious actor finds an exploitable error in a function. The function getting exploited gives up the control flow of the transaction because the smart contract makes an external call to the contract written by the malicious actor. The unfavorable contract then makes a recursive call to the contract holding the funds, where it then drains it. &lt;/p&gt;

&lt;p&gt;Reentrancy attacks are of three types, namely: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single Reentrancy Attack&lt;/li&gt;
&lt;li&gt;Cross-function Reentrancy Attack&lt;/li&gt;
&lt;li&gt;Cross-contract Reentrancy Attack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These attacks can happen in different ways. However, a common pseudo-code example is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;


contract PaymentSystem {
    mapping (uint256 =&amp;gt; bool) public payments;


    function payUserA(uint256 userId) public {
        if (!payments[userId]) {
            // pay user
            payments[userId] = true;
        }
    }


    function payUserB(uint256 userId) public {
        payments[userId] = true;
        if (!payments[userId]) {
            // pay user
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you check the functions in the code, the first one is exploitable because if a malicious actor is smart enough, they can send multiple transaction payout requests and get paid more money before the mapping gets set to true. However, the second function checks for this, and the first interaction or call gets set to true and subsequent requests won’t pay out. &lt;/p&gt;

&lt;p&gt;Using common best security smart contract strategies to prevent reentrancy attacks is the best route to take, namely: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pullpayments&lt;/li&gt;
&lt;li&gt;ReentrancyGuards&lt;/li&gt;
&lt;li&gt;Pausable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using this in your smart contract will help against a reentrancy attack. Pullpayments prevent these attacks by preventing a paying smart contract from interacting directly with a reciever address. Here the malicious actor won’t be able to block execution. To use it in your smart contract, do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "@openzeppelin/contracts/security/PullPayment.sol";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, ReentrancyGuards prevent a reentrancy attack on a function, like demonstrated above, to use this in your code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pausable is an emergency stop mechanism you can use in your smart contracts by an authorized account. To use it in your code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "@openzeppelin/contracts/security/Pausable.sol";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Perform Audits and Maintenance
&lt;/h2&gt;

&lt;p&gt;Applying security best practices to your smart contract codes is a good thing. However, you will want always to be ahead of any situation should it arise. And for that reason, you should perform regular security audits on your smart contracts because this will help you discover bugs that must have escaped scrutiny.&lt;/p&gt;

&lt;p&gt;During security audits, when vulnerabilities are found, you want to carry out a patch or an update quickly before a malicious actor discovers it. Security is always necessary, and you must regularly and constantly carry out audits and maintenance to safeguard against attacks. &lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Security practices are used to give the best positive outcome possible, increase trust, and prevent exploits of smart contracts. When building your Django applications and integrating smart contracts into them, you wouldn’t want any user to fall prey to security vulnerabilities because smart contracts mainly deal with funds.&lt;/p&gt;

&lt;p&gt;We looked at the common types of attacks on smart contracts and how they happen, from arithmetic overflow and underflow to reentrancy attacks. This article's discussions and case studies are enough to set you on a path for applying best security practices in your contracts.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>django</category>
      <category>web3</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Building Smart Contracts for Django Applications</title>
      <dc:creator>Ebuka Ukatu </dc:creator>
      <pubDate>Fri, 21 Jul 2023 18:20:22 +0000</pubDate>
      <link>https://dev.to/tallnerd/building-smart-contracts-for-django-applications-369c</link>
      <guid>https://dev.to/tallnerd/building-smart-contracts-for-django-applications-369c</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Contracts are binding and have been around for a long time. Until the internet revolution in the 1990s, contracts were only enforceable by the action of a third party. For example, if you had an agreement with someone who sells fashion clothes, and part of the agreement was that you paid an advance to receive it at a decided date where you would also pay up the remaining cash. &lt;/p&gt;

&lt;p&gt;And for some reason, the other party doesn’t hold their end of the bargain; the third party makes this enforceable, usually using force. &lt;/p&gt;

&lt;p&gt;In contracts, you hope and trust that the other party comes through, and there won’t have to be a need for the enforceable actions of a third party, which brings up unfavorable situations. &lt;/p&gt;

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

&lt;p&gt;Smart contracts are programmed agreements written in code, which executes the programmed command when all agreement conditions are met. &lt;/p&gt;

&lt;p&gt;To break down this understanding, let’s use the analogy of the workings of a vending machine:&lt;/p&gt;

&lt;p&gt;A young man Bobby visits his friend in a hospital and is exhausted and needs to have a carbonated drink, Coke. Bobby decides to use a vending machine he finds in the vicinity, he walks up to the device and chooses Coke, and the engine returns the amount he needs to release the drink. &lt;/p&gt;

&lt;p&gt;He inserts the required amount, and the vending machine verifies the correct amount. Afterward, it dispenses the carbonated drink to Bobby. &lt;/p&gt;

&lt;p&gt;From the analogy, Bobby couldn’t have committed any fraud to get the carbonated drink because the vending machine is programmed only to dispense when the other party has inputted the correct amount required for a product. &lt;/p&gt;

&lt;p&gt;Another example would be the use of an escrow service. Alice is a woman who lives in California, USA, and goes on a trip to Europe, and unfortunately runs out of dollars. She needs it and decides to use an escrow to exchange her dollars for euro with a local she met, whose name is Isaac. &lt;/p&gt;

&lt;p&gt;Alice sends the amount she wants to exchange to the digital escrow, and Isaac sends an equivalent of the euros to the same digital escrow. After the program verifies that both have sent the required amount for the transaction to happen, it releases the euros to Alice and the dollars to Isaac.&lt;/p&gt;

&lt;p&gt;Here there is no requirement for using a third party to enforce the agreement between both parties, and the smart contract program handles that, which makes the transaction more credible and boosts trust. &lt;/p&gt;

&lt;h2&gt;
  
  
  I. - Getting Started with Blockchain Smart Contract Development
&lt;/h2&gt;

&lt;p&gt;Blockchain smart contract development is the programming and deploying of agreements in code to the blockchain where it is transparent for all to check and which executes agreements on the blockchain.&lt;/p&gt;

&lt;p&gt;Here is a guide on getting started with Blockchain smart contract development:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install Solidity
&lt;/h3&gt;

&lt;p&gt;To install solidity, we will use an easy approach. First, you have to install &lt;a href="https://en.wikipedia.org/wiki/Visual_Studio_Code" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt; on your system. To download Visual Studio Code: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Head over to the &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;website&lt;/a&gt; to download the software; depending on the operating system you use, as shown in the diagram below, choose your operating system, download, and run. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After downloading and running Visual Studio Code, head over to this part of the software as highlighted below:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Fpvso2l4bp23md6xp426g.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%2Fpvso2l4bp23md6xp426g.png" alt="Building Smart Contracts for Django Applications" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When there, input “solidity”  in the search bar as seen below: &lt;/li&gt;
&lt;/ul&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%2Fpqavpm5k95ljbfkwnl8v.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%2Fpqavpm5k95ljbfkwnl8v.png" alt="Building Smart Contracts for Django Applications" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Afterward, click on the first result shown below, and click install:&lt;/li&gt;
&lt;/ul&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%2Fp5bifph8zfjl5tjk2cmm.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%2Fp5bifph8zfjl5tjk2cmm.png" alt="Building Smart Contracts for Django Applications" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To confirm you have installed solidity correctly, you should check this part of the picture highlighted below:&lt;/li&gt;
&lt;/ul&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%2Fclcevg79frtlf12ez9c2.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%2Fclcevg79frtlf12ez9c2.png" alt="Building Smart Contracts for Django Applications" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You are good to go now that solidity is installed in your Visual Studio Code.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.  Choose an Integrated Development Environment (IDE) like Remix
&lt;/h3&gt;

&lt;p&gt;For easy coding, deploying, and debugging of smart contracts for Ethereum and EVM-compatible chains, Remix online Integrated Development Environment (IDE) is the safest and fastest option you could go with. &lt;/p&gt;

&lt;p&gt;To use Remix IDE, head over to the &lt;a href="https://remix.ethereum.org/" rel="noopener noreferrer"&gt;website&lt;/a&gt; and launch it. When you do this, this is what you should see on your screen:&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%2F8q9d9yqtbmse87vrtwk9.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%2F8q9d9yqtbmse87vrtwk9.PNG" alt="Building Smart Contracts for Django Applications" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you have this, let’s create our first contract.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Write and Compile your First Smart Contract
&lt;/h3&gt;

&lt;p&gt;To write your first contract, we need to create a file for the solidity in its format or extension, which is &lt;code&gt;.sol&lt;/code&gt;. To do this, head to the contracts folder.&lt;/p&gt;

&lt;p&gt;When you do, right-click on the folder and create a new file using the name you want your file to be, ending it with &lt;code&gt;.sol&lt;/code&gt;. I called mine &lt;code&gt;Django_App&lt;/code&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%2F7ucbkruf12wwqfmsvbva.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%2F7ucbkruf12wwqfmsvbva.png" alt="Building Smart Contracts for Django Applications" width="800" height="136"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You must understand the basic syntax and style guide to write your first solidity code. For consistency, the style guide requires four spaces for indentation; for declarations, you should surround them with two blank lines. When declaring functions, they should have an empty line space.&lt;/p&gt;

&lt;p&gt;Let us write a simple, smart contract. To do this, open the file you created earlier, ending with the &lt;code&gt;.sol&lt;/code&gt; extension. Next, we need an SPDX license identifier comment at the top of the code; this helps trust and makes source code available. Note that in solidity, every comment begins with &lt;code&gt;//&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write your first line of code, which is the comment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: GPL-3.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we use the &lt;code&gt;pragma&lt;/code&gt; keyword, as this helps the compiler we use to run specific features and checks. To prevent future problems with compilation incompatibility, we use a &lt;code&gt;version pragma&lt;/code&gt;. Here we use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity &amp;gt;=0.7.0 &amp;lt;0.9.0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means this code is written for solidity version 0.7.0 up till 0.9.0 and no more. Subsequently, we create a contract and name it App:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract App {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The contract we have created so far is empty because it doesn’t contain codes that tell the contract to perform a specific action. &lt;/p&gt;

&lt;p&gt;To compile your contract, click on compile interface highlighted below:&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%2Fhvdckq4y2foffk4q2lkl.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%2Fhvdckq4y2foffk4q2lkl.png" alt="Building Smart Contracts for Django Applications" width="368" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you do so, and there are no syntax errors in your code, you should click here to compile your contract:&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%2F4wof5efuircgrc854o0w.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%2F4wof5efuircgrc854o0w.png" alt="Building Smart Contracts for Django Applications" width="368" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4.Testing your Smart Contract Using Ganache and Metamask
&lt;/h3&gt;

&lt;p&gt;Now that you have written your first smart contract using an IDE named Remix and have complied with it let’s test it.&lt;/p&gt;

&lt;p&gt;To do so, we will use Ganache, a personal Ethereum blockchain that provides you with virtual accounts and test Ethereum tokens to test your smart contracts before deploying to the mainnet. &lt;/p&gt;

&lt;p&gt;To set up your Ganache, head to the &lt;a href="https://trufflesuite.com/ganache/" rel="noopener noreferrer"&gt;website&lt;/a&gt;, choose your operating system, download it, and then run the application. To set up your account with Metamask, do this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, choose the quickstart option; you will see this interface below. All you have to do is choose an account you will use:&lt;/li&gt;
&lt;/ul&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%2F5vpxfu4kjeua2ibqcr12.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%2F5vpxfu4kjeua2ibqcr12.png" alt="Building Smart Contracts for Django Applications" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next, you need to connect Ganache to your Metamask. If you don’t have it installed, use this &lt;a href="https://support.metamask.io/hc/en-us/articles/360015489531-Getting-started-with-MetaMask" rel="noopener noreferrer"&gt;guide&lt;/a&gt; and get started. To connect with Ganache, head to the settings display on your Metamask:&lt;/li&gt;
&lt;/ul&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%2Fmhw64ohdowdy9bj5h269.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%2Fmhw64ohdowdy9bj5h269.png" alt="Building Smart Contracts for Django Applications" width="366" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next, copy the RPC from your Ganache desktop application:&lt;/li&gt;
&lt;/ul&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%2Fbahlfoxhwrfovmwnvcl8.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%2Fbahlfoxhwrfovmwnvcl8.png" alt="Building Smart Contracts for Django Applications" width="800" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; After you have, you should input it in Metamask to connect it. In the settings area, choose networks. After you have chosen networks, proceed to add the network you want. And manually input the network, placing the right answers in the necessary sections below. However, note that in the RPC server section, you should paste the one you copied:&lt;/li&gt;
&lt;/ul&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%2Fsays4uxyd5f65t0vwdwx.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%2Fsays4uxyd5f65t0vwdwx.png" alt="Building Smart Contracts for Django Applications" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After putting the required information, save it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to import any of the Ganache accounts into your Metamask so you can easily deploy and test your contract. Choose any account and tap on the key icon and copy the private key:&lt;/li&gt;
&lt;/ul&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%2F2pa6a2lxh7c9tt4rjfs7.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%2F2pa6a2lxh7c9tt4rjfs7.png" alt="Building Smart Contracts for Django Applications" width="800" height="94"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After you have copied the private key, go into your Metamask and import it:&lt;/li&gt;
&lt;/ul&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%2Fxe5pz8vhheln2cvp4tnc.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%2Fxe5pz8vhheln2cvp4tnc.png" alt="Building Smart Contracts for Django Applications" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you did so correctly, this is what you would have on your screen:&lt;/li&gt;
&lt;/ul&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%2Fcmhk2q3ir1qkudzsj7ri.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%2Fcmhk2q3ir1qkudzsj7ri.png" alt="Building Smart Contracts for Django Applications" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After you have followed all the steps outlined and have correctly done so, You need to connect your Metamask wallet to Remix IDE. To do this, head to Remix and choose the deploy and run option. After you have done so, choose  “injected provider-Metamask” and connect your wallet. Next, choose a contract, in this case, the &lt;code&gt;Django_App&lt;/code&gt; contract we created earlier, and deploy:&lt;/li&gt;
&lt;/ul&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%2F7175z7dpozdquh6mfpve.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%2F7175z7dpozdquh6mfpve.png" alt="Building Smart Contracts for Django Applications" width="371" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Afterward, confirm the transaction on your Metamask:&lt;/li&gt;
&lt;/ul&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%2F9yfh3ffetis9xds4rblo.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%2F9yfh3ffetis9xds4rblo.png" alt="Building Smart Contracts for Django Applications" width="347" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the contract deployed successfully, you should get this:&lt;/li&gt;
&lt;/ul&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%2Fmyffd720i9rhtd0fjip9.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%2Fmyffd720i9rhtd0fjip9.png" alt="Building Smart Contracts for Django Applications" width="800" height="79"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you got the notification, congratulations! Your contract worked, and you are good to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  II. - Integrating Smart Contracts in a Django App
&lt;/h2&gt;

&lt;p&gt;Django is a framework that helps Python developers build scalable web applications. And in this section, you will learn how to integrate a smart contract in a Django app. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Create a Django Project
&lt;/h3&gt;

&lt;p&gt;Since the installation we ran applies to all the systems, we should create a Django application to perform further activities. After you have completed creating a Django project following this &lt;a href="https://www.w3schools.com/django/django_getstarted.php" rel="noopener noreferrer"&gt;guide&lt;/a&gt;, you should install the necessary packages we need. &lt;/p&gt;

&lt;p&gt;Get into your terminal, still in the virtual environment you had begun to create your Django project, and run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;web3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see an output as this after you have successfully installed the package: &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%2F4wozaff65gy8wb999890.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%2F4wozaff65gy8wb999890.png" alt="Building Smart Contracts for Django Applications" width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Connecting to a Blockchain Network Using Django
&lt;/h3&gt;

&lt;p&gt;To connect to the Django framework, you first need to choose the network you wish to connect to, and for this guide, we will connect to the Ethereum Blockchain; to do so, we will use the &lt;code&gt;web3.py&lt;/code&gt; library. &lt;/p&gt;

&lt;p&gt;Create an app within your Django project and create an &lt;code&gt;eth.py&lt;/code&gt; file within your designed app, and import &lt;code&gt;web3&lt;/code&gt; there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from web3 import Web3, AsyncWeb3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we need to connect to a blockchain network and have decided it will be Ethereum, we will use Infura provider. Open an account and copy the API keys:&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%2Fc8ta556kpchfnzonz9iu.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%2Fc8ta556kpchfnzonz9iu.png" alt="Building Smart Contracts for Django Applications" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And head over to the &lt;code&gt;eth.py&lt;/code&gt; file of your Django app and input this there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;infura_url = "https://mainnet.infura.io/v3/78955d8b4dde48dbbc963ac94fce1b92"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It helps us specify our provider, which is in &lt;code&gt;HTTPProvider&lt;/code&gt;. Also,  note that you must assign your URL to a variable; here, you assign your API to the &lt;code&gt;infura_url&lt;/code&gt; variable. Next, instantiate a connection using this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web3 = Web3(Web3.HTTPProvider(infura_url))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check if your connection worked, add this line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(web3.is_connected())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it is connected, you will get a &lt;code&gt;true&lt;/code&gt; statement in your terminal when you run the code.&lt;/p&gt;

&lt;p&gt;So far, you should have something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
from web3 import Web3, AsyncWeb3

infura_url = "https://mainnet.infura.io/v3/78955d8b4dde48dbbc963ac94fce1b92"

web3 = Web3(Web3.HTTPProvider(infura_url))

print(web3.is_connected())

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Sending a Transaction Using &lt;code&gt;web3.py&lt;/code&gt; in your Django Project
&lt;/h3&gt;

&lt;p&gt;You can send eth using &lt;code&gt;web3.py&lt;/code&gt; from your Django app. To do this, get into the &lt;code&gt;views.py&lt;/code&gt; file in your Django app and import &lt;code&gt;web3&lt;/code&gt;, views:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from web3 import Web3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we did with Infura earlier, specify the ganache API within  a variable; here, we use &lt;code&gt;ganache_url&lt;/code&gt;, given as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ganache_url = (YOUR_API_KEY/RPC)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, instantiate the connection using this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web3 = Web3(Web3.HTTPProvider(ganache_url))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, you load your account via the eth account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;acct1= w3.eth.account.from_key(pk)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From your Ganache account, let's choose the first account, get the private key, and put it in place of &lt;code&gt;pk&lt;/code&gt; in the line of code above. What this does is load the account's data into variable &lt;code&gt;acct1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we assign the address we want to send eth to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;some_address = "0xe1488a47b923aD2FC150Ac4DeE4dcE5605Ed5fce" 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the address to assign to &lt;code&gt;some_address&lt;/code&gt;, use the second account or any of your choice in your ganache.&lt;/p&gt;

&lt;p&gt;Next, we need to build out the transaction to send eth. To do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transaction = {
      'from': acct1.address,
      'to': some_address, 
      'value': 1000000000,
      'nonce': web3.eth.get_transaction_count(acct1.address), 
      'gas': 250000,
      'gasPrice': web3.to_wei(8,'gwei'),
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code, &lt;code&gt;from&lt;/code&gt; specifies the account you are sending the eth from &lt;code&gt;to&lt;/code&gt; identifies the receiving address. The &lt;code&gt;value&lt;/code&gt; is the amount of eth you wish to send, the &lt;code&gt;nonce&lt;/code&gt; is put to ensure it sends once, &lt;code&gt;gas&lt;/code&gt; specifies how much you want to send, and &lt;code&gt;gasPrice&lt;/code&gt; is the gas price for the &lt;code&gt;gas&lt;/code&gt; you wish to pay for the transaction. &lt;/p&gt;

&lt;p&gt;Next, you should sign the transaction using the private key of acc1. Here's how to do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;signed = w3.eth.account.sign_transaction(Transaction, private key)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, we send the signed transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)
tx = w3.eth.get_transaction(tx_hash)
assert tx[" from'] == acct1.address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from web3 import Web3

ganache_url = "HTTP://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(ganache_url))

acct1 = web3.eth.account.from_key("0xc1bf30defcd506bc3692de90ac0295763c29e79eb92c81bbcf78378d50a76db3")

some_address = "0xe1488a47b923aD2FC150Ac4DeE4dcE5605Ed5fce" 

transaction = {
      'from': acct1.address,
      'to': some_address, 
      'value': 1000000000,
      'nonce': web3.eth.get_transaction_count(acct1.address), 
      'gas': 250000,
      'gasPrice': web3.to_wei(8,'gwei'),
}

signed = web3.eth.account.sign_transaction(transaction, "0xc1bf30defcd506bc3692de90ac0295763c29e79eb92c81bbcf78378d50a76db3")

tx_hash = web3.eth.send_raw_transaction(signed.rawTransaction)
tx = web3.eth.get_transaction(tx_hash)
assert tx['from'] == acct1.address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To send the eth, run the file in your terminal. To confirm it worked, check your Ganache and your transactions. You should find this:&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%2Fszxivhsnoggbnsea5v0w.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%2Fszxivhsnoggbnsea5v0w.png" alt="Building Smart Contracts for Django Applications" width="800" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Deploying a Smart Contract from your Django Project using web3.py
&lt;/h3&gt;

&lt;p&gt;Earlier, we had created a smart contract and deployed it using injected Web3 provider in Remix IDE; this time, we will be deploying the smart contract from our Django project using &lt;code&gt;web3.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As we had done earlier, create a new file, name it &lt;code&gt;deploy.py&lt;/code&gt;, open it, and initiate a connection to Ganache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from web3 import Web3

ganache_url = ()
web3 = Web3(Web3.HTTPProvider(ganache_url))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, using the contract we had created earlier. Head on to your remix, and get the &lt;code&gt;abi&lt;/code&gt; and &lt;code&gt;bytecode&lt;/code&gt; of the contract. After you have done so, head over to the deploy.py file and write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bytecode = contract_interface['bin']
abi = contract_interface['abi']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Input your contract’s bytecode and abi into ‘bin’ and ‘abi’. The &lt;code&gt;bytecode&lt;/code&gt; allows us to execute the code, and the &lt;code&gt;abi&lt;/code&gt; lets us interact with the executed code.&lt;/p&gt;

&lt;p&gt;Next, we have to set the pre-funded account as the sender. Here’s how to do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web3.eth.default_account = web3.eth.accounts[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you have done this, to instantiate it in Python and deploy we do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App = web3.eth.contract(abi=abi, bytecode=bytecode)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where App is the name of the contract we had created earlier, after deploying to get the transaction hash and receipt, we write this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tx_hash = App.constructor().transact()

tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from web3 import Web3

ganache_url = "HTTP://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(ganache_url))

bytecode = '6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220a2bebf97e45daed66e8c174b0fd5f4b9a2c72e36e6e3f6a7da610f0fa2a8fdc364736f6c63430008120033'
abi = '[]'

web3.eth.default_account = web3.eth.accounts[0]
App = web3.eth.contract(abi=abi, bytecode=bytecode)

tx_hash = App.constructor().transact()

tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congrats! You have deployed your contract from Django. To get your contract, go into your Ganache app, and check the transactions. You should find this:&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%2Fl3hotijps4y2m4v4tj1w.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%2Fl3hotijps4y2m4v4tj1w.png" alt="Building Smart Contracts for Django Applications" width="800" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Django is a Python web development framework you can use to carry out Web3 operations when you have web3.py installed. You have learned the concept of smart contracts and how to create and deploy one.&lt;/p&gt;

&lt;p&gt;In addition, there are different considerations when it comes to smart contracts. Due to its nature, there are security vulnerabilities to watch out for, testing, auditing, and upgrading. Luckily, you have learned how to build smart contracts for Django Applications.&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>django</category>
      <category>web3</category>
    </item>
    <item>
      <title>Use of AI and Machine Learning to Build More Effective Decentralized Applications for Web3</title>
      <dc:creator>Ebuka Ukatu </dc:creator>
      <pubDate>Mon, 17 Jul 2023 22:42:31 +0000</pubDate>
      <link>https://dev.to/tallnerd/use-of-ai-and-machine-learning-to-build-more-effective-decentralized-applications-for-web3-322a</link>
      <guid>https://dev.to/tallnerd/use-of-ai-and-machine-learning-to-build-more-effective-decentralized-applications-for-web3-322a</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Decentralized applications (Dapp) are revolutionizing the way we approach decentralized Web3. They get built on the infrastructure of Web3. Usually, they are programs and protocols that a centralized authority can't control. These applications have helped change the scope of Web3 development.&lt;/p&gt;

&lt;p&gt;However, considering this scope, there are spots or instances where the effectiveness and efficiency of decentralized applications could experience different drawbacks with maintenance, where updates are frequently needed. &lt;/p&gt;

&lt;p&gt;Also, there is the scaling issue where the required level of security, integrity, transparency, and reliability, the computational power needed, would be great, and this is where artificial intelligence (AI) and machine learning (ML) could play a significant role.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applications of Artificial Intelligence (AI) and Machine Learning (ML) in Web3
&lt;/h2&gt;

&lt;p&gt;Artificial Intelligence (AI) mirrors human intelligence, the ability to reason and infer by computerized systems. AI systems can self-learn, self-correct, reason, and also display creativity. We could improve effectiveness and efficiency faster when aligned with Web3 and in application to decentralized applications.&lt;/p&gt;

&lt;p&gt;By feeding these AI systems data and through machine learning, there could be an adaptation to peculiar situations without the need for explicit instructions using statistical models and algorithms that work with given data. &lt;/p&gt;

&lt;p&gt;This way, AI could perform in a more efficient way, far better than humans could. Below are different applications of AI and machine learning in Web3.&lt;/p&gt;

&lt;h3&gt;
  
  
  I. -  Fraud Detection and Risk Management
&lt;/h3&gt;

&lt;p&gt;One of the drawbacks of decentralized applications is security, and there have been reports of different types of exploits and frauds. For example, bad actors could, through the use of phishing links, make unsuspecting individuals carry out transactions against their will.&lt;/p&gt;

&lt;p&gt;Fraudulent transactions could be identified, traced, and retrieved using anomaly detection models; this can help improve the trust and efficiency in dapps and reduce the number of those who fall into fraudulent scams and attacks.&lt;/p&gt;

&lt;p&gt;We could apply artificial intelligence and machine learning to risk management in Web3 and decentralized applications. Users often need a way to access the risk of certain transactions they take and can only take a wild guess; this leads to situations where users sign transactions that give malicious actors access to their dapps.&lt;/p&gt;

&lt;p&gt;However, using AI and ML, there can be a predictive risk assessment of any transaction you want. Users can then decide whether transactions are safe to carry out, reducing the frauds they fall into.&lt;/p&gt;

&lt;h3&gt;
  
  
  II. - Improving Smart Contract Security
&lt;/h3&gt;

&lt;p&gt;There have been different cases of smart contract exploitation, from &lt;a href="https://en.wikipedia.org/wiki/Poly_Network_exploit" rel="noopener noreferrer"&gt;Poly Network&lt;/a&gt; to &lt;a href="https://www.coindesk.com/tech/2022/03/29/axie-infinitys-ronin-network-suffers-625m-exploit/" rel="noopener noreferrer"&gt;Ronin&lt;/a&gt;; all of this has resulted in a lapse in the security of their contracts. Artificial intelligence and machine learning can help reduce these sorts of events.&lt;/p&gt;

&lt;p&gt;We can detect vulnerabilities in smart contracts using artificial intelligence and machine learning. By seeing these vulnerabilities, smart contract engineers and programmers can improve the overall security of a contract by implementing correct logic.&lt;/p&gt;

&lt;p&gt;Also, using ML classifiers for smart contract auditing could help smart contract auditors do a faster and better job than they would naturally.&lt;/p&gt;

&lt;h3&gt;
  
  
  III. - Optimizing Decentralized Infrastructure
&lt;/h3&gt;

&lt;p&gt;Using AI, developers could allocate resources optimally to achieve a desired goal. Unlike when AI uses available data to make decisions quickly, it could take longer for humans, with more margins for error and bias, to reduce the overall robustness of resource optimization.&lt;/p&gt;

&lt;p&gt;Also, applying this to load balancing in decentralized networks. AI can help distribute network traffic equally across multiple pools that support a decentralized application. &lt;/p&gt;

&lt;p&gt;In addition, it could get used to maximize the performance of distributed storage and computing.&lt;/p&gt;

&lt;h3&gt;
  
  
  IV. - Content Recommendation and Curation
&lt;/h3&gt;

&lt;p&gt;There's a lot of content in Web3, from seemingly good ones to others that are useless or targeted at misleading. &lt;/p&gt;

&lt;p&gt;Using personalized machine learning-powered recommendations for content and suggestions for marketplaces and Web3 tools could reduce the time people spend researching. &lt;/p&gt;

&lt;p&gt;These recommendations would reduce the number of frauds or scams people fall victim to and help them have better options for making choices.&lt;/p&gt;

&lt;h3&gt;
  
  
  V. - Automated AI Model Deployment
&lt;/h3&gt;

&lt;p&gt;In decentralized computing infrastructure, one of the lapses is the time it takes to individually carry out functions from different networks working together for a system. &lt;/p&gt;

&lt;p&gt;Although in contrast to the centralized computing infrastructure, it’s known to be more effective because the performance of each network pars the needs of any application.&lt;/p&gt;

&lt;p&gt;We could deploy AI and ML models to handle the workloads on this infrastructure further to enhance the performance of this decentralized computing infrastructure, making it operationally faster and more effective.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges in Using AI and Machine Learning to Build More Effective Decentralized Applications for Web3
&lt;/h2&gt;

&lt;p&gt;There are different challenges the use of AI and ML for decentralized applications is bound to face due to the nature of decentralization itself, and we go through a few here.&lt;/p&gt;

&lt;h3&gt;
  
  
  I. - Data Collection in a Decentralized Environment
&lt;/h3&gt;

&lt;p&gt;One of the challenges is the collection of valuable data in a decentralized environment. Since there is no centralization, accessing data and collecting would be threefold tricky due to the inherent structure of decentralization itself. &lt;/p&gt;

&lt;p&gt;Individually scraping out and cleaning data from different independent networks would take a while and might prove unnecessarily tiresome for many.&lt;/p&gt;

&lt;h3&gt;
  
  
  II. - Securing ML Models Deployed on Blockchain
&lt;/h3&gt;

&lt;p&gt;There's a challenge with the security of models deployed on the blockchain, which is honest. What chances are models deployed won't get corrupted by malicious actors? A model inversion poses the greatest threat to the security of models. &lt;/p&gt;

&lt;p&gt;In an attack, malicious actors aim to expose the privacy of training data which can hurt confidentiality in models.&lt;/p&gt;

&lt;p&gt;Also, there could be data poisoning, where AI models get trained with data manipulated to predict this AI's behavior. An attack such as this could alter, for example, a personalized recommendation system by AI models, where fraudulent marketplaces get shown as options for users to choose from, increasing the possibility of them falling for scams.&lt;/p&gt;

&lt;h3&gt;
  
  
  III. - Model Training Using Distributed Datasets
&lt;/h3&gt;

&lt;p&gt;Training models using distributed datasets could be cumbersome and time-consuming due to labeling and cleaning. &lt;/p&gt;

&lt;p&gt;Although, it could be fast when the model training is done concurrently for each dataset and could help for time-sensitive tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Prospects
&lt;/h2&gt;

&lt;p&gt;There are different future possibilities that AI and ML present for decentralized Web3, and we touch on a few possible below.&lt;/p&gt;

&lt;h3&gt;
  
  
  I. - AI-driven Smart Contracts and Dapps
&lt;/h3&gt;

&lt;p&gt;We could train AI to engineer smart contract development and make the whole development cycle from scratch to deploy easier, faster, and more secure. Also, decentralized applications could be run solely by AI-trained models, reducing the possibility of interference by centralized malicious actors.&lt;/p&gt;

&lt;h3&gt;
  
  
  II. Self-learning Decentralized Applications
&lt;/h3&gt;

&lt;p&gt;We could do dapps that self-learn and take inputs from user experience to help make the experience of users more smooth and more effective. &lt;/p&gt;

&lt;p&gt;For example, if a user encounters a bug when using a decentralized application, it could learn from that and help to make other users not face the same bug that caused the previous user to have a horrible experience.&lt;/p&gt;

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

&lt;p&gt;There has been a recent fear of losing out (FOMO) on AI and what it poses for the future of humans and technology. &lt;/p&gt;

&lt;p&gt;However, through AI alignment in decentralized applications for decentralized Web3, there could be advances that would prompt robust advancement and progress. &lt;/p&gt;

&lt;p&gt;However, this isn't without challenges, as there are security, data collection, and training using distributed data sets that could make this goal harder to reach.&lt;/p&gt;

&lt;p&gt;Regardless, there are bright prospects that the alignment of AI in decentralized Web3 poses to bring to the user experience and technological advancement over time. &lt;/p&gt;

</description>
      <category>ai</category>
      <category>web3</category>
      <category>machinelearning</category>
      <category>cryptocurrency</category>
    </item>
  </channel>
</rss>
