Please consider this slightly contrived (I am investigating a pattern) example
module Main where
import DA.Date
import Daml.Script
type Data = Text
doWork : Data -> Data
doWork s = s <> " Done!"
template THistory with
s : Party
observed : [(Data, Date)]
where
signatory s
choice Observe : (ContractId THistory, ContractId T) with
result : Data
tId : ContractId T
controller s
do
t <- fetch tId
assertMsg "Must be same." $ t.workDatesId == self
workDate <- toDateUTC <$> getTime
workDatesId' <- create this with observed = (result, workDate) :: observed
tId' <- create t with workDatesId = workDatesId'
return (workDatesId', tId')
template T with
s : Party
workDatesId : ContractId THistory
where
signatory s
key s : Party
maintainer key
postconsuming choice DoWork : ContractId T with
input : Data
controller s
do
let result = doWork input
snd <$> exercise workDatesId Observe with tId = self, ..
demo : Script ()
demo = do
s <- allocateParty "s"
workDatesId <- s `submit` do
createCmd THistory with
s, observed = []
tId <- s `submit` do
createCmd T with ..
tId' <- s `submit` do
exerciseCmd tId DoWork with
input = "Ok"
pure ()
This throws a commit error on the unique key of T
. Now the uniqueness of the key is important for this example, but i am confused as to why it is being violated by the DoWork
choice. The postconsuming
is intentional, but afaiu it does archive the current/old T
at the end.
Thank you