Skip to content

Instantly share code, notes, and snippets.

@hurryabit
Last active Aug 29, 2019
Embed
What would you like to do?
The DAML example used in the HaskellerZ meeting on August 29th, 2019.
daml 1.2
module BikeShop where
import DA.Date
import DA.Time
template Template t => Proposal t with
proposer: Party
receiver: Party
proposal: t
where
signatory proposer
observer receiver
choice Accept: ContractId t
controller receiver
do
create proposal
choice Reject: ()
controller receiver
do
pure ()
data Currency = USD | EUR | GBP | CHF
deriving (Eq, Show)
template Cash with
issuer: Party
owner: Party
currency: Currency
amount: Decimal
where
signatory [issuer, owner]
ensure amount > 0.0
choice Split: (ContractId Cash, ContractId Cash) with
splitAmount: Decimal
controller owner
do
assert $ 0.0 < splitAmount && splitAmount < amount
cashId1 <- create this with amount = splitAmount
cashId2 <- create this with amount = amount - splitAmount
pure (cashId1, cashId2)
choice Transfer: ContractId Cash with
newOwner: Party
controller [owner, newOwner]
do
create this with owner = newOwner
template instance CashProposal = Proposal Cash
template BikeRepair with
bikeShop: Party
bikeOwner: Party
description: Text
price: Decimal -- in CHF
paymentDue: Date
where
signatory [bikeShop, bikeOwner]
choice Pay: () with
cashId: ContractId Cash
controller bikeOwner
do
cash <- fetch cashId
assert $ cash.currency == CHF && cash.amount == price
exercise cashId Transfer with newOwner = bikeShop
pure ()
choice Notice: () with
controller bikeShop
do
assertAfter (time (addDays paymentDue 1) 0 0 0)
-- TODO: Do something!
pure ()
template instance BikeRepairProposal = Proposal BikeRepair
test = scenario do
bank <- getParty "SwissBank"
martin <- getParty "Martin"
bikeShop <- getParty "BikeShop"
let cash = Cash with
issuer = bank
owner = martin
currency = CHF
amount = 200.0
chf200PropId <- submit bank do
create Proposal with
proposer = bank
receiver = martin
proposal = cash
chf200Id <- submit martin do
exercise chf200PropId Accept
let bikeRepair = BikeRepair with
bikeShop
bikeOwner = martin
description = "Fix brakes"
price = 50.0
paymentDue = date 2019 Sep 15
bikeRepairPropId <- submit bikeShop do
create Proposal with
proposer = bikeShop
receiver = martin
proposal = bikeRepair
bikeRepairId <- submit martin do
exercise bikeRepairPropId Accept
(chf50Id, chf150Id) <- submit martin do
exercise chf200Id Split with splitAmount = 50.0
submit martin do
exercise bikeRepairId Pay with cashId = chf50Id
pure ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment