written by
Victoria Egba

Ethereum & Solidity: The Complete Guide For Beginners.

Tech Industries 15 min read , March 24, 2022

The development community is currently determining the optimal approach to use Ethereum in creating new and innovative applications. But, first, understand the fundamentals of Ethereum and the benefits it provides.

Since the internet's inception, Ethereum has emerged as the most revolutionary technology. It is an excellent blockchain platform that has sped up the world's transition to a decentralised future. By understanding how to create and interact with Ethereum smart contracts, you can build compelling distributed applications capable of disrupting entire industries. It entails writing and deploying smart contracts and even creating JS applications capable of reading/writing data from contracts. As the Ethereum revolution begins, developers are in high demand to capitalise on this game-changing technology.

This article will cover the basics of Ethereum, understanding smart contracts, Solidity, and the essential things to know on reading and writing data from smart contracts. Read on.

What is Ethereum?

Ethereum is a decentralised financial platform that enables smart contracts to create and execute. Eth utilises its cryptocurrency, Ether. However, unlike Bitcoin or Litecoin, it's not for use as a medium of exchange. Rather than that, app developers can use Ether to pay for Ethereum network services and transaction fees.

| blog.tedbree.com | header image | Ethereum and Solidity: The Complete Guide For Beginners|
What is Ethereum?

Vitalik Buterin first proposed Ethereum in 2013. Following Bitcoin, Ethereum is the most notable blockchain-based project due to the introduction of the smart contract concept. Ethereum is a decentralised application platform built on a blockchain that enables developers to create and deploy decentralised applications. These applications execute precisely as intended, with no possibility of fraud, censorship, or third-party interference.

You can learn more about Blockchain Marketplaces, NFTS and Crypto by reading more on Blockchain Technology: A Complete Guide to Blockchain Marketplaces.

Besides its smart contract concept, it also serves as a currency. Users can send, receive and store ETH, the currency of the Ethereum network (and the second most valuable crypto asset), in a secure wallet. On platforms like Uphold, you can link a bank account and exchange USD, EUR, GBP and other currencies with your Ethereum account through ACH. However, our focus is on using Ethereum with a smart contract.

What is Solidity?

Solidity is a robust programming language that enables the creation of Smart Contracts. It is pretty similar to Javascript, making it accessible to Web Developers. Consider it a way to control a bank account via code remotely. Solidity was proposed by Dr Gavin Wood, who collaborated with Vitalik Buterin on Ethereum. It integrates with Smart Contracts, which are essentially self-executing contracts enforced by code.

You can create applications that simulate a crowdfunding campaign, a lottery, a loan, or any other type of financial instrument using Solidity.

Solidity and other smart contract languages enable anyone to send money directly to a piece of code, managing the incoming funds according to your specified rules. It eliminates the need for financial transactions facilitated by a bank or regulatory body, which can be costly and time-consuming!

Benefits of Solidity

The main benefits of Solidity include— ease of development and deployment, flexibility, and the ability to quickly deploy smart contracts to the blockchain. Here are a few benefits of Solidity programming.

Ease of Use

Solidity is a rich language with features like inheritance and libraries, but it is also simple. Its design is for the easy development of smart contracts. Complex operations have smaller, reusable building blocks. So when you want to build a smart contract, Solidity has the tools you need at your fingertips - all while remaining accessible enough that anyone who knows how to program can pick it up quickly.

Secure Transactions

There are numerous benefits of using Solidity and Ethereum, but most importantly, the security offered. The blockchain is a secure digital ledger, making it highly reliable for smart contracts. The code and transactions cannot be tampered with, allowing a safe and reliable process.

Multiple Level Inheritance

Solidity supports inheritance and contract creation of hierarchical structures and interfaces, simple libraries and contracts that can hold Ether. In addition, it includes support for multiple level inheritance properties inherited from other contracts.

Limitations of Solidity

Solidity has one major disadvantage; the codes cannot have flaws. When writing Solidity smart contracts, it is critical to use clean and simple code— any bugs might result in lost money. Mistakes and transactions are irreversible, and to fix attacks, you'll have to change the Ethereum code.

What Are Smart Contracts?

