Here it is. The only important parts here are
- 41-42 to get the service
- 45 to query parties
- 57-61 to allocate a party
import com.daml.ledger.api.v1.admin.PartyManagementServiceGrpc;
import com.daml.ledger.api.v1.admin.PartyManagementServiceGrpc.PartyManagementServiceBlockingStub;
import com.daml.ledger.api.v1.admin.PartyManagementServiceOuterClass.AllocatePartyRequest;
import com.daml.ledger.api.v1.admin.PartyManagementServiceOuterClass.AllocatePartyResponse;
import com.daml.ledger.api.v1.admin.PartyManagementServiceOuterClass.ListKnownPartiesRequest;
import com.daml.ledger.api.v1.admin.PartyManagementServiceOuterClass.ListKnownPartiesResponse;
import com.daml.ledger.api.v1.admin.PartyManagementServiceOuterClass.PartyDetails;
import com.google.gson.Gson;
import io.grpc.Channel;
import io.grpc.ManagedChannelBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
public class PartyManagementMain {
private final static Logger logger = LoggerFactory.getLogger(PartyManagementMain.class);
// application id used for sending commands
public static final String APP_ID = "PartyManagementApp";
public static void main(String[] args) {
// Extract host and port from arguments
if (args.length < 3) {
System.err.println("Usage: HOST PORT PARTY");
System.exit(-1);
}
String host = args[0];
int port = Integer.parseInt(args[1]);
String party = args[2];
// create a stub for the PackageManagementService
Channel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
PartyManagementServiceBlockingStub pms = PartyManagementServiceGrpc.newBlockingStub(channel);
// query the ledger for parties
ListKnownPartiesResponse parties = pms.listKnownParties(ListKnownPartiesRequest.newBuilder().build());
// check whether party exists
Optional<PartyDetails> partyDetails = parties.getPartyDetailsList().stream()
.filter(p -> p.getParty().equals(party))
.findAny();
// create party if needed and log the result
Gson gson = new Gson();
if(partyDetails.isPresent()) {
logger.info("Party found: " + gson.toJson(partyDetails.get()));
} else {
AllocatePartyResponse allocateResponse = pms.allocateParty(
AllocatePartyRequest.newBuilder()
.setDisplayName(party)
.setPartyIdHint(party)
.build());
logger.info("New party allocated: " + gson.toJson(allocateResponse.getPartyDetails()));
}
}
}