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

时间:2019-06-04 06:03:24

标签: daml

在DAML中,我可以在合同A中保存B的contractId吗?我尝试了以下操作,但是创建合同会返回更新,并且无法将更新保存到任何地方,甚至无法访问其数据。


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()

1 个答案:

答案 0 :(得分:1)

使用cid <- ...有了正确的主意,但这将创建一个包含合同ID的新局部变量cid。 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。创建p需要OneContract with someParty = p的权限,并且在CreateOneContracts选择的上下文中唯一可用的权限是anyone的权限。