/
Functions.daml
348 lines (295 loc) · 11.9 KB
/
Functions.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
-- Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
-- This is the companion module (is that a word? now it is!) to DA.Internal.Template
-- which provides functions and typeclasses.
-- DA.Internal.Template ends up in its own module with
-- a fixed package id so that the hash of the types does not change.
-- | MOVE Prelude Daml-LF primitives, just templates/contracts
module DA.Internal.Template.Functions where
import DA.Internal.Any
import DA.Internal.LF
import DA.Internal.Prelude
import DA.Internal.Template
import GHC.Types (primitive)
type TemplateOrInterface t =
( HasTemplateTypeRep t
, HasToAnyTemplate t
, HasFromAnyTemplate t
)
-- | Constraint satisfied by templates.
type Template t =
( HasSignatory t
, HasObserver t
, HasEnsure t
, HasAgreement t
, HasCreate t
, HasFetch t
, HasArchive t
, HasTemplateTypeRep t
, HasToAnyTemplate t
, HasFromAnyTemplate t
)
-- | Exposes `signatory` function. Part of the `Template` constraint.
class HasSignatory t where
-- | The signatories of a contract.
signatory : t -> [Party]
-- | Exposes `observer` function. Part of the `Template` constraint.
class HasObserver t where
-- | The observers of a contract.
observer : t -> [Party]
-- | Exposes `ensure` function. Part of the `Template` constraint.
class HasEnsure t where
-- | A predicate that must be true, otherwise contract creation will fail.
ensure : t -> Bool
-- | Exposes `agreement` function. Part of the `Template` constraint.
class HasAgreement t where
-- | The agreement text of a contract.
agreement : t -> Text
-- | Exposes `create` function. Part of the `Template` constraint.
class HasCreate t where
-- | Create a contract based on a template `t`.
create : t -> Update (ContractId t)
-- | Exposes `fetch` function. Part of the `Template` constraint.
class HasFetch t where
-- | Fetch the contract data associated with the given contract ID.
-- If the `ContractId t` supplied is not the contract ID of an active
-- contract, this fails and aborts the entire transaction.
fetch : ContractId t -> Update t
-- | Exposes `archive` function. Part of the `Template` constraint.
class HasArchive t where
-- | Archive the contract with the given contract ID.
archive : ContractId t -> Update ()
-- | Exposes `templateTypeRep` function in Daml-LF 1.7 or later.
-- Part of the `Template` constraint.
class HasTemplateTypeRep t where
-- | HIDE
_templateTypeRep : proxy t -> TypeRep
-- | Exposes `toAnyTemplate` function in Daml-LF 1.7 or later.
-- Part of the `Template` constraint.
class HasToAnyTemplate t where
-- | HIDE
_toAnyTemplate : t -> Any
-- | Exposes `fromAnyTemplate` function in Daml-LF 1.7 or later.
-- Part of the `Template` constraint.
class HasFromAnyTemplate t where
-- | HIDE
_fromAnyTemplate : Any -> Optional t
-- | The stakeholders of a contract: its signatories and observers.
stakeholder : (HasSignatory t, HasObserver t) => t -> [Party]
stakeholder t = signatory t ++ observer t
-- | Constraint satisfied by choices.
type Choice t c r =
( TemplateOrInterface t
, HasExercise t c r
, HasToAnyChoice t c r
, HasFromAnyChoice t c r
)
-- | Exposes `exercise` function. Part of the `Choice` constraint.
class HasExercise t c r | t c -> r where
-- | Exercise a choice on the contract with the given contract ID.
exercise : ContractId t -> c -> Update r
-- | (1.dev only) Exposes `exerciseGuarded` function.
-- Only available for interface choices.
class HasExerciseGuarded t c r | t c -> r where
-- | (1.dev only) Exercise a choice on the contract with
-- the given contract ID, only if the predicate returns `True`.
exerciseGuarded : (t -> Bool) -> ContractId t -> c -> Update r
-- | Exposes `toAnyChoice` function for Daml-LF 1.7 or later.
-- Part of the `Choice` constraint.
class HasToAnyChoice t c r | t c -> r where
-- | HIDE
_toAnyChoice : proxy t -> c -> Any
-- | Exposes `fromAnyChoice` function for Daml-LF 1.7 or later.
-- Part of the `Choice` constraint.
class HasFromAnyChoice t c r | t c -> r where
-- | HIDE
_fromAnyChoice : proxy t -> Any -> Optional c
-- | Constraint satisfied by template keys.
type TemplateKey t k =
( Template t
, HasKey t k
, HasLookupByKey t k
, HasFetchByKey t k
, HasMaintainer t k
, HasToAnyContractKey t k
, HasFromAnyContractKey t k
)
-- | Exposes `key` function. Part of the `TemplateKey` constraint.
class HasKey t k | t -> k where
-- | The key of a contract.
key : t -> k
-- | Exposes `lookupByKey` function. Part of the `TemplateKey` constraint.
class HasLookupByKey t k | t -> k where
-- | Look up the contract ID `t` associated with a given contract key `k`.
--
-- You must pass the `t` using an explicit type application. For
-- instance, if you want to look up a contract of template `Account` by its
-- key `k`, you must call `lookupByKey @Account k`.
lookupByKey : k -> Update (Optional (ContractId t))
-- | Exposes `fetchByKey` function. Part of the `TemplateKey` constraint.
class HasFetchByKey t k | t -> k where
-- | Fetch the contract ID and contract data associated with a given
-- contract key.
--
-- You must pass the `t` using an explicit type application. For
-- instance, if you want to fetch a contract of template `Account` by its
-- key `k`, you must call `fetchByKey @Account k`.
fetchByKey : k -> Update (ContractId t, t)
-- NOTE(F. Mazzoli): the motivation for this function to return both the
-- contract ID and the contract instance is that `fetchByKey` results in
-- a fetch node in the transaction structure, and the fetch node
-- contains the contract data, so we might as well include it here.
--
-- The reason why turning it into a fetch node is necessary is that:
-- 1. We want to have it a more relaxed authorization rule than
-- `lookupByKey`, which gets turned into a LookupByKey node;
-- 2. We want it to have the same authorization rules of a normal
-- fetch, and to implement _that_, we need to know what the
-- stakeholders of the fetched contract are, which requires
-- getting the contract instance.
-- | Exposes `maintainer` function. Part of the `TemplateKey` constraint.
class HasMaintainer t k | t -> k where
-- | HIDE
_maintainer : proxy t -> k -> [Party]
-- | The list of maintainers of a contract key.
maintainer : forall t k. HasMaintainer t k => k -> [Party]
maintainer = _maintainer ([] : [t])
-- | Exposes `toAnyContractKey` function in Daml-LF 1.7 or later.
-- Part of the `TemplateKey` constraint.
class HasToAnyContractKey t k | t -> k where
-- | HIDE
_toAnyContractKey : proxy t -> k -> Any
-- | Exposes `fromAnyContractKey` function in Daml-LF 1.7 or later.
-- Part of the `TemplateKey` constraint.
class HasFromAnyContractKey t k | t -> k where
-- | HIDE
_fromAnyContractKey : proxy t -> Any -> Optional k
-- | Exposes `exerciseByKey` function.
class HasExerciseByKey t k c r | t -> k, t c -> r where
-- | HIDE
_exerciseByKey : proxy t -> k -> c -> Update r
-- | Exercise a choice on the contract associated with the given key.
--
-- You must pass the `t` using an explicit type application. For
-- instance, if you want to exercise a choice `Withdraw` on a contract of
-- template `Account` given by its key `k`, you must call
-- `exerciseByKey @Account k Withdraw`.
exerciseByKey : forall t k c r. HasExerciseByKey t k c r => k -> c -> Update r
exerciseByKey = _exerciseByKey ([] : [t])
-- | Create a contract and exercise the choice on the newly created contract.
createAndExercise : forall t c r. (HasCreate t, HasExercise t c r) => t -> c -> Update r
createAndExercise t c = do
cid <- create t
exercise cid c
-- | Accepted ways to specify a list of parties: either a single party, or a list of parties.
class IsParties a where
-- | Convert to list of parties.
toParties : a -> [Party]
instance IsParties Party where
toParties p = [p]
instance IsParties [Party] where
toParties ps = ps
instance IsParties (Optional Party) where
toParties None = []
toParties (Some p) = [p]
instance Eq AnyTemplate where
(==) = primitive @"BEEqual"
instance Eq AnyChoice where
(==) = primitive @"BEEqual"
instance Eq AnyContractKey where
(==) = primitive @"BEEqual"
instance Ord AnyTemplate where
(<=) = primitive @"BELessEq"
instance Ord AnyChoice where
(<=) = primitive @"BELessEq"
instance Ord AnyContractKey where
(<=) = primitive @"BELessEq"
-- | Generate a unique textual representation of the template id.
templateTypeRep : forall t. HasTemplateTypeRep t => TemplateTypeRep
templateTypeRep = TemplateTypeRep (_templateTypeRep ([] : [t]))
instance Eq TemplateTypeRep where
TemplateTypeRep a == TemplateTypeRep b = a == b
deriving instance Ord TemplateTypeRep
#ifdef DAML_EXPERIMENTAL
-- | (experimental, 1.dev only) Get the template id (or interface id)
-- represented by a TemplateTypeRep, as a string.
templateTypeRepToText : TemplateTypeRep -> Text
templateTypeRepToText (TemplateTypeRep x) =
case primitive @"ETypeRepTyConName" x of
None -> error "Invalid TemplateTypeRep, does not contain a type constructor."
Some y -> y
instance Show TemplateTypeRep where
show = templateTypeRepToText
#endif
-- | Wrap the template in `AnyTemplate`.
--
-- Only available for Daml-LF 1.7 or later.
toAnyTemplate : HasToAnyTemplate t => t -> AnyTemplate
toAnyTemplate x = AnyTemplate (_toAnyTemplate x)
-- | Extract the underlying template from `AnyTemplate` if the type matches
-- or return `None`.
--
-- Only available for Daml-LF 1.7 or later.
fromAnyTemplate : HasFromAnyTemplate t => AnyTemplate -> Optional t
fromAnyTemplate (AnyTemplate x) = _fromAnyTemplate x
-- | Wrap a choice in `AnyChoice`.
--
-- You must pass the template type `t` using an explicit type application.
-- For example `toAnyChoice @Account Withdraw`.
--
-- Only available for Daml-LF 1.7 or later.
toAnyChoice : forall t c r. (HasTemplateTypeRep t, HasToAnyChoice t c r) => c -> AnyChoice
toAnyChoice c =
AnyChoice
(_toAnyChoice ([] : [t]) c)
(templateTypeRep @t)
-- | Extract the underlying choice from `AnyChoice` if the template and
-- choice types match, or return `None`.
--
-- You must pass the template type `t` using an explicit type application.
-- For example `fromAnyChoice @Account choice`.
--
-- Only available for Daml-LF 1.7 or later.
fromAnyChoice : forall t c r. (HasTemplateTypeRep t, HasFromAnyChoice t c r) => AnyChoice -> Optional c
fromAnyChoice (AnyChoice any typeRep)
| Some c <- _fromAnyChoice ([] : [t]) any
, templateTypeRep @t == typeRep = Some c
| otherwise = None
-- | Wrap a contract key in `AnyContractKey`.
--
-- You must pass the template type `t` using an explicit type application.
-- For example `toAnyContractKey @Proposal k`.
--
-- Only available for Daml-LF 1.7 or later.
toAnyContractKey : forall t k. (HasTemplateTypeRep t, HasToAnyContractKey t k) => k -> AnyContractKey
toAnyContractKey k =
AnyContractKey
(_toAnyContractKey ([] : [t]) k)
(templateTypeRep @t)
-- | Extract the underlying key from `AnyContractKey` if the template and
-- choice types match, or return `None`.
--
-- You must pass the template type `t` using an explicit type application.
-- For example `fromAnyContractKey @Proposal k`.
--
-- Only available for Daml-LF 1.7 or later.
fromAnyContractKey : forall t k. (HasTemplateTypeRep t, HasFromAnyContractKey t k) => AnyContractKey -> Optional k
fromAnyContractKey (AnyContractKey any rep)
| Some k <- _fromAnyContractKey ([] : [t]) any
, templateTypeRep @t == rep = Some k
| otherwise = None
deriving instance Eq Archive
deriving instance Show Archive
-- | True if contract exists, submitter is a stakeholder, and all maintainers
-- authorize. False if contract does not exist and all maintainers authorize.
-- Fails otherwise.
visibleByKey : forall t k. (HasLookupByKey t k) => k -> Update Bool
visibleByKey k = do
m <- lookupByKey @t k
case m of
Some _ -> pure True
None -> pure False