A Byte of Blockchain - Week 47
Smart Contract - An Introduction

A Byte of Blockchain - Week 47 Smart Contract - An Introduction

·

4 min read

Recap

Last week we discussed Transaction value & Data fields in an Ethereum transaction data structure. The transaction value is nothing but the amount of Ether that is sent through a transaction.

Then we explored Data field in a transaction. If a transaction involves only a funds transfer between two EoAs (Externally Owned Accounts), then this field is irrelevant. Why? Because there is no activity between two wallets other than sending & receiving funds. However, when a transaction is for a smart contract, then the value in this field interacts with the smart contract through a function call & executes the smart contract. We also explained a function call with a simple example using python programming language.

Smart_Contract_Function.jpg

Today let us understand the basics of a smart contract.

Smart Contract - An Introduction

As explained above, when there is only value & no data in an Ethereum transaction, it is normally a payment transaction addressed to an Externally Owned Account (EoA) because there is no activity between two wallets other than sending & receiving funds. Hence, there are no conditions to be checked & satisfied.

The value in the data field is irrelevant in such a transaction. But what if there is a value in the data field in a payment transaction? Most wallets & the Ethereum blockchain ignore any value in the data field of a transaction.

So, that means if there is value in a data field of a transaction, it is intended to be addressed to a smart contract. What is a smart contract?

Smart contracts are nothing but computer programs which run on the Ethereum Blockchain.

These contracts run automatically when certain condition(s) are satisfied. The instructions in these programs are developed using a programming language.

As usual, let us take a step back & understand what is a contract.

A contract is an agreement :

1. Between parties

2. creating mutual obligations that are enforceable by law

In simple terms, contracts are promises that the law will enforce. (Source : here)

Thus, there needs to be

  1. two or more parties

  2. some arrangement between them which has their assent &

  3. which requires them to do certain action(s).

If one or more of them don't perform the required action(s), then they can take recourse to the law of the land for compensation or to ensure agreed action is implemented by all parties.

Now that we have a basic understanding of a contract, let us now step forward to smart contracts. The term "smart contract" was coined by Nick Szabo.

When the terms & conditions of a contract are turned into computer code that automatically executes depending on those conditions, that code is a smart contract. The word "contract" in the context of a smart contract has no relevance or legal meaning.

A very common & famous analogy of a smart contract - is a vending machine. Let us see how.

pexels-deybson-mallony-5196187.jpg

When we go to a vending machine,

  1. We select the required item (assume it is orange juice)

  2. Pay the required amount (assume it is $10)

The vending machine then dispenses the orange juice if it is available & the correct amount ($10) has been paid. If the required item is not available or wrong amount has been paid, the cash will be returned.

So the logic of the relevant section of the code will be :

if orange_juice > 0 and cash = 10:
        dispense orange_juice
else:
        return cash back

So, the vending machine executes the "contract" if the relevant conditions viz.,

  1. Availability of the item (viz., orange juice) &

  2. Correct amount (viz., $10)

are satisfied, otherwise it returns the amount back.

If_Then_Else.jpg

Thus, smart contracts work by simple If then... else instructions written into code & deployed on a blockchain. If the conditions are satisfied, the code is executed.

The above execution is done automatically without any human intervention at all and the outcome will always remain the same for a particular condition which means a smart contract is deterministic.

Once deployed, the code forming the smart contract cannot be changed which means it is immutable.

More on smart contracts next week.