Asset Modeling in DAML (Tutorial-7 Deep Dive Part 2)

Overview

In Part 1 we have seen how asset is created and transferred. In this part we focus on asset trading in Tutorial 7.

  • Bob holds an Asset issued by Y-Registry, which is NASDAQ:AAPL 10.0.
  • Alice and Bob are trading their assets. The result is that the asset issued by X-Bank (USD 1,000) goes to Bob, while the Asset issued by Y-Registry (NASDAQ:AAPL 10.0) goes to Alice.

Flow of Asset Trading

The overall flow to complete a trade in this test,

  1. Alice and Bob include the other side as observers. We see later this helps both sides to create the Trade contract and exercise Trade_Settle, respectively.
  2. Alice prepares the information needed to create Trade contract (see analysis below).
  3. Alice creates Trade contract. This serves as an offer to Bob.
  4. Bob accepts this offer by exercising Trade_Settle in Trade contract. To accept this offer, Bob needs to provide additional information to complete the flow (see analysis below).
  5. The logic built-in Trade_Settle completes the whole process.

Analysis of Contract Design

What information is needed to complete the Trade_Settle? Here are all we need.

Flow Walk-through

Step 1: Setup of AssetHolder and Asset Contracts for both parties

This is identical to what we have depicted in Part 1. Here we just showed the test script part.

Step 2: Observer setting

A choice SetObservers is inside Asset contract template.

SetObservers choice in Asset template (Asset.daml)

Step 3: Preparation of Information for Trade Contract

As discussed above, Alice needs four items to create the Trade contract. Among them, her Asset contract ID and data are already known to her. As an observer she can fetch the Bob’s Asset contract detail. The missing item is her Preapproval.

Choice Preapprove_Transfer in AssetHolder template (Asset/Role.daml)
  • Line 174 is to obtain the Asset detail of Bob’s Asset (as now Alice is an observer.)
  • Line 176 is to obtain the Asset detail of Alice’s own Asset.
  • Line 178–180 is to create the approval of Bob’s asset and Alice as the new owner.

Step 4: Creation of Trade Contract

With the items ready in Step 3, we are ready to construct the Trade contract. Here is the Trade contract template (choices not shown yet).

Trade template (Asset/Trade.daml)

Step 5: Preparation for Trade Settlement

As discussed above, Bob needs two items to exercise the Trade_Settle choice. Besides his own Asset contract ID, which is known to him, he needs a TransferApproval contract. The flow is similar to Alice’s in Step 3: first X-Bank issues AssetHolder contract to Bob, and Bob exercises Preapprove_Transfer choice by giving Alice’s asset detail.

  • Line 202 is to obtain the asset detail of Alice’s asset (as now Bob is an observer.)
  • Line 204–206 is to create the approval of Alice’s asset and Bob as the new owner.

Step 6: Trade Settlement

After Bob has all items needed to settle the trade, he exercises Trade_Settle in Alice’s Trade Contract.

Choice Trade_Settle in Trade template (Asset/Trade.daml)
Choice TransferApproval_Transfer in TransferApproval template (Asset.daml)

Summary

In this part we have walked through how an atomic transaction is made between two parties. With careful design in the templates and various choices, the asset swap is made to ensure both transfers were made with approval from both sides, and this was done in one transaction (one commit).

Visit http://www.ledgertech.biz/kcarticles.html for all my works. Reach me on https://www.linkedin.com/in/ktam1/ or follow me @kctheservant in Twitter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store