Creating and deploying smart contracts on Ethereum is getting easier with Solidity, contract-oriented programming language. Developers extensively use Solidity to write smart contracts in Ethereum.
What Is Ethereum, Anyway?
Surely, you’ve heard about blockchain and its ability to change the game, right? Invented in 2008, blockchain technology came before Bitcoin. This tendency continued in similar cryptocurrencies, like Ethereum. Before we get to know Ethereum, let’s define its features and properties.
Ethereum is a decentralized platform that keeps smart contracts running. It allows developers to write dApps that protect against fraud and eliminate third parties.
Moreover, the decentralized Ethereum blockchain makes every computer in the network its server, without any need for a central authority. This means that every transaction is secured with a hash, and only network nodes (current users) may sign it.
Ethereum’s blockchain is similar to Bitcoin’s, meaning that it is a distributed record of transaction histories. Obviously, that’s not all; so, to describe it further, we will go deeper into its principles.
Considering Bitcoin and Ethereum as similar ecosystems, we will draw a parallel and present their differences. While Bitcoin uses unspent transactions to track how many coins each node has, the Ethereum network uses accounts. This picture will specify the process:
In contrast with Bitcoin, the Ethereum blockchain is capable of communication with smart contracts as well their deployment. Buterin’s promise has worked well.
In order to read or execute any contract, we trigger the Ethereum Virtual Machine (EVM). EVM is an engine of the network, which confirms transactions (including contract-creation transactions), guarantees and ensures security, and provides assurance that the smart contract will work similarly on all nodes.
Apart from storing an up-to-date status, nodes also keep a full history of all actions in the network.
Let’s see why Ethereum wins as a platform for decentralized apps.
The Benefits of Ethereum
As we continue to explore the cryptomarket, Ether comes in second after Bitcoin as a cryptocurrency used worldwide. Despite the fact that it appeared five years after Bitcoin, its success is skyrocketing. Here are some reasons why:
How Widespread Is It?
Jeremy Allaire, CEO and co-founder of FinTech, assumes the Ethereum blockchain will be as popular as the internet one day. Allaire says Ethereum has blasted the market, as it allows creation of apps and the ability to issue tokens, as well. Therefore, it is unlimited.
Also, new kinds of financial-contract development are now possible due to smart-contract technology.
Lower Cost
Similarly to Bitcoin’s mining, nodes on the Ethereum blockchain mine Ether (its cryptocurrency). Ether fuels the system. As soon as miners solve an NP-class problem, they set a fee (gas) to add the solved transaction to the blockchain. While the gas amount per operation is fixed, the gas price depends on market conditions. By paying bigger gas, you potentially decrease confirmation time for your transactions, because miners give higher priority to transactions with the bigger gas amounts in the transaction pool.
Time-Consuming Approach
While Bitcoin’s block-mining time remains at about 10 minutes, it takes approximately 14 seconds to mine an Ether block. Although parties require 30 confirmations of the transaction to get Ether, time consumption is still relatively less.
Overall, Ethereum is a sophisticated platform for starting decentralized applications and smart contracts. We’ve added some extra Ethereum blockchain advantages to this picture:
Smart Contracts
To make the Ethereum ecosystem live and function, developers fall back on the writing of smart contracts. They create functions and commands that are activated, self-executed, and deactivated according to certain conditions.
To put it simply, a smart contract is a set of code lines that is like a self-executing computer program, but also eliminates intermediaries. The main issue with a traditional treaty law is security, which smart contracts principally ensure.
In blockchain, smart contracts are digital assets, developed to store funds, receive funds, and carry out payments.
In brief, smart contracts function on the Ethereum blockchain with their own Ethereum address and balance. One of the functions sends and receives money. When a smart contract gets its function trigger, it activates.
Smart-Contract Implementation
Bitcoin creates smart contracts using C++ or Java programming languages. Bitcoin smart contracts serve as multisignature accounts, payment channels, or a multi-party lottery without an operator.
Sidechain technology makes RootStock a platform similar to Bitcoin. A Smart contract built for RootStock is quite comparable with a smart contract on Ethereum.
Ethereum works with a contract-oriented, Turing-complete programming language called Solidity. Smart contracts self-execute their functions via Ethereum Virtual Machine.
Solidity Programming Language
Originally suggested in 2014 by Gavin Wood, Solidity has become the most popular programming language for Ethereum smart contract creation. C++, JavaScript, and PowerShell had a bit of influence on its development.
Furthermore, Solidity is a statically-typed, Turing-complete language. For EVM to read Solidity, its compiler converts it to bytecode.
With experience in writing code in an object-oriented language, it won’t be difficult to start using Solidity, a contract-oriented language. This is due to the similarity of the contract with the OOP class.
Solidity’s documentation says:
With its features, developers can create blind auctions, crowdfunding events, multi-signature wallets, and much more. You can find examples of listed smart contracts here.
Additionally, there is no ability to generate random numbers in Solidity (without using Oracle) due to two factors:
Complex algorithms are quite expensive. At the same time, each computation increases the total gas price needed for a transaction, which is why Solidity requires a more plain approach.
As multiple nodes will run the contract, it should be degenerative.
One more peculiarity is time. The Solidity time concept is relative, as it depends on gas, the number of transactions in the blockchain pool, and market conditions.
It’s worth mentioning that Solidity is limited in string function. According to Solidity documentation, you’d better use bytes for arbitrary-length, raw-byte data, and string for arbitrary-length string data. If possible, cut the length and use one of bytes1 to bytes32, as they are cheaper.
Solidity Tutorials
Even though Solidity is relatively young and is still developing, there are plenty of sensible, comprehensive tutorial books and videos on it.
Learning the Solidity programming language may get easier with Chris Dannen’s contribution: “Introducing Ethereum and Solidity.” Unlike other resources, this book will help many people in addition to professional programmers who decide to redirect their skills to blockchain technology. Chris Dannen makes it comprehensive enough even for beginners in programming, as his tutorial contains full and complete information on the Ethereum project and Solidity programming language. “Introducing Ethereum and Solidity” contains:
- an overview of the Ethereum network
- a comparison of distributed and web apps
- an example of a smart contract written in Solidity
- basic and intermediate smart-contract tutorials
- dApp, coin, and blockchain deployment
The video tutorials mentioned below may also help you figure out how Solidity programming works:
- Tutorial 1: Basics;
- Tutorial 2: Inheritance;
- Tutorial 3: Custom Modifiers and Error Handling;
- A Beginner’s Gguide to Ssmart C-contracts in Solidity;
- How to Build an Ethereum dApp.
Create Your Smart Contract in Solidity
As mentioned earlier, EVM, Ethereum’s environment for processing transactions and smart contract code approval, runs smart contracts (a digital asset) through every node. Let’s consider in detail how a Solidity smart contract should be structured.
The first line in the contract shows the Solidity compiler version used for writing this code. “Pragmas” are instruction for the compiler code execution process.
pragma solidity ^0.4.24;
Through the next lines of code, we designate that this is a smart contract for simple token creation. We set token balance mapping (key -> value pair); its key is an ETH address, and its value is presented by a token balance. The commands you see will store the balance of each token holder:
contract ERC20ExampleToken {
mapping (address => uint256) balanceOf;
The next step is to set the total amount of tokens you are willing to issue:
uint256 public totalSupply;
“string” commands will set the name of your token and its symbol. The token’s symbol is abbreviated from the name. None of them is unique; therefore, there is the possibility of encountering two tokens of the same name. The only thing that distinguishes one from the other is their ETH address.
string public name;
string public symbol;
The “constructor” function is a kind of entrance. It will run the contract when deployed to the blockchain.
constructor
uint256 _totalSupply,
string _name,
string _symbol
) public {
Then we assign values from a constructor to our smart contract local variables:
totalSupply = _totalSupply;
name = _name;
symbol = _symbol;
Now we set the balance of the contract creator to _totalSupply. After the line presented below, the owner of the contract will get the supply of tokens on his/her balance:
balanceOf[msg.sender] = totalSupply;
}
When implementing the ERC-20 token standard, the “transfer” function expects to have 2 parameters: _to (for the token receiver) and _amount (for the number of tokens we want to transfer):
function transfer(address _to, uint256 _amount) public {
The “require” statement appears for checking that the receiver is not a zero address, and the number of tokens for transfer is larger than zero:
require(_to != address(0));
require(_amount => 0);
balanceOf[msg.sender] = balanceOf[msg.sender] - _amount;
balanceOf[_to] = balanceOf[_to] + _amount;
}
}
Also, you can find tutorials on smart-contract development in Applicature blog. For any help or more information contact us here.
Conclusion
- Ethereum is an open platform based on blockchain technology. Ethereum serves to create dApps and store transaction data.
- The engine of the platform is the Ethereum Virtual Machine (EVM), which operates smart contracts. In this case, “operating” means executing functions and commands in smart contracts.
- The cryptocurrency that keeps the Ethereum ecosystem alive is Ether. It is much faster to mine than Bitcoin.
- A smart contact is a digital protocol that guarantees security and eliminates third parties (banks, logistics companies, retailers, etc.)
- Developers prefer writing Ethereum smart contracts in the Solidity contract-oriented programming language. Smart contracts in Solidity provide the opportunity to write programs that are able to solve computational problems and apply complex logic.
- Solidity is a statically typed language that supports inheritance, libraries, and complex user-defined types, among other features.
Great Article! I really appreciate this. You are so awesome! This issue has and still is so important and you have addressed it so Informative. blockchainfirm.io/smart-contract-development-services
Regards,
Thank you for your comment! In case you would like to start cooperation, exchange experience or just have a pleasant blockchain-oriented conversation, our consulting team is always open. https://applicature.com/blockchain-development-consulting/
Best regards,
Applicature