da-syndication-finance-temp.../daml/Workflow/Transfer.daml

56 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-05-08 20:09:16 +00:00
module Workflow.Transfer where
import DA.Assert ((===))
import DA.Set (fromList)
import Daml.Finance.Interface.Holding.Base qualified as Holding (I)
import Daml.Finance.Interface.Holding.Transferable qualified as Transferable (I, Transfer(..))
import Daml.Finance.Interface.Holding.Util (getAmount, getInstrument)
import Daml.Finance.Interface.Types.Common.Types (AccountKey(..), InstrumentKey)
-- | Initiate / Accept template to transfer a holding to a new owner.
template Request
with
receiverAccount : AccountKey
-- ^ The account where the holding is sent.
instrument : InstrumentKey
-- ^ The instrument referenced by the holding to be transferred.
amount : Decimal
-- ^ Number of units to be transferred.
currentOwner : Party
-- ^ The owner of the holding to be transferred.
where
signatory receiverAccount.owner
observer currentOwner
ensure amount > 0.0
choice Accept : ContractId Holding.I
with
holdingCid : ContractId Holding.I
controller currentOwner
do
-- Sanity checks
holding <- fetch holdingCid
getAmount holding === amount
getInstrument holding === instrument
-- DO_TRANSFER_BEGIN
let transferableCid = coerceInterfaceContractId @Transferable.I holdingCid
newTransferableCid <- exercise transferableCid Transferable.Transfer with
actors = fromList [currentOwner, receiverAccount.owner]
newOwnerAccount = receiverAccount
pure $ toInterfaceContractId @Holding.I newTransferableCid
-- DO_TRANSFER_END
choice Decline : ()
-- ^ Decline the request.
controller currentOwner
do pure ()
choice Withdraw : ()
-- ^ Withdraw the request.
controller receiverAccount.owner
do pure ()