Skip to content

Instantly share code, notes, and snippets.

@marco-martins
Forked from CardanoDVPR/Main.hs
Created June 17, 2022 09:35
Show Gist options
  • Save marco-martins/0cd5a641946eab26a87dda37fef19013 to your computer and use it in GitHub Desktop.
Save marco-martins/0cd5a641946eab26a87dda37fef19013 to your computer and use it in GitHub Desktop.
Bank or Client Pay in Haskell
{-# LANGUAGE OverloadedStrings #-}
module Example where
import Language.Marlowe.Extended
main :: IO ()
main = printJSON $ contract "Bank" "Client" 5 50 (TimeParam "BankDeadline") (TimeParam "ClientDeadlines")
{- Define a contract where the Bank makes an initial deposit and the Client makes a series of deposits of
a specified amount until the Bank contract is paid off. If the Client fails to make all the deposits, the
Bank is paid its deposit, plus the client deposits, otherwise the Client receives all the deposits. -}
contract :: Party -> Party -> Integer -> Integer -> Timeout -> Timeout -> Contract
contract bank client count amount clientDeadline bankDeadline = When
[Case (deposit bank) (clientObligation 1)]
bankDeadline
Close
where
pay :: Party -> Party -> Contract --Payment Contract
pay role1 role2 = Pay role1 (Party role2) ada (AvailableMoney role1 ada) Close --allows for two switchable "Role" parties
deposit :: Party -> Action --Deposit Contract
deposit p = do
if p == "Client" --checking for "Client" or "Bank"
then do
Deposit p p ada (Constant (amount * 1000)) --50*1000 (converting to ada)
else do
Deposit p p ada (Constant (amount * count * 1000)) --50*count*1000 (full deposit converted to ada)
clientObligation :: Integer -> Contract --function made to check for client's deposit obligation
clientObligation i = do
if (count - i) > 0
then do
(When [Case (deposit client)
(clientObligation (i + 1))] --recursive call
clientDeadline
(pay client bank)
)
else do
(When [Case (deposit client) --client makes last obligated deposit
(pay bank client)]
clientDeadline
Close
)
{"valueParameterInfo":[],"timeParameterDescriptions":[],"roleDescriptions":[],"contractType":"Other","contractShortDescription":"Unknown","contractName":"Unknown","contractLongDescription":"We couldn't find information about this contract","choiceInfo":[]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment