DAML Triggers: How can I pass arguments in DAML Trigger?
App Development2 posts185 viewsLast activity May 2022
NA
NagatoPeinI1OP
May 2022Sample code:
module Main.User where
data Currency = INR | USD | EUR
template User with
username: Party
following: [Party]
where
signatory username
observer following
key username: Party
maintainer key
nonconsuming choice Follow: ContractId User with
userToFollow: Party
controller username
do
archive self
create this with following = userToFollow :: following
nonconsuming choice SendMessage: ContractId Message with
sender: Party
content: Text
messagingFees: Optional Decimal
feeType: Optional Currency
controller sender
do
assertMsg "Designated user must follow you back to send a message" (elem sender following)
now <- getTime
create Message with sender, receiver = username, content, messagingFees, feeType, receivedAt = now
template Message with
sender: Party
receiver: Party
content: Text
receivedAt: Time
messagingFees: Optional Decimal
feeType: Currency
where
signatory sender, receiver
DAML Trigger:
import Main.User as User
import qualified Daml.Trigger as T
-- import qualified User
import qualified DA.List.Total as List
import DA.Action (when)
import DA.Optional (whenSome)
autoReply : T.Trigger ()
autoReply = T.Trigger
{ initialize = pure ()
, updateState = \_ -> pure ()
, rule = \p -> do
message_contracts <- T.query @User.Message
let messages = map snd message_contracts
debug $ "Messages so far: " <> show (length messages)
let lastMessage = List.maximumOn (.receivedAt) messages
debug $ "Last message: " <> show lastMessage
whenSome lastMessage $ \m ->
when (m.receiver == p) $ do
users <- T.query @User.User
debug users
let isSender = (\user -> user.username == m.sender)
let replyTo = List.head $ filter (\(_, user) -> isSender user) users
whenSome replyTo $ \(sender, _) ->
T.dedupExercise sender (User.SendMessage p "Please, tell me more about that." 1000.0 USD)
, registeredTemplates = T.AllInDar
, heartbeat = None
}
How can I pass values in this T.dedupExercise sender (User.SendMessage p "Please, tell me more about that." 1000.0 USD) from different templates?
E.g. If I’m listing to Message template from some other then how can I use value of that message template and pass value to our choice?
RI
rikotacards
May 2022You’ll have to grab the message contracts, similar to the beginning lines of this trigger,
message_contracts <- T.query @User.Message
let messages = map snd message_contracts
this gives us a list of message contracts, (not CIDs)
and you can map through them, and access the message field, message.content, that is what you’ll want to pass in as an argument to the choice.