| blog.tedbree.com | header image | Ethereum and Solidity: The Complete Guide For Beginners|
What Are Smart Contracts?

Smart contracts are self-executing programs that store and execute code on a blockchain. These applications run automatically with fulfilled trigger conditions, allowing users to automate essential business activities and save time, money, and effort.

The code and the agreements are distributed over a decentralised blockchain network. Smart contracts render transactions traceable, transparent, and irreversible.

The smart contract is a term used in describing computer codes that can facilitate the exchange of money, property, shares, contents and virtually anything that has value.

The programs control how accounts behave within the Ethereum state. Programs like C++, Python, and JavaScript inspired smart contracts, and their creation was with Ethereum Virtual Machine in mind (EVM).

The Ethereum Virtual Machine- How It Works

The Ethereum Virtual Machine operates as a decentralised computer which means there exists no central server or points of failure. The EVM allows users to perform operations without a centralised database with global access. It has become the virtual machine considered the bedrock of Ethereum's complete operating structure. It provides many users with anonymity as they complete untraceable transactions.

The EVM supports smart contract-based tokens and applications. People refer to this plot as "Turing-incomplete" because it can execute code only that is fully predetermined. Some argue that EVM protects against security loopholes and bugs, covering most Blockchain networks.

Ethereum Virtual Machine is the underlying component of the Ethereum blockchain which executes smart contract bytecode. It ensures the accessible building of all smart contracts regardless of the programming language.

Further, EVM runs with a modified version of the Nakamoto consensus, solving blockchain networks' problems. Since there is no D-App without smart contracts, there is no need for blockchain without EVM's existence. It is not an overstatement since the system plays a crucial role in making Ethereum world-famous.

Types of Ethereum Accounts

Ethereum has two types of accounts —Externally Owned Accounts (EOA) and Contract Account.

Externally Owned Accounts (EOAs) are owned by private keys, with Ethereum addresses and Ether like regular bank accounts. On the Other hand, Contract Accounts have one or more smart contracts that can store code and data on the blockchain. Instead of two different account types and differing semantics on how they are created and controlled, contracts are now all just contracts. They are equal and no longer have the unique "create" privilege that account abstraction offers.

| blog.tedbree.com | header image | Ethereum and Solidity: The Complete Guide For Beginners|
Types of Ethereum Accounts

OA is an account controlled by its owner's private keys. It is an independent entity for ethers(Ether is the currency of the Ethereum platform, you can use Ether to purchase gas for transactions). However, the contract account is held within the Smart Contract (smart wallet), making it self-executing.

A contract written in smart-contract coding translates to a format known as bytecode. Most of the source code for smart contracts is written in Solidity programming language.

Creating a Smart Contract

The Ethereum Virtual Machine (EVM) is the Ethereum platform's runtime environment for smart contracts. It is sandboxed, but it is also entirely isolated, which means that code running within the EVM has no access to the network, disk, or other processes. As a result, the EVM cannot directly execute smart contract languages; they are compiled to low-level machine instructions known as opcodes.

Opcodes

EVM executes specified tasks through Opcodes. There are about 140 opcodes that are for EVM. When these opcodes are combined, the EVM becomes Turing-complete.

The opcode is the lowest programming language level in the Ethereum Virtual Machine (EVM). It is a computer language explicitly created to allow developers to build distributed applications and software on EVM. It means the code can, in theory, compute anything given the computational and memory capacity. The EVM is composed of a stack, a memory, and storage. A stack stores information about the state of the program/process. A storage store stores key/value pairs representing persistent storage for accounts and smart contracts.

The maximum amount of opcodes depends on the byte size and how many bytes are needed to decode the opcode. 16² = 256, 32² = 1024, 64⁴ = 65536 and so on.

The EVM supports several opcodes that manipulate the stack, memory and contract storage. They perform the essential operation(s) within the stack and one-word data on the heap. Some categories of opcodes include; Stack-manipulating opcodes (POP, PUSH, DUP, SWAP), Arithmetic/comparison/bitwise opcodes (ADD, SUB, GT, LT, AND, OR), Environmental opcodes (CALLER, CALLVALUE, NUMBER), Memory-manipulating opcodes (MLOAD, MSTORE, MSTORE8, MSIZE), Storage-manipulating opcodes (SLOAD, SSTORE), Program counter related opcodes (JUMP, JUMPI, PC, JUMPDEST), and Halting opcodes (STOP, RETURN, REVERT, INVALID, SELFDESTRUCT).

