Triggers, intermediate state? Using a new template generated from a command in the same trigger

What’s the best way to deal with this, it has to do with a swap.

Initial state, userAdmin has 0 JPY assets.

bob sends a request to swap 10 USD for Admin’s 10 JPY.

  1. userAdmin receives a TradeRequest contract, (we can detect this from T.registeredTemplates, @TradeRquest)
  2. Since userAdmin has 0 JPY, the default behavior I want is: to create a JPY contract with the requested amount each time the userAdmin receives TradeRequest. We issue new JPYs each time we get a TradeRequest so we can gaurantee we always have enough to swap with.
  3. the newly created JPY template needs to be used as an argument to settle the swap.

So basically

  1. useAdmin receives TradeRequest
  2. userAdmin creates JPY asset, from for example TradeRequest.requestedTicker, TradeRequest.requestedAmount
  3. use this new asset in TradeRequest.settle with JPY Asset.

However, what is the best way to use a new template generated from exercising a choice, within this current trigger? Do I need to use
updateState = \_ -> pure ()

From the trigger docs,
https://docs.daml.com/triggers/index.html
The updateState function is called on new transactions and command completions and can be used to update your user-defined state based on the ACS and the transaction or completion. Since our Daml trigger does not have any interesting user-defined state, we will not go into details here.

I see two options neither involving trigger state:

  1. Create a template with the user admin as signatory and a single choice. That choice first creates the JPY asset and then settles the trade. You can call that choice via createAndExerciseCmd. That would settle the trade in a single transaction.
  2. If your trigger sees a transaction, first check if you have a JPY asset with the right amount. Then do one of two things:
    2.1. If you have an asset with the right amount, call the choice to settle the trade.
    2.2. If not, submit a command to create the asset and only that. At some point that contract is created and your trigger will run into 2.1 and settle the contract.

2 is a bit more flexible but if 1 fits your usecase, I’d probably try that.

1 Like

1 sounds more straightforward, I’ll try that! Thanks!

1 Like