What Is A Smart Contract? And How Does It Work?

If you are trying to educate yourself about NFTs, Cryptocurrencies, and, Blockchain technology then you might have heard of this term called “Smart Contract”.

But what is it? Is it really a contract? If so, how is this a smart one?

Worry not! I am here. Let’s begin the story.

What is a Smart Contract?

The story of the Smart Contract began, even before the invention of a cryptocurrency called Bitcoin in 2008.

Nick Szabo, a cryptographer who is known for his research in digital contracts and digital currency proposed smart contracts in 1994.

In fact, Szabo is often rumored to be the real Satoshi Nakamoto, the anonymous inventor of Bitcoin. He denied the rumours.

Ok, But what is it?

First of all, a smart contract is still like a real contract which is an agreement between the buyer and seller. But unlike a real contract (agreement on paper), a smart contract is just a code that is stored in a blockchain.

It is a program that runs on a blockchain when predetermined conditions are met.

From Ethereum’s perspective, it is a code that will be written using the Solidity language using the if/when & else conditions and deployed on the Ethereum blockchain. All the participants in this contract will be certain about the outcome.

Fun Fact: Do you know? Bitcoin’s smart contract language is Script which is very tricky to use. This is one of the main reasons why Vitalik Buterin designed the Ethereum Blockchain Platform.

Simply put, a smart contract is just a piece of code that you can write and run on the Ethereum blockchain (or other blockchains that support smart contracts) to exchange value without the need for a 3rd party (more about that in the next section).

How Smart Contracts Work + Examples

For example, a smart contract could be programmed to pay the salaries of the employees, every month. How?

Firstly, a smart contract which is just a program has to be written using the if/when – then conditions.

If “employee – mid-level graphic designer” – worked for n number of days, then he should get paid x amount of ETH (Ether – cryptocurrency) on the 1st of every month. Obviously, this is just to give you context. The code is written in Solidity language.

But for the smart contract to get executed, it should receive a trigger or input. So in this example, the trigger can be the data of the employee’s fingerprint registrations via the biometric system.

Now, how does the smart contract receives this “Employee Attendance” data? Well, you need to know about Oracles then.

Oracles are data feeds (it’s a mechanism to receive updated data from data sources) that send and verify real-world information to blockchains if the data is needed to execute the smart contract.

That’s how the smart contracts get the input.

But for the program (smart contract) to run on the Ethereum Blockchain, you need to pay a certain amount called a gas fee.

For any kind of transaction to happen on the Ethereum Blockchain, it takes some kind of energy for the EVM (Ethereum Virtual Machine) and this is the reason why you should pay a certain amount (Ether) to compensate for it. It’s the same concept as how we pay for the gas of a car.

Back to the main point: based on the attendance, the employee gets paid every month.

Another example can be selling a house.

“If you send me x ETH then you’ll own my house”. That’s it. It’s that simple.

No broker, lawyer, or bank is involved. And talking about the proof, every transaction that gets recorded on the blockchain is timestamped. So, there is no need to worry about the conflict.

What’s So Great About It?

There is a reason why they are termed “smart” contracts. Well, actually there are a few reasons that make them smart.

No More 3rd Parties

As we saw in the previous example, no broker is required.

If you and I want to bet against each other on something (like who wins the world cup or what color outfit Kim Kardashian is gonna wear for the MetGala), we both can interact with a smart contract (that is written for this purpose).

Both of our money will be stored in the contract. And the money gets released based on the output once the event takes place.

Kim wore black, so we both lost. The money gets returned to our wallets now.

Simply put, smart contracts eliminate the need for third parties.

Keep calm and trust the code!

Automated & Accurate

Smart contracts get executed automatically (with a piece of code and trigger – oracles) and impartially (everyone is equal before the code). As per the first example, you don’t have to update somewhere that you need your salary on the 1st. The smart contract would know that data via the oracles and will execute the code itself.

Since smart contracts don’t need human inputs once deployed on the blockchain, there is no room for error.

But this is true only if the code that is deployed doesn’t have any bugs and also if the oracle is not compromised (more about that soon).

Immutable

Smart contracts are immutable. And that means once the code is deployed on the blockchain, you can’t change it. Not even the one who has written the code. The technology won’t allow it.

While you might be thinking that it’s a good thing, no one can temper the code and change the contract to his/her side for monetary results.

True, but the downside is if the code has bugs then that can lead to a huge monetary loss. There is currently no way to undo this.

Distributed & Decentralised

Smart contracts exist on a distributed and decentralized blockchain network. Decentralization means there is no such authority that can have control of the transactions that are happening. And that’s what makes smart contracts trustworthy.

Talking about the distributed system, what does that mean is all parts of the system are located in different physical locations. Ok, so what’s great about that? No single point of failure. The failure of one node will not take down the entire system.

Talking about the bet we made on Kardashain’s outfit color, for you to cheat me and win the bet, you need to change the code (that whatever happens, you get the money). For you to change the code, you need to convince at least 51% of the nodes.

Yeah, good luck with that!

What Can Go Wrong With Smart Contracts?

Smart contracts are smart because they execute themselves, automatically. But are they intelligent enough to solve our problems?

Coding Issue

Well, the first thing to know is it’s not possible to change the code (well, it’s probably a feature, right). And that means if there is any bug in the code, that’s gonna create a problem. So as per the above example (salary credit automation), the employees might receive only x/10 salary or no salary at all.

Burden On Coders

Ethereum’s smart contract language Solidity is Turing Complete. And so, it allows us to write more complicated contracts but only at the expense of making them more difficult to analyze. This places the whole burden on the smart-contract writers. A small bug in a complicated contract can lead to a big problem.

And all coders understand that it’s very hard or almost impossible to write bug-free code. And as the complexity of the contract increases, writing a code without bugs is gonna be impossible.

Of course, with all the advancements in web3 tech, this might not be a bigger problem in the future.

Code Is No Longer The Law – DAO Attack

Ethereum is built on the principle of “Code Is Law”. Nobody has the authority to go against the contract.

But after the DAO attack, everyone understood that code is no longer the law. Someone managed to withdraw over $50 million worth of Ether from the fund of $ 150M. Due to this attack, the Ethereum core developers overrode the smart contract.

Have you ever wondered why there are two Ethereums? Ethereum Classic & Ethereum (the one most of us know about). Well, that’s because of ‘The DAO attack’. The one we are using or know about is the forked version.

The Oracle Problem

For the smart contracts to execute, it has some questions (like the employee attendance) and the oracle will answer these questions.

But what if the oracle is compromised? An oracle has the power to control the output of the smart contract. This is referred to as the ‘Oracle Problem‘.

Chainlink has a solution to this problem, and hence might not be an issue in the future.

Conclusion

Can Smart Contracts change the world? Hell Yes!!

Yes, there are issues and money exploits. But so are some of the best minds of our time trying to solve these issues.

Leave a reply:

Your email address will not be published.

Site Footer