`Update` in `ensure` clause
- bernhardOP 3
I have an ensure clause as follows -
ensure validateAllocatedCash allocatedCashCid this
Where -validateAllocatedCash: ContractId C.Cash -> TradeProposalDVP -> Update Bool validateAllocatedCash cashCid tradeDetails = do cash <- fetch cashCid pure (cash.owner == tradeDetails.buyer && cash.amount >= tradeDetails.tradePrice * intToDecimal tradeDetails.quantity && cash.currency == tradeDetails.currency)Obviously this doesn’t work because
ensureexpects aBoolwhereasvalidateAllocatedCashreturnsUpdate Bool. Is there a way I could have transactions invalidateAllocatedCashinstead of just pure expressions? Basically I’m looking for an equivalent ofensureA… - cocreature 2
The
ensureclause is supposed to to hold during the entire lifetime of a contract. Currently this is enforced by checking it at contract creation and because it can only depend on the template data instead of an arbitraryUpdateexpression this is guaranteed to never be invalidated if it holds at contract creation.Now consider an
ensureAthat allows for anUpdateexpression like thefetchin your example. This could hold at contract creation but if the contract gets archived later it will not hold anymore. For this reason we currently do not have such a construct.If you do only want to check a condition at the time of contract creation, you might be able to create that contract from a choice and enforce the condition in that choice. One example of this are role contracts with multiple signatories and a nonconsuming choice for creating a contract. You can find more information on that pattern at https://docs.daml.com/daml/intro/6_Parties.html#use-role-contracts-for-ongoing-authorization.