Laptop with Cardano Logo on it.

Marlowe-Susu: Building a Simple Financial Contract in Cardano (Marlowe)

A while back I worked with a few friends to build CryptoSusu, our first smart contract on Ethereum. It was basically an experiment to help learn the programming language of Ethereum, Solidity, as well as the intricacies of the platform. As I’ve been neck-deep in Cardano for the past couple months, I thought it would be a good revisit the Susu project, but this time build out the same functionality on Cardano. I was pleasantly surprised by the quality of the platform and am super-optimistic about its potential. I’d like to share that experience with you.

The Susu Contract

Just to review, a “Susu” is a West African and Caribbean saving tradition where a group of people come together to form a saving group (the Susu). If the Susu has 5 members, for example, then the group will meet 5 times. Each meeting, each member will contribute an equal amount to the Susu (say $100) and one member will take the entire pot ($500). At the next meeting a different member will take the total pot and the rotation continues until each member has had a chance to receive the money. Once a rotation is complete, the Susu could disband or continue for another rotation. Because I’m a visual person, here’s a video describing a functioning Susu. (Note the many times where trust becomes a big part in the execution of a Susu. At a basic level, one benefit of executing a Susu on the blockchain is that some of this trust dependency would theoretically go away.)

Building the Susu on Cardano

For re-developing the Susu on Cardano, the first question is, “What is the programming language of Cardano?” That actually isn’t the most straight-forward question to answer. It’s important to note that as of the time of this writing Cardano hasn’t actually rolled-out smart contract functionality to either the public blockchain nor a publicly-available test-net. That is scheduled for later this year with the “Alonzo” update. The IOHK team has, however, provided several language tools that developers can experiment with to build smart contracts. The official main programming language for Cardano is Plutus, a Haskell-based general purpose language for blockchain programming on Cardano. In addition to Plutus (which I’ve only begun to study) Cardano also has Marlowe, a domain-specific language for writing fixed-term financial smart contracts. Marlowe is a lot simpler than Plutus so I decided to use it for the Susu contract. (Here is the documentation for Marlowe.)

There are actually several different ways to construct a Marlowe smart contract as outlined in the image above. As someone totally unfamiliar with the language features and unwilling to sit through multiple tutorials on the topic, I decided to jump right into it using “Blocky” which is a visual programming tool to help non-coders to write valid smart contracts. After a few hours of experimentation (and some help from this guy), I got the first version of Marlowe-Susu complete.

The Marlowe-Susu Contract

An image of the Marlowe visual Susu contract.
The first part Marlowe-Susu Contract in the visual editor.

In the image above I have just a section of the full Marlowe contract as its constructed in the “Blocky” Marlowe editor. You basically read and construct these contracts from left-to-right, top-to-bottom. This section is meant to represent one iteration of a Susu circle containing only two people (a two-person Susu is kinda silly in the real world but I’m doing it here for simplicity). So starting at the left-top (letter “A” in the above image), this single round of the Susu starts with a deposit/contribution by Person A of 10 ADA (ADA is the native currency of Cardano) into their own account in the contract. Then, I have a “When” keyword that denotes a time period. In this case, I’m creating a window of up to 7 slots during which Person B can make their contribution of 10 ADA (letter “B”). For the sake of simplicity, lets say that 1 slot = 1 day so I’m giving Person B 1 week to make their contribution…

(Note: There is no way that 1 Slot = 1 Day! A Slot is an abstract period of time in Cardano related to the production of blocks on its blockchain. According to the documentation, in the original Byron-Era of Cardano, a Slot lasted 20 seconds. In the current Shelly-Era of Cardano a Slot is only 1 second. We’re currently on the verge of moving to Goguen-Era and I don’t know how slots will work there. Since the main focus of Goguen is the implementation of Smart Contracts, this means I can’t currently deploy this Marlowe contract anyway. So time doesn’t really matter, and for this example I’ll call a Slot a day 😁. You can read about the entire Cardano Timeline here.)

…After Person B makes their deposit, the contract immediately pays Person A the total 20 ADA. Of course, no single account in the smart contract has 20 ADA so the payout occurs as two payouts of 10 ADA, one from each persons’ contract-account.

What’s interesting about Marlowe is how time is a central factor in the construction of these contracts. Notice that the purple “When” constructor has basically a second condition at the bottom where it says “continue as…Close” What this means is that if Person B doesn’t make their deposit by Slot/Day 7, “Close” the contract. When the contract is closed, all money left in each person’s contract-account is refunded out of the contract to the respective person. This architecture makes it hard (impossible?) to write a contract with a logical flaw such that money deposited in the contract gets stuck there indefinitely (a REAL concern with Ethereum-Solidity).

So far with this contract we’ve only done one iteration of the Susu where all (actually, “both” 🙄) people contribute and Person A gets paid. Obviously, to complete the “circle” another round is required where both people contribute and Person B gets paid.

The full Marlowe-Susu Contract

In the Marlowe playground this was as simple as copying the first sub-contract (letter “D”), pasting it to the playground, changing the payees to Person B (letter “E”), and then connecting both sub-contracts inside a larger parent contract as in the image above. Finally, if I’d like to see the actual Marlowe code generated by Blocky, it’s as easy as tapping “View as Marlowe” button in the Marlowe Playground and I see the following:

This contract comes out to only 65 lines. In the next post I’d like to compare writing these types of contracts in Ethereum, Cardano, and off-chain as I put on my lawyer hat and compare them to executing the same deal with traditional legal tools. (If you’d like a preview of some of my thoughts, check out this article.)