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 pathBond.daml
131 lines (106 loc) · 3.3 KB
/
Bond.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
-- Copyright (c) 2019, Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
daml 1.2
module Bond where
import Daml.Script
type BondId = ContractId Bond
template Bond
with
issuer : Party
owner : Party
isin : Text
amount : Decimal
where
ensure amount > 0.0
signatory issuer, owner
controller owner can
Transfer : BondTransferRequestId
with newOwner : Party
do
create BondTransferRequest with newOwner, ..
Split : (BondId, BondId)
with splitAmount : Decimal
do
f <- create this with amount = splitAmount
s <- create this with amount = amount - splitAmount
return (f, s)
Merge : BondId
with otherCid : BondId
do
otherBond <- fetch otherCid
assert $ this == otherBond with amount
archive otherCid
create this with amount = amount + otherBond.amount
type BondTransferRequestId = ContractId BondTransferRequest
template BondTransferRequest
with
issuer : Party
owner : Party
newOwner : Party
isin : Text
amount : Decimal
where
signatory issuer, owner
ensure amount > 0.0
controller newOwner can
Accept : BondId
do
create Bond with owner = newOwner, ..
Reject : BondId
do
create Bond with ..
controller owner can
Withdraw : BondId
do
create Bond with ..
bondSplitMay : Party -> BondId -> Decimal -> Update (BondId, Optional BondId)
bondSplitMay owner bondCid splitAmount = do
bond <- fetch bondCid
assert $ bond.owner == owner
if bond.amount == splitAmount
then return (bondCid, None)
else do
r <- exercise bondCid Split with splitAmount
return (fst r, Some $ snd r)
main = script do
acmeBank <- allocateParty "AcmeBank"
alice <- allocateParty "Alice"
bob <- allocateParty "Bob"
bondAlice1Cid <-
submit acmeBank do
createCmd BondTransferRequest with
issuer = acmeBank
owner = acmeBank
newOwner = alice
isin = "1234"
amount = 100.0
bondAlice1Cid <-
submit alice do exerciseCmd bondAlice1Cid Accept
bondBob1Cid <-
submit acmeBank do
createCmd BondTransferRequest with
issuer = acmeBank
owner = acmeBank
newOwner = bob
isin = "1234"
amount = 20.0
bondBob1Cid <-
submit bob do exerciseCmd bondBob1Cid Accept
(bondAlice1Cid, bondAlice2Cid) <-
submit alice do exerciseCmd bondAlice1Cid Split with splitAmount = 30.0
bondBob2Cid <-
submit alice do exerciseCmd bondAlice1Cid Transfer with newOwner = bob
bondBob2Cid <-
submit bob do exerciseCmd bondBob2Cid Accept
bondBob2Cid <-
submit bob do exerciseCmd bondBob1Cid Merge with otherCid = bondBob2Cid
Some c <- queryContractId alice bondAlice2Cid
assertMsg "unexpected issuer" $ c.issuer == acmeBank
assertMsg "unexpected owner" $ c.owner == alice
assertMsg "unexpected isin" $ c.isin == "1234"
assertMsg "unexpected amount" $ c.amount == 70.0
Some c <- queryContractId bob bondBob2Cid
assertMsg "unexpected issuer" $ c.issuer == acmeBank
assertMsg "unexpected owner" $ c.owner == bob
assertMsg "unexpected isin" $ c.isin == "1234"
assertMsg "unexpected amount" $ c.amount == 50.0