Help Interpret Error Message

Trying to write Triggers,

/Users/maxhsu/Desktop/projects/wallet-refapp/triggers/daml/AcceptSwapTrigger.daml:34:21: error:
• No instance for (DA.Internal.Record.HasField
“asset” (Optional AssetInSwap) Asset.Asset)
arising from a use of ‘getField’
• In the ‘asset’ field of a record
In the third argument of ‘T.dedupExerciseByKey’, namely
‘Preapprove_Transfer_In {asset = (getField @“asset” offeredAsset)}’
In the expression:
((getField @“assetType” (getField @“asset” offeredAsset)), p)
Preapprove_Transfer_In {asset = (getField @“asset” offeredAsset)}typecheck

acceptSwapTrigger: T.Trigger ()
acceptSwapTrigger = T.Trigger 
 { initialize = pure (),
  updateState = \_  -> pure (),
  registeredTemplates = T.RegisteredTemplates [T.registeredTemplate @Trade, T.registeredTemplate @AssetHoldingAccount, T.registeredTemplate @TransferPreApproval],
  rule = \p -> do
    tradeRequests <- T.query @Trade
    let isMe = (\tradeRequests -> tradeRequests.receiver == p)
    let meList = filter (\(_, contract) -> isMe contract) tradeRequests

    debug ("asset holding account invites", meList)

    unless ( DA.Foldable.null meList ) do 
      case meList of 
        [] -> pure ()

        (cid, c) :: _ -> do
          optOfferedAsset <- T.queryContractId c.offeredAssetCid
          let offeredAsset = (fromSome optOfferedAsset)
          transferPreApproval <- T.queryContractId c.requestedAssetsTxPreApprovalCid
          if(isSome offeredAsset) then
            T.dedupExerciseByKey @AssetHoldingAccount (offeredAsset.asset.assetType, p) Preapprove_Transfer_In with 
            asset = offeredAsset.asset
            debug "NOPE"
    debug $ "TRIGGERED",
  heartbeat = None

offeredAsset has the type

: Optional (Optional AssetInSwap) -> Optional AssetInSwap

In my test scripts, I’m able to do

  optOfferedAsset <- queryContractId alice trade.offeredAssetCid
  let offeredAsset = (fromSome optOfferedAsset).asset

It turns the optional optOfferedAsset optOfferedAsset : Optional AssetInSwap
type offeredAsset : Asset

But I can’t do that in the trigger. Hence I’m trying the isSome

fromSome will crash if the value is None and otherwise give you back the underlying value. But then you’re checking if the result of fromSome, i…e, offeredAsset is Some which is a type error because offeredAsset is of type OfferedAsset.

Instead drop the fromSome and pattern match:

optOfferedAsset <- T.queryContractId c.offeredAssetCid
case optOfferedAsset of
  Some offeredAsset ->
    T.dedupExerciseByKey @AssetHoldingAccount (offeredAsset.asset.assetType, p) Preapprove_Transfer_In with
      asset = offeredAsset.asset
    debug "NOPE"
1 Like