Finance Library Interface Casting

Hi Finance team!

I have a template that implements Fungible interface.
In order to Merge or Split I need to work with ContractId Fungible type.
Imagine that my input is ContractId Holding.I type.
Is it right that in order to cast from ContractId Holding.I to ContractId Fungible I have to go through three steps. Like this:

holdingCid = fromInterfaceContractId @Holding holdingIcid -- holdingIcid : Holding.I
fungibleIcid = toInterfaceContractId @Fungible.I holdingCid
fungibleCid = fromInterfaceContractId @Fungible fungibleIcid

I tested it in the Update context and it works. Is it the only way, or I missing something?
Iā€™m asking mostly for the sake of understanding. This is not a production code.

Thanks!

1 Like

Hi @VictorShneer,

You can use the following casting functions.

  • template cid ā†’ interface cid : toInterfaceContractId
  • interface cid ā†’ template cid : fromInterfaceContractId
  • interface cid ā†’ interface cid : coerceInterfaceContractId (or coerceContractId for an SDK version earlier than 2.5.0

In your case, you can use the third option to convert a ContractId Holding.I to a ContractId Fungible.I

2 Likes

Cool thanks! Is it documented?


coerceInterfaceContractId will be documented in the documentation for the 2.5.0 release.

coerceContractId, on the other hand, is not documented.