How to Create Your First Ethereum Smart Contract With Remix IDE

Smart contracts are almost everywhere you look around in a discussion on the decentralized web or emerging blockchain technologies. Powered by innovation, smart contracts have evolved for different use cases across multiple industries. Over the course of time, tools for developing and deploying smart contracts have also advanced by considerable margins. 

You can create smart contract using Remix IDE, followed by compiling and deploying the contract. Since Ethereum is the commonly used platform for developing smart contracts and supports Solidity programming language for scripting smart contract code, they are the top favorites of smart contract developers. However, a specific piece of code is only good if you can compile it into machine-readable language. 

Remix IDE is the choice of a compiler for Ethereum smart contracts. You can capitalize on the flexibility for creating smart contract using Solidity and Remix IDE with a few simple steps. The following discussion helps you learn about the methods for creating, compiling, testing and deploying your own smart contract. The smart contract example would serve as a tool for basic banking activities alongside highlighting other important tasks with the contract.

Excited to learn the basic and advanced concepts of ethereum technology? Enroll Now in The Complete Ethereum Technology Course

What do You Need to Start Creating an Ethereum Smart Contract?

The prerequisites to write a smart contract in Remix IDE are the foremost priorities for any smart contract developer. One of the mandatory requirements for creating a smart contract in Ethereum would refer to knowledge of Solidity. You must have in-depth knowledge of Ethereum and Solidity before you start writing smart contract code. In addition, you need to learn the basics of Remix IDE and its different components. 

A general impression of the best practices to write and test smart contract as well as deploy them with Remix IDE can provide the foundation to develop smart contracts. A remix is an open-source tool that offers an environment for developing smart contracts directly from your browser. The first step in creating your own smart contract on Remix IDE starts with opening Remix IDE itself.

You can access the Remix IDE from any browser and then begin the process to create your own smart contract by creating a new file. The new file would have the “.sol” extension, and you can use it for the Solidity smart contract code. 

Want to get an in-depth understanding of Solidity concepts? Become a member and get free access to Solidity Fundamentals Course Now!

Steps to Develop Smart Contract on Remix IDE

If you want to write and deploy smart contract on Remix IDE, then you must follow the recommended steps. Here is an outline of the best practices for each step of writing and deploying an Ethereum smart contract using Remix IDE.  

  • Declaration of Contract

The new file with “.sol” extension can serve as the stepping stone in starting off the smart contract development process. However, the actual process of writing a smart contract begins with declaring the contract. The discrepancy in Solidity version can cause issues when you compile smart contract with different compiler versions. You must declare the Solidity version you plan on using before the next step. Here is the syntax for declaring the Ethereum smart contract.

pragma solidity ^0.6.6;

contract BankContract { }

The “BankContract” refers to the smart contract title assumed as the example in this discussion.

  • Definition of State Variables, Data Structures and Data Types

The second step in the process to create smart contract using Remix IDE would focus on defining state variables, data structures and data types. You would need a client object for keeping the client’s information, and it would leverage the “struct” element for linking up with the contract. The client object keeps important information in the contract, such as the ID, balance and address of client. Subsequently, you have to develop a “client_account” type array for maintaining the information of all clients. Here is the syntax for defining the client object in the example smart contract. 

pragma solidity ^0.6.6;

contract BankContract {

    struct client_account{

        int client_id;

        address client_address;

        uint client_balance_in_ether;

    }

    client_account[] clients;

}    

Now, you have to allocate an ID for every client at every instance they join the contract. Therefore, you must define an “int” counter alongside setting it to “0” in the constructor field for the concerned smart contract. You can add the following lines of code and continue the definition further.

int clientCounter;

    constructor() public{

        clientCounter = 0;

    }

}    

The steps to write a smart contract with Remix would also need the definition of “address” variable for a manager. It would also need a “mapping” element for maintaining the last interest date of clients. If you want to limit the time needed for sending interest to any account, the ‘mapping’ element can check whether the time has passed for sending the amount. You can just add the following lines of code, like the following example continuing with the previous lines of code. 

client_account[] clients;

    int clientCounter;

    address payable manager;

    mapping(address => uint) public interestDate;

    constructor() public{

        clientCounter = 0;

    }

}

