Difference between interface keys and contract keys? Why do we need interface keys?

Can somebody explain the gibberish in this paragraph?

– Type synonym for Reference. This type is currently used as a work-around given the lack of interface keys.


type R = Reference

– HIDE


– This template is used to key an Account contract. It allows for looking up this contract|
– by key then acquiring the Account contract by fetching its contract id on this contract.|
– As updates are made to an Account, this Reference contract is required to be kept in sync.|
template Reference

1 Like

An interface can be implemented by several templates. Each template can have a key type, but these types are independent of each other. So those keys cannot be used as an abstract means to look up contracts of templates that implement interface Account.

It is worth noting that ContractId Account is an interface contract ID, and the key of the contract it references is of indeterminate type and structure. Reference effectively unifies them as follows:

  1. contract maps to interface view for Account, View, no matter its template
  2. View maps to AccountKey
  3. that is the template key type for Reference

The idea of interface keys is not trivial, either design-wise or implementation-wise, and so it isn’t available in the current interfaces alpha.

1 Like

Can you please provide references to better understand these concepts? Sorry I am struggling to not think in terms of OOP here. So far I got this Reference: Interfaces — Daml SDK 2.4.0 documentation).

Hi @code_monkey,

Thanks for your question. Let me try to clarify why we use the Reference workaround. I assume here that you are familiar with contract keys (which apply to templates and are documented here).

In Daml Finance we work with interfaces and, as Stephen mentions, Account is in this case an interface type, for which in principle multiple template implementations can exist.

We wanted a way to reference an Account by key, but interface keys are not currently supported: for interfaces we can only use ContractIds.

Hence the following workaround:

  • define a concrete template called Reference which is keyed (with a contract key)
  • have Reference store the ContractId of an Account

We then define some helper functions called exerciseInterfaceByKey which effectively

  • fetch the Reference contract by key
  • exercise a choice on the corresponding Account contract using the ContractId that is stored in the Reference

Hopefully we will be able to remove this workaround once interface keys are supported.

Matteo

2 Likes