Error using coerceInterfaceContractId

G-d willing

Hello,
In regards to a question I had a couple of months ago regarding interfaces and their conversions (with no strong answer except repeating what is written in the documentation), I am trying to retrieve (from within a choice) the payload from a Fungible.I interface that is stored as a Holding.I interface.
I have a list of ContractId Holding.I, and I need to get the quantity of each one of them, which is taken from the Fungible.I. I first try to convert the Holding.I to Fungible.I with the coerceInterfaceContractId like so:

forA holdingCids (\holdingCid -> do
            a <- coerceInterfaceContractId @Fungible.I holdingCid
            b <- fetchFromInterface @Fungible.Fungible a
            return True
            )

I am getting an error saying:

error:
    • Couldn't match type ‘ContractId’ with ‘Update’
      Expected type: Update Fungible.I
        Actual type: ContractId Fungible.I

When I am doing the 2 operations in one command it does work:

   a <- fetchFromInterface @Fungible.Fungible (coerceInterfaceContractId @Fungible.I tenderParticipationData.holdingCid)

Why the first way doesn’t work and the second one does work?
Thanks

Hi @cohen.avraham,

coerceInterfaceContractId function returns ContractId j

however you are using directly that in your monad composition (the expressions in the do). The expressions in the do need to return Update a , which is a Monad. fetchFromInterface will work because returns Update, but not coerceInterfaceContractId.

To fix that you might do:

a <- return $ coerceInterfaceContractId @Fungible.I holdingCid

return lift a non-monadic value like the value returned by coerceInterfaceContractId inside the monad Update in this case.

(I highly recommend this book to get familiar with functional programming principles: http://learnyouahaskell.com/)

Hope that helps!
Jose

2 Likes