Lesson 6: Extensibility using interfaces
Nothing is loaded from YouTube until you press play, and then without tracking cookies.
About this video
For a better learning experience, sign up and enroll in this free course at: daml.talentlms.com/plus/catalog/courses/155
Transcript
1,662 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
one of the design principles widely followed in software engineering for application extensibility is to separate the application into two layers an abstract layer and a concrete layer this gives you the freedom to change the implementation layer while keeping the abstract layer the same and keeping the external users of the application unimpacted this lesson covers how this principle is applied in daml applications using daml interfaces the
- 0:30
daml interface is an abstract type that has three key components first is the view record of type view type all interfaces have a special method called view which returns a value of the type declared by view type which is a record when a template implements an interface it provides values for the fields in this view record which can then be used by the methods and choices the second component is the method signature
- 1:00
these methods need to be implemented by the template that implements this interface the third component is the choices that the template will get when implementing this interface the choices are fully coded in the interface body but they use the methods that are yet to be implemented by the template there are three key tasks for a template to implement an interface first create an instance of the interface
- 1:27
second provide data for its U record third Implement its methods let us look at a scenario to understand how this works let us say that a bank creates five transfer proposals to transfer $1 each of cash to Alice it also creates five transfer proposals to transfer $1 each of bonds to Bob both Alice and Bob accept these proposals then Alice wants to swap one of her $1 cash with Bob's $1
- 1:58
Bond so so she creates an asset swap proposal for Bob which Bob accepts let us see how we will Design This using daml Code one way is to have a cash template a bond template and transfer proposals for each of them so that you can use the propose accept pattern for a party to swap cash with Bond it will need a cash to bond swap proposal because the data types of what is offered is different from what is requested so far so good but the problem
- 2:30
in this design is that if the business wants to swap another type of instrument such as a token you need more swap proposals since swap functionality is used by a front-end application it must also be changed also as indicated by the direction of arrows these swap proposals depend on the underlying cash and bond templates which in turn have their own dependencies so the frontend application is impacted by changes in any any of these dependencies this design approach makes
- 3:03
your application brittle and difficult to extend daml interfaces address this problem you can create an interface layer that has four components needed for the asset swap scenario the first component is an interface called I asset that will be implemented by instruments such as cash and bond this interface has v asset as its view type recall that this View U type is a record that contains data about the asset the V asset record has issuer owner observers
- 3:37
quantity and asset type the value in the asset type field will differentiate cash from Bond there are four method signatures set owner set OBS set quantity and transfer to the interface provides only the signature the templates must Implement these methods finally there are three choices set observers proposed transfer and transfer since each asset type requires its own transfer proposal you need another interface shown here called it
- 4:11
transfer proposal note that it has its own record V transfer proposal for view type and asset of type I asset in its field it has one choice named accept transfer which returns a contract ID of type I asset also note that there is no direct reference to any of the specific asset types such as cash or Bond now your interface layer design is ready here is the implementation layer in the assets module the cash and bond templates implement the I asset
- 4:46
interface for that they will create an instance of the interface and implement the four methods set owner set OBS set quantity and transfer to similarly the bond transfer proposal and cash transfer proposals implement the it transfer proposal interface once you have these two layers set up you can create the swap functionality by using the I asset interface from the interface layer this way it will not get impacted by any changes in the implementation layer it
- 5:20
also allows adding new types of assets making swap functionality extensible now let us look at the code first the interface module is in a package named extensible swap the V asset data record is defined here with issuer owner OBS quantity and a set type this is then used as view type in the I asset interface there are four method signatures set owner set OBS set quantity and transfer to the three
- 5:52
choices are set observers proposed transfer and transfer this interface is implemented by by the cache template the template has its own Fields issuer owner OBS and quantity in the wear block it creates an instance of the interface in the line interface instance I asset for Cash wear within this wear block two things are implemented first it allocates V asset values to The View with asset type as cash then it implements the four
- 6:26
functions from the interface note the use of the two interface function here the two interface function converts a template value into an interface value in this code the function this will give the value of cash type but the function signature should evaluate to I asset type so two interface will convert cach to its interface I asset type there are several other functions in the daml library such as from interface to interface contract ID and from interface
- 6:59
contract ID that you will find useful when working with daml interfaces also note that these methods are pure code they do not have a ledger interaction this is important because it allows them to be later combined in the choices if required without adding extraneous intermediate Ledger events similarly the bond template implements the I asset template in this example we have only asset type assigned to the text value Bond and the transfer
- 7:31
to function using Bond transfer proposal in a real world application the differences between cash and bond asset types will be much more complex finally there is I transfer proposal that has the view type V transfer proposal and one choice named except transfer notice that there are no methods in it which may happen in some interfaces the two templates implementing this interface are are cash transfer proposal and bond transfer
- 8:02
proposal now you have the interface and the implementation layer ready to build the swap functionality in this swap module there is an asset swap proposal template it takes requestor and receiver as two parties offer spec and requested spec as two tuples and contract ID of the I asset offered for swap in the settle choice you exercise the transer for choice on both of them one by one this completes the swap as you may already know a swap in a real world scenario
- 8:37
will have many more checks and balances before the transfer happens but this should give you a basic idea of how interfaces work the key idea is that the swap functionality did not have to know which types of assets are being swapped as long as they have implemented the I asset interface finally let us look at the script to play lay out the scenario first set up The Ledger starting state by creating some parties using the init party script which creates three parties
- 9:08
P bank P Alice and P Bob this script Returns the test scripts to be used in setting up initial assets init assets creates the payload for cash and bond then pbank creates five cash transfer proposals for Alice and five Bond transfer proposals for Bob finally Alice and Bob accept the proposals which creates assets for them if you run this script The Ledger will look like this Alice has five cash
- 9:40
assets and Bob has five Bond Assets Now let us test the swap template run the init asset script to get assets ready then Alice gets one cash asset ready to swap on that asset she exercises the set observer's choice voice to make Bob an observer note the use of the two interface contract ID function to convert the cach C ID to its interface I asset CID then prepare the offer spec and requested spec for cash and bond respectively then Alice creates a swap
- 10:18
proposal making herself as requester and Bob as receiver as well as giving the offer and requested specs then Bob queries the bonds to to find one bond that matches the requested spec finally Bob settles the choice when you run this script here is what The Ledger will look like you can see that Alice now has a bond and Bob now has a cash asset let us now summarize daml interfaces have a view type record method signatures and choices the choices are fully
- 10:53
implemented but the methods are not daml templates can Implement these interfaces es by creating an interface instance filling data in the view type record and implementing the methods daml interfaces help you build extensible daml applications for that you create an interface layer that contains all the interfaces this layer forms the API that the front-end application can use the second layer will have the interface implementations separating the two
- 11:26
layers gives you the flexibility to add new featur features and helps make your upgrades easier as your daml application evolves