Ethereum smart contract development languages
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.
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
- Solidity
- Vyper
Other languages also support like Yul or Yul+ etc for more experience users. This story mostly cover Solidity programming language.
Solidity
- Object-Oriented, high-level language for implementation smart contracts
- Curly- bracket language that has most profoundly influenced by C++
- Statically typed
- Supports: Inheritance, Libraries, complex user-defined types
Writing your first smart contract using Solidity
Disclaimer
This tutorial won’t focus
- how the blockchain works
- What can achieved with a smart contract
- gas fee
Set up the Environment
- Head over the Remix. Remix IDE allows developing, deploying and administering smart contracts for Ehereum like blockchains.
Case study enable chat on the blockchain
Lets consider simple chat application use case
- allow a user to post a message that will stored on the blockchain
- allow us to retrieve store messages
Write first Smart contract example using Solidity
- Create a new file in the contracts folders and lets name it as Chat.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Chat {
}
- 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;
}
- 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
- function: declare function
- sendMessage: function name
- string: string data type
- calldata: data area where a variable is stored.
- message: function parameter variable to get value
- public: it’s public function. It can be called from outside of the contract.
- messages.push: add new element into Message array.
- msg.sender: msg contains properties that allow access to the blockchain. msg.sender return address where the call came.
- block: current block being added to the blockchain.
function getMessages() view public returns (Message[] memory)
- view: View is Modifier. It states that this function is read-only. Inside functions that are of type view, state variables cannot be modified.
- returns: what data function will return. getMessages function return Messages array.
Compile chat smart contract
- Go to Remix and open Chat.sol file
- click Solidity Complier tab in the sidebar
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.
About artifacts
- 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.
- 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.
Important links
Solidity
- Documentation
- Solidity Language Portal
- Solidity by Example
- GitHub
- Solidity Gitter Chatroom bridged to Solidity Matrix Chatroom
- Cheat Sheet
- Solidity Blog
- Solidity Twitter
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