Converting one interface to another

G-d willing

Hi everyone,
I am having some difficulties in converting one interface to another from within a Script.
I am using the Finance Library trying to convert a Holding to Fungible.

import Daml.Finance.Interface.Holding.Base qualified as Holding
import Daml.Finance.Interface.Holding.Fungible qualified as Fungible

convertHoldingToFungible : ContractId Holding.I -> Party -> Script ()
convertHoldingToFungible holdingCid owner = do
  fungibleHoldingCid <- toInterfaceContractId @Fungible.I holdingCid
  pure ()

The line that is doing toInterfaceContractId is having an error indicating:

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

However, when I am doing it this way, it works:

  fungibleHoldingCid <-  toInterfaceContractId @Fungible.I . fst . head <$> queryInterface @Fungible.I owner

Can you please explain to me how can I do it correctly?

Thanks,

If you bind a value x <- expr in a do block, expr needs to have type m a where m is the type of the Action you’re do block is for. x will then have type a. If you just want to use a local variable use let x = expr instead where expr can have any type t and x will also have type t.

In your example m = Script but toInterfaceContractId @Fungible.I holdingCid has type ContractId Fungible.I.

So try this instead:

convertHoldingToFungible : ContractId Holding.I -> Party -> Script ()
convertHoldingToFungible holdingCid owner = do
  let fungibleHoldingCid = toInterfaceContractId @Fungible.I holdingCid
  pure ()

That function itself is not very useful though since younever use fungibleHoldingCid. Maybe you intended to return it from the function?