-
Notifications
You must be signed in to change notification settings - Fork 199
/
domain.scala
173 lines (139 loc) · 5.66 KB
/
domain.scala
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
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.fetchcontracts
import com.daml.lf
import lf.data.Ref
import util.ClientUtil.boxedRecord
import com.daml.ledger.api.{v1 => lav1}
import com.daml.ledger.api.refinements.{ApiTypes => lar}
import com.daml.scalautil.nonempty.NonEmpty
import scalaz.std.list._
import scalaz.std.option._
import scalaz.std.string._
import scalaz.syntax.std.option._
import scalaz.syntax.traverse._
import scalaz.{@@, Applicative, Order, Semigroup, Show, Tag, Tags, Traverse, \/}
package object domain {
type LfValue = lf.value.Value
type ContractIdTag = lar.ContractIdTag
type ContractId = lar.ContractId
val ContractId = lar.ContractId
type PartyTag = lar.PartyTag
type Party = lar.Party
val Party = lar.Party
type PartySet = NonEmpty[Set[Party]]
type Offset = String @@ OffsetTag
private[daml] implicit final class `fc domain ErrorOps`[A](private val o: Option[A])
extends AnyVal {
def required(label: String): Error \/ A =
o toRightDisjunction Error(Symbol("ErrorOps_required"), s"Missing required field $label")
}
}
package domain {
final case class Error(id: Symbol, message: String)
object Error {
implicit val errorShow: Show[Error] = Show shows { e =>
s"domain.Error, ${e.id: Symbol}: ${e.message: String}"
}
}
sealed trait OffsetTag
object Offset {
private[daml] val tag = Tag.of[OffsetTag]
def apply(s: String): Offset = tag(s)
def unwrap(x: Offset): String = tag.unwrap(x)
def fromLedgerApi(
gacr: lav1.active_contracts_service.GetActiveContractsResponse
): Option[Offset] =
Option(gacr.offset).filter(_.nonEmpty).map(x => Offset(x))
def fromLedgerApi(
gler: lav1.transaction_service.GetLedgerEndResponse
): Option[Offset] =
gler.offset.flatMap(_.value.absolute).filter(_.nonEmpty).map(x => Offset(x))
def fromLedgerApi(tx: lav1.transaction.Transaction): Offset = Offset(tx.offset)
def toLedgerApi(o: Offset): lav1.ledger_offset.LedgerOffset =
lav1.ledger_offset.LedgerOffset(lav1.ledger_offset.LedgerOffset.Value.Absolute(unwrap(o)))
implicit val semigroup: Semigroup[Offset] = Tag.unsubst(Semigroup[Offset @@ Tags.LastVal])
implicit val `Offset ordering`: Order[Offset] = Order.orderBy[Offset, String](Offset.unwrap(_))
}
final case class TemplateId[+PkgId](packageId: PkgId, moduleName: String, entityName: String)
object TemplateId {
type OptionalPkg = TemplateId[Option[String]]
type RequiredPkg = TemplateId[String]
type NoPkg = TemplateId[Unit]
def fromLedgerApi(in: lav1.value.Identifier): TemplateId.RequiredPkg =
TemplateId(in.packageId, in.moduleName, in.entityName)
def qualifiedName(a: TemplateId[_]): Ref.QualifiedName =
Ref.QualifiedName(
Ref.DottedName.assertFromString(a.moduleName),
Ref.DottedName.assertFromString(a.entityName),
)
def toLedgerApiValue(a: RequiredPkg): Ref.Identifier = {
val qfName = qualifiedName(a)
val packageId = Ref.PackageId.assertFromString(a.packageId)
Ref.Identifier(packageId, qfName)
}
}
final case class ActiveContract[+LfV](
contractId: ContractId,
templateId: TemplateId.RequiredPkg,
key: Option[LfV],
payload: LfV,
signatories: Seq[Party],
observers: Seq[Party],
agreementText: String,
)
object ActiveContract {
def matchesKey(k: LfValue)(a: domain.ActiveContract[LfValue]): Boolean =
a.key.fold(false)(_ == k)
def fromLedgerApi(
gacr: lav1.active_contracts_service.GetActiveContractsResponse
): Error \/ List[ActiveContract[lav1.value.Value]] = {
gacr.activeContracts.toList.traverse(fromLedgerApi(_))
}
def fromLedgerApi(in: lav1.event.CreatedEvent): Error \/ ActiveContract[lav1.value.Value] =
for {
templateId <- in.templateId required "templateId"
payload <- in.createArguments required "createArguments"
} yield ActiveContract(
contractId = ContractId(in.contractId),
templateId = TemplateId fromLedgerApi templateId,
key = in.contractKey,
payload = boxedRecord(payload),
signatories = Party.subst(in.signatories),
observers = Party.subst(in.observers),
agreementText = in.agreementText getOrElse "",
)
implicit val covariant: Traverse[ActiveContract] = new Traverse[ActiveContract] {
override def map[A, B](fa: ActiveContract[A])(f: A => B): ActiveContract[B] =
fa.copy(key = fa.key map f, payload = f(fa.payload))
override def traverseImpl[G[_]: Applicative, A, B](
fa: ActiveContract[A]
)(f: A => G[B]): G[ActiveContract[B]] = {
import scalaz.syntax.apply._
val gk: G[Option[B]] = fa.key traverse f
val ga: G[B] = f(fa.payload)
^(gk, ga)((k, a) => fa.copy(key = k, payload = a))
}
}
}
// This allows us to avoid rewriting all the imports and references
// to http.domain. We can snap the indirections and remove these
// as convenient
private[daml] trait Aliases {
import com.daml.fetchcontracts.{domain => here}
type Error = here.Error
final val Error = here.Error
type LfValue = here.LfValue
type TemplateId[+PkgId] = here.TemplateId[PkgId]
final val TemplateId = here.TemplateId
type ContractId = here.ContractId
final val ContractId = here.ContractId
type Party = here.Party
final val Party = here.Party
type PartySet = here.PartySet
type Offset = here.Offset
final val Offset = here.Offset
type ActiveContract[+LfV] = here.ActiveContract[LfV]
final val ActiveContract = here.ActiveContract
}
}