Within these categories are essential opcodes. The pushing and popping of items onto and off of the stack by PUSH, POP, DUP and SWAP are imperative. The arithmetic operations (ADD SUB GT LT) are also vital. Finally, environmental information like how much a transaction will cost to process (GAS COST).

Bytecode

Bytecode is the language of the machine. All programs written in the Turing language are compiled down to bytecode and stored on the blockchain. Each Bytecode instruction takes up one byte of space. Most instructions also require additional space: 1-32 bytes for arguments.

Opcodes convert to bytecode for adequate storage. Bytecode's size is one byte for every opcode. Each byte consists of an opcode (left) and its parameter (right).

The bytecode is decoded to opcodes and pushed onto a stack as execution flow. At each step of the opcode, the bytecode calculates the gas cost for the execution. If there's an exception, the EVM reverts to the previous state and consumes all gas. The bytecode (deployed code) divides into bytes, where every byte corresponds to an opcode. A single transaction may contain multiple calls to various smart contracts and their functions, data pushes, and signatures.

The initial instruction is 0x60, which corresponds to the PUSH1 function. As a result, we know the push data is one byte in length and increment the stack by one byte. Now that the stack has only one item, we may proceed to the following instruction. Given that 0x01 is a component of a PUSH instruction, the next instruction to execute is another 0x60 (PUSH1) with the same data. The last instruction is 0x01, which corresponds to the addition operation. This command removes two items from the stack and adds their total. 0x02 is currently the only item on the stack.

You can learn more about Blockchain Marketplaces, NFTS and Crypto by reading more on Blockchain Technology: A Complete Guide to Blockchain Marketplaces.

Contract State & Ethereum

When writing smart contract programs, it's crucial to understand how values are passed to functions since Solidity is an Ethereum Virtual Machine (EVM) based language. Unlike higher-level languages such as JavaScript, Ruby and Python, the EVM uses stacks to pass parameters rather than directly allowing users to pass arguments.

The EVM stack is a 256-bit register stack that stores 1024 items. The sixteen most recent items can be accessed or modified and read in chronological order: the right to left (the first item in the stack is 1, second is 2, third is 3, etc.). Therefore, it would be best to use the Virtual Machine's available opcodes to manipulate the stack. These opcodes implement functions for push, pop, look-up and manipulate data in the stack. However, the EVM's stack is limited in size (it can hold up to 1024 items), so it will only allow access to the top 16 items.

Due to the stack size constraint, complex opcodes can only obtain or send data using Contract Memory. Memory is where most data for each contract is stored, even more so for complex opcodes.

However, the memory contents are not stored after the contract is complete. While the stack is comparable to function arguments, memory is comparable to defining variables.

Storage

While contracts cannot store the state themselves (the EVM is a state machine), they can store data in external, public databases. External storage is always available to contracts since it operates as a public database, enabling any external account or contract to read and write to values stored on the blockchain. Contract Storage is similar to a public database, except that Keys and Values are used instead of tables, rows, and columns. The Key must have a value of 32 bytes. If a contract wishes to store data, it creates a Key and assigns a Value that other contracts may read. When a contract wishes to erase data from storage, it resets the value to 0.

Besides, reading from storage is always free, writing to storage is more costly than accessing memory; therefore, (use write) only when necessary.

DAPPS and Ethereum

| Blog.tedbree.com | header image | Why Engineers Create Apps With Ethereum |
Why Engineers Create Apps With Ethereum

A decentralised application (or 'dApp') is an application that serves some specific purpose to its users. Like how a BitTorrent client enables you to share files and a messaging app allows you to send messages, dApps are helpful for anything, from tracking ownership of digital assets to serving up prediction markets (e.g., Augur).

By eliminating intermediaries and centralised servers, dApps built on the Ethereum Network can offer their users more secure, private, and reliable services. At the same time, dApps running on the Ethereum Network can provide its users with control and flexibility.

