Smart Contract Modelling

Baah Kusi
7 min readMay 26, 2023
Photo by Clint Adair on Unsplash

There’s nothing smart about a smart contract is what you might probably say after reading this post. However, one can’t help but marvel at the kind of technical ingenuity that goes into building smart contracts. I’m going to take some real-world examples to demonstrate the thinking that goes into designing and building smart contracts.

Runtime Environment

There are different job descriptions for developers, here’s a non-exhaustive list.

  1. Web Frontend Developer
  2. Backend Developer
  3. Mobile Frontend Developer
  4. Smart Contract Developer
  5. Full Stack Developer

The common thing about all these roles is that they are all writing code. One big differentiating factor is where their code will finally end up running. This is the runtime environment. Understanding your environment as a developer is key to being able to play your role. One sure way to improve upon your skill is to get familiar with your runtime environment more and more. So if you are a mobile developer, get to know more about phone devices and what is possible within them. A web developer should strive to understand the ins and out of how a browser works. This would make you more competitive in your field.

When it comes to smart contracts, the runtime environment is the blockchain. If you are already a software developer in another field, then the only thing preventing you from becoming a smart contract developer is your lack of knowledge of the blockchain and how it works.

Understand the blockchain and you can easily apply your existing programming knowledge to write smart contracts.

My goal in this article is to make you comfortable with the blockchain environment and get you to write smart contracts in no time. You will understand why smart contracts are ideal for some real-life scenarios. It's important because not all applications should be implemented on the blockchain.

Programming Language

Depending on the environment you’re working in, some programming languages are most commonly used. For instance, a web developer is mainly limited to Javascript. Backend developers do have a multitude of choices to make, from Java to Javascript, PHP, etc … Similarly, on the blockchain, you are limited to a few programming languages you can use for writing smart contracts. The most common one is Solidity which is supported by most blockchains. Rust is also another language that has seen a rise in its usage as a smart contract/blockchain programming language. In this article, I will use both in my examples for the various blockchains.

Programming language should not be a barrier, you just need to understand your environment and know what you need to do.

I will not dive into the ins and out of the programming languages I will use in this article, but things should still be clear to you as daylight. After reading the article, you can later familiarize yourself more with the programming languages. Check my article on how to quickly learn a new programming language https://medium.com/@baahkusi/learn-a-new-programming-language-in-a-day-or-three-7aaadd534780.

Problem Statement

To be more practical, I will use this problem as an example throughout the remainder of the article to explain my points.

Ghana has two important systems in place;

  1. Ghana Card — a unique identification card for every Ghanaian. It is an identification system that assigns every Ghanaian a special code.
  2. Ghana Post GPS — a system that assigns a unique code (address) to every 100² m plot of land in Ghana. An identification system for lands.

We want to develop a simple land registration system on the blockchain where we assign a land address to the owner. Here’s a quick list of features it should have.

  1. Allow an admin to assign the ownership of a unit of land to a Ghanaian.
  2. Allow an admin to change the ownership of a unit of land.
  3. When the system is queried with the ID of a Ghanaian, it should return all the lands owned by them.
  4. When the system is queried with the address of a unit of land it should return the Ghanaian who owns the land.

Let’s name our project GhanaAsaase

The need for blockchain and smart contracts

Before moving on, I would like to discuss why would we even need to use blockchain and smart contracts for this. The need for blockchain always comes down to its core features;

  1. Decentralized
  2. Transparent
  3. Immutable
  4. Secure

It is important to note that before the invention of blockchain, there wasn’t any software system that could technically meet all of these requirements.

And so when trying to validate the application of blockchain for a particular use case, you just need to access it if the use case requires these features. In the case of our problem statement, it makes sense to use blockchain.

  1. Everyone with access to the blockchain can quickly and easily tell if a piece of land is owned by someone else. This will solve a lot of land litigation issues.
  2. Only the people with the right access can change ownership of the land. This is something that could be done by the right authorities in the country.
  3. No hacker can change what is recorded on the blockchain without the right access. Even if it happens, it will be transparent on the blockchain and can easily be noticed by all stakeholders.

