# Custom function on Trigger

**URL:** <https://forum.canton.network/t/custom-function-on-trigger/4989>\
**Category:** App Development\
**Created:** [July 28, 2022, 2:51am UTC](https://forum.canton.network/t/custom-function-on-trigger/4989 "2022-07-28T02:51:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ariscatan](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/ariscatan/32/2249_2.png) [@ariscatan](https://forum.canton.network/u/ariscatan)\
**Post date:** [July 28, 2022, 2:51am UTC](https://forum.canton.network/t/custom-function-on-trigger/4989/1 "2022-07-28T02:51:44Z")

</div>

Hi,  
I am still learning DAML. I have this trigger and I want to implement a custom function which I will call inside the rule of the Trigger I defined.

But I get this error

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/a/a9460f010581ade797578f18579b204182bc548c.png)

Here is my code :

```haskell
processTrade : (ContractId OB.OrderBook,OB.OrderBook) -> (ContractId OB.OrderBook,OB.OrderBook) -> Decimal -> Decimal -> ()
processTrade (sellCid,currentSell) (bCid,currentBuy) bidPrice askPrice = do 
   let buyTotalAmount = (askPrice * intToDecimal(currentBuy.qty)) * (1.01)
         buyTransactionFee = (askPrice * intToDecimal(currentBuy.qty)) * (0.01)
         sellTotalAmount = (bidPrice * intToDecimal(currentSell.qty)) * (1.01)
         sellTransactionFee = (bidPrice * intToDecimal(currentSell.qty)) * (0.01)

   debugRaw $ "MATCHING : " <> show (currentSell.qty) <> "=" <> show (currentBuy.qty)
   when(currentSell.qty <= currentBuy.qty) $ do 
      T.dedupExercise bCid OB.ProcessTrade with ..

```

I get error on return type, Im still struggling to undertand some return type mismatch.  
Thanks for the help, let me know whats the best way to implement this.

Best,

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [July 28, 2022, 8:24am UTC](https://forum.canton.network/t/custom-function-on-trigger/4989/2 "2022-07-28T08:24:24Z")

</div>

Hi @ariscatan,

In Daml, a `do` block returns the value of its last expression. The signature of your function says it returns `()`, but the code of the function has

```haskell
   when(currentSell.qty <= currentBuy.qty) $ do 
      T.dedupExercise bCid OB.ProcessTrade with ..

```

as its last expression, so the return type will be that of `when`. Looking at [its documentation](https://docs.daml.com/daml/stdlib/DA-Action.html#function-da-action-when-35467), you can see that it’s returning `f ()`, which isn’t all that helpful if we don’t know what `f` is. But the signature tells us it has to be the same `f` as the second argument, namely

```haskell
  do T.dedupExercise bCid OB.ProcessTrade with ..

```

If you look at [the documentation for `dedupExercise`](https://docs.daml.com/triggers/api/Daml-Trigger.html#function-daml-trigger-dedupexercise-2238), you’ll see that _its_ return type is `TriggerA s ()`, meaning that in this case `f` is `TriggerA s`. In this type signature, `s` is not bound to anything, so you can leave it unbound too; this means that the return value of `when` in this particular case is

```haskell
TriggerA s ()

```

and thus the signature of your function should be

```haskell
processTrade : (ContractId OB.OrderBook,OB.OrderBook) -> (ContractId OB.OrderBook,OB.OrderBook) -> Decimal -> Decimal -> TriggerA s ()

```

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [July 28, 2022, 2:41pm UTC](https://forum.canton.network/t/custom-function-on-trigger/4989/3 "2022-07-28T14:41:27Z")

</div>

One quick fix that works for most functions: if you delete the type annotation, i.e. the line starting with `processTrade :`, _and_ your module has no errors in it, the inferred type signature will appear as a link above your function definition. You can click on it to insert it automatically.

Some functions cannot be inferred without a type signature, so this doesn’t always work, but it works most of the time.

---

<div class="post-metadata">

**Author:** ![ariscatan](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/ariscatan/32/2249_2.png) [@ariscatan](https://forum.canton.network/u/ariscatan)\
**Post date:** [July 28, 2022, 6:04pm UTC](https://forum.canton.network/t/custom-function-on-trigger/4989/4 "2022-07-28T18:04:57Z")

</div>

Awesome! this solve my problem. Thanks

---

<div class="post-metadata">

**Author:** ![ariscatan](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/ariscatan/32/2249_2.png) [@ariscatan](https://forum.canton.network/u/ariscatan)\
**Post date:** [July 29, 2022, 4:54am UTC](https://forum.canton.network/t/custom-function-on-trigger/4989/5 "2022-07-29T04:54:38Z")

</div>

Hi,  
I have another concern. I want to pass the current party that runs the trigger to my custom function. I tried this but it gives me an error. I made another trigger function in my Trigger file and call it, but Id like to pass also the party that runs the ledger so that I can perform exerciseByKey on my function.

```haskell
updateBidAndAskPrice : Party -> T.TriggerA s ()
updateBidAndAskPrice p = do 
    -- do exerciseByKey

```

But when I build I get these errors

```haskell
daml/Triggers/AutoAcceptTrade3.daml:96:13: error:
  • Couldn't match expected type ‘T.TriggerA () ()’
  with actual type ‘Party -> T.TriggerA s4 ()’
  • Probable cause: ‘updateBidAndAskPrice’ is applied to too few arguments
  In a stmt of a 'do' block: updateBidAndAskPrice
  In the second argument of ‘($)’, namely
  ‘do debugRaw $ "LB -> LS"
  let (sCid, currentSell) = List.head limit_orders_sell
  let limit_orders_buy_sorted = sortOrderBookByQty limit_orders_buy
  let (bCid, currentBuy) = List.head limit_orders_buy_sorted

```

Let me know if this is possible. Thanks

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 29, 2022, 6:48am UTC](https://forum.canton.network/t/custom-function-on-trigger/4989/6 "2022-07-29T06:48:06Z")

</div>

From the error it looks like you are not passing the party to `updateBidAndPrice`. You need to call it with something like `updateBidAndPrice yourpartyhere`. If you show the code that calls it, we might be able to provide a more concrete suggestion.

---

<div class="post-metadata">

**Author:** ![ariscatan](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/ariscatan/32/2249_2.png) [@ariscatan](https://forum.canton.network/u/ariscatan)\
**Post date:** [August 5, 2022, 7:50am UTC](https://forum.canton.network/t/custom-function-on-trigger/4989/7 "2022-08-05T07:50:01Z")

</div>

Thanks @cocreature I learned how to make it work 🙂
