Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Building Smart Contracts for Django Applications

Posted on Jul 21 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. And for some reason, the other party doesn’t hold their end of the bargain; the third party makes this enforceable, usually using force. 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. Smart contracts are programmed agreements written in Code, which executes the programmed command when all agreement conditions are met. To break down this understanding, let’s use the analogy of the workings of a vending machine: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. He inserts the required amount, and the vending machine verifies the correct amount. Afterward, it dispenses the carbonated drink to Bobby. 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. 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. 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.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. 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.Here is a guide on getting started with Blockchain smart contract development:To install solidity, we will use an easy approach. First, you have to install Visual Studio Code on your system. To download Visual Studio Code: Head over to the website to download the software; depending on the operating system you use, as shown in the diagram below, choose your operating system, download, and run. After downloading and running Visual Studio Code, head over to this part of the software as highlighted below:You are good to go now that solidity is installed in your Visual Studio Code.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. To use Remix IDE, head over to the website and launch it. When you do this, this is what you should see on your screen:Now that you have this, let’s create our first contract.To write your first contract, we need to create a file for the solidity in its format or extension, which is .sol. To do this, head to the contracts folder.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 .sol. I called mine Django_App: 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.Let us write a simple, smart contract. To do this, open the file you created earlier, ending with the .sol 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 //.Write your first line of code, which is the comment:Next, we use the pragma keyword, as this helps the compiler we use to run specific features and checks. To prevent future problems with compilation incompatibility, we use a version pragma. Here we use: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:The contract we have created so far is empty because it doesn’t contain codes that tell the contract to perform a specific action. To compile your contract, click on compile interface highlighted below:When you do so, and there are no syntax errors in your code, you should click here to compile your contract:Now that you have written your first smart contract using an IDE named Remix and have complied with it let’s test it.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. To set up your Ganache, head to the website, choose your operating system, download it, and then run the application. To set up your account with Metamask, do this:After putting the required information, save it.If you got the notification, congratulations! Your contract worked, and you are good to go.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. 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 guide, you should install the necessary packages we need. Get into your terminal, still in the virtual environment you had begun to create your Django project, and run this command:You will see an output as this after you have successfully installed the package: 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 web3.py library. Create an app within your Django project and create an eth.py file within your designed app, and import web3 there: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:And head over to the eth.py file of your Django app and input this there:It helps us specify our provider, which is in HTTPProvider. Also, note that you must assign your URL to a variable; here, you assign your API to the infura_url variable. Next, instantiate a connection using this:To check if your connection worked, add this line of code:If it is connected, you will get a true statement in your terminal when you run the code.So far, you should have something like this:You can send eth using web3.py from your Django app. To do this, get into the views.py file in your Django app and import web3, views:As we did with Infura earlier, specify the ganache API within a variable; here, we use ganache_url, given as:Next, instantiate the connection using this:Next, you load your account via the eth account:From your Ganache account, let's choose the first account, get the private key, and put it in place of pk in the line of code above. What this does is load the account's data into variable acct1.Next, we assign the address we want to send eth to a variable:For the address to assign to some_address, use the second account or any of your choice in your ganache.Next, we need to build out the transaction to send eth. To do this:In the code, from specifies the account you are sending the eth from to identifies the receiving address. The value is the amount of eth you wish to send, the nonce is put to ensure it sends once, gas specifies how much you want to send, and gasPrice is the gas price for the gas you wish to pay for the transaction. Next, you should sign the transaction using the private key of acc1. Here's how to do it:Lastly, we send the signed transaction:Here's the full code:To send the eth, run the file in your terminal. To confirm it worked, check your Ganache and your transactions. You should find this: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 web3.py.As we had done earlier, create a new file, name it deploy.py, open it, and initiate a connection to Ganache:Next, using the contract we had created earlier. Head on to your remix, and get the abi and bytecode of the contract. After you have done so, head over to the deploy.py file and write this:Input your contract’s bytecode and abi into ‘bin’ and ‘abi’. The bytecode allows us to execute the code, and the abi lets us interact with the executed code.Next, we have to set the pre-funded account as the sender. Here’s how to do it:After you have done this, to instantiate it in Python and deploy we do this: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:Here is the full code: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:ConclusionDjango 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.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.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse ADEYINKA - Jul 2 MedCode - Jul 2 Akshay Ballal - Jul 1 Emanuele Bartolesi - Jul 12 Once suspended, tallnerd will not be able to comment or publish posts until their suspension is removed. Once unsuspended, tallnerd will be able to comment and publish posts again. Once unpublished, all posts by tallnerd will become hidden and only accessible to themselves. If tallnerd is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Ebuka-Ukatu. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag tallnerd: tallnerd consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging tallnerd will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Building Smart Contracts for Django Applications

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×