Is it possible to resuse custom function script in trigger

Hi,
I come up with a Test scenario and almost same code to be used on my TriggerA.
Is it possible to have a common function that can be used on both Script and TriggerA?

I just want to make the both close together by reusing the function and just return either Script or TriggerA

Thanks,

In the abstract, it’s definitely possible to share some code between Scripts and Triggers. Can you give us a little bit more information about what’s blocking you? Perhaps share the code itself, if possible, or describe what you have tried and how it failed for you?

Hi @Gary_Verhaegen

Im planning this kind of concept

func : Text -> Party -> Script() or TriggerA()
func : caller = do 
-- function that returns eiher script or triggerA
-- if caller = triiger return TriggerA else Script

test : Script()
  func "script" alice

triggerTest : TriggerA s ()
  func "trigger" alice

Thanks

I think something like this may work, though I would be in a better position to give you working code if you’d given me working code to start with :slight_smile:

func : Action a => Text -> Party -> a ()
func : caller = do 
-- Can do anything you can do on an Action, which both TriggerA
-- and Script are. Cannot do Script- or TriggerA-specific things,
-- though.

-- No change needed below this; func will "instanciate" `a` to either
-- `Script` or `TriggerA` based on context.
test : Script()
  func "script" alice

triggerTest : TriggerA s ()
  func "trigger" alice

I wanted something like this myself but once you look into the details, the cases where this actually makes sense unfortunately mostly disappear.

The two main differences between Script & Triggers that matter here are the following:

  1. A trigger always runs as a single party whereas a Daml Script can submit commands as many different parties.
  2. A trigger submits commands asynchronously. One of the many consequences of that is that you do not get access to the result. A Script on the other hand submits synchronously so you get access to the result and you know that the command has succeeded before moving on to the next one.

So porting an arbitrary script to triggers just does not work. There is a subset of single-party scripts that could be ported in theory but even for those you need to worry about pending sets for triggers which just don’t exist in scripts.

So in practice, the cases where this work are relatively tiny and usually triggers just do different things than your daml scripts.

What can sometimes be helpful if putting some shared logic in a choice that you call via createAndExerciseCmd.

1 Like

Ah I see, so it will be look like this

in Script → createAndExerciseCmd DoStuff
in Triggers → createAndExerciseCmd DoStuff

which shares same logic.

Yeah, that’s what I was trying to suggest.

1 Like