# Rational in implementing multiple interfaces views in a template

**URL:** <https://forum.canton.network/t/rational-in-implementing-multiple-interfaces-views-in-a-template/5396>\
**Category:** App Development\
**Tags:** daml\
**Created:** [October 10, 2022, 3:46am UTC](https://forum.canton.network/t/rational-in-implementing-multiple-interfaces-views-in-a-template/5396 "2022-10-10T03:46:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![code\_monkey](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/code_monkey/32/4936_2.png) [@code\_monkey](https://forum.canton.network/u/code_monkey)\
**Post date:** [October 10, 2022, 3:46am UTC](https://forum.canton.network/t/rational-in-implementing-multiple-interfaces-views-in-a-template/5396/1 "2022-10-10T03:46:17Z")

</div>

What is the rational in implementing views of two separate interfaces in a template? How to visualize data being serialized under the hood? Is it template instance will preserve both views and can only be accessed by toInterfaceContractId for example **“exerciseCmd (toInterfaceContractId @Participant.I cid) Participant.GetView with viewer = admin”**

> <https://github.com/digital-asset/daml-finance/blob/b843327e535b7c5a5d81625058138a0d9c3cc348/src/main/daml/Daml/Finance/Settlement/Instruction.daml#L65>

interface instance Disclosure.I for Instruction where  
view = Disclosure.View with disclosureControllers = S.fromList [step.sender, step.receiver]; observers

…  
interface instance Instruction.I for Instruction where  
view = Instruction.View with requestors; settlers; signed; batchId; id; step; allocation; approval

---

<div class="post-metadata">

**Author:** ![Matteo\_Limberto](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/matteo_limberto/32/2823_2.png) [@Matteo\_Limberto](https://forum.canton.network/u/Matteo_Limberto)\
**Post date:** [October 10, 2022, 8:20am UTC](https://forum.canton.network/t/rational-in-implementing-multiple-interfaces-views-in-a-template/5396/2 "2022-10-10T08:20:17Z")

</div>

Hi @code_monkey.

An interface definition includes a `viewtype` that defines its serializable part.

A template implementing two interfaces will need to define how both views are generated. One can then access each view by using `toInterfaceContractId`, as you suggest.

I believe that the rationale for this is an upcoming feature where you will be able to query for interfaces via the Ledger API and retrieve the corresponding interface views.

For instance, in your UI application you will be able to query for all contracts implementing the [base `Holding` interface](https://github.com/digital-asset/daml-finance/blob/e541ef29f2b26f86c3133821f62675ed21ac6391/src/main/daml/Daml/Finance/Interface/Holding/Base.daml) and print out the corresponding `amount` (which is defined in the view).

Matteo

---

<div class="post-metadata">

**Author:** ![code\_monkey](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/code_monkey/32/4936_2.png) [@code\_monkey](https://forum.canton.network/u/code_monkey)\
**Post date:** [October 12, 2022, 3:12pm UTC](https://forum.canton.network/t/rational-in-implementing-multiple-interfaces-views-in-a-template/5396/3 "2022-10-12T15:12:55Z")

</div>

Can share some diagram how to visualize Interfaces, InterfaceContractId, Views etc?

---

<div class="post-metadata">

**Author:** ![Matteo\_Limberto](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/matteo_limberto/32/2823_2.png) [@Matteo\_Limberto](https://forum.canton.network/u/Matteo_Limberto)\
**Post date:** [October 13, 2022, 9:46am UTC](https://forum.canton.network/t/rational-in-implementing-multiple-interfaces-views-in-a-template/5396/4 "2022-10-13T09:46:17Z")

</div>

Let me try to illustrate these concepts with an example drawn from the [Daml Finance](https://github.com/digital-asset/daml-finance/) library.

**Interfaces**

These work similarly to what you would expect from interfaces in object-oriented programming:

- an interface defines a set of methods and choices
- a template that implements an interface needs to provide the implementation of the corresponding methods
- when writing Daml code, we can access the choices via the interface

As an example, consider the setup in the picture below

 ![Example interfaces](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/5/54824e711619a68ad510ad63aa631750556af6df.png)

We have two contracts on the ledger

- C1 which is a [Fungible holding](https://github.com/digital-asset/daml-finance/blob/2953c1ca59fc20194f89740143109a114626cffd/src/main/daml/Daml/Finance/Holding/Fungible.daml#L29)
- C2 which is a [Non-Fungible holding](https://github.com/digital-asset/daml-finance/blob/2953c1ca59fc20194f89740143109a114626cffd/src/main/daml/Daml/Finance/Holding/NonFungible.daml#L28)

Both templates implement the [Base holding interface](https://github.com/digital-asset/daml-finance/blob/2953c1ca59fc20194f89740143109a114626cffd/src/main/daml/Daml/Finance/Interface/Holding/Base.daml#L27), which gives access to the `GetView` choice.

Only the Fungible holding implements the [Fungible interface](https://github.com/digital-asset/daml-finance/blob/2953c1ca59fc20194f89740143109a114626cffd/src/main/daml/Daml/Finance/Interface/Holding/Fungible.daml#L44), which gives access to the `Split` and `Merge` choices.

**Interface ContractId**

Assume now that you want to write a new Daml template where the `owner` of a holding can claim a prize if the `amount` they hold is greater than `100.0`.

You want to write generic code that works for both C1 and C2, given that they both have an `amount` and `owner` field. Specifically, you can write your template to work for any contract implementing the Base holding interface (which gives access to `owner` and `amount` fields via the `GetView` choice). In order to do that, we need a way to reference this set of templates: an interface ContractId.

Your new template will look as follows:

```haskell
template ClaimPrize
  with 
  ... 
  nonconsuming choice Claim : ContractId Prize
    with
      ctrl : Party
      holdingCid : ContractId Base.I -- any contract that implements the base holding interface
    controller ctrl
    do
      view <- exercise holdingCid GetView
      assert view.owner == ctrl
      if view.amount > 100 then create Prize with ... else abort ""

```

**Interface Views**

Finally, an interface view is just a special method which

- must be defined for each interface
- must return a serializable object

The reason why views are useful is explained in my post above.

I hope this helps you get a better understanding of the mechanics of interfaces in Daml. If you find this example useful, we could consider adding it to the Daml Finance documentation. I also encourage you to have a look at the [Daml Finance getting started tutorial](https://docs.daml.com/daml-finance/tutorials/getting-started/transfer.html) in case you haven’t done so.

Matteo

---

<div class="post-metadata">

**Author:** ![code\_monkey](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/code_monkey/32/4936_2.png) [@code\_monkey](https://forum.canton.network/u/code_monkey)\
**Post date:** [October 13, 2022, 3:22pm UTC](https://forum.canton.network/t/rational-in-implementing-multiple-interfaces-views-in-a-template/5396/5 "2022-10-13T15:22:49Z")

</div>

@Matteo_Limberto

 ![whos-awesome-youre-tljclb](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/c/c3a96b86784134ec752bf8ae36e8a40067305969.jpeg)
