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
digital-asset/daml-finance/blob/0687d78cdccf9eb113d5245f2e860a05ea5a1c61/src/main/daml/Daml/Finance/Interface/Account/Account.daml#L132
- do
- creditAndDebit this arg
-
- -- | Type constraint for requiring templates to implement `Account` along with `Disclosure`.
- type Implementation t = (HasToInterface t I, Disclosure.Implementation t)
- instance HasToInterface I Disclosure.I where _toInterface = asDisclosure
- class (Implementation t) => HasImplementation t
- instance HasImplementation I
-
- -- | 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
- with
- accountView : View
- -- ^ The default view for accounts.
- cid : ContractId Account
- -- ^ The contract id of the account.
- observers : PartiesMap
- where
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:
- contract maps to interface view for
Account,View, no matter its template -
Viewmaps toAccountKey - 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.
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
Referencewhich is keyed (with a contract key) - have
Referencestore theContractIdof anAccount
We then define some helper functions called exerciseInterfaceByKey which effectively
- fetch the
Referencecontract by key - exercise a choice on the corresponding
Accountcontract using theContractIdthat is stored in theReference
Hopefully we will be able to remove this workaround once interface keys are supported.
Matteo