This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCash.daml
156 lines (127 loc) · 4.44 KB
/
Cash.daml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
-- Copyright (c) 2019, Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
daml 1.2
module Cash where
import DA.Optional
import DA.Action
import Daml.Script
isLocked : Cash -> Bool
isLocked this = this.owner /= this.locker
assertUnlocked : Cash -> Update ()
assertUnlocked this = whenSome this.lockMaturity assertAfter
type CashId = ContractId Cash
template Cash
with
issuer : Party
owner : Party
currency : Text
amount : Decimal
locker : Party
lockMaturity : Optional Time
where
signatory issuer, owner
ensure amount > 0.0
controller owner can
Transfer : CashTransferRequestId
with newOwner : Party
do
when (locker /= newOwner) $ assertUnlocked this
create CashTransferRequest with newOwner = newOwner; ..
Split : (CashId, CashId)
with splitAmount : Decimal
do
f <- create this with amount = splitAmount
s <- create this with amount = amount - splitAmount
return (f, s)
Merge : CashId
with otherCid : CashId
do
assertUnlocked this
c <- fetch otherCid
assertUnlocked c
assert $ this == c with amount
exercise otherCid Archive
create this with amount = amount + c.amount
Lock : CashId
with _locker : Party
_lockMaturity : Time
do
assertUnlocked this
create this with locker = arg._locker; lockMaturity = Some arg._lockMaturity
type CashTransferRequestId = ContractId CashTransferRequest
template CashTransferRequest
with
issuer : Party
owner : Party
newOwner : Party
currency : Text
amount : Decimal
locker : Party
lockMaturity : Optional Time
where
signatory issuer, owner
controller newOwner can
Accept : CashId
do
create Cash with owner = newOwner, locker = newOwner, lockMaturity = None, ..
Reject : CashId
do
create Cash with lockMaturity = None, ..
controller owner can
Withdraw : CashId
do
whenSome lockMaturity assertAfter
create Cash with lockMaturity = None, ..
cashSplitMay : Party -> CashId -> Decimal -> Update (CashId, Optional CashId)
cashSplitMay owner cashCid splitAmount = do
cash <- fetch cashCid
assert $ cash.owner == owner
if cash.amount == splitAmount
then return (cashCid, None)
else do
r <- exercise cashCid Split with splitAmount
return (fst r, Some $ snd r)
main = script do
acmeBank <- allocateParty "AcmeBank"
alice <- allocateParty "Alice"
bob <- allocateParty "Bob"
cashAlice1Cid <-
submit acmeBank do createCmd CashTransferRequest with
issuer = acmeBank
owner = acmeBank
newOwner = alice
currency = "USD"
amount = 100.0
locker = acmeBank
lockMaturity = None
cashAlice1Cid <-
submit alice do exerciseCmd cashAlice1Cid Accept
cashBob1Cid <-
submit acmeBank do createCmd CashTransferRequest with
issuer = acmeBank
owner = acmeBank
newOwner = bob
currency = "USD"
amount = 20.0
locker = acmeBank
lockMaturity = None
cashBob1Cid <-
submit bob do exerciseCmd cashBob1Cid Accept
(cashAlice1Cid, cashAlice2Cid) <-
submit alice do exerciseCmd cashAlice1Cid Split with splitAmount = 30.0
cashBob2Cid <-
submit alice do exerciseCmd cashAlice1Cid Transfer with newOwner = bob
cashBob2Cid <-
submit bob do exerciseCmd cashBob2Cid Accept
cashBob2Cid <-
submit bob do exerciseCmd cashBob1Cid Merge with otherCid = cashBob2Cid
Some c <- queryContractId alice cashAlice2Cid
assertMsg "unexpected issuer" $ c.issuer == acmeBank
assertMsg "unexpected owner" $ c.owner == alice
assertMsg "unexpected currency" $ c.currency == "USD"
assertMsg "unexpected amount" $ c.amount == 70.0
Some c <- queryContractId bob cashBob2Cid
assertMsg "unexpected issuer" $ c.issuer == acmeBank
assertMsg "unexpected owner" $ c.owner == bob
assertMsg "unexpected currency" $ c.currency == "USD"
assertMsg "unexpected amount" $ c.amount == 50.0