💰 101 Smart Contracts and Decentralized Apps in #Ethereum
Tags: cryptocurrency, security
Published: 2021-07-12 12:38:31

#### ⛓ Smart contracts are a new way to build decentralized applications in a #Blockchain.

### <br>

## What is Blockchain?

We probably started hearing about Blockchain almost a decade ago when someone under the pseudonym of Satoshi Nakamoto released the first Bitcoin reference implementation. If you ever wonder who Satoshi is, you are not alone, and I am pretty confident it's a secret; nobody will reveal it in the years to come. Bitcoin not only introduced a digital currency as we know it today but also made popular the theory behind one of the most important inventions in the last decade, Blockchain technology. But what is Blockchain exactly? We will try to give a definition leaving all the fuzzwords behind. A Blockchain represents a distributed ledger of transactions running in a peer-to-peer network, where those transactions, once confirmed, can not be deleted or modified.

![What is Blockchain](https://images.ctfassets.net/23aumh6u8s0i/18cm59kQCwh1sIoAzDok67/388fd1ccc5a01ca53afd1be28c26350b/blockchain01.jpg)

The image above gives a visual representation of the Blockchain so you can grasp the concept. We can see it as transactions grouped in blocks, which are chained together. The chain is a logical pointer from one block to the previous one.

As it would happen with any peer-to-peer network you might use in the past for sharing files, the same concepts apply to Blockchain. It does not have any central authority or server, and all the participants in the network replicate the ledger of transactions. The participants who join the network for submitting transactions or reading data from the ledger are known as Nodes. Last but not least important, most Blockchains are public, making the information on them available on-demand for everyone and representing a single source of truth.

## Transactions and Blocks

Transactions and blocks are the first citizens of any Blockchain. Transactions are atomic operations that modify the state of the ledger. Any user or application is free to join the network and submit transactions, which are later broadcast to other nodes for validation and execution. Those transactions can be executed in different manners depending on the Blockchain they run. For example, Bitcoin primarily allows transactions to transfer funds between accounts. It supports scripting, but it is limited. Ethereum, on the other hand, and as we will see later in this article, enables transferring funds and running programs.

The sample below shows how a transaction might look like,

![Transactions and Blocks](https://images.ctfassets.net/23aumh6u8s0i/6tG1aVYW2RiQkwKKpp2sJG/389fc3793951f7b2a2e4c75ae3a7a073/blockchain02.jpg)

We can not finish this topic without mentioning one of the common denominators for all the Blockchain implementations, Public Key Cryptography (PKI). A pair of private and public keys gives us the ability to send and receive transactions in a Blockchain network. We use private keys to sign transactions before submitting them to the Blockchain. On the other hand, we can derive public keys from a private key and share them with other users or applications to receive transactions. As public keys represent a chunk of bytes, it becomes difficult for us to share them. For that reason, every Blockchain uses different encoding techniques and algorithms to convert them first in a user-friendly string. That encoded string is called "Address".

Another important aspect related to the private/public keys is that changing the state of the Blockchain implies a cost, which anyone sending transactions must pay somehow. Every Blockchain uses native currency or tokens to express how funds are rendered (for instance, we will see Ether in Ethereum, Bitcoin in Bitcoin, or ADA in Cardano). We can not simply send arbitrary transactions to a Blockchain; we also need to have tokens associated with our private key for paying that cost.

Now it is time to ask yourself the following question; how can I get tokens in the first place to start operating in a Blockchain?

Like any country in the world that uses a Treasury Department for printing fiat currency, Blockchain relies on particular nodes, the Miners, for token emission. Miners run a decentralized strategy known as "Mining," which consists of validating transactions, grouping them in blocks, and adding those to the ledger. It's expensive in terms of energy consumption. For that reason, Miners get paid by collecting fees and rewards (issued by the network in small coin fractions) after they successfully add blocks to the ledger. However, before doing anything, Miners must ensure they will get that pay. That's where a particular transaction called "Coinbase" enters the scene. A "Coinbase" is no other thing than a check, pointing to a Miner's public address. If a miner could speak, they would say, "I did my job already, so pay me here." It is the first transaction added to any block by a Miner.

The chicken and egg dilemma. If a Miner wants to get paid for committing blocks into the ledger, how do they get paid when a Blockchain network starts running and no tokens are available? The secret is on the rewards or tokens issued by the network. The first blocks committed to any Blockchain only have Coinbase transactions, which pay Miners with rewards. Miners later transfer those funds to other users, making tokens start flowing through the network.

What I said before about tokens being associated with a private key is technically correct, but not how it works in real life. Tokens never leave the Blockchain. Instead, they are associated with a pointer to public addresses. Since those public addresses can only be derived from your private key, that is how you prove the tokens belong to you. The same thing happens when you want to pay for a transaction or transfer tokens. The private key that you use for signing the transaction is also used to derive the public keys where the tokens are.

[Read more...](https://auth0.com/blog/101-smart-contracts-and-decentralized-apps-in-ethereum/?utm_source=content_synd&utm_medium=sc&utm_campaign=ethereum)