At its core, a decentralised app (dApp) uses the blockchain public ledger to store transactions transparently and permanently. Like other apps, dApps have a front end (the interface) and a back end (the code). It's the back end code written to the blockchain (as opposed to a private server in traditional apps).

Furthermore, a dApp runs on a decentralised network. It contrasts to traditional apps, which are centralised, runs in the cloud and interacts with a database. For example, an app for a ride-hailing service connects riders to drivers and then processes payment for the service. Dapps decentralise this activity by clicking riders with drivers through smart contracts, requiring no central authority to process payments.

dApps run on the Ethereum platform, a public distributed computing platform that uses cryptocurrencies. Its strength lies in its ability to have no single point of failure due to its decentralised nature. As the world's first open public blockchain, Ethereum allows anyone to download and run software that sits on top of its protocol without interference from any third party. Furthermore, these dapps operate decentralised (more control for users and less for companies). As a result, they're censorship-resistant, transparent, and trustless—allowing for total disintermediation.

Benefits of dApps

With its decentralised nature, dApps provides a security level that is not achievable with other solutions. Since dApps encrypts all transactions across a network of thousands of nodes, there is no single point of failure that a hacker can exploit. In addition, when a transaction is processed, it is encrypted and dispersed over thousands of nodes, making it virtually impossible for any other party to access.

Ethereum is perhaps the best platform for building dApps. Solidity enables developers to form smart contracts using the Ethereum Virtual Machine, thanks to its own language.

The Decentralised Application (dApp) is the longer-term perspective of the projects. It could be a game, service or tool, an online marketplace, etc. The idea is to use Blockchain and Smart Contracts as the new back-end, where contracts replace and extend traditional APIs.

Just like regular apps, dApps are user-facing services on the Web. The main difference is that dApps are executed on a decentralised peer-to-peer network and not through a legacy client/server architecture. An example in the real world could be an Application that manages an open community with no central authority or managers, like a Decentralised Social Network or a Decentralised Wiki that stores its data in a Blockchain and not in a centralised server.

You can learn more about Blockchain Marketplaces, NFTS and Crypto by reading more on Blockchain Technology: A Complete Guide to Blockchain Marketplaces.

Why Engineers Create Apps With Ethereum

Decreased fraudulent activity

The combination of decentralisation, encryption, and immutability inherent in blockchain technology aid in the reduction of fraudulent activity. Since the Ethereum platform enables developers to construct decentralised apps easily, it's natural that they'd choose this technology to develop their banking application. Furthermore, a decentralised system assures the security and privacy of all data. The system records all transactions and ownership information in a distributed database due to the data integrity of the blockchain ledger.

Community-driven developments:

Applications like Bitcoin, Ethereum and many other cryptocurrencies are on open source platforms, which means developers can build an application and adapt their version depending on their use case. It is great for rapid development, updating and scaling.

Efficient and straightforward Processes: A well-designed, comprehensive app may be quite effective in promoting your product or service. A blockchain application has the potential to expedite corporate operations, increase efficiency and security, and fundamentally transform how you connect with consumers. While we discussed using smart contracts to automate corporate processes, the concept's usefulness extends beyond merely checking a box. By removing intermediaries and optimising procedures, Ethereum blockchain technology can increase efficiency. As a result, businesses can simplify their operations and increase their efficiency. Additionally, when users swap in-game objects or prizes such as tokens and points for a public token, they become more invested in the app's performance.

Cost savings

Third-party intermediates, such as bankers and government organisations, are commonly utilised to authenticate individuals' identities, form and enforce contracts, and ensure the smooth operation of transactions. Ethereum, as a distributed system, does not require any of this.

Smart contracts run automatically when a predefined condition is satisfied, saving time and money on administrative expenditures. Ethereum's blockchain technology intends to fundamentally alter how the internet operates – from providing a decentralised, digital record for transaction verification to enabling users to agree on wire transfers and other issues that previously required an intermediary.

Businesses can generate revenue with Ethereum without incurring significant merchant and transaction costs.

Ethereum Smart contracts Creating apps EVM Solidity Ether Blockchain Ethereum Virsual Machine