Steps to Create, Test and Deploy Ethereum Smart Contract
Last Updated :
20 May, 2024
Smart contracts are self-execution programs stored on a blockchain that are automatically executed when predefined conditions are met. They allow the participants on the blockchain to transact with each other without a trusted central authority. After creating a smart contract, the next step is to deploy the smart contract onto a blockchain for execution. For this purpose, developers often use development tools such as Remix IDE. This article focuses on discussing steps to create, test, and deploy the Ethereum Smart Contract on Remix Tool.
What is Remix IDE?
Remix IDE is a no-setup tool with a GUI that is used for smart contract development. It is famous for the visual debugger and allows for a simple deployment process.
- It is a browser-based editor.
- It is used for writing, testing, and debugging smart contracts written in Solidity programming language.
- It comes with syntax highlighting, auto-completion, and code analysis features.
- It provides a user-friendly interface making it accessible to developers of all skill levels.
Getting Started With Remix IDE
Step 1: Open the Remix IDE in your browser following the URL https://remix.ethereum.org/. You will be presented to the following screen:
Remix IDEStep 2: Click on the file explorer icon onto the left side bar.
File ExplorerStep 3: Click on contracts as indicated in the below figure:
contractsStep 4: Click on HelloWorld.sol and you will be presented with the code editor with the default code on the editor.
HelloWorld.sol
Code EditorWhat is Solidity Programming Language?
Solidity is a statically typed programming language that is designed for developing smart contracts that run on Ethereum.
- It is a contract-oriented and high-level programming language.
- It supports complex programming features like inheritance, libraries, and user-defined data types, among other features.
- It has syntax similar to that of JavaScript.
- The file extension of the Solidity file is .sol.
- It is used to create smart contracts for various purposes such as voting, muti-signature wallets, and many more.
Below is the Solidity program of the default code in the code editor:
Solidity
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.12 <0.9.0;
contract HelloWorld {
/**
* @dev Prints Hello World string
*/
function print() public pure returns (string memory) {
return "Hello World!";
}
}
Explanation:
1. SPDX License Identifier
// SPDX-License-Identifier: MIT
SPDX License Identifiers are standardized abbreviations for common open-source software licenses.
2. Version Pragma
pragma solidity >=0.6.12 <0.9.0;
Pragma are the instructions to the compiler on how to treat the code. This line states that the code is compatible with a version greater than or equal to 0.6.12 but less than 0.9.0.
3. The contract keyword
contract HelloWorld {
}
The contract keyword is used to declare a contract HelloWorld consisting of functions and data.
4. Function declaration
function print() public pure returns (string memory)
The print() function will print the string value on the console. The function has an access modifier public and is declared as pure as it does not read or modify the variables of the state.
5. Return statement
return "Hello World!";
The return statement prints the string Hello World! on the console.
Output:
Default Code OutputHow to Develop Smart Contract on Remix IDE?
Follow the steps below to develop a smart contract on Remix IDE:
Step 1: Open the Remix IDE and click on the File Explorer.
Step 2: Select create a new workspace option from the drop-down menu.
create a new workspaceStep 3: Enter the name of the new workspace GeeksforGeeks.
New workspaceStep 4: In the workspace GeeksforGeeks, right-click on the option contracts and select the option New File.
Create New FileStep 5: Create a new file Bank.sol.
Bank.solStep 6: Paste the below code in Bank.sol.
Solidity
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract Bank
{
address Owner;
// Creating the Mapping for the Adding &
// Transfer Amount in Account
mapping(address=>uint)Balance;
// Constructor for the address of the Owner
constructor()
{
Owner = msg.sender;
}
// Function for adding the Ethereum in Account
function depositBalance(uint amount) public returns(uint)
{
// First check whether it is Owner's Account or Not
require(msg.sender == Owner, "This is Owner's Account !!");
Balance[msg.sender] = Balance[msg.sender] + amount;
return Balance[msg.sender];
}
// Function to Get the Balance from an Account
function displayBalance() public view returns(uint)
{
return Balance[msg.sender];
}
// Function to transfer the Amount from Owner to Recipient
function Transfer(address recipient, uint amount) public
{
// Function to check the Self account is or not
require(msg.sender != recipient, "Can't Transfer !! Self Account.");
// Function to check the owner has balance is available or not
require(Balance[msg.sender] >= amount, "Insufficient Balance !!");
_transfer(msg.sender, recipient, amount);
}
function _transfer(address From, address To, uint Amount) private
{
Balance[From] = Balance[From] - Amount;
Balance[To] = Balance[To] + Amount;
}
}
Explanation:
There are 4 functions in the smart contract Bank.sol:
- depositBalance()
- require(msg.sender == Owner, "This is Owner's Account !!") : Address is checked to determine if it is owner's address or not.
- Balance[msg.sender] = Balance[msg.sender] + amount : Amount is added to the available balance in the account.
- transfer()
- require(msg.sender != recipient, "Can't Transfer !! Self Account.") : This checks recipient address against sender's address, if the address is matching a message is displayed "Can't Transfer !! Self Account".
- require(Balance[msg.sender] >= amount, "Insufficient Balance !!") : This checks whether the requested amount is less than the available balance or not. If not, then a message is displayed "Insufficient Balance".
- _transfer(msg.sender, recipient, amount): If recipient address is correct and balance is available then _transfer() function is invoked.
- _transfer()
- Balance[From] = Balance[From] - Amount: Balance is deducted from sender's account.
- Balance[To] = Balance[To] + Amount: Balance is added to recipient's account.
- displayBalance()
- return Balance[msg.sender]: This will display sender's account balance.
Steps to Compile the Smart Contract
Smart Contact can be Compiled by two methods:
- Manual Compile
- Auto Compile
Manual Compile
In manual compilation, the developer has to manually compile the smart contract after every change before deploying it.
Step 1: Click on the Compile Bank.sol option as shown in the figure below:
Compile Bank.solStep 2: This will show the compilation details.
Compilation DetailsAuto Compile
This option when enabled will automatically compile the code after every change and there is no need to manually perform the above steps for compiling the smart contract before deploying it.
Select the Auto Compile option as shown below:
Auto CompileBy selecting this option, the smart contract will be compiled automatically.
Steps to Deploy the Smart Contract
Follow the step below to execute the code after successful compilation:
Step 1: To execute the code, click on Deploy button under the Deploy and Run transactions window.
Deploy Bank.sol
Working of Deployed Smart Contract
After deploying the code, click on the method button under the drop-down of deployed contracts to invoke the method, and for the output scroll down to see the result.
Execute Bank.solFunction 1: Deposit Balance
Add amount and click on transact. Click on displayBalance and the total balance in the account will be displayed.
Deposit BalanceFunction 2: Transfer:
To transfer amount, add Recipient address, amount to transfer and click on transact button. Click on displayBalance button and the total balance in the account will be displayed.
Transfer BalanceConclusion
Writing and deploying smart contracts may seem challenging but with the right development tools and knowledge, it can be a simple process. In this article, we have walked through the process of using Remix IDE for developing and deploying the smart contract. By following the step-by-step guide, one can start building a smart contract and harness the power of Blockchain Technology.
Similar Reads
How to Simply Deploy a Smart Contract on Ethereum?
Smart contracts are blocks of code that reside on the blockchain. It is like an Ethereum account but there is a critical difference between an external account and a smart contract. Unlike a smart contract, an external account can connect to multiple Ethereum networks (Goerli testnet, mainnet, etc.)
7 min read
How to Test a Smart Contract for Ethereum?
Public Blockchains like Ethereum are immutable, it is difficult to update the code of a smart contract once it has been deployed. To guarantee security, smart contracts must be tested before they are deployed to Mainnet. There are several strategies for testing contracts, but a test suite comprised
15+ min read
Deploy a Smart Contract on Ethereum with Python, Truffle and web3py
This article discusses deploying a Smart Contract on Ethereum with Python, Truffle, and web3py. Prerequisite: In order to write a simple smart contract, deploy it to a personal Ethereum blockchain, and call the contract using Python3, install the below software on a PC: 1. Python3 v3.5.3 or later ve
6 min read
How to Test a Smart Contract with Remix?
The Remix IDE is an open-source development environment for building, testing, and deploying smart contracts on the Ethereum blockchain. One of the most notable features of Remix IDE is its integration with the Solidity programming language. Solidity is a contract-oriented language that is specifica
7 min read
How to Test Smart Contracts on the Ganache?
Smart contracts are self-executing pieces of code powered by blockchain technology which ensures transparency, immutability, and trust in a decentralized ecosystem. However smart contracts need thorough testing to ensure that they are flawless because once a smart contract is deployed in a main-net
6 min read
Creating TestRail Projects - Steps to Create New Projects
TestRail is a test management tool used for testing our project, creating test cases, and test plans, and providing various features. We will easily interact with the team members, and QA team and manage and track our projects. This article focuses on providing a step-by-step guide to creating a new
6 min read
How to use MetaMask to Deploy a Smart contract in Solidity (Blockchain)?
Smart contracts are self-executing contracts. They were first proposed by Nick Szabo in the 90s. They are set of rules and protocols which two parties agree upon and have to follow. One of the main features is that they are immutable once deployed on the blockchain. It is widely used in the Ethereum
3 min read
Deploying Smart Contract on Test/Main Network Using Truffle
A Smart Contract is a computer program that directly and automatically controls the transfer of digital assets between the parties under certain conditions. A smart contract works in the same way as a traditional contract while also automatically enforcing the contract. Smart contracts are programs
5 min read
Solidity - Deploy a Smart Contract for Marks Management System
Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol. What are Smart Contracts?Solidityâs code is encapsulated in contracts which means a contract in Solidity
3 min read
Interacting With Ethereum Smart Contract Using Web3js
Ethereum is a cryptocurrency platform in the market just like Bitcoin. It is an open-source & decentralized blockchain featuring working on smart contracts. It has its own cryptocurrency known as ether. The smart contracts in Ethereum are written in solidity. TestRPC The Ethereum TestRPC is lik
4 min read