Writing a Plutus Smart Contract

From One Layperson to Another

Ron Hill — [DECNT]
5 min readAug 3, 2021
General Smart Contract Process Flow

I’d like to share what I’ve learned on my journey thus far and how I would approach putting together an idea for a smart contract.

Before we begin, I’d like to start by saying I’m writing this in the capacity of someone with:

  • No prior experience working in the blockchain industry
  • No programming experience in a professional setting
  • Never created a DApp or even an app before

With that being said, I do have a career in systems engineering, quite a lot of experience in the business world and more than a few years sunk into the blockchain space.

Thus, what I hope to demonstrate in this article is how anyone with an of understanding of process flow and the basics of business (contractual agreements) can put together a draft for a smart contract.

Smart Contracts — Simply Explained

Right now, for any digital exchange of money, goods, or services we must rely on third party applications or entities to facilitate the transactions as well as settle any disputes that might occur if the outcome did not happen according to the consensual agreement.

To illustrate this, I’ll use a popular example of running an auction. The platform eBay is widely used to facilitate consumer-to-consumer or business-to-consumer sales through it’s website. There can be online auctions set up by someone looking to sell an item and those that wish to buy that item can place their bids during the auction. Both the buyers and sellers trust eBay to ensure when the bidding process is complete the exchange of payment and item will happen satisfactorily. To make this happen eBay primarily uses a popular online electronic money transfer service called PayPal.

In this case, the buyer wants to get paid the amount agreed to in the auction and the winning bidder wants to receive their item. The seller will send the item once the winning bidder makes their payment on PayPal, which will be held and not paid to the seller until the successful delivery of the item.

Without going into a lot of the details simply put, a smart contract could replace the need for eBay and PayPal — the third-party intermediaries we must trust to facilitate a transaction after validating the requirements of an agreement in a contract are met.

Just like the pictograph above shows there are three main steps to a smart contract

  1. Deploying the smart contract with the terms and conditions written in the code.
  2. Events take place with resultant outcomes according to the specified conditions in the smart contract.
  3. Automatic execution of the contract code and value transferred to the relevant parties based on terms of the contract ending with the transaction(s) being validated and recorded on the blockchain.

There are many benefits to using a smart contract over a centralized third-party service, but a few of the main features that make them superior are:

Self-executing, Self-enforcing Contracts —

They are controlled by the explicit terms and conditions laid out within them. The terms are fully visible and transparent to all parties with no way to dispute them once the contract is established. With guaranteed outcomes this could potentially reduce or eliminate the need for litigation and courts as by using a self-executing contract parties commit themselves to the rules and determinations of the underlying code.

Immutable —

The smart contract cannot be changed by anyone after being deployed. Essentially, tamper proof code that will not be modified behind someone’s back.

Distributed —

Output of the smart contract is validated by everyone on the network. The validator nodes will all determine if the transaction outputs are valid and either agree to pass it on the blockchain or not.

Thinking Through The Idea First

Before developing a smart contract it is a good idea to have a structured thought process around the initial idea(s). Below is a mind-map that has been started to help with this process.

As we learn our way through developing valuable smart contract solutions we can add to this work-in-progress mind map.

Before The Code Is Laid

Not all ideas should be put on the blockchain. It is important to first understand the purpose of your process and what it will accomplish. Once that is determined then one can decide if it necessitates living on-chain.

The following are some criteria to consider for determining if your idea warrants being put on the blockchain — please note, this is not a comprehensive list and still fleshing these out to serve as a guide:

  1. Trustless situation for exchange
  2. Peer-to-Peer removing intermediaries
  3. Self-custody of data — not relying on a central server to hold your data
  4. Use of a decentralized token
  5. Decentralized authentication

Mapping the Process Flow

When putting an idea together for a smart contract one must first map out how the process will work, who are the actors/ users, what they will be doing, and what the outcomes will be.

To start this process we must first have some understanding of the (E)UTxO model and how this works. Check out this video for an excellent breakdown of the model, which starts on the third timestamp. Feel free to continue the series of Plutus Pioneer program courses if so inclined. ;)

The (E)UTxO-model — Third Timestamp

If you’d rather read about it here is a great article by the same person, which covers (E)UTxO and more about Plutus smart contracts.

Some questions to begin with are:

  • What are the endpoints in the smart contract, i.e. wallets/addresses, scripts, etc.?
  • What will exist off-chain and what on-chain?
  • Is external off-chain data needed — oracle solution required?

Placeholder for example of mapping out a smart contract.

Creating the Smart Contract

Having a great idea for a smart contract and mapping it out can be enough if you do not possess the technical know-how to write it yourself. While the inventory of available developers is limited at the moment that is quickly changing as thousands of enthusiastic and talented individuals are moving through the Plutus Pioneer program, now currently with the second cohort of participants in progress with over two thousand individuals registered this time.

After the smart contract process flow is mapped out the next step is determining the components needed to bring it to life. There are already a lot of great open source tools being built and libraries for others to use that could help with a smart contract being developed and drastically reduce the workload required to build everything from scratch.

Once the necessary components needed to be built out are determined the really fun part begins. Whether you will embark on building the smart contract yourself or employ others to make it happen is a personal choice with some obvious advantages/ disadvantages for either.

Placeholder for example of deploying and running of simple smart contract once they’re live!

--

--

Ron Hill — [DECNT]

Engineer, musician, artist, creative entrepreneur. Father loving life & making the world a more decent reality for all by building a Decentralized Reality.