Skip to content

Instantly share code, notes, and snippets.

@johnshearing
Last active June 21, 2022 02:54
Show Gist options
  • Save johnshearing/5165bbef2874eed53868d2cd5b469590 to your computer and use it in GitHub Desktop.
Save johnshearing/5165bbef2874eed53868d2cd5b469590 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
module Example where
import Language.Marlowe.Extended
-- Input loan amount
loan_amount_in_ADA = 353.3 :: Double
-- Input number of payments
number_of_payments = 4 :: Integer
{-
Simulates a bank loan
Based on Haskell example by a_juggler which is linked below.
https://gist.github.com/ajuggler/c9b2617295112cb2f4839f4cda4a6b58
Thank you @a_juggler for providing this amazing example of foldr and replicate.
@IndyCoin did the task using a loop as seen below:
https://gist.github.com/CardanoDVPR/0a3bc218d0ab98564d723cc4e40dfebc
Lars has provided a nice example of code generation found at the link below and is discussed in the Lesson 4 at 30 minutes into the video:
https://github.com/input-output-hk/marlowe-pioneer-program/blob/lecture05/code/src/lecture04/multi-pay.hs
It would be fun to use the formula for compound interest to calculate payments when periods and loan amount is given.
UPDATE 06/17/22: From lesson 5 it looks like ACTUS demo app has no trouble with interest.
http://demo.actusfrf.org/form/PAM
I hope to work interest payments into this contract shortly.
Also hope to work in support for NFTs which serve as property deeds as discussed at the link below:
https://cardano.stackexchange.com/questions/8459/nft-support-for-marlowe
-}
-- Specify function to convert ADA to Lovelace
convert_ADA_To_Lovelace :: Num a => a -> a
convert_ADA_To_Lovelace x = x * 1000000
loan_amount_in_lovelace_as_type_double = convert_ADA_To_Lovelace loan_amount_in_ADA
loan_amount_in_lovelace = round loan_amount_in_lovelace_as_type_double :: Integer
payment_amount_in_lovelace = (fromIntegral loan_amount_in_lovelace) `div` (fromIntegral number_of_payments)
main :: IO ()
main = printJSON $ contract loan_amount_in_lovelace payment_amount_in_lovelace number_of_payments (TimeParam "Deadline Bank Deposit") (TimeParam "Payment Deadline")
contract :: Integer -> Integer -> Integer -> Timeout -> Timeout -> Contract
contract loan_amount_in_lovelace payment_amount_in_lovelace number_of_payments deadlineBankDeposit paymentDeadline =
When
[Case
(Deposit
(Role "Bank")
(Role "Bank")
(Token "" "")
(Constant loan_amount_in_lovelace)
)
(deposits number_of_payments)]
(TimeParam "deadlineBankDeposit")
Close
where
deposit :: Action
deposit =
Deposit
(Role "Bank")
(Role "Borrower")
(Token "" "")
(Constant payment_amount_in_lovelace)
deposits :: Integer -> Contract
deposits m = foldr addContract payClient $ replicate (fromIntegral m) True
where
addContract :: Bool -> Contract -> Contract
addContract x y = case x of
True -> (When [Case deposit y] paymentDeadline Close)
False -> Close
payClient :: Contract
payClient = (Pay
(Role "Bank")
(Account (Role "Borrower"))
(Token "" "")
(Constant loan_amount_in_lovelace)
Close
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment