How to build smart contracts with Marlowe
Marlowe is a domain-specific smart contract language designed specifically for financial applications. Unlike Solidity, Marlowe is simple, intuitive, and easy for non-technical users.
1. What is Marlowe?
Are smart contracts tough to code?
With Solidity, yes.
With Marlowe, no!
Marlowe is a domain-specific smart contract language designed specifically for financial applications. Unlike Solidity, Marlowe is simple, intuitive, and easy for non-technical users.
Examples of what you can create using Marlowe:
Asset-backed securities
Betting contracts
Bonds
Crowdfunding contracts
Derivatives contracts
Escrow contracts
Invoicing and payment systems
Insurance contracts
Investment contracts
Loan contracts
Payment agreements
Real estate contracts
Supply chain finance contracts
2. Sample Marlowe code
Let's take an example of a smart contract that specifies a payment agreement between two parties - rohas and the party who is executing the contract.
Here’s the code:
Now let’s examine this code in detail.
{-# LANGUAGE OverloadedStrings #-}
This is a directive in the Haskell programming language that makes it easier to work with string values as it avoids the need for explicit type conversions and can improve code readability.
import Marlowe
This line imports the Marlowe library into the code.
This library provides the necessary tools and functions for creating financial contracts in the Marlowe programming language.
By importing this library, the code will have access to all the functions and types available in Marlowe, allowing it to create contracts that can be executed on the blockchain.
paymentAgreement :: Contract
The code defines a constant named "paymentAgreement" which is of type Contract.
paymentAgreement = when (choice "Accept" "Reject")
[ case of
True -> pay (getParty1) (getAmount)
False -> zero
]
This defines a payment agreement contract that allows a party (Party1) to make a payment to another party (the contract).
The payment is made only if the other party accepts the payment (choice "Accept"). If the other party does not accept the payment, it is rejected (choice "Reject").
The contract uses the when function to define the conditions for the payment. The choice function creates a choice between "Accept" and "Reject".
The case of expression uses pattern matching to check the outcome of the choice. If the choice is True (Accept), the pay function is used to make a payment from Party1 to the contract for the specified amount (getAmount). If the choice is False (Reject), the contract returns the value zero.
getParty1 :: Party -> Bool
Here, Party is a type provided by Marlowe, which represents a participant in a contract.
getParty1 p = p == rohas
The getParty1 function takes a Party type argument p and returns a boolean value. It returns True if the input p is equal to the constant value rohas, otherwise, it returns False.
getAmount :: Int
getAmount :: Int is a function signature that defines the return type of the function getAmount. It says that the getAmount function returns a value of type Int, which stands for integer.
getAmount = 100
getAmount is a function in Haskell with type signature Int. It is used to return an integer value representing an amount of money.
In this example, the function has a hardcoded return value of 100.
Fantastic. It is like no-code for smart contracts. What about the security? What steps should one take care?