Steps to Create, Test and Deploy Ethereum Smart Contract
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.
Table of Content
- What is Remix IDE?
- Getting Started With Remix IDE
- What is Solidity Programming Language?
- How to Develop Smart Contract on Remix IDE?
- Steps to Compile the Smart Contract
- Steps to Deploy the Smart Contract
- Conclusion
- Frequently Asked Questions related to Steps to Create, Test and Deploy Ethereum Smart Contract
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:
Step 2: Click on the file explorer icon onto the left side bar.
Step 3: Click on contracts as indicated in the below figure:
Step 4: Click on HelloWorld.sol and you will be presented with the code editor with the default code on the editor.
What 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:
// 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:
How 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.
Step 3: Enter the name of the new workspace w3wiki.
Step 4: In the workspace w3wiki, right-click on the option contracts and select the option New File.
Step 5: Create a new file Bank.sol.
Step 6: Paste the below code in Bank.sol.
// 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:
Step 2: This will show the compilation details.
Auto 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:
By 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.
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.
Function 1: Deposit Balance
Add amount and click on transact. Click on displayBalance and the total balance in the account will be displayed.
Function 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.
Conclusion
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.
Frequently Asked Questions related to Steps to Create, Test and Deploy Ethereum Smart Contract
What is the limit of smart contract in Ethereum?
The maximum size of solidity smart contract that can be deployed on Ethereum is 24.576 kilobytes
What is the benefit of Ethereum smart contract?
By Eliminating intermediaries Ethereum smart contract reduce transaction cost and increase efficiency.
Can a smart contract hold multiple tokens?
Yes, Single smart contract can create multiple tokens that have different properties.
Contact Us