Skip to content

Instantly share code, notes, and snippets.

@kctam
Created February 6, 2021 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kctam/4db58f791c59959d5f9fae24273a7157 to your computer and use it in GitHub Desktop.
Save kctam/4db58f791c59959d5f9fae24273a7157 to your computer and use it in GitHub Desktop.
Deep Dive to DAML Tutorial 7
module Test.Intro.Testplayground where
import Daml.Script
import Intro.Asset
import Intro.Asset.Role
import Intro.Asset.Trade
test1 = do
alice <- allocateParty "Alice"
bank <- allocateParty "X-Bank"
ah_invite_alice_bank <- submit bank do
createCmd AssetHolderInvite with
issuer = bank
owner = alice
ah_bank_alice <- submit alice do
exerciseCmd ah_invite_alice_bank AssetHolderInvite_Accept
return (alice, bank, ah_bank_alice)
test2 = do
setupResult@(alice, bank, ah_bank_alice) <- test1
submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 1000.0
submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 700.0
pure()
test3 = do
setupResult@(alice, bank, ah_bank_alice) <- test1
assetCid <- submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 1000.0
splitResult <- submit alice do
exerciseCmd assetCid Split with
splitQuantity = 300.0
pure()
test4 = do
setupResult@(alice, bank, ah_bank_alice) <- test1
asset1Cid <- submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 1000.0
asset2Cid <- submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 500.0
assetMergedCid <- submit alice do
exerciseCmd asset1Cid Merge with
otherCid = asset2Cid
pure()
test5 = do
setupResult@(alice, bank, ah_bank_alice) <- test1
bob <- allocateParty "Bob"
ah_invite_bob_bank <- submit bank do
createCmd AssetHolderInvite with
issuer = bank
owner = bob
ah_bank_bob <- submit bob do
exerciseCmd ah_invite_bob_bank AssetHolderInvite_Accept
assetCid <- submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 1000.0
transferProposal <- submit alice do
exerciseCmd assetCid ProposeTransfer with
newOwner = bob
newAssetCid <- submit bob do
exerciseCmd ah_bank_bob Accept_Transfer with
transferProposalCid = transferProposal
pure()
test5_1 = do
setupResult@(alice, bank, ah_bank_alice) <- test1
bob <- allocateParty "Bob"
assetCid <- submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 1000.0
transferProposal <- submit alice do
exerciseCmd assetCid ProposeTransfer with
newOwner = bob
newAssetCid <- submitMustFail bob do
exerciseCmd transferProposal TransferProposal_Accept
pure()
test6 = do
-- step 1: setup AssetHolder and Asset for both side
alice <- allocateParty "Alice"
bob <- allocateParty "Bob"
bank <- allocateParty "X-Bank"
reg <- allocateParty "Y-Registry"
ah_invite_alice_bank <- submit bank do
createCmd AssetHolderInvite with
issuer = bank
owner = alice
ah_bank_alice <- submit alice do
exerciseCmd ah_invite_alice_bank AssetHolderInvite_Accept
ah_invite_bob_reg <- submit reg do
createCmd AssetHolderInvite with
issuer = reg
owner = bob
ah_reg_bob <- submit bob do
exerciseCmd ah_invite_bob_reg AssetHolderInvite_Accept
asset_bank_alice_Cid <- submit bank do
exerciseCmd ah_bank_alice Issue_Asset with
symbol = "USD"
quantity = 1000.0
asset_reg_bob_Cid <- submit reg do
exerciseCmd ah_reg_bob Issue_Asset with
symbol = "NASDAQ:AAPL"
quantity = 10.0
-- step 2: make Asset Contracts visible to each other
asset_bank_alice_Cid <- submit alice do
exerciseCmd asset_bank_alice_Cid SetObservers with
newObservers = [bob]
asset_reg_bob_Cid <- submit bob do
exerciseCmd asset_reg_bob_Cid SetObservers with
newObservers = [alice]
-- step 3: Alice prepares the information required for
-- the trade
ah_invite_alice_reg <- submit reg do
createCmd AssetHolderInvite with
issuer = reg
owner = alice
ah_reg_alice <- submit alice do
exerciseCmd ah_invite_alice_reg AssetHolderInvite_Accept
Some shareAsset <- queryContractId alice asset_reg_bob_Cid
Some bankAsset <- queryContractId alice asset_bank_alice_Cid
preapprove_shareAsset_Cid <- submit alice do
exerciseCmd ah_reg_alice Preapprove_Transfer with
asset = shareAsset
-- step 4: Alice create Trade contract with the info
-- prepared in step 3
tradeCid <- submit alice do
createCmd Trade with
baseAssetCid = asset_bank_alice_Cid
baseAsset = bankAsset
quoteApprovalCid = preapprove_shareAsset_Cid
quoteAsset = shareAsset
-- step 5: Bob prepares to settle the trade
ah_invite_bob_bank <- submit bank do
createCmd AssetHolderInvite with
issuer = bank
owner = bob
ah_bank_bob <- submit bob do
exerciseCmd ah_invite_bob_bank AssetHolderInvite_Accept
Some bankAsset <- queryContractId bob asset_bank_alice_Cid
preapprove_bankAsset_Cid <- submit bob do
exerciseCmd ah_bank_bob Preapprove_Transfer with
asset = bankAsset
-- step 6: Bob can settle the trade with the info
-- prepared in step 5
submit bob do
exerciseCmd tradeCid Trade_Settle with
quoteAssetCid = asset_reg_bob_Cid
baseApprovalCid = preapprove_bankAsset_Cid
pure()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment