Smart Contracts: A Beginner’s Guide

Miguel Palhas
UTRUST
Published in
6 min readMar 12, 2019

With all the blockchain hype, one concept that gets thrown around a lot is that of Smart Contracts.

But what exactly are these contracts, and what makes them smart? And if you’re no cryptocurrency expert or computer science engineer, should you care? This post aims to shed some light on what a Smart Contract is, mostly from an end-user perspective.

Before we start, let’s dive into some key concepts:

What’s a transaction, really?

When hearing the word “transaction”, most people will immediately think of money. It’s when you give money to get something in exchange (or the other way around), whether that happens through a credit card, paper money or, more recently, with cryptocurrencies.

Actually, that’s a special kind of transaction — the so-called Financial Transaction.

But there’s a more general definition of a transaction, which is what we’ll try to explain in this section:

An atomic update of state

It may sound a bit technical, but it’s actually relatively simple to understand.

When you purchase a 3$ cup of coffee, here’s what happens:

  • You end up with 3$ less than what you originally had;
  • Starbucks ends up with 3$ more;
  • You now own a cup of hot coffee.

These are all the things that change from the moment you request a coffee at the counter, and the moment you receive it. In this example, “state” means everything each entity owns. An update to that state happens every time you give or receive part of it.

All 3 things happened as part of one transaction. If any of them had failed (e.g.: you forgot your wallet at home), none of them would have happened. This is the “atomic” part. As many actions as it may entail, it’s an all-or-nothing deal.

Transactional systems are exactly that: they ensure multiple dependent operations happen in an all-or-nothing manner.

Blockchains are transactional systems. It is actually mathematically impossible for you to send Ether to someone without that same balance being deducted from your own account.

The same thing also holds for traditional banking systems. But those rely a lot more on coordination between multiple financial institutions rather than the mathematical rules built into blockchain protocols.

Ok, so what?

We’ve established what a transaction is from a technical standpoint.

Now, to understand what a Smart Contract does, let’s consider a very simple example: a Rock-Paper-Scissors game.

Or rather, a version of the game where two players bet some money and choose their move. After both players have done so, the winner of the round takes all the prize money.

There are 3 different moments to this game:

  1. Player 1 chooses his move, and bets the required amount;
  2. Player 2 chooses his move, and bets the required amount;
  3. Both players make their move and the winner takes the prize money.

These are 3 state changes (or 3 transactions), and each of them should be atomic. You don’t really want a player choosing a move without placing his bet, as he wouldn’t have anything to lose then. It’s also interesting to tie together the winner selection and the award, to prevent a fraudulent player from claiming it.

The system we just pictured is exactly what a smart contract allows us to do: take advantage of a blockchain’s transactional capabilities and implement more than just money. In fact, in Ethereum, we can code entire programs into the blockchain. The distributed ledger works as our storage (where we keep track of everything we need), and a Smart Contract is the code we write, and rules we define for our program.

And from all this, kittens are born.

So… it’s a computer program?

Yes! But one that runs on a blockchain. Interaction is done either by sending transactions to it or by reading the blockchain’s state to assess the current status.

In our Rock-Paper-Scissors example, we can at any point query the contract to know if either player has made its choice already. But to actually chose a move (that is, to change the state and move the game forward) we need to actually send a specially crafted transaction.

This transaction, rather than saying “send X Ether to the address 0x1234….” will instead specify “I’m sending 1 Ether to the contract, and my move is Scissors”

Wait, are you sending Ether to a contract?

Again, yes!

In Ethereum, smart contracts have pretty much the same privilege as regular users. Except that instead of free will, they can only perform whatever instructions were coded into them.

At least until they become sentient… ⓒMarketWatch photo illustration

This means that Smart Contracts have an address, can receive, hold and send Ether and ERC20 tokens, and can interact with other Smart Contracts.

Why are they so different from regular programs?

Not only their state is stored in a blockchain, but the code for the contract itself is as well.

Creating a Smart Contract is actually done by sending a transaction to the Ethereum network, where the transaction data is the code for the new contract.

So the contract itself is bound by the same rules that make blockchains interesting. Mainly, they’re immutable and public for everyone to see. This means they cannot be changed or tampered with by any third party, and that anyone (with the required technical knowledge) can audit them and ensure they do exactly what they’re supposed to.

This effectively allows us to securely interact with others without even trusting them. Trust is abstracted away and ensured by the blockchain.

Cryptocurrencies allow us securely and confidently use a financial system with no central authority.

Smart Contracts can potentially enable those same benefits for a lot of other systems.

In layman’s terms, they allow us to play Rock-Paper-Scissors without fearing our opponent will cheat, and peek at our hand before playing.

Speaking of cheating (it’s only fair to wonder)…

If blockchain data is all public, couldn’t my opponent do just that by querying the blockchain? Well, that’s where cryptographic hashes and signatures come into play. A topic to expand on a future post, if interest calls for it.

Ok. What’s the catch?

Of course there is one. And a big one, actually.

If all rules are coded into a Smart Contract, and no one can change them, what happens if they’re wrong?

Well… we’re screwed?

Programmers make mistakes all the time. It’s what causes your phone to crash occasionally, or that website to take too long to load. Those are harmless, and often easy to solve.

When programs deal with people’s money, though, tolerance is much lower. And when those mistakes are irreversible by design, then it’s much worse.

It can happen that very subtle mistakes can lock away large quantities of money in a Smart Contract, or even make it vulnerable to attacks. This is exactly what happened with the famous DAO Hack back in 2016. A Smart Contract bug allowed an attacker to steal the token sale funds from the organisation. The eventual community disagreements about how to deal with such a large theft are also what led to a hard fork, and the creation of Ethereum Classic.

It should be noted, however, that Smart Contracts and everything in the blockchain space, really, is still in its infancy. We should not take these kinds of problems lightly, as they cause real issues for real people, but we should also weigh them against the fact that this is an immature technology and there’s still a lot of room for improvement.

Want to know more?

This was an extremely superficial view of a rather extensive subject.

If you’re interested in knowing more, both as a cryptocurrency investor, or as a tech-savvy blockchain enthusiast, there are plenty of resources out there.

If you’re looking for reading material, anything by Andreas M. Antonopoulos should be worth looking into. Particularly his two books, Mastering Bitcoin and Mastering Ethereum, are great whether you’re looking to learn more as a user, or as a developer.

The free Ethereum 101 offered by B9Lab is also an excellent resource if you’re looking for a tech-oriented introduction to the subject of Smart Contracts and the Ethereum blockchain.

The example of a Rock-Paper-Scissors game also wasn’t given at random. While finishing the Ethereum Developer Course, this was one of the proposed exercises. I’ve implemented a Rock-Paper-Scissors-Lizard-Spock Smart Contract, which you can check on my Github account if you’re interested in looking at the actual code for a Smart Contract.

--

--

Miguel Palhas
UTRUST
Writer for

Professional over-engineer @Subvisual. Building the future of payments @UTRUST. Pathologically sarcastic. Will one day go to outer space.