Concat/collab dataset from multiple contracts and create another contract

Ok, I can work with that. Here is some code that I believe does what you’re asking for here:

module Main where

import Daml.Script

template ContractA
  with
    sig: Party
    id: Text
    argument: Text
    obs: Party
  where
    signatory sig
    observer obs
    key (sig, id) : (Party, Text)
    maintainer key._1

template ContractB
  with
    sig: Party
    collabData: [(Party, Text)]
  where
    signatory sig


setup : Script ()
setup = script do
  party1 <- allocatePartyWithHint "party1" (PartyIdHint "party2")
  party2 <- allocatePartyWithHint "party2" (PartyIdHint "party1")
  mainParty <- allocatePartyWithHint "mainParty" (PartyIdHint "mainParty")

  -- party1 creates ContractA (signatory party1, observer mainParty) with key (party1,keyData)
  submit party1 do
    createCmd ContractA with sig = party1, obs = mainParty, id = "keyData", argument = "some data"

  -- party2 creates ContractA (signatory party2, observer mainParty) with key (party2,keyData)
  submit party2 do
    createCmd ContractA with sig = party2, obs = mainParty, id = "keyData", argument = "some other data"

  -- mainParty
  submit mainParty do
    createAndExerciseCmd (DoCollection with sig = mainParty) CreateContractB with parties = [party1, party2], id = "keyData"
  return ()

template DoCollection
  with
    sig: Party
  where
    signatory sig
    preconsuming choice CreateContractB : ContractId ContractB
      with parties: [Party]
           id: Text
      controller sig
      do
        -- 1) iterate over given parties (here [party1,party2]), and
        -- 3)Then store it in variable results
        results <- forA parties (\party -> do
          -- 2) fetch the contract; note: there can only be (at most) one because
          --    keys enforce uniqueness
          (_, payload) <- fetchByKey @ContractA (party, id)
          -- If there is no contract with this key, or mainParty is not an observer,
          -- we crash here.

          -- 2) Once I get the respective contracts I will retrieve subset of data
          --    from fetched ContractA
          return (party, payload.argument))
        -- 4) Once all( in this case it’s just 2 ) the subset is stored in local
        --    variable call next contract (ContractB) with these dataset
        create ContractB with collabData = results, ..
2 Likes