Skip to content

Instantly share code, notes, and snippets.

@CardanoDVPR
Last active June 23, 2022 15:55
Show Gist options
  • Save CardanoDVPR/0a3bc218d0ab98564d723cc4e40dfebc to your computer and use it in GitHub Desktop.
Save CardanoDVPR/0a3bc218d0ab98564d723cc4e40dfebc 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":[]}
@ajuggler
Copy link

Thanks for posting your solution. Interesting to see how you solved it using a counter. Maybe you would be interested in comparing with my solution, where I use 'foldr' ; see line 38 in https://gist.github.com/ajuggler/c9b2617295112cb2f4839f4cda4a6b58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment