Lesson 2: Design Patterns
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/151
Transcript
2,226 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
in this lesson you will learn how to transfer the authority to act on a contract from one party to another using design patterns daml offers several design patterns of which this lesson focuses on two the propose and accept pattern which is also known as the initiate and accept pattern and the delegation pattern the propose and accept pattern is a powerful workflow that allows modeling the establishment of bilateral
- 0:34
agreements between two parties propose and accept pattern consists of two main steps one party initiates a contract proposal that contains the terms of the agreement the initiator specifies the necessary details and conditions for the contract such as parties involved payment terms and other relevant data The Proposal is a contract created on The Ledger with the initiator as the signatory and the recipient as the
- 1:06
Observer the recipient party reviews the terms on the contract proposal if the recipient party agrees to the terms they accept the proposal this results in the creation of a new contract on The Ledger archiving The Proposal itself this new contract represents the executed agreement between the parties and all the specified terms and conditions are now active and enforcable when there are more than two parties involved for instance three the
- 1:38
pattern could evolve to propose accept except and so on until you have enough accepts to proceed this pattern is commonly used in various real world scenarios such as trade agreements legal contracts and financial transactions as it serves several essential purposes let's take a look at how the pattern is implemented in daml the propose and accept pattern typically involves two contracts The Proposal contract and the result
- 2:09
contract for the code demo there is the transfer proposal template for the proposal contract and the IOU template for the result contract the IOU template has issuer and owner as signatories on an IOU contract the owner can exercise the transfer choice which is a consuming Choice exercising the transfer Choice archives the IOU contract and instead of immediately creating a new IOU contract
- 2:41
with a new owner it creates a transfer proposal contract a transfer proposal contract is created with the recipient which is passed as the argument of the transfer Choice also the ioo record of this is created which is the payload of the I OU contract on which the transfer choice is being exercised on the transfer proposal contract the issuer and owner are extracted out of the ioe record to be
- 3:12
the signatory the recipient is the Observer it is crucial that both the issuer and owner be the signatories in the transfer proposal template in this workflow the transfer proposal contract is created in place of the io contract that is archived upon exercising the transfer Choice which is consuming when the issuer and owner of the IOU are different parties if the issuer is the only signatory of the
- 3:43
transfer proposal contract the issuer can always exercise the implicit archive choice and cancel the debt without the owner's agreement therefore there must be at least two signatories for the transfer proposal contract also they must be equal to that of the IOU contract the issuer and the owner the recipient of the IOU can exercise either accept or reject on the transfer proposal contract if the recipient accepts The Proposal a new IOU
- 4:19
contract is created with the recipient as the new owner notice that the payload of the old IOU contract is used for creating a new IOU contract this is because the owner is the only information that needs to be updated regarding the IOU if the recipient rejects The Proposal a new contract is Created from the old IOU payload without updating any information also if the original owner of the IOU decides to cancel a proposal a new contract is created with the old
- 4:57
IO you payload without updating any information a new contract is Created from the payload of the old IOU contract when the proposal is either rejected or cancelled this is necessary because the original IOU contract is archived when the original owner of the IOU attempts to transfer the ownership to the recipient by exercising the transf for Choice since the IOU itself needs to remain regardless of whether the prop
- 5:29
proposal of the asset is rejected or cancelled a new IOU contract is Created from the original IOU payload on the other hand the proposal itself needs to be archived upon being accepted rejected or cancelled therefore all of the choices on the transfer proposal contract are consuming choices if any of the choices on the proposal contract is non-consuming either the recipient or the owner will be able able to exercise choices as many
- 6:01
times as they want creating as many IOU contracts as they want without any consequences which must be avoided in the test script there are Alice and Bob parties Bob creates an IOU contract as both issuer and owner since Bob is the owner of the IOU Bob can also exercise transfer on the IOU contract passing Alice as the recipient exercising the transfer choice on the IOU contract does not create a
- 6:35
new IOU contract instead it creates a new transfer proposal contract which Alice has an opportunity to either accept or reject here Alice accepts the proposal and becomes the new owner of the IOU contract the key of the proposed and accept pattern is the transfer of the authorization if a new IOU contract is created immediately when the transfer choice is exercised the recipient does not have
- 7:08
the opportunity to reject the offer by creating a proposal contract and assigning the recipient to be the controller of the accept and reject choices the authorization to create a new IOU contract is transferred from the original owner of the contract to the recipient of the contract in the transaction View at number 20 Alice exercises the accept choice on the proposal since this is the
- 7:37
root action of tx2 it is authorized by the submitting party Alice this is where Alice's Authority is captured the consequence of the exercise action is the creation of a new IOU contract which is authorized by the controller of the accept Choice Alice and the signatory of the transfer proposal contract Bob with Alice as the new owner and Bob as the issuer a new IOU contract is
- 8:07
created let's move on to the delegation pattern the delegation pattern is a workflow where one party transfers specific rights or permissions to another it authorizes the agent to exercise a choice on a contract without the principal explicitly committing the action the design pattern is implemented by one party delegating their Authority via an additional contract where both parties authorities are captured the agent exercises a choice on
- 8:40
a contract on behalf of the principal with lasting Authority in the example the pattern empowers party B with the authority to exercise a choice on behalf of party a this capability allows party B to perform actions on behalf of party a without party a having to to explicitly commit to each action let's revisit the IOU contract to understand how the pattern is implemented in daml as a reminder once an IO you
- 9:12
contract is created by an issuer and an owner the owner can exercise transfer to create a transfer proposal which the recipient can either accept or reject this is where the propose and accept pattern ends the workflow can be further elaborated by involving the delegation pattern the owner of an IOU can delegate their authorization to a bank and have the bank exercise transfer on behalf of the owner the delegation pattern typically
- 9:45
involves a contract on which controller party a has a choice and intends to delegate execution of the choice to party B in this example an IOU owner intends to delegate the transfer choice to an agent to initiate the delegation process the IOU owner can be the principal and as a signatory create a delegation agreement contract where both principal and agents authorities are captured here
- 10:16
a bank can be the agent to exercise the delegate transfer choice on the delegation agreement contract as the controller of the choice the delegate transfer Choice takes recipient and I oou ID as an IOU contract ID as arguments upon the agent exercising delegate transfer the transfer choice is exercised on the IOU contract notice that the IOU contract has a new field disclosed to since the agent exercising delegate transfer results in a consequence of exercising
- 10:55
transfer which is exercised on an IO contract the IOU contract must be disclosed to the agent by assigning the agent to be the Observer on the IOU contract the agent is authorized to view the contract to exercise transfer on behalf of the principal the owner also in the propose and accept pattern the owner directly exercises transfer but in the delegation pattern the agent's Authority is captured as the
- 11:29
controller of the delegate transfer Choice which leads to exercising transfer exercising transfer on an IOU creates a new transfer proposal contract which the recipient can accept or reject Let's test it out in the test script three parties are allocated Bank Alice and Bob first Bob creates an IOU contract where Bob is both the owner and the issuer since Bob intends to delegate his
- 12:01
authority to exercise transfer to a bank the IOU contract is disclosed to the bank party then Bob creates a delegation agreement contract where Bob is the principal and the bank is the agent the bank party exercises the delegate transfer choice on the agreement contract where the choice takes Alice as the recipient and Bob's initial IO you contract as arguments finally Alice accepts the proposal and a new IOU contract is created let's take a closer look at the
- 12:38
very first transaction where Bob creates a new IOU contract when the I OU contract is disclosed to the bank party for Bob to delegate his authority later everything works fine but what will happen if the contract is not disclosed to the bank let's change the disclos close to field from bank to Bob as soon as the contract is not disclosed to the bank party there is an error it says attempt to fetch or exercise a contract not visible to the
- 13:12
reading parties contract IOU in the delegation file act as Bank read as blank and disclosed to Bob it means that the bank party attempted to exercise a choice on an IOU contract as the acting party but the contract was only disclosed to Bob the committed transaction shows that the creation of the IOU and delegation agreement contracts was successful however the failed exercise indicates that attempting to exercise
- 13:46
delegate transfer failed under this subtransaction exercising delegate transfer was authorized by the submitting party Bank since it's the root action of the transaction the direct consequences of the action were supposed to be authorized by the controller of the Choice bank and the signatory of the agreement contract however the consequence of exercising delegate transfer is to exercise transfer on the IOU contract
- 14:17
which the bank is not authorized to view this explains the error as soon as the disclos to field on the IOU contract is switched back to the Bank party the error goes away The Ledger action number 10 shows the creation of the delegation agreement contract and number 20 shows the bank party exercising delegate transfer choice on the contract created from number one0 the consequence of number two is
- 14:51
number 21 where the transfer choice is exercised on the IOU contract one major difference between the propose and accept pattern and the delegation pattern is that the propose and accept pattern allows one party to authorize the other party to act on a contract once but the delegation pattern can allow one party to Grant the other party a lasting authority to act on their behalf multiple
- 15:21
times the delegate transfer Choice may become a non-consuming choice in which case the delegation agreement contract remains active upon exercising the choice in this scenario the principal grants a lasting authority to the agent to act on behalf of the principal rather than a single time authorization on the other hand every choice on the proposal contract needs to be a consuming one so that the recipient party may not accept the proposal
- 15:52
multiple times and the proposal itself becomes archived once the recipient uses the S single time authorization to accept the IOU once this lesson focused on how to transfer the authority to act on a contract from one party to another using the propose and accept pattern which is also known as the initiate and accept pattern and the delegation pattern the propose and accept pattern allows modeling bilateral agreements between
- 16:23
two parties while the delegation pattern authorizes the agent to exercise a choice on a contract without the principal explicitly committing the action the major difference between the two design patterns is that the propose and accept pattern allows one party to authorize the other party to act on a contract once but the delegation pattern allows one party to Grant the other party a lasting authority to act on their behalf multiple
- 16:57
times