Javier Casas

A random walk through computer science

Plutus Smart Contracts I: Understanding Cardano and Plutus

The Cardano blockchain

Cardano is a desentralised public blockchain that seeks to deliver more advanced features than any other blockchain constructed previously. It is based on the Outboros Proof of Concept: the first blockchain that has been researched, analyzed, peer reviewed and proved to have good properties. Cardano is resistant to most known attacks, and not only is resistant: there is an actual proof of its resistance, instead of just claims of it to be strong.

The coin that Cardano offers is called Ada. There will be a total of 45,000,000,000 Ada available, and each Ada can be divided in a total of 1,000,000 Lovelaces.

Why do we need another cryptocurrency? The main current contender is Bitcoin, but Bitcoin is based on Proof of Work. The bad part of Proof of Work is that mining blocks, required for securing the blockchain and admitting new transactions, requires electrical power proportional to the people mining for blocks. As the result of the success of Bitcoin, it is estimated that all the Bitcoin miners are using more electricity than some smaller countries. All this power cost is everything but good for the environment. Cardano is based on Proof of Stake, which is an alternative mining method that allows creating blocks by people who have a big stake in the system - hence Proof of Stake.

Cardano is being developed by:

  • IOHK: a software consultancy in Honk Kong that has hired some of the strongest Haskell developers in the world.
  • The Cardano Foundation: it is an entity that is working on connecting partners that want to develop on Cardano across the world.
  • Emurgo: the main people working on driving the adoption of Cardano.

Anatomy of a transaction

Each block is effectively a transaction in the blockchain. Essentially, it aggregates a bunch of unspent inputs, with their corresponding signatures, and converts them into a bunch of outputs to new addresses that spend the inputs provided.

So we have two main concepts:

  • Transaction input: It's a reference to an unspent transaction output from a previous block, accompanied with a signature proving that the transaction input can be spent.
  • Transaction output: It's a new unspent transaction, annotated with the current block, that allows the owner of the new address to further spend it later.

In some sense, you don't own certain amount of ADA in the Cardano blockchain. You own several unspent outputs in different blocks of the Cardano blockchain, and you can spend them for purchasing goods and services.

Because you spend a full transaction input, it's very common that you create two transaction outputs from a single transaction input: one output paying whatever you agreed to pay, and the other paying the rest back to your own address.

On each transaction you convert some unspent inputs into other unspent inputs. How do we get the first unspent inputs? The Cardano foundation has sold the first initial unspent inputs in a fundraising campaign for the whole project. So part of the initial 45,000,000,000 ADA has been sold, and the rest will be created through minting: a process that gives stakeholders some extra ADA.

Plutus

Plutus is a programming language for writing smart contracts in the Cardano blockchain. It is based on Lambda Calculus and Haskell, and looks to inherit the most interesting features from both camps.

Plutus is based on Haskell, and uses Template Haskell to allow you to write Plutus pretending it's Haskell. You surround your Haskell with quasiquotes, splice in entries from the Plutus prelude, and write plain old Haskell.

But not everything is plain old Haskell. For starters, Plutus is strict, unlike Haskell, and infinite data structures are obviously out of the game. You have to use one of the few Plutus data structures, or lift your own data structures to Plutus. The same happens with functions: there is a Plutus prelude and builtins, and you should use them instead of the Haskell ones. Finally, errors and debugging in Plutus is very annoying, at least for now. If your smart contract doesn't compile, you will usually see it as a massive error of doom, with a small indication of what went wrong on the top of it. If your code compiles, then you have to check if it runs properly. Printf-debugging is quite different here, so if you rely on this as your main tool, you are up for a surprise.

Smart contract

A smart contract is a Plutus program that runs on the blockchain. But why would you like to do this? The real reason for a smart contract is creating arbitrary programs that decide how money moves, but without requiring trusted computers.

Technically, the main Plutus smart contract is the validator script. It can help define how a transaction can be spent. You pay to the address of a smart contract, and the only way to spend the ADA you paid will be by obeying what the smart contract demands.

Next steps

On the next articles, we will get to learn some of the inner details on how Plutus works and how to construct smart contracts.

  • Are you looking for a way to build your dApp on Cardano?
  • Do you need someone to kickstart your next product using Plutus?
  • Contact Javier Casas
Back to index
Copyright © 2018-2023 Javier Casas - All rights reserved.