Privacy and Security

I would like to make an important note that the problem statement does not focus on privacy and security. This is for demonstration's sake. If we were developing this application for real life we would need to put in place several schemes to ensure better security and privacy of landowners. Here is a non-exhaustive list of ways to improve privacy and security.

  1. Use the hash of the Ghana Card ID on the blockchain instead of the actual Ghana Card ID. This way unless I know exactly the Ghana Card ID of someone, I won’t be able to tell what they own just by looking on the blockchain.
  2. Use Multisig so that multiple admins will need to sign before the land is assigned to someone on the blockchain.
  3. When transferring land, make sure that the current owner of the land also provides their signature before the transfer can go through.

These are just a few additions we could make to improve the privacy and security of the smart contract. However, we won’t be implementing these in this article. Maybe after finishing the article, if you are feeling motivated you could add these to your implementation. I would love to see what you come up with so definitely let me know if you end up doing something.

Modeling

Breakdown of a Smart Contract

A smart contract is made up of code and data. By data, I refer to data structures persisted by the blockchain for that smart contract. The code is the logic to manipulate this persistent data.

When you deploy your code to the blockchain a unique address is generated for the smart contract. With this address, you can directly interact with the smart contract by creating a transaction that calls a function in the smart contract and provides all the necessary input. More practice on this later, but it is as straightforward as it sounds.

The code of the smart contract is organized into functions. Each function defines its own inputs and can be called separately.

Model

Now we model our problem statement. First, we define the functions that we need to expose. Note that we are only creating a general model of the problem, we are not writing code in any programming language yet. I use typescript to create the model because it has a fairly comprehensive type system that just makes sense for everyone.

abstract calss GhanaAsaase {
abstract assignOwnership(ownerId: string, landAddress: string);
abstract changeOwnerdhip(landAddress: string, oldOwnerId: string, newOwnerId: string);
abstract getOwnerLands(ownerId: string);
abstract getLandOwner(landAddress: string);
}

Next, we define the data structure we are going to use to store the necessary data. The following data needs to be stored;

  • Land Address and associated owner
  • Owner and all lands they own.

The data definitions would look something like this;

type LandOwner = Map<string, string>; // a map where key is landAddress and value is ownerid
type OwnerLands = Map<string, Set<string>>; // a map where key is ownerid and value is a set of landAddresses owned by the owner
type Admins = Array<string>; // a list of admin addresses used to validate admin actions

The LandOwner data structure makes sense to have since it assigns each land to an owner. The debatable one is OwnerLands. It seems we are duplicating data. However, this is to allow us to meet the requirement of being able to query all lands owned by a single owner. There might be different ways to deal with this but we would not go into that discussion in this article. It’s a tradeoff we’ve made to ensure faster queries but it increases the storage used.

Now with our model, we are ready to implement our smart contract.

Implementation

We will now implement our smart contract on three different blockchains.

  1. Ethereum using solidity.
  2. Near using rust.

You can find the full repo here https://github.com/baahkusi/ghana_asaase where I will keep on adding several other blockchains whenever I get the time. Feel free to make a PR to add your own blockchains as well.

Ethereum (Solidity)

Solidity can run any EVM-compatible blockchain, hence this smart contract can also be deployed on other blockchains aside from Ethereum. To name a few; polygon, celo, Binance Smart Chain, etc …

Near (Rust)

Deployment & Interaction

In this post, the goal was to give you a very personal feel of how to model and implement smart contracts. The next step after having coded your smart contract is to deploy it onto the blockchain and interact with it from apps. I will create a future in-depth tutorial about that depending on how many people ask. In the meantime, you can find helpful resources on the web or read their docs.

--

--

Baah Kusi

Blockchain Architect | Distributed Systems Engineer