How to get already allocated parties in a DAML script?

I have two scripts, one that allocates the parties on startup, and another that interacts with the ledger at runtime. How can I get a reference to my parties in the second one? If I use allocatePartyWithHint again I get Invalid argument: Party already exists.

The same issue exists I want to interact with a running and initialized ledger using the DAML REPL.

4 Likes

To get a reference to an already existing party you can use the --input-file feature and pass them in externally. That way it is also not hardcoded and you can change the party without having to change your code. To provide a simple example, let’s say you want to pass in a single party. First turn your script into a function:

test : Party -> Script ()
test p = do
  submit p $ createCmd …

Next create a JSON file that contains your argument. The format is the same DAML-LF JSON encoding used in the HTTP JSON API so here it is a simple string containing the party:

"Alice"

Now you can pass the path to that JSON file via --input-file and the script will be executed after applying it to the argument.

For daml repl there is no nice option for doing this atm. As a hack you can use partyFromText so something like Some x <- pure $ partyFromText "Alice".

3 Likes

Oh, this is a good hint. Is this an experimental feature? I had the same issue but couldn’t find any explanation in the manual.

It would be even more convenient if there would be a way to get the ListParties result from the party management endpoint. Then the dance with the JSON file wouldn’t be necessary.

--input-file has been there since the first version of DAML Script and is mentioned in the docs at https://docs.daml.com/daml-script/index.html. partyFromText is a bit of a questionable hack so I’m not sure if it is documented.

As for integrating with listParties, I fully agree that this is a great idea. It is tracked in Expose more of the party management API in DAML Script · Issue #5979 · digital-asset/daml · GitHub atm.

Oh really. You are right. I overlooked this.

Thanks, this answer helped me too.

Note that in SDK 1.3.0, you also have access to listKnownParties which is usually a nicer solution to this.

1 Like

I have tried that:

daml> lkp <- listKnownParties
daml> debug lkp
[DA.Internal.Prelude:540]: [PartyDetails {party = 'NetworkAdmin', displayName = Some "NetworkAdmin", isLocal = True},PartyDetails {party = 'ProcessAdmin', displayName = Some "ProcessAdmin", isLocal = True}]

But couldn’t extrct the party I need from the list. How can I do that?

Here is an example that hopefully illustrates it. If you have further questions, let me know!

daml> allocateParty "Alice"
'party-cdb498a2'
daml> listKnownParties
[PartyDetails {party = 'party-cdb498a2', displayName = Some "Alice", isLocal = True}]
daml> find (\d -> d.displayName == Some "Alice") <$> listKnownParties
Some (PartyDetails {party = 'party-cdb498a2', displayName = Some "Alice", isLocal = True})
daml> Some details <- find (\d -> d.displayName == Some "Alice") <$> listKnownParties
daml> details.party
'party-cdb498a2'

How is it that you don’t have to use the debug command? If I try issue listKnownParties I don’t get back any result:

daml> listKnownParties
daml> parties <- listKnownParties
daml> parties
File:     Line2.daml
Hidden:   no
Range:    12:10-12:17
Source:   typecheck
Severity: DsError
Message: 
  Line2.daml:12:11: error:
  • Couldn't match type ‘[]’ with ‘Script’
  Expected type: Script PartyDetails
  Actual type: [PartyDetails]
  • In a stmt of a 'do' block: _ <- parties
  In the expression:
  do _ <- parties
  return ()
  In an equation for ‘expr’:
  expr () (parties)
  = do _ <- parties
  return ()
daml> debug parties
[DA.Internal.Prelude:540]: [PartyDetails {party = 'NetworkAdmin', displayName = Some "NetworkAdmin", isLocal = True},PartyDetails {party = 'ProcessAdmin', displayName = Some "ProcessAdmin", isLocal = True}]
daml> 

Sorry, should have clarified this. I’m using the latest snapshot which automatically prints results.

1 Like

Ok, thanks