5 steps to programming in Blockchain (Smart-Contract)

Gianluca Busato
Enkronos
Published in
7 min readOct 14, 2022

Introduction

Have you ever wondered what a blockchain is, how it works, and how you can even make apps using it yourself? In this article I will introduce you to the necessary concepts for developing your first crypto project and a practical example using the Ethereum platform.

In the blockchain industry, I found that programming blended with my desire to make a considerable impact today. I see this as a rare opportunity and the blockchain industry needs inspired and motivated people more than ever to innovate current systems.

If you are familiar with the blockchain basics (smart contract, dApps, Web3, transactions) you can easily move on to the practical side.

Smart Contracts and Ethereum 2.0: The Most Detailed Explanation Ever

What is blockchain and how does it work?

Blockchain is a new network that helps us decentralize transactions and allows them to be peer-to-peer without using intermediaries. To trade via blockchain you don’t have to know the person or go to the store. At the same time, through blockchain you are guaranteed to get the money or product you want. Basically, blockchain guarantees through its technology that you will get what you want without using intermediary companies or banks.

Blockchain is made up of a network of computers that share the same transaction history, and this network is owned by everyone. Instead of this list of transactions sitting on one company’s server, it is decentralized and stored on multiple computers. In short, it’s a distributed database across a network of computers.

What are dApps?

Decentralized blockchain applications, or dApps, can be written on blockchain platforms such as Ethereum, Cosmos, Solana or Avalanche. Apps of this type can be in the area of finance (DeFi), art and collectibles (NFTs), gaming, digital wallets, smart contracts.

In the area of Finance (DeFi) these apps bring a considerable improvement and come as an alternative to the current financial system. These products (dApps) can be for lending, borrowing, borrowing, saving and more. The technologies used to build dApps are open source and here you can innovate and contribute to the decentralization of any field.

Another area that was decentralized and trending in 2021 was the arts, where dApps helped content creators gain control over their creations via blockchain. This way, artists can easily prove ownership of their creation eliminating the risk of forgery, track the history of the created product back to the current owner, and get paid better.

Vitalik Buterin Explaining How Smart Contracts Work

About the Ethereum platform

Ethereum is an open source blockchain platform where programmers can build and customize applications in industries such as (DeFi, NFT, security, energy, insurance, health, wallet, gaming).

Ether is a cryptocurrency used to conduct transactions on the Ethereum platform. If you are a user of the platform or a programmer, you have to pay fees in Ether to transact and run applications on the Ethereum network.

The programming language to build applications on the Ethereum platform is called Solidity. Solidity is a high-level, object-oriented language and is used exclusively to write smart contracts on the Ethereum platform.

Why Smart Contracts?

A Smart Contract is the digital version of real-world contracts. It is a software program written on a computer and stored inside a blockchain. The advantage of a smart contract stored on the blockchain is that it cannot be modified or deleted, has increased security and is transparent (being public, anyone can see what it is used for).

Decentralized applications (dApps) rely on smart contracts, which are used by programmers to execute operations and maintain data on the blockchain.

The example below will be a smart contract written in the Solidity language on the Ethereum platform.

1. Development environment

In the next few minutes, I’ve set out to demonstrate how quickly and easily an application can be programmed on the blockchain.

To write a smart contract I used the Remix IDE development environment, an online platform that helps write and compile Solidity code.

For deploying to the production environment, Truffle or Hardhat may be much better alternatives, but also require a bit more effort to use.

If you want to focus more on the front-end, you can leave the back-end to tools like Alchemy, Moralis, Infura that make it even easier to develop dApps.

2. How do I write a smart contract?

Below is an example of a smart contract that implements some functions to store data and ether storage in the contract. The mechanism is like OOP (object-oriented programming) classes.

First we need to inform the compiler which version of solidity we are using and it is recommended to use the latest version.

The uint nonPayableData line declares a uint variable (unsigned integer of 256 bits) whose states can be changed via the setNonPayableData() function. Public is used when we want the states to be changed from outside the contract but normally we do not want this.

The setPayableData() function is followed by the keyword payable which means that the function can store Ether in the contract. The amount of ether must be set before calling the function.

First we need to inform the compiler which version of solidity we are using and it is recommended to use the latest version.

The uint nonPayableData line declares a uint variable (unsigned integer of 256 bits) whose states can be changed via the setNonPayableData() function. Public is used when we want the states to be changed from outside the contract but normally, we do not want this.

The setPayableData() function is followed by the keyword payable which means that the function can store Ether in the contract. The amount of ether must be set before calling the function.

3. Connecting to Testnet via Metamask

Before deploying you need to choose the environment where you will take the smart contract. To connect to the Testnet via Metamask, you will choose Injected Web3 which will connect you to Rinkeby (test blockchain that works the same way as the blockchain network itself, called mainnet).

3.1 — Deploy environment

The transaction will be done using the Metamask virtual wallet. This is an open-source wallet used to interact with the Ethereum blockchain network.

4. Deploy

Fees paid for transactions (called gas) are always paid in the native currency, in this case ETH. (Since a fraction of 1 ETH is paid, the unit of measurement is called gwei)

On the left side the contract will appear collapsed and inside we will see the functions declared in the contract as buttons.

The blue buttons do not create transactions; therefore they will not change the state and will display contract data without paying any fees.

Yellow buttons can change the contract states without paying commission in ether (ETH).

The red buttons are for transactions (functions) for which a commission is paid.

5. Testing

Remix has a useful command line for testing (available in the Remix interface immediately after deployment), where we can see details about the transaction, such as: address, commission, value set in the smart contract.

You can also view transaction details directly on the test blockchain (rinkeby.etherscan.io/).

Conclusions

Blockchain and decentralized applications are still in their infancy and adoption is on an upward trend. This brings many opportunities and should excite you whether you are a programmer, work in the IT industry, or are planning a professional conversion to the sector. So, learning to program dApps in 2022 is like learning to program web apps in the 2000s.

Smart contracts are a digital alternative to regular contracts, greatly improving the speed, efficiency, and accuracy of a contract. They are cheaper, more secure and can be used in areas as diverse as finance and healthcare, but also in other areas that are not 100% transparent and can be manipulated.

If you have questions about blockchain and need guidance in your personal project we are waiting for you with questions. Contact Enkronos today.

How to develop smart contracts with Enkronos

Source

--

--