0

In DAML, can I save a contractId of B in contract A? I tried the followings but creating contract returns an update and I cannot save that update anywhere or even access to its data.


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

Frankie
  • 113
  • 5

1 Answers1

1

You've got the right idea with cid <- ..., but that will create a new local variable cid with the contract id in it. All data in DAML is immutable, which means you can't write into this.cid. You have to archive the contract and recreate it to change the data stored on it:

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

Note that this will only work with anyone == p, though. p's authority is needed to create a OneContract with someParty = p, and the only authority available in the context of the CreateOneContracts choice is that of anyone.

bame
  • 960
  • 5
  • 7
  • it works. Thanks a lot. One more question, can this be done in one statement? e.g. without using newCid? – Frankie Jun 04 '19 at 22:32