Write Smart Contract In Solidity

In this article, we will walk you through the steps required to write smart contract and deploy it on the Ethereum blockchain.

We will cover the basics of Solidity syntax, the development environment, and the tools needed to write, compile, and deploy your first smart contract.



Write Smart Contract

Smart contracts are self-executing computer programs that automatically enforce the terms of an agreement when certain predefined conditions are met.

The first step in writing a smart contract in Solidity is defining the contract.

You can do this by creating a new file with a .sol extension and then defining the contract

Remix IDE is used to compile and run Solidity code.

Step 1: Enter the given code in the Remix IDE Code Section.

Example: 

pragma solidity ^0.8.0; contract SolidityTest {uint mrx; uint ample;constructor() public{ mrx=7; ample=6; }function getSum() public view returns(uint){ return mrx+ample; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

 

Step 2: Click on Start to Compile under Compile Tab.

Step 3: Click Deploy under the Run Tab.

Step 4: Choose SolidityTest at 0x… in the drop-down list under Run Tab.

Step 5: The result can be viewed by clicking the getResult button.

Output

Write Smart Contract solidity

The following program returns the product of two numbers as shown below:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{uint mrx; uint ample;constructor() public {mrx=4; ample=5;}function getProduct() public view returns(uint){ // The Product of 4 x 5 is : 20 return mrx*ample; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Output:

Write Smart Contract output

Example Explanation

Above example is a simple Solidity smart contract that defines a contract called SolidityTest.

The contract contains two unsigned integer variables called mrx and ample that are assigned the values of 4 and 5, respectively, in the constructor function.

The getProduct function is defined with the public visibility modifier, meaning that it can be accessed by other contracts and external accounts.

It is also defined with the view modifier, indicating that it does not modify the state of the contract.

The getProduct function returns the product of the mrx and ample variables, which is calculated as mrx * ample.

The function also includes a comment that explains the result of the calculation.

The pragma statement at the top of the contract specifies the version of the Solidity compiler that should be used to compile the contract.

In this case, it is set to version 0.5.13, which is the minimum version required for the contract to compile.

Congratulation! you just Write Smart Contract in Solidity.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *