在 DAML 中,如何在选择中获取 ContractId

In DAML, how to get ContractId in a choice

在DAML中,我可以在合约A中保存一个B的contractId吗?我尝试了以下操作,但创建了合同 returns 更新,我无法将该更新保存在任何地方,甚至无法访问其数据。


template OneContract
  with
    someParty : Party
    someText : Text
    someNumber : Int  
  where
    signatory someParty



template Main
  with
    anyone :  Party
    cid : ContractId OneContract
  where
    signatory anyone

    controller anyone can
      nonconsuming CreateOneContracts : ()
        with 
          p : Party
          int : Int
        do
-- cid is not bind to cid in the contract
          cid <- create OneContract with someParty = p, someText = "same", 
someNumber = int
-- let won't work since create returns an update 
          let x = create OneContract with someParty = p, someText = "same", 
someNumber = int
          pure()

您对 cid <- ... 的想法是正确的,但这将创建一个新的局部变量 cid,其中包含合约 ID。 DAML 中的所有数据都是不可变的,这意味着您无法写入 this.cid。您必须存档合同并重新创建它以更改存储在其中的数据:

template Main
  with
    anyone :  Party
    cid : ContractId OneContract
  where
    signatory anyone

    controller anyone can
      CreateOneContracts : ContractId Main
        with 
          p : Party
          int : Int
        do
          newCid <- create OneContract with someParty = p, someText = "same", someNumber = int
          create this with cid = newCid

请注意,这仅适用于 anyone == p。创建 OneContract with someParty = p 需要 p 的权限,在 CreateOneContracts 选择的上下文中唯一可用的权限是 anyone.

的权限