daml/compiler/damlc/tests/bond-trading/Dvp.daml
2019-08-13 17:23:03 +01:00

177 lines
4.7 KiB
Haskell

-- Copyright (c) 2019 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
daml 1.2
module Dvp where
import Bond hiding (Withdraw, Reject, Accept)
import Bond qualified
import Cash hiding (Withdraw, Reject, Accept)
import Cash qualified
import DvpTerms
import DvpNotification hiding (Accept)
import DvpNotification qualified
import DA.Date
template DvpProposal
with
c : DvpTerms
where
signatory c.buyer
ensure isValidDvp c
controller c.seller can
Accept : DvpId
do
create Dvp with c = c
Reject : ()
do
return ()
controller c.buyer can
Withdraw : ()
do
return ()
type DvpId = ContractId Dvp
template Dvp
with
c : DvpTerms
where
ensure isValidDvp c
signatory c.buyer, c.seller
controller c.buyer can
Allocate : (DvpAllocatedId, CashId)
with cashCid : CashId
do
cash <- fetch cashCid
assert $ not $ isLocked cash
assert $ cash.amount == c.cashAmount
assert $ cash.issuer == c.cashIssuer
assert $ cash.currency == c.cashCurrency
lid <- create LockPoA with locker = c.seller; issuer = cash.issuer; owner = cash.owner
cashCid <- exercise cashCid Lock with lid; _lockMaturity = c.settleTime
alloc <- create DvpAllocated with c=c; cashCid
return (alloc, cashCid)
type DvpAllocatedId = ContractId DvpAllocated
template DvpAllocated
with
c : DvpTerms
cashCid : CashId
where
ensure isValidDvp c
signatory c.buyer, c.seller
controller c.seller can
Settle : SettleResult
with bondCid : BondId
do
assertAfter c.settleTime
bond <- fetch bondCid
assert $ bond.amount == c.bondAmount
assert $ bond.issuer == c.bondIssuer
assert $ bond.isin == c.bondIsin
assert $ bond.owner == c.seller
alloc <- create DvpAllocated with c; cashCid
bondCid <- exercise bondCid Bond.Transfer with newOwner = c.buyer
bondCid <- exercise bondCid Bond.Accept
cash <- fetch cashCid
assert $ isLocked cash
assert $ cash.amount == c.cashAmount
assert $ cash.issuer == c.cashIssuer
assert $ cash.currency == c.cashCurrency
assert $ cash.owner == c.buyer
cashCid <- exercise cashCid Cash.TransferToLocker
notificationCid <- create DvpNotification with c = c
return SettleResult with ..
data SettleResult = SettleResult
with
bondCid : BondId
cashCid : CashId
notificationCid : DvpNotificationId
main = scenario do
now <- passToDate $ date 2018 May 14
--2018-05-14T00:00Z
acmeBank <- getParty "AcmeBank"
alice <- getParty "Alice"
bob <- getParty "Bob"
cashAliceCid <-
submit acmeBank do
create CashTransferRequest with
issuer = acmeBank
owner = acmeBank
receiver = alice
currency = "USD"
amount = 100.0
locker = acmeBank
lockMaturity = None
cashAliceCid <-
submit alice do exercise cashAliceCid Cash.Accept
bondBobCid <-
submit acmeBank do create BondTransferRequest with issuer = acmeBank
owner = acmeBank
newOwner = bob
isin = "1234"
amount = 100.0
bondBobCid <-
submit bob do exercise bondBobCid Bond.Accept
dvpProposalCid <-
submit alice do
create DvpProposal with
c = DvpTerms with
buyer = alice
seller = bob
bondIssuer = acmeBank
bondIsin = "1234"
bondAmount = 100.0
cashIssuer = acmeBank
cashCurrency = "USD"
cashAmount = 100.0
settleTime = datetime 2018 Aug 14 0 0 0
dvpId = "abc"
dvpCid <- submit bob do exercise dvpProposalCid Accept
passToDate $ date 2018 Aug 14
(dvpAllocatedCid, _) <-
submit alice do exercise dvpCid Allocate with cashCid = cashAliceCid
r <-
submit bob do exercise dvpAllocatedCid Settle with bondCid = bondBobCid
submit alice do exercise r.notificationCid DvpNotification.Accept
submit alice do
c <- fetch r.bondCid
assertMsg "unexpected issuer" $ c.issuer == acmeBank
assertMsg "unexpected owner" $ c.owner == alice
assertMsg "unexpected isin" $ c.isin == "1234"
assertMsg "unexpected amount" $ c.amount == 100.0
submit bob do
c <- fetch r.cashCid
assertMsg "unexpected issuer" $ c.issuer == acmeBank
assertMsg "unexpected owner" $ c.owner == bob
assertMsg "unexpected currency" $ c.currency == "USD"
assertMsg "unexpected amount" $ c.amount == 100.0