Documentation Index
Fetch the complete documentation index at: https://docs.canton.network/llms.txt
Use this file to discover all available pages before exploring further.
Reference: Interfaces
In Daml, an interface defines an abstract type together with a behavior specified by its view type, method signatures, and choices. For a template to conform to this interface, there must be a correspondinginterface instance definition where all the methods of the interface (including the special view method) are implemented. This allows decoupling such behavior from its implementation, so other developers can write applications in terms of the interface instead of the concrete template.
Configuration
To use this feature your Daml project must target Daml-LF version1.15 or higher, which is the current default.
If using Canton, the protocol version of the sync domain should be 4 or higher, see Canton protocol version for more details.
Interface Declaration
An interface declaration is somewhat similar to a template declaration.Interface Name
- This is the name of the interface.
- It’s preceded by the keyword
interfaceand followed by the keywordwhere. - It must begin with a capital letter, like any other type name.
Implicit abstract type
- Whenever an interface is defined, an abstract type is defined with the same name. “Abstract” here means:
- Values of this type cannot be created using a data constructor. Instead, they are constructed by applying the function
toInterfaceto a template value. - Values of this type cannot be inspected directly via case analysis. Instead, use functions such as
fromInterface. - See
daml-ref-interface-functionsfor more information on these and other functions for interacting with interface values.
- Values of this type cannot be created using a data constructor. Instead, they are constructed by applying the function
- An interface value carries inside it the type and parameters of the template value from which it was constructed.
- As for templates, the existence of a local binding
bof typeI, whereIis an interface does not necessarily imply the existence on the ledger of a contract with the template type and parameters used to constructb. This can only be assumed ifbthe result of a fetch from the ledger within the same transaction.
Interface Methods
- An interface may define any number of methods.
- A method definition consists of the method name and the method type, separated by a single colon
:. The name of the method must be a valid identifier beginning with a lowercase letter or an underscore. - A method definition introduces a top level function of the same name:
-
If the interface is called
I, the method is calledm, and the method type isM(which might be a function type), this introduces the functionm : I -> M: -
The first argument’s type
Imeans that the function can only be applied to values of the interface typeIitself. Methods cannot be applied to template values, even if there exists aninterface instanceofIfor that template. To use an interface method on a template value, first convert it using thetoInterfacefunction. -
Applying the function to such argument results in a value of type
M, corresponding to the implementation ofmin the interface instance ofIfor the underlying template typet(the type of the template value from which the interface value was constructed).
-
If the interface is called
- One special method,
view, must be defined for the viewtype. (see Viewtype below).
Interface View Type
- All interface instances must implement a special
viewmethod which returns a value of the type declared byviewtype. - The type must be a record.
- This type is returned by subscriptions on interfaces.
Interface Choices
- Interface choices work in a very similar way to template choices. Any contract of a template type for which an interface instance exists will grant the choice to the controlling party.
- Interface choices can only be exercised on values of the corresponding interface type. To exercise an interface choice on a template value, first convert it using the
toInterfacefunction. - Interface methods can be used to define the controller of a choice (e.g.
method1) as well as the actions that run when the choice is exercised (e.g.method2andmethod3). - As for template choices, the
choicekeyword can be optionally prefixed with thenonconsumingkeyword to specify that the contract will not be consumed when the choice is exercised. If not specified, the choice will beconsuming. Note that thepreconsumingandpostconsumingqualifiers are not supported on interface choices. - See
choicesfor full reference information, but note that controller-first syntax is not supported for interface choices.
Empty Interfaces
- It is possible (though not necessarily useful) to define an interface without methods, precondition or choices. However, a view type must always be defined, though it can be set to unit.
Interface Instances
For context, a simple template definition:interface instance clause
- To make a template an instance of an existing interface, an
interface instanceclause must be defined in the template declaration. - The template of the clause must match the enclosing declaration. In other words, a template
Tdeclaration can only containinterface instanceclauses where the template isT. - The clause must start with the keywords
interface instance, followed by the name of the interface, then the keywordforand the name of the template, and finally the keywordwhere, which introduces a block where all the methods of the interface must be implemented. - Within the clause, there’s an implicit local binding
thisreferring to the contract on which the method is applied, which has the type of the template’s data record. The template parameters of this contract are also in scope. - Method implementations can be defined using the same syntax as for top level functions, including pattern matches and guards (e.g.
method3). - Each method implementation must return the same type as specified for that method in the interface declaration.
- The implementation of the special
viewmethod must return the type specified as theviewtypein the interface declaration.
Empty interface instance clause
- If the interface has no methods, the interface instance only needs to implement the
viewmethod:
Interface Functions
interfaceTypeRep
| Type |
|
| Instantiated Type | MyInterface -> TemplateTypeRep |
| Notes | The value of the resulting TemplateTypeRep indicates what template was used to construct the interface value. |
toInterface
| Type |
|
| Instantiated Type | MyTemplate -> MyInterface |
| Notes | Converts a template value into an interface value. |
fromInterface
| Type |
|
| Instantiated Type | MyInterface -> Optional MyTemplate |
| Notes | Attempts to convert an interface value back into a template value. The result is None if the expected template type doesn’t match the underlying template type used to construct the contract. |
toInterfaceContractId
| Type |
|
| Instantiated Type | ContractId MyTemplate -> ContractId MyInterface |
| Notes | Converts a template Contract ID into an Interface Contract ID. |
fromInterfaceContractId
| Type |
|
| Instantiated Type | ContractId MyInterface -> ContractId MyTemplate |
| Notes | Converts an interface contract id into a template contract id. This function does not verify that the given contract id actually points to a contract of the resulting type; if that is not the case, a subsequent fetch, exercise or archive will fail. Therefore, this should only be used when the underlying contract is known to be of the resulting type, or when the result is immediately used by a fetch, exercise or archive action and a transaction failure is the desired behavior in case of mismatch. In all other cases, consider using fetchFromInterface instead. |
coerceInterfaceContractId
| Type |
|
| Instantiated Type | ContractId SourceInterface -> ContractId TargetInterface |
| Notes | Converts an interface contract id into a contract id of a different interface. This function does not verify that the given contract id actually points to a contract of the resulting type; if that is not the case, a subsequent fetch, exercise or archive will fail. Therefore, this should only be used when the underlying contract is known to be of the resulting type, or when the result is immediately used by a fetch, exercise or archive action and a transaction failure is the desired behavior in case of mismatch. |
fetchFromInterface
| Type |
|
| Instantiated Type |
|
| Notes | Attempts to fetch and convert an interface contract id into a template, returning both the converted contract and its contract id if the conversion is successful, or None otherwise. |
Required Interfaces
-
An interface can depend on other interfaces. These are specified with the
requireskeyword after the interface name but before thewherekeyword, separated by commas. -
For an interface declaration to be valid, its list of required interfaces must be transitively closed. In other words, an interface
Icannot require an interfaceJwithout also explicitly requiring all the interfaces required byJ. The order, however, is irrelevant. For example, givenThis declaration for interfaceSquarewould cause a compiler errorExplicitly addingShapeto the required interfaces fixes the error -
For a template
Tto be a validinterface instanceof an interfaceI,Tmust also be aninterface instanceof each of the interfaces required byI.
Interface Functions
| Function | Notes |
|---|---|
toInterface | Can also be used to convert an interface value to one of its required interfaces. |
fromInterface | Can also be used to convert a value of an interface type to one of its requiring interfaces. |
toInterfaceContractId | Can also be used to convert an interface contract id into a contract id of one of its required interfaces. |
fromInterfaceContractId | Can also be used to convert an interface contract id into a contract id of one of its requiring interfaces. |
fetchFromInterface | Can also be used to fetch and convert an interface contract id into a contract and contract id of one of its requiring interfaces. |
Interfaces on Canton Network
Interfaces are central to interoperability across the Canton Network. Two Canton Improvement Proposals (CIPs) define standard interfaces that app developers should be aware of:- CIP-0056 — Token Standard: Defines a standard interface for fungible tokens on Canton Network. If your application issues or transfers tokens, implementing this interface ensures compatibility with wallets and other applications in the ecosystem.
- CIP-0103 — dApp Standard: Defines standard interfaces for decentralized applications. This CIP is particularly relevant for developers coming from Ethereum, as it maps familiar ERC-style patterns to Daml interfaces.