Excited to learn about the key elements of Solidity? Check the presentation Now on Introduction To Solidity

  • Implementation of Modifiers

Modifiers are an important element you would need while learning how to write and compile smart contract by using Remix. You have to restrict access to specific people for calling a specific method in a smart contract. The “modifier” is essential for verification of the implemented condition and identifying the feasibility of executing a concerned method. Y

  • Implementing the Fallback Function

The next crucial component in the process to create your own smart contract would refer to the ‘fallback’ function. It is important to ensure that the example smart contract could receive ETH from any address. You must note that the “receive” keyword is new to the Solidity 0.6.x versions and serves as a ‘fallback’ function for receiving Ether.   

  • Create the Methods

The methods would help in defining the core functionalities of the smart contract you develop. Once you are done with contract declaration, variable definition and implementation of the modifier and fallback functions, you have to work on developing methods specific to the smart contract goals. In the case of the “BankContract” example, you can try developing the following methods,

“setManager” method for configuration of manager address to defined variables with assumption of “managerAddress” as a parameter.

“joinAsClient” method for ensuring that clients join the contract. Upon the joining of a client, the contract would set the interest date and add client information in the “client” array. 

Subsequently, you can write and test smart contract for banking applications with the “deposit” method for sending ETH from client account to the smart contract. 

You can define some other methods in the process of creating your smart contract by using Remix IDE. For example, “withdraw,” “sendInterest,” and “getContractBalance” can be a few methods you would need in the “BankContract” Ethereum smart contract.

Once you have completed the writing process, you will find the final output as something like this,

https://gist.githubusercontent.com/burakcanekici/157e77857eb90f27c3486be34efe88ff/raw/452aa49d9019b7d078a4d855fc910f96bad9164f/BankContract.sol 

Want to learn the basic and advanced concepts of Ethereum? Enroll in our Ethereum Development Fundamentals Course right away!

  • Compilation of the Smart Contract

After completing the scripting process, you must proceed to the next phase of creating smart contract using Solidity and Remix IDE with a compilation of the contract. You can compile your smart contract directly in Remix without switching platforms. Interestingly, the compilation with Remix IDE plays a vital role in testing your contract before deploying it. 

The Solidity Compiler in the Remix IDE can help you test smart contract code you have scripted in this example. Most important of all, the Solidity Compiler offers an easy-to-use interface, which specifies the Solidity Compiler version. That’s it; you are ready to compile the “BankContract.sol” file and move to the next step.

  • Deploying the Contract 

The final step after compiling a smart contract would focus on how to deploy smart contract in Remix IDE. In the case of Remix IDE, you would find a direct option for deploying smart contracts. You can go with the “Deploy & Run Transactions” functionality in Remix for deploying the smart contracts you have scripted. 

Remix IDE serves different options to compile smart contract and deploy it in different environments. The best thing about creating smart contracts and deploying them on Remix IDE is the flexibility of choosing desired configuration. You can select the deployment environment from options such as “web3 provider”, “JavaScript VM,” and “injected web3” environments. 

Once you have set up the environment and the account parameters in the Deploy & Run Transactions section, you can click on the “Deploy” button. You can witness the resultant transaction in the terminal, which shows successful deployment of the smart contract to the selected account. 

Want to know the real-world examples of smart contracts and understand how you can use it for your business? Check the presentation Now on Examples Of Smart Contracts

Final Words

The details of the steps to create your own smart contract through Remix IDE show a simple approach to smart contract development. As the demand for smart contracts continues to increase, developers seek innovative, effective and faster solutions. Remix IDE or Integrated Development Environment serves all the important functionalities required for writing, compiling, testing and deploying smart contracts. 

Therefore, Remix IDE takes away the trouble of switching between different tools and platforms for creating your smart contract. In addition, the flexibility of integration with other tools allows better scope for interoperability in smart contract development. Learn more about Remix and its capabilities as a tool for smart contract development alongside the best practices now. 

Join our annual/monthly membership program and get unlimited access to 35+ professional courses and 60+ on-demand webinars.

The post How to Create Your First Ethereum Smart Contract With Remix IDE appeared first on 101 Blockchains.

Read the original article on 101blockchains.com