Skip to content
CCPEDIAby Unity Nodes
App Development

Reusing function implemented in Trigger context in a Script

2 posts253 viewsLast activity Jan 2023

  1. Joao_Santos_92OP

    Hello community,

    I’ve got this scenario where I would like to perform some tests and for that purpose reuse a function
    implemented in a trigger.
    I’ve got these two templates defined elsewhere similar to I show below:
    template T1
    with
    a: Party
    b: Text
    c: Int
    .. (other fields)
    choice Processing_Choice
    with
    d : ContractId T2
    controller a template T2
    with
    a: Party
    b: Text
    c: Int
    key (a,b,c) : (Party, Text, Int)

    Then there is a trigger that is on the lookout for these templates as well as is processing them, a trigger with a code similar to what is described below:
    trigger
    t1s <- query T1
    calls nontrivial_match_func t1s >>= case
    Some (t1Cid, t2Cid) -> exercises t1Cid Processing_Choice

    nontrivial_match_func t1s
    find and return the first pair of t1 and t2 cid which match using a predicate by issuing queryContractKey repeatedly

    I’ve tried several approaches to try to implement a function that could be used in the trigger as well as called in a script to perform the tests I want, but no luck so far.
    I would kindly ask for your assistance.
    Best Regards, João Santos

  2. a_putkov

    I don’t believe it’s possible to use the same implementation of an impure function in a Daml Script and in a Daml Trigger. An impure function used in a Script cannot have the same signature as a function used in a Trigger.
    Let’s look at the query function as an example. In Daml Script and in Daml Trigger you can use a function named query to retrieve contracts from the ledger. But it’s not the same function. It’s two different functions implemented in two different libraries (Daml.Script and Daml.Trigger), albeit they share the same function name. The function imported from Daml.Script library has the signature
    query: (Template t, IsParties p) => p -> Script [(ContractId t, t)]
    And the function imported from Daml.Trigger library has the signature
    query: (Template a, ActionTriggerAny m) => m [(ContractId a, a)]