Ethereum smart contract development languages

Rohit Nisal
Rohit Nisal
Published in
7 min readMar 14, 2023

Note:

Please read my first Ethereum smart contract story.

Before start lets go through some terminology

What is a blockchain?

A blockchain is a public database that is updated and shared across many computers in a network.

Block” refers to data and state being stored in consecutive groups known as “blocks”. If you send ETH to someone else, the transaction data needs to be added to a block to be successful.

Chain” refers to the fact that each block cryptographically references its parent. In other words, blocks get chained together. The data in a block cannot change without changing all subsequent blocks, which would require the consensus of the entire network.

What is Ehereum?

Ethereum is a blockchain with a computer embedded in it. It is the foundation for building apps and organizations in a decentralized, permissionless, censorship-resistant way.

https://ethereum.org/

What is ETH?

Ether (ETH) is the native cryptocurrency of Ethereum. The purpose of ETH is to allow for a market for computation. Such a market provides an economic incentive for participants to verify and execute transaction requests and provide computational resources to the network.

Ethereum accounts

An Ethereum account is an entity with an ether (ETH) balance that can send transactions on Ethereum. Accounts can be user-controlled or deployed as smart contracts.

Ethereum has two account types:

  • Externally-owned account (EOA) — controlled by anyone with the private keys
  • Contract account — a smart contract deployed to the network, controlled by code.

Both account types have the ability to:

  • Receive, hold and send ETH and tokens
  • Interact with deployed smart contracts

Ethereum smart contract development languages

Smart contract can be programmed using relatively developer-friendly languages.

The two most active and maintained languages are

  1. Solidity
  2. Vyper

Other languages also support like Yul or Yul+ etc for more experience users. This story mostly cover Solidity programming language.

Solidity

  1. Object-Oriented, high-level language for implementation smart contracts
  2. Curly- bracket language that has most profoundly influenced by C++
  3. Statically typed
  4. Supports: Inheritance, Libraries, complex user-defined types

Writing your first smart contract using Solidity

Disclaimer

This tutorial won’t focus

  1. how the blockchain works
  2. What can achieved with a smart contract
  3. gas fee

Set up the Environment

  1. Head over the Remix. Remix IDE allows developing, deploying and administering smart contracts for Ehereum like blockchains.
Remix IDE and create Solidity contract, compile and deploy

Case study enable chat on the blockchain

Lets consider simple chat application use case

  1. allow a user to post a message that will stored on the blockchain
  2. allow us to retrieve store messages

Write first Smart contract example using Solidity

  1. Create a new file in the contracts folders and lets name it as Chat.sol
first Contract ( like class in Solidity)
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract Chat {

}
  1. Since making source code available always touches on legal problems with regards to copyright, the Solidity compiler encourages the use of machine-readable SPDX license identifiers. Every source file should start with a comment indicating its license:

// SPDX-License-Identifier: GPL-3.0

2. Pragma: Pragma keyword is used to enable certain complier features or checks. A pragma directive is always local to a source file, so you have to add the pragma to all your files if you want to enable it in your whole project. If you import another file, the pragma from that file does not automatically apply to the importing file.

3. Version Pragma: Source files can (and should) be annotated with a version pragma to reject compilation with future compiler versions that might introduce incompatible changes

4. contract: Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types and Enum Types. Furthermore, contracts can inherit from other contracts.

Create Message data model using struct

pragma solidity >=0.7.0 <0.9.0;

contract Chat {

// Message
// struct: Structs are custom defined types that can group several variables
struct Message {
//actual message to store
string message;
// Holds a 20 byte value (size of an Ethereum address).
address waver;
//Signed and unsigned integers of various size
uint timestamp;
}

//Hold the Messages
//State variables are variables whose values are permanently stored in contract storage.
Message[] messages;
}
  1. struct: Structs are custom defined types that can group several variables

2. Message[] messages: State variables are variables whose values are permanently stored in contract storage.

3. address: Holds a 20 byte value (size of an Ethereum address).

Add send message and get messages functions

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Chat {

// Message
// struct: Structs are custom defined types that can group several variables
struct Message {
//actual message to store
string message;
// Holds a 20 byte value (size of an Ethereum address).
address waver;
//Signed and unsigned integers of various size
uint timestamp;
}

//Hold the Messages
//State variables are variables whose values are permanently stored in contract storage.
Message[] messages;

function sendMessage(string calldata message) public {
uint messageLength = bytes(message).length;
require(messageLength > 0, "Message can't be empty!. Please provide valid message");
messages.push(Message(message, msg.sender, block.timestamp));
}

function getMessages() view public returns (Message[] memory) {
return messages;
}
}

function sendMessage(string calldata message) public

  1. function: declare function
  2. sendMessage: function name
  3. string: string data type
  4. calldata: data area where a variable is stored.
  5. message: function parameter variable to get value
  6. public: it’s public function. It can be called from outside of the contract.
  7. messages.push: add new element into Message array.
  8. msg.sender: msg contains properties that allow access to the blockchain. msg.sender return address where the call came.
  9. block: current block being added to the blockchain.

function getMessages() view public returns (Message[] memory)

  1. view: View is Modifier. It states that this function is read-only. Inside functions that are of type view, state variables cannot be modified.
  2. returns: what data function will return. getMessages function return Messages array.

Compile chat smart contract

  1. Go to Remix and open Chat.sol file
  2. click Solidity Complier tab in the sidebar
Compile Chat.sol

3. Click “Complie Chat.sol”. If all good without any issue you will green tick on Solidity Complier tab and you should get the smart contract artifacts added into contracts/artifacts in the file explorer.

Chat.json file

About artifacts

  1. artifacts/Chat.json: contain below details

a. libraries,

b. the bytecode

c. the deployed bytecode

d. the gas estimation,

e. the method identifiers,

d. the ABI.

2. artifacts/Chat_metadata.json: contains the metadata from the output of Solidity compilation.

Test Smart Contract

Testing contract extensively before deploying it to the blockchain. The blockchain is by design immutable. So you can’t deploy the contract and fix. If any bug found that simply not possible.

  1. create Chat_test.sol file under tests folder.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "remix_tests.sol";
import "../contracts/Chat.sol";
contract ChatTest {
Chat chatToTest;
/// 'beforeAll' runs before all other tests
function beforeAll () public {
chatToTest = new Chat();
}
function checkSendMessage () public {
// create and send first message
chatToTest.sendMessage("Hello Friends!");
// Ensure the messages list contains 1 or greater messages
Assert.equal(chatToTest.getMessages().length, uint(1), "messages state variable should contain 1 message");
// Ensure first message's content is "Hello Friends!"
Assert.equal(chatToTest.getMessages()[0].message, string("Hello Friends!"), "The first Message in message should be \"Hello Friends!\"");
}
}

2. Run test file: sidebar, click on the Solidity Unit Testing tab and click on run button. If run successfully then able to see below output.

Chat_test.sol
Chat_test.sol test case pass

Important links

Solidity

Development Remix IDE

Ethereum

Conclusion

Congratulations for making till the end and read first Ethereum smart contract! This was a very first step in Solidity.

Smart contract development is a whole different paradigm and will cover more interesting topic, development and stories.

Thank you for reading.

Note:

Please read my first Ethereum smart contract story.

Coming Next…..

Deploy Our Smart Contract to the Test Network

--

--

Rohit Nisal
Rohit Nisal

Manager, Software Engineering | iOS | Android | Flutter | Swift | SwiftUI | Kotlin | Jetpack compose | Kotlin Multiplatform | Blockchain and Solidity | NodeJS