/
Service.daml
158 lines (137 loc) · 5.14 KB
/
Service.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
157
158
-- Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
module Daml.Finance.App.Custody.Service where
import Daml.Finance.Interface.Account.Account qualified as Account (Controllers, Credit(..), Debit(..), R)
import Daml.Finance.Interface.Account.Factory qualified as Account (F, Create(..), Remove(..))
import Daml.Finance.Interface.Holding.Factory qualified as Holding (F)
import Daml.Finance.Interface.Holding.Base qualified as Holding (I)
import Daml.Finance.Interface.Account.Util (getAccount)
import Daml.Finance.Interface.Lifecycle.Rule.Claim qualified as Lifecycle (Claim)
import Daml.Finance.Interface.Types.Common.Types (AccountKey(..), Id, InstrumentKey, InstrumentQuantity, PartiesMap)
import Daml.Finance.App.Custody.Model qualified as Custody
import Daml.Finance.App.Util (fetchAndArchive)
template Service
with
operator : Party
provider : Party
customer : Party
claimRuleCid : ContractId Lifecycle.Claim
accountFactoryCid : ContractId Account.F
holdingFactoryCid : ContractId Holding.F
where
signatory operator, provider, customer
key (operator, provider, customer) : (Party, Party, Party)
maintainer key._1
nonconsuming choice RequestOpenAccount : ContractId Custody.OpenAccountRequest
with
id : Id
description : Text
controllers : Account.Controllers
observers : PartiesMap
controller customer
do
create Custody.OpenAccountRequest with ..
nonconsuming choice RequestCloseAccount : ContractId Custody.CloseAccountRequest
with
account : AccountKey
controller customer
do
create Custody.CloseAccountRequest with ..
nonconsuming choice RequestDeposit : ContractId Custody.DepositRequest
with
quantity : InstrumentQuantity
account : AccountKey
controller customer
do
create Custody.DepositRequest with ..
nonconsuming choice RequestWithdrawal : ContractId Custody.WithdrawalRequest
with
instrument : InstrumentKey
holdingCid : ContractId Holding.I
controller customer
do
create Custody.WithdrawalRequest with ..
nonconsuming choice OpenAccount : AccountKey
with
openAccountRequestCid : ContractId Custody.OpenAccountRequest
controller provider
do
Custody.OpenAccountRequest{id; description; controllers; observers} <- fetchAndArchive openAccountRequestCid
let account = AccountKey with custodian = provider; owner = customer; id
exercise accountFactoryCid Account.Create with account; description; controllers; holdingFactoryCid; observers
pure account
nonconsuming choice CloseAccount : ()
with
closeAccountRequestCid : ContractId Custody.CloseAccountRequest
controller provider
do
Custody.CloseAccountRequest{account} <- fetchAndArchive closeAccountRequestCid
exercise accountFactoryCid Account.Remove with account
nonconsuming choice Deposit : ContractId Holding.I
with
depositRequestCid : ContractId Custody.DepositRequest
controller provider
do
Custody.DepositRequest{quantity; account} <- fetchAndArchive depositRequestCid
(_, ref) <- fetchByKey @Account.R account
exercise ref.cid Account.Credit with quantity
nonconsuming choice Withdrawal : ()
with
withdrawalRequestCid : ContractId Custody.WithdrawalRequest
controller provider
do
Custody.WithdrawalRequest{holdingCid} <- fetchAndArchive withdrawalRequestCid
holding <- fetch holdingCid
let account = getAccount holding
(_, ref) <- fetchByKey @Account.R account
exercise ref.cid Account.Debit with holdingCid
choice Terminate : ()
with
actor : Party
controller actor
do
assert $ actor == operator || actor == provider || actor == customer
pure ()
template Offer
with
operator : Party
provider : Party
customer : Party
claimRuleCid : ContractId Lifecycle.Claim
accountFactoryCid : ContractId Account.F
holdingFactoryCid : ContractId Holding.F
where
signatory operator, provider
observer customer
choice Accept : ContractId Service
controller customer
do
create Service with operator; provider; customer; claimRuleCid; accountFactoryCid; holdingFactoryCid
choice Decline : ()
controller customer
do pure ()
choice Withdraw : ()
controller provider
do pure ()
template Request
with
customer : Party
provider : Party
where
signatory customer
observer provider
choice Cancel : ()
controller customer
do pure ()
choice Reject : ()
controller provider
do pure ()
choice Approve : ContractId Service
with
operator : Party
claimRuleCid : ContractId Lifecycle.Claim
accountFactoryCid : ContractId Account.F
holdingFactoryCid : ContractId Holding.F
controller operator, provider
do
create Service with operator; provider; customer; claimRuleCid; accountFactoryCid; holdingFactoryCid