- Blockchain: Architecting Trust in a Trustless World
- What are Smart Contracts
- A little history on Zilliqa
- Why Scilla?
- Variables and Datatypes
- Contract Structure
- Wrap Up
Architecting Trust in a Trustless World
Blockchain technology has the potential to revolutionize our world. At the core of blockchain, is the concept of a transaction. What really is a transaction? Depending on who you ask, you might get different answers. Ask an accountant, and you will get one answer. Ask an economist, and you might hear something along the lines of “It’s when a buyer and seller exchange goods, which ultimately creates value between the two”. We live in a transactional world, and our lives are filled with making transactions. When you buy a watch or a pair of a pair of jeans, that was transactional in nature, creating an exchange of value between two parties. Information processing is also transactional in nature. Harry sent an email to tracy, or you got a notification that Tanya liked your twitter post, etc. those are transactions. So as you can see, we have been transacting for a millennia! From the barter system era, to a world now where we do transactions over the internet.
Transacting with each other, however has brought about its problems over the years. In the case of purchasing the watch or jeans for example, how do you know that the item you are purchasing is genuine? Or that it wasn’t stolen? If it was a painting, how do you know it is an original, and signed by the actual painter? Being able to have traceability and history of an asset when dealing with transactions of value is essential. This is what blockchain is about and tries to solve. With transparency as one of its keys features, it has the potential to solve the issue of trust. If you are able to have insight into an asset before transacting, then you can trust it, because you can verify its authenticity.
What are Smart Contracts?
One of the key concepts that power blockchain technologies, are smart contracts. Smart contracts provide a new way to help remove the issue of mistrust between parties when conducting a transaction. In the real world, we rely on third parties to perform these “trust” actions. Escrows, brokers, lawyers, etc. act as middlemen when conducting transactions with others. Not sure whether you will be paid after you do some freelance work? Why not get a lawyer involved or perhaps an escrow? Maybe a third party website helps facilitate that you get paid when you do the work. We trust third party entities to perform these role. But what if the third party is corrupt? What if the person you are transacting with has made a deal with the escrow? And you are never paid your money because the escrow wont verify your end of the deal was met. As you can see middlemen as subject to corruption. Smart contracts help solve the issue of trust on blockchain networks. Below is an illustration of two individuals transacting via a middle or third party operator. Like we said before, you cannot fully trust middlemen.
A smart contract (also known as Smart Code or Chain Code) is a piece of code running in a blockchain that is executed when a condition is met. As long as the blockchain itself remains fully functional, the smart contract cannot be altered or stopped. Like getting into a contract with another people, once signed, certain conditions must be met. Perhaps the contract says, if Gary doesn’t pay you by months end, then you are obliged to cancel the contract, etc. Within a smart contract you would write computer code that outline the conditions that need to be meet. Yup! All in code. What if someone does not fulfill their obligations, what happens then, etc etc. All of these need to be written in code. As you can see this is very powerful and disruptive to entities that provide middlemen service. Not only are these smart contracts going to execute just like the third party entity, it does so in a manner that is not centralized and prone to manipulation. Some qualities of smart contracts are that they are immutable, trustless, disintermediated, automated and between two or more parties. In the illustration below, the third party is now replaced with code (smart contracts to the rescue). A smart contract takes care of everything, and we can trust that the code will do exactly what it was programmed to do. Which can be verified by all.
Now that we know what a smart contract is, lets slowly get into the topic of this post. There are many blockchains in existence today. One of my favorites is Zilliqa, a high-security, high-performance blockchain platform. There are many other blockchain solutions out there with smart contract capabilities, such an Ethereum, Cardano, Tezos, Cosmos, etc. We will be focusing on Zilliqa in this series. So why did i choose Zilliqa and not any of the other blockchains mentioned? In order to answer that question i think its important to give you a brief history of the Zilliqa blockchain. It is worth hearing this history as to why and how Zilliqa come into being, then you will understand why i chose to write this tutorial series. But feel free to skip by clicking on the section your want in the table of contents at the beginning of this post. But i promise it wont be boring, so do reach this short section.
A little history on Zilliqa
Zilliqa was confounded in 2017 by a group of computer scientists, researchers and entrepreneurs at NUS (National University of Singapore). The team was researching blockchain from a scalability point of view. Why was that? If you are new to the blockchain or cryptocurrencies, 2017 was the year of the ICO (Initial Coin Offering) boom. Many ICO’s were founded in that year. Most of these ICO’s didn’t have their own blockchains (or were in the process of creating one), and so ended up running their projects on the Ethereum blockchain network. So here we are with thousands of blockchain projects running on the Ethereum network. What happened? A congestion of the network. The Ethereum network couldn’t handle the volume of transactions being made by all the projects runnings on it. It was around this same time that the Zilliqa team was researching on how to solve this problem. A research paper on the findings was born detailing how to solve the scalability issues (using a technique known as Sharding) faced by blockchain projects such as Ethereum. This is what eventually became Zilliqa. It is worth knowing that as of the date of writing this post, Ethereum is trying to implement their own version of Sharding (state sharding) in its version 2.0. It still faces network congestion, especially with the reach of Defi (decentralized finance) running on the chain. There were two things that Zilliqa wanted to solve ultimately. The first, which we have talked about, is the problem of scalability as the volume of transactions on the network increased, and the second was to improve some of the issues with smart contracts that the world has seen with existing blockchains. So what smart contract issues are we talking about? One of them was the DAO Hack, where a substantial amount of money was stolen from an ethereum smart contract (the contract was hacked). There were many other such incidences where smart contracts code were exploited, resulting in hundreds of millions of dollars lost. Another exploit, was the Parity Multi-Sig Contract hack. In order to solve this problem, Zilliqa developed their own smart contract programming language called Scilla. Before Scilla, the most popular smart contract language was Ethereum’s Solidity, which makes sense because most ICOs were running on Ethereum, giving users no other choice but to use it.
Smart contracts are an integral part to blockchain technologies. Scilla gives us the opportunity to write bug free smart contracts that are hacker proof. Now i must warn you, the security and safety features the Scilla language offers, come with its pros and cons in my opinion. The pros being you get to deploy smart contracts that are highly secure, safe by design and is verifiable! By verifiable, it means you can formally guarantee that a contract written in Scilla does exactly what it was intended to do and nothing else. Isn’t that powerful? The Scilla programming language is functional in nature, as such i personally dont think its “easy” to pick up for beginners (not coming from functional programming backgrounds). Functional programming design principles allows it to be more friendly towards static checks and formal verification. BUT! that is why you are here, so you can learn to build your own smart contracts on the Zilliqa blockchain, and in a way that is easy to understand. I want to help make the Scilla language approachable to all levels of developers, but more specially to help junior to mid level developers get a deeper understanding of the language. For more senior and experienced developers however Scilla is “may be” fairly easy (lucky them huh?). How do we make writing smart contract code approachable to all? I have taken on this task, and hope you will join me, by engaging, asking questions and or making suggestions as to how to many makes this series of tutorials awesome for all who decide to learn the language. In order to keep it simple i will sometimes strip out harder concepts to understand and reintroduce it in a later module expanding on the idea. Teach a man to fish, and you feed him for a lifetime is what a great sage once said. You will not learn everything in the series, but you will get enough to move forward and build your own smart contracts.
Variables and Datatypes
A concept that one cannot run away from in any programming language is the idea surrounding variables and datatypes. Consider variables in computer programming as storage addresses with an associated symbolic name that contain some known or unknown quantity referred to as a value. Let me explain with an example. We can have a storage address called “name” storing a value of “Kofi”. So name = “Kofi”. Or we could have an address called “location” storing a value of “Ukraine”. You get the point. In a software program, when location is called it will return “Ukraine”. Now the values of these addresses have something called a type. For example, “Ukraine” is considered a String type, because the nature of it is a String! Consider a variable called “amount” with a value of 10. Now 10 is an integer, so the type of amount is Integer. In Scilla, all variables MUST have a type. Types that are baked into a programming language are called primitive types. By baked in i mean they are part of the core language. You get them by default. If you ever consider building your own type in a language, its not a primitive type. It is important to know the difference. Scilla also comes in with some built in variables. Here are some of them
|Built-in Variable Name||Declaration|
|Current Contract Balance||_balance|
|Amount of Zil sent by sender||_amount|
|Current Block Height||& BLOCKNUMBER|
In your contracts, you can call these built-in variables to access their values. For example, if you needed to get the senders address before doing something in your contract, you can access it direction by using the _sender variable name to get the value. The rest is self explanatory. Lets move on!
Mutability of variables is also important to understand. Variables in Scilla are either mutable or immutability. This should be fairly easy to understand. Mutability is the act of changing something. Variable mutability therefore means the act of changing a variable. In Scilla, variables can be declared as mutable or immutable. Finally ALL variables must be initialized when a smart contract is deploy. This means that when you declare a variable in your program, it must have a default value. Now you may wondering about immutability? Well imagine deploying your own smart contract and you the owner of that contract can be changed by anyone! That wouldn’t be cool would it? So you may want to make whoever owns the contract, immutable to change once the contract is deployed. Its important to note that once a smart contract is deployed, mutable variables defined in the contract can be changed in something called a Transition (more on that in later modules of this series). For those of you that are curious, think of transitions as functions, they define business logic including state changes. For those of you who dont understand this, dont worry at all we will discuss this in full detail later on in the series! We also have the idea of Procedures which are similar to transitions (they are functions). If you have functions that you wanted to be used by multiple contracts, you would write a procedure instead. This enables better code reusability and readability. Again more on this to come, for now i want to bake into your minds ideas that will be coming later on in the series. Lets get back to variables and datatypes.
Below are the datatypes you should expect to find or use when writing your own smart contracts with Scilla
|Integers||Int32, Uint32, Int128, Uint128 Int256, Uint256|
In the above table you will see some of the datatypes we have in Scilla. On the left is the name of the type, and on the right is how you would declare them. There are other data types not in the table such as Map, and Message. I think we can leave those out for now, but its worth mentioning that they do exist. In Scilla you will find datatypes such as Integers and Strings for storing integer and string values, Hash for storing hash values, Address for storing address values and block number for storing block numbers. These seen pretty self explanatory. However what if you wanted more advanced datatypes? For example what if you wanted to store a collection of numbers or perhaps and boolean (true or false value). Beyond Primitives data types, we have abstract / algebraic datatypes (ADT). The abstract datatype is special kind of datatype, whose behavior is defined by a set of values and set of operations. In later modules we will touch deeply on them.
|List||List Type (e.g List Bool, List Int32)|
|Pair||Pair Type1 Type2 (e.g Pair Int32 Bnum)|
Boolean is used to define true or false values in contracts, options are useful for initializing fields where the value is not yet known (think of a field as a variable for now). List is a list of numbers or values (think of arrays if you are coming from another programming languages). We dont need to talk of Natural Number for now. You will hardly use them. A “Pair” value may contain values of different types (like a pair of integers).
Lets talk about how a scilla smart contract looks like. The structure of a contract is very explicit. Below you will find a sample structure. Now i am not expecting you to understand the code, but as a developer you should be able to quickly glance through to get an idea of what is going on. First we declare the version, then the library, then the contract name, immutable variables, the mutable variables , and then transitions/procedures. Please note that this structure is STRICTLY imposed, so dont go about structuring things how you like 🙂 Lets take a look!
Beautiful isn’t it? Here is a table outlining the structure for you.
|Immutable Variables||(owner : ByStr20)|
|Mutable Variables||field welcome_msg : String = “”|
|Transition||transition Name (msg: String)|
There are a few things in this table that aren’t in the code sample. But you can see the order in which the contract is laid out. We briefly spoke about mutable and immutable variables but we haven’t learned how to declare them in a contract yet. We will do that in the next module, however i believe this gives you slight insight as to what is going on. You can see there is a variable name called owner what is immutable and has a type of ByStr20! You can also see there is a mutable variable of name welcome_msg of type string, with a strange field identifier next to it (remember fields ways to declare variables). Finally you also see some transition code. We will be diving deeper in the next module what all these mean.
I hope you gained a very basic insight into Zilliqa smart contract language; Scilla. We have barely scratched the surface, and there is so much more to learn. In part 2 of the series we will be diving deeper into Scilla. We will learn more about declaring variables, transitions / procedures, expressions and statements, and communication (how to talk to other smart contracts and user accounts through something called messages). We will also touch up on emitting events in contracts as well.
Before we take a look at a “hello world” sample, i believe its important to dive into these concepts first. So that is exactly what we will do! Excited? See you in part 2 of this series!