├── PingPong
├── multi-package.yaml
├── script
│ ├── daml.yaml
│ └── daml
│ │ └── PingPongTest.daml
├── main
│ ├── daml.yaml
│ └── daml
│ │ └── PingPong.daml
├── src
│ └── main
│ │ ├── resources
│ │ └── logback.xml
│ │ └── java
│ │ └── examples
│ │ └── pingpong
│ │ ├── codegen
│ │ ├── PingPongCodegenMain.java
│ │ └── PingPongProcessor.java
│ │ └── grpc
│ │ ├── PingPongGrpcMain.java
│ │ └── PingPongProcessor.java
├── pom.xml
├── start.sh
└── README.rst
├── StockExchange
├── daml.yaml
├── src
│ └── main
│ │ ├── resources
│ │ └── logback.xml
│ │ └── java
│ │ └── examples
│ │ └── stockexchange
│ │ ├── ParticipantSession.java
│ │ ├── parties
│ │ ├── Bank.java
│ │ ├── Buyer.java
│ │ ├── Seller.java
│ │ └── StockExchange.java
│ │ └── Common.java
├── canton_ledger.conf
├── pom.xml
├── setup.sh
├── daml
│ └── StockExchange.daml
├── stock_exchange_bootstrap_script.canton
└── README.rst
├── .gitignore
├── README.rst
├── .github
└── workflows
│ └── cla.yml
└── LICENSE
/PingPong/multi-package.yaml:
--------------------------------------------------------------------------------
1 | packages:
2 | - ./main
3 | - ./script
--------------------------------------------------------------------------------
/PingPong/script/daml.yaml:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | # SPDX-License-Identifier: Apache-2.0
3 |
4 | sdk-version: 3.4.9
5 | name: ex-java-bindings-script
6 | source: daml/PingPongTest.daml
7 | init-script: PingPongTest:setup
8 | version: 0.0.2
9 | dependencies:
10 | - daml-prim
11 | - daml-stdlib
12 | - daml-script
13 |
--------------------------------------------------------------------------------
/StockExchange/daml.yaml:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | # SPDX-License-Identifier: Apache-2.0
3 |
4 | sdk-version: 3.4.9
5 | name: ex-java-bindings-stock-exchange
6 | source: daml/StockExchange.daml
7 | version: 0.0.1
8 | dependencies:
9 | - daml-prim
10 | - daml-stdlib
11 | codegen:
12 | java:
13 | package-prefix: examples.codegen
14 | output-directory: src/main/java/
15 |
--------------------------------------------------------------------------------
/PingPong/main/daml.yaml:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | # SPDX-License-Identifier: Apache-2.0
3 |
4 | sdk-version: 3.4.9
5 | name: ex-java-bindings
6 | source: daml/PingPong.daml
7 | init-script: PingPong:setup
8 | version: 0.0.2
9 | dependencies:
10 | - daml-prim
11 | - daml-stdlib
12 | codegen:
13 | java:
14 | package-prefix: examples.pingpong.codegen
15 | output-directory: ../src/main/java/
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2020, Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | # SPDX-License-Identifier: Apache-2.0
3 |
4 | lib/
5 | logs/
6 | log/
7 | target/
8 | **/navigator.log
9 | **/sandbox.log
10 | dependency-reduced-pom.xml
11 | .navigator.conf
12 | navigator.history
13 | *.iml
14 | .idea/
15 | example-ping-pong-java.iml
16 | .vscode/settings.json
17 | **/.daml
18 | StockExchange/temp_stock_exchange_example
19 | StockExchange/src/main/java/examples/codegen
20 |
--------------------------------------------------------------------------------
/PingPong/script/daml/PingPongTest.daml:
--------------------------------------------------------------------------------
1 | -- Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | -- SPDX-License-Identifier: Apache-2.0
3 |
4 | module PingPongTest where
5 |
6 | import Daml.Script
7 |
8 | setup : Script()
9 | setup = script do
10 | -- Set up parties
11 | alice <- allocatePartyByHint (PartyIdHint "Alice")
12 | bob <- allocatePartyByHint (PartyIdHint "Bob")
13 |
14 | -- Needed in 2.0, see https://docs.daml.com/tools/navigator/index.html
15 | aliceId <- validateUserId "alice"
16 | bobId <- validateUserId "bob"
17 | createUser (User aliceId (Some alice)) [CanActAs alice]
18 | createUser (User bobId (Some bob)) [CanActAs bob]
19 |
--------------------------------------------------------------------------------
/PingPong/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
14 |
15 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/StockExchange/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
14 |
15 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | Java Bindings Examples
2 | ----------------------
3 |
4 | ::
5 |
6 | Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
7 | SPDX-License-Identifier: Apache-2.0.0
8 |
9 | This repository contains two subprojects that demonstrate building of Daml client applications with the usage of Java Bindings:
10 |
11 | - `Ping-Pong `_ is a collection of three examples that shows how a Java application would use the `Java Binding library `_ to connect to and exercise a Daml model running on a ledger
12 | - `Stock Exchange `_ shows an advanced use-case of the Java bindings for building a Daml client application that leverages off-ledger data distribution by using `Explicit Contract Disclosure `_
13 |
--------------------------------------------------------------------------------
/PingPong/main/daml/PingPong.daml:
--------------------------------------------------------------------------------
1 | -- Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | -- SPDX-License-Identifier: Apache-2.0
3 |
4 | module PingPong where
5 |
6 | template Ping
7 | with
8 | sender: Party
9 | receiver: Party
10 | count: Int
11 | where
12 | signatory sender
13 | observer receiver
14 |
15 | choice RespondPong : ()
16 | controller receiver
17 | do
18 | if count > 10 then return ()
19 | else do
20 | create Pong with sender = receiver; receiver = sender; count = count + 1
21 | return ()
22 |
23 | template Pong
24 | with
25 | sender: Party
26 | receiver: Party
27 | count: Int
28 | where
29 | signatory sender
30 | observer receiver
31 |
32 | choice RespondPing : ()
33 | controller receiver
34 | do
35 | if count > 10 then return ()
36 | else do
37 | create Ping with sender = receiver; receiver = sender; count = count + 1
38 | return ()
39 |
--------------------------------------------------------------------------------
/StockExchange/canton_ledger.conf:
--------------------------------------------------------------------------------
1 | canton {
2 | participants {
3 | stockExchangeParticipant {
4 | http-ledger-api.port = 5013
5 | admin-api.port = 5012
6 | ledger-api.port = 5011
7 | }
8 | bankParticipant {
9 | http-ledger-api.port = 5023
10 | admin-api.port = 5022
11 | ledger-api.port = 5021
12 | }
13 | buyerParticipant {
14 | http-ledger-api.port = 5033
15 | admin-api.port = 5032
16 | ledger-api.port = 5031
17 | }
18 | sellerParticipant {
19 | http-ledger-api.port = 5043
20 | admin-api.port = 5042
21 | ledger-api.port = 5041
22 | }
23 | }
24 | sequencers.local {
25 | public-api {
26 | address = localhost
27 | port = 5018
28 | }
29 | admin-api {
30 | address = localhost
31 | port = 5019
32 | }
33 | }
34 | mediators.localMediator {
35 | admin-api {
36 | address = localhost
37 | port = 5017
38 | }
39 | }
40 | // enable ledger_api commands for setup simplicity of the Ledger API
41 | features.enable-testing-commands = yes
42 | }
43 |
--------------------------------------------------------------------------------
/PingPong/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | com.daml.ledger.examples
6 | example-ping-pong-java
7 | jar
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | UTF-8
12 | 11
13 | 11
14 | 3.4.9
15 |
16 |
17 |
18 |
19 | com.daml
20 | bindings-java
21 | ${sdk-version}
22 |
23 |
24 | ch.qos.logback
25 | logback-classic
26 | 1.4.12
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/StockExchange/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | com.daml.ledger.examples
6 | example-stock-exchange-java
7 | jar
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | UTF-8
12 | 11
13 | 11
14 | 3.4.9
15 |
16 |
17 |
18 |
19 | com.daml
20 | bindings-java
21 | ${sdk-version}
22 |
23 |
24 | ch.qos.logback
25 | logback-classic
26 | 1.4.12
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/PingPong/start.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | set -euo pipefail
3 |
4 | function getSandboxPid(){
5 | ss -lptn 'sport = :7575' | grep -P -o '(?<=pid=)([0-9]+)'
6 | }
7 | function cleanup(){
8 | echo "Cleaning up"
9 | sandboxPID=$(ss -lptn 'sport = :7575' | grep -P -o '(?<=pid=)([0-9]+)')
10 | if [[ $sandboxPID ]]; then
11 | # kill the sandbox which is running in the background
12 | kill $sandboxPID
13 | rm ports.json
14 | echo "Done"
15 | fi
16 | }
17 |
18 | trap cleanup ERR EXIT
19 |
20 | echo "Compiling daml"
21 | dpm build --all
22 |
23 | pushd main
24 | packageId=$(dpm damlc inspect-dar --json .daml/dist/ex-java-bindings-0.0.2.dar | jq '.main_package_id' -r)
25 |
26 | echo "Generating java code"
27 | dpm codegen-java
28 | popd
29 |
30 | echo "Compiling code"
31 | mvn compile
32 |
33 | # Could also run this manually in another terminal without the redirects
34 | echo "Starting sandbox"
35 | dpm sandbox \
36 | --ledger-api-port 7600 \
37 | --json-api-port 7575 \
38 | --canton-port-file ports.json \
39 | --dar main/.daml/dist/ex-java-bindings-0.0.2.dar \
40 | > sandbox.log 2>&1 & PID=$!
41 |
42 | echo "Waiting for sandbox to write the port file"
43 | until [ -e ports.json ]; do sleep 1; done
44 |
45 | echo "Running the script"
46 | dpm script \
47 | --dar script/.daml/dist/ex-java-bindings-script-0.0.2.dar \
48 | --ledger-host localhost \
49 | --ledger-port 7600 \
50 | --script-name PingPongTest:setup
51 |
52 |
53 | while [[ "$(getSandboxPid)" -eq '' ]]
54 | do
55 | sleep 1
56 | done
57 |
58 | echo "Running the java program"
59 | mvn exec:java -Dexec.mainClass=$1 -Dpackage.id=$packageId -Dexec.args="localhost 7600"
60 |
--------------------------------------------------------------------------------
/StockExchange/setup.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | set -o pipefail
3 |
4 | function build_example() {
5 | echo "Compiling daml"
6 | dpm build
7 |
8 | echo "Generating java code"
9 | dpm codegen-java
10 |
11 | echo "Compiling code"
12 | mvn compile
13 | }
14 |
15 | function start_canton() {
16 | CANTON_PATH=$1
17 | if [[ -z "$CANTON_PATH" ]]; then
18 | echo "Pass the path to the Canton install dir"
19 | else
20 | echo "Starting Canton"
21 | "$CANTON_PATH"/bin/canton daemon -c canton_ledger.conf --bootstrap stock_exchange_bootstrap_script.canton
22 | fi
23 | }
24 |
25 | function run_stock_exchange() {
26 | echo "Running StockExchange"
27 | stockExchangePartiesFile="temp_stock_exchange_example/stock_exchange_parties.txt"
28 | if [ -e "$stockExchangePartiesFile" ]; then
29 | buyerPartyId=$(sed -n "3p" $stockExchangePartiesFile)
30 | sellerPartyId=$(sed -n "4p" $stockExchangePartiesFile)
31 | stockExchangePartyId=$(sed -n "1p" $stockExchangePartiesFile)
32 | mvn exec:java -Dexec.mainClass=examples.stockexchange.parties.Bank -Dexec.args="5021 Bank ""$buyerPartyId"" 10"
33 | mvn exec:java -Dexec.mainClass=examples.stockexchange.parties.StockExchange -Dexec.args="5011 StockExchange ""$sellerPartyId"" Daml 3"
34 | mvn exec:java -Dexec.mainClass=examples.stockexchange.parties.Seller -Dexec.args="5041 Seller ""$stockExchangePartyId"""
35 | mvn exec:java -Dexec.mainClass=examples.stockexchange.parties.Buyer -Dexec.args="5031 Buyer"
36 | echo "Finished StockExchange example"
37 | else
38 | echo "'$stockExchangePartiesFile' does not exist. Check that the current user has write rights in the current dir and run start_canton before running this function"
39 | fi
40 | }
41 |
--------------------------------------------------------------------------------
/StockExchange/src/main/java/examples/stockexchange/ParticipantSession.java:
--------------------------------------------------------------------------------
1 | package examples.stockexchange;
2 |
3 | import com.daml.ledger.api.v2.CommandServiceGrpc;
4 | import com.daml.ledger.api.v2.StateServiceGrpc;
5 | import com.daml.ledger.api.v2.admin.UserManagementServiceGrpc;
6 | import com.daml.ledger.api.v2.admin.UserManagementServiceGrpc.UserManagementServiceBlockingStub;
7 | import com.daml.ledger.javaapi.data.GetUserRequest;
8 | import com.daml.ledger.api.v2.StateServiceGrpc.StateServiceBlockingStub;
9 | import io.grpc.ManagedChannel;
10 | import io.grpc.ManagedChannelBuilder;
11 |
12 | public class ParticipantSession implements AutoCloseable {
13 | private final String partyId;
14 | private final ManagedChannel channel;
15 | private final StateServiceBlockingStub stateService;
16 | private final UserManagementServiceBlockingStub userManagementService;
17 | private final CommandServiceGrpc.CommandServiceBlockingStub commandService;
18 |
19 | public ParticipantSession(int ledgerApiPort, String userId) {
20 | channel = ManagedChannelBuilder.forAddress("127.0.0.1", ledgerApiPort).usePlaintext().build();
21 | stateService = StateServiceGrpc.newBlockingStub(channel);
22 | userManagementService = UserManagementServiceGrpc.newBlockingStub(channel);
23 | commandService = CommandServiceGrpc.newBlockingStub(channel);
24 | this.partyId =
25 | userManagementService
26 | .getUser(new GetUserRequest(userId).toProto())
27 | .getUser()
28 | .getPrimaryParty();
29 | }
30 |
31 | public String getPartyId() {
32 | return partyId;
33 | }
34 |
35 | public StateServiceBlockingStub getStateService() { return stateService; }
36 | public CommandServiceGrpc.CommandServiceBlockingStub getCommandService() { return commandService; }
37 |
38 | @Override
39 | public void close() throws Exception {
40 | channel.shutdown();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/StockExchange/src/main/java/examples/stockexchange/parties/Bank.java:
--------------------------------------------------------------------------------
1 | package examples.stockexchange.parties;
2 |
3 | import com.daml.ledger.javaapi.data.Command;
4 | import com.daml.ledger.javaapi.data.CommandsSubmission;
5 | import com.daml.ledger.javaapi.data.SubmitAndWaitRequest;
6 | import examples.codegen.stockexchange.IOU;
7 | import examples.stockexchange.Common;
8 | import examples.stockexchange.ParticipantSession;
9 | import java.util.List;
10 | import java.util.Optional;
11 | import java.util.UUID;
12 | import org.slf4j.Logger;
13 | import org.slf4j.LoggerFactory;
14 |
15 | public class Bank {
16 | private static final Logger logger = LoggerFactory.getLogger(Bank.class);
17 |
18 | public static void main(String[] args) throws Exception {
19 | if (args.length < 4)
20 | throw new IllegalArgumentException(
21 | "Arguments: ");
22 | int ledgerApiPort = Integer.parseInt(args[0]);
23 | String userId = args[1];
24 | String buyerPartyId = args[2];
25 | long issuedIouValue = Long.parseLong(args[3]);
26 |
27 | logger.info("BANK: Initializing");
28 |
29 | try (ParticipantSession participantSession = new ParticipantSession(ledgerApiPort, userId)) {
30 | issueIou(participantSession, buyerPartyId, issuedIouValue);
31 | }
32 | }
33 |
34 | private static void issueIou(
35 | ParticipantSession participantSession, String buyerPartyId, long issuedIouValue) {
36 | List newIouCommand =
37 | new IOU(participantSession.getPartyId(), buyerPartyId, issuedIouValue).create().commands();
38 | CommandsSubmission commandsSubmission =
39 | CommandsSubmission.create(Common.APP_ID, UUID.randomUUID().toString(), Optional.empty(), newIouCommand)
40 | .withWorkflowId("Bank-issue-IOU")
41 | .withActAs(participantSession.getPartyId());
42 |
43 | logger.info("BANK: Issuing IOU with value {} to {}", issuedIouValue, buyerPartyId);
44 | participantSession
45 | .getCommandService()
46 | .submitAndWait(SubmitAndWaitRequest.toProto(commandsSubmission));
47 |
48 | logger.info("BANK: Done");
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/StockExchange/daml/StockExchange.daml:
--------------------------------------------------------------------------------
1 | module StockExchange where
2 |
3 | import DA.Assert
4 | import DA.Action
5 |
6 | template IOU
7 | with
8 | issuer: Party
9 | owner: Party
10 | value: Int
11 | where
12 | signatory issuer
13 | observer owner
14 |
15 | choice IOU_Transfer: ()
16 | with
17 | target: Party
18 | amount: Int
19 | controller owner
20 | do
21 | -- Check that the transferred amount is not higher than the current IOU value
22 | assert (value >= amount)
23 | create this with issuer = issuer, owner = target, value = amount
24 | -- No need to create a new IOU for owner if the full value is transferred
25 | if value == amount then pure ()
26 | else void $ create this with issuer = issuer, owner = owner, value = value - amount
27 | pure ()
28 |
29 | template Stock
30 | with
31 | issuer: Party
32 | owner: Party
33 | stockName: Text
34 | where
35 | signatory issuer
36 | observer owner
37 |
38 | choice Stock_Transfer: ()
39 | with
40 | newOwner: Party
41 | controller owner
42 | do
43 | create this with owner = newOwner
44 | pure ()
45 |
46 | template PriceQuotation
47 | with
48 | issuer: Party
49 | stockName: Text
50 | value: Int
51 | where
52 | signatory issuer
53 |
54 | nonconsuming choice PriceQuotation_Fetch: PriceQuotation
55 | with fetcher: Party
56 | controller fetcher
57 | do pure this
58 |
59 | template Offer
60 | with
61 | seller: Party
62 | quotationProducer: Party
63 | offeredAssetCid: ContractId Stock
64 | where
65 | signatory seller
66 |
67 | choice Offer_Accept: ()
68 | with
69 | priceQuotationCid: ContractId PriceQuotation
70 | buyer: Party
71 | buyerIou: ContractId IOU
72 | controller buyer
73 | do
74 | priceQuotation <- exercise
75 | priceQuotationCid PriceQuotation_Fetch with
76 | fetcher = buyer
77 | asset <- fetch offeredAssetCid
78 |
79 | -- Assert the quotation issuer and asset name
80 | priceQuotation.issuer === quotationProducer
81 | priceQuotation.stockName === asset.stockName
82 |
83 | _ <- exercise
84 | offeredAssetCid Stock_Transfer with
85 | newOwner = buyer
86 |
87 | _ <- exercise
88 | buyerIou IOU_Transfer with target = seller, amount = priceQuotation.value
89 | pure ()
90 |
--------------------------------------------------------------------------------
/StockExchange/stock_exchange_bootstrap_script.canton:
--------------------------------------------------------------------------------
1 | import java.nio.file.{Paths, Files, StandardOpenOption}
2 | import java.nio.charset.StandardCharsets
3 | import com.digitalasset.canton.config.RequireTypes.PositiveInt
4 | import com.digitalasset.canton.version.ProtocolVersion
5 |
6 | nodes.local.start()
7 |
8 | val staticSynchronizerParameters = StaticSynchronizerParameters.defaults(local.config.crypto, ProtocolVersion.forSynchronizer)
9 | val synchronizerOwners = Seq(local, localMediator)
10 | bootstrap.synchronizer("mysynchronizer", Seq(local), Seq(localMediator), synchronizerOwners, PositiveInt.one, staticSynchronizerParameters)
11 |
12 | stockExchangeParticipant.synchronizers.connect_local(local, "mysynchronizer")
13 | bankParticipant.synchronizers.connect_local(local, "mysynchronizer")
14 | buyerParticipant.synchronizers.connect_local(local, "mysynchronizer")
15 | sellerParticipant.synchronizers.connect_local(local, "mysynchronizer")
16 |
17 | println("All nodes are ready.")
18 |
19 | val javaBindingsDarPath = ".daml/dist/ex-java-bindings-stock-exchange-0.0.1.dar"
20 |
21 | stockExchangeParticipant.dars.upload(javaBindingsDarPath)
22 | bankParticipant.dars.upload(javaBindingsDarPath)
23 | buyerParticipant.dars.upload(javaBindingsDarPath)
24 | sellerParticipant.dars.upload(javaBindingsDarPath)
25 |
26 | val stockExchange = stockExchangeParticipant.parties.enable("stockExchange")
27 | val bank = bankParticipant.parties.enable("bank")
28 | val buyer = buyerParticipant.parties.enable("buyer")
29 | val seller = sellerParticipant.parties.enable("seller")
30 |
31 | // Write party ids to a file for allowing easy discovery
32 | val partiesFileContent = s"${stockExchange.toProtoPrimitive}\n${bank.toProtoPrimitive}\n${buyer.toProtoPrimitive}\n${seller.toProtoPrimitive}"
33 | Files.createDirectories(Paths.get("temp_stock_exchange_example"));
34 | Files.write(Paths.get("temp_stock_exchange_example/stock_exchange_parties.txt"), partiesFileContent.getBytes(StandardCharsets.UTF_8))
35 |
36 | println("Waiting for the parties to appear on their hosting participants' Ledger API...")
37 | utils.retry_until_true(buyerParticipant.ledger_api.parties.list().exists(_.party.toProtoPrimitive.toString.startsWith("buyer::")))
38 | utils.retry_until_true(sellerParticipant.ledger_api.parties.list().exists(_.party.toProtoPrimitive.toString.startsWith("seller::")))
39 | utils.retry_until_true(bankParticipant.ledger_api.parties.list().exists(_.party.toProtoPrimitive.toString.startsWith("bank::")))
40 | utils.retry_until_true(stockExchangeParticipant.ledger_api.parties.list().exists(_.party.toProtoPrimitive.toString.startsWith("stockExchange::")))
41 |
42 | stockExchangeParticipant.ledger_api.users.create("StockExchange", actAs = Set(stockExchange), primaryParty = Some(stockExchange))
43 | bankParticipant.ledger_api.users.create("Bank", actAs = Set(bank), primaryParty = Some(bank))
44 | buyerParticipant.ledger_api.users.create("Buyer", actAs = Set(buyer), primaryParty = Some(buyer))
45 | sellerParticipant.ledger_api.users.create("Seller", actAs = Set(seller), primaryParty = Some(seller))
46 |
47 | println("Canton server initialization DONE")
48 |
--------------------------------------------------------------------------------
/StockExchange/src/main/java/examples/stockexchange/Common.java:
--------------------------------------------------------------------------------
1 | package examples.stockexchange;
2 |
3 | import com.daml.ledger.api.v2.StateServiceGrpc.StateServiceBlockingStub;
4 | import com.daml.ledger.api.v2.StateServiceOuterClass.GetLedgerEndRequest;
5 | import com.daml.ledger.javaapi.data.*;
6 | import com.google.protobuf.ByteString;
7 | import java.io.*;
8 | import java.util.Base64;
9 | import java.util.Collections;
10 | import java.util.Optional;
11 | import java.util.Set;
12 |
13 | public class Common {
14 | public static final String APP_ID = "StockExchangeApp";
15 | public static final String PRICE_QUOTATION_DISCLOSED_CONTRACT_FILE =
16 | "temp_stock_exchange_example/price_quotation_disclosed_contract.txt";
17 | public static final String STOCK_DISCLOSED_CONTRACT_FILE =
18 | "temp_stock_exchange_example/stock_disclosed_contract.txt";
19 | public static final String OFFER_DISCLOSED_CONTRACT_FILE =
20 | "temp_stock_exchange_example/offer_disclosed_contract.txt";
21 |
22 | public static DisclosedContract fetchContractForDisclosure(
23 | StateServiceBlockingStub client, String reader, ContractFilter contractFilter) {
24 | Long ledgerEnd = client.getLedgerEnd(GetLedgerEndRequest.newBuilder().build()).getOffset();
25 | EventFormat eventFormat = contractFilter.withIncludeCreatedEventBlob(true).eventFormat(Optional.of(Set.of(reader)));
26 | CreatedEvent event = CreatedEvent.fromProto(
27 | client
28 | .getActiveContracts(new GetActiveContractsRequest(eventFormat, ledgerEnd).toProto())
29 | .next()
30 | .getActiveContract()
31 | .getCreatedEvent());
32 | return new DisclosedContract(
33 | event.getTemplateId(), event.getContractId(), event.getCreatedEventBlob());
34 | }
35 |
36 | public static void shareDisclosedContract(DisclosedContract disclosedContract, String fileName)
37 | throws IOException {
38 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, false))) {
39 | writer.append(
40 | String.format(
41 | "%s,%s,%s,%s,%s",
42 | disclosedContract.contractId.orElse(""),
43 | disclosedContract.templateId.map(Identifier::getPackageId).orElse(""),
44 | disclosedContract.templateId.map(Identifier::getModuleName).orElse(""),
45 | disclosedContract.templateId.map(Identifier::getEntityName).orElse(""),
46 | Base64.getEncoder()
47 | .encodeToString(disclosedContract.createdEventBlob.toByteArray())));
48 | }
49 | }
50 |
51 | public static DisclosedContract readDisclosedContract(String fileName) throws IOException {
52 | try (FileReader fr = new FileReader(fileName);
53 | BufferedReader bufferedReader = new BufferedReader(fr)) {
54 | return Optional.ofNullable(bufferedReader.readLine())
55 | .map(
56 | line -> {
57 | String[] splitted = line.split(",");
58 | return new DisclosedContract(
59 | new Identifier(splitted[1], splitted[2], splitted[3]),
60 | splitted[0],
61 | ByteString.copyFrom(Base64.getDecoder().decode(splitted[4])));
62 | })
63 | .orElseThrow(
64 | () -> new IllegalArgumentException(String.format("File %s was empty", fileName)));
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/.github/workflows/cla.yml:
--------------------------------------------------------------------------------
1 | name: "CLA Assistant"
2 | on:
3 | issue_comment:
4 | types: [created]
5 | pull_request_target:
6 | types: [opened,closed,synchronize]
7 |
8 | # explicitly configure permissions, in case your GITHUB_TOKEN workflow permissions are set to read-only in repository settings
9 | permissions:
10 | actions: write
11 | contents: write # this can be 'read' if the signatures are in remote repository
12 | pull-requests: write
13 | statuses: write
14 |
15 | jobs:
16 | CLAAssistant:
17 | runs-on: ubuntu-latest
18 | steps:
19 | - name: "CLA Assistant"
20 | if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have hereby read the Digital Asset CLA and agree to its terms') || github.event_name == 'pull_request_target'
21 | uses: digital-asset/cla-action@v0.0.2
22 | env:
23 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24 | # the below token should have repo scope and must be manually added by you in the repository's secret
25 | # This token is required only if you have configured to store the signatures in a remote repository/organization
26 | PERSONAL_ACCESS_TOKEN: ${{ secrets.PAT_FG_CCI_VALIDATOR_CLA }}
27 | with:
28 | path-to-document: 'https://github.com/digital-asset/daml/blob/main/CODE_OF_CONDUCT.md' # e.g. a CLA or a DCO document
29 | # branch should not be protected
30 | branch: 'main'
31 | allowlist: bot*
32 | custom-notsigned-prcomment: '🎉 Thank you for your contribution! It appears you have not yet signed the Agreement [DA Contributor License Agreement (CLA)](https://gist.github.com/digitalasset-cla), which is required for your changes to be incorporated into an Open Source Software (OSS) project. Please kindly read the and reply on a new comment with the following text to agree:'
33 | custom-pr-sign-comment: 'I have hereby read the Digital Asset CLA and agree to its terms'
34 | custom-allsigned-prcomment: '✅ All required contributors have signed the CLA for this PR. Thank you!'
35 | # Remote repository storing CLA signatures.
36 | remote-organization-name: DACH-NY
37 | remote-repository-name: cla-action-data
38 | # Branch where CLA signatures are stored.
39 | path-to-signatures: signatures/signatures.json
40 |
41 | # the followings are the optional inputs - If the optional inputs are not given, then default values will be taken
42 | #remote-organization-name: enter the remote organization name where the signatures should be stored (Default is storing the signatures in the same repository)
43 | #remote-repository-name: enter the remote repository name where the signatures should be stored (Default is storing the signatures in the same repository)
44 | #create-file-commit-message: 'For example: Creating file for storing CLA Signatures'
45 | #signed-commit-message: 'For example: $contributorName has signed the CLA in $owner/$repo#$pullRequestNo'
46 | #custom-notsigned-prcomment: 'pull request comment with Introductory message to ask new contributors to sign'
47 | #custom-pr-sign-comment: 'The signature to be committed in order to sign the CLA'
48 | #custom-allsigned-prcomment: 'pull request comment when all contributors has signed, defaults to **CLA Assistant Lite bot** All Contributors have signed the CLA.'
49 | #lock-pullrequest-aftermerge: false - if you don't want this bot to automatically lock the pull request after merging (default - true)
50 | #use-dco-flag: true - If you are using DCO instead of CLA
51 |
--------------------------------------------------------------------------------
/StockExchange/README.rst:
--------------------------------------------------------------------------------
1 | Example of Explicit Disclosure with Java Bindings
2 | ----------------------------------------------
3 |
4 | ::
5 |
6 | Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
7 | SPDX-License-Identifier: Apache-2.0.0
8 |
9 | This project demonstrates the usage of `Explicit Contract Disclosure `_
10 | in Daml client applications built with the `Java Binding library `_.
11 |
12 | In this example, four parties, each hosted on their own participant (see the topology configuration in `canton_ledger.conf `_), are involved in a simplified trade.
13 | Each party interacts with the Canton ledger via a standalone Java application. The example interaction flow is modelled as follows:
14 |
15 | - Party **Bank** (see `Bank `_) issues ``IOU`` as units of cash to the **Buyer**
16 | - Party **StockExchange** (see `StockExchange `_) issues ``Stock`` as on-ledger asset to the **Seller** party.
17 | Additionally, it issues price ticks for the stock as ``PriceQuotation``. Since **StockExchange** is the sole stakeholder of the ``PriceQuotation``,
18 | it discloses the contract for usage as reference data in commands requiring it as an input.
19 | - Then, party **Seller** (see `Seller `_) owns a unit of ``Stock`` issued by the **StockExchange**.
20 | **Seller** creates an ``Offer`` contract on-ledger that can be accepted by any interested party (see ``Offer_Accept`` in the Daml model).
21 | Similarly to the **StockExchange**, the **Seller** discloses its ``Stock`` and ``Offer`` contracts off-ledger
22 | for interested parties.
23 | - **Buyer** (see `Buyer `_) owns an amount of ``IOU`` issued by **Bank**.
24 | **Buyer** wants to exchange with the **Seller** and accepts its ``Offer`` on-ledger at the correct ``IOU`` market value in exchange of **Seller** s ``Stock``.
25 | In the command submission that exercises ``Offer_Accept``, the **Buyer** uses contracts previously disclosed by the **Seller** and **StockExchange**.
26 |
27 | **Note**: For illustration, the disclosed contracts in this project are shared via files.
28 | (see `Common.shareDisclosedContract `_).
29 |
30 | The Daml model for the templates involved is located in `daml/StockExchange.daml `_`.
31 |
32 | For a better understanding of the explicit disclosure concept and off-ledger data sharing, refer to the
33 | `Explicit Contract Disclosure `_ documentation
34 | where this example's flow is also presented in more detail.
35 |
36 | Running the example
37 | ===================
38 |
39 | #. If you do not have it already, install the DAML SDK by running::
40 |
41 | curl -sSL https://get.digitalasset.com/install/install.sh | sh -s 3.4.9
42 |
43 | #. Use the setup script for exposing the bash example utility functions in two shell terminal windows
44 |
45 | source setup.sh
46 |
47 | #. In one terminal, build the project
48 |
49 | build_example
50 |
51 | #. In the other terminal, start the Canton ledger and wait for initialization until the process prints *Canton server initialization DONE*
52 |
53 | start_canton
54 |
55 | #. In the first terminal, run the example
56 |
57 | run_stock_exchange
58 |
--------------------------------------------------------------------------------
/StockExchange/src/main/java/examples/stockexchange/parties/Buyer.java:
--------------------------------------------------------------------------------
1 | package examples.stockexchange.parties;
2 |
3 | import com.daml.ledger.api.v2.*;
4 | import com.daml.ledger.javaapi.data.*;
5 | import examples.codegen.stockexchange.IOU;
6 | import examples.codegen.stockexchange.Offer;
7 | import examples.codegen.stockexchange.PriceQuotation;
8 | import examples.stockexchange.Common;
9 | import examples.stockexchange.ParticipantSession;
10 | import java.io.IOException;
11 | import java.util.*;
12 |
13 | import org.slf4j.Logger;
14 | import org.slf4j.LoggerFactory;
15 |
16 | public class Buyer {
17 | private static final Logger logger = LoggerFactory.getLogger(Buyer.class);
18 |
19 | public static void main(String[] args) throws Exception {
20 | if (args.length < 2)
21 | throw new IllegalArgumentException("Arguments: ");
22 | int ledgerApiPort = Integer.parseInt(args[0]);
23 | String userId = args[1];
24 |
25 | logger.info("BUYER: Initializing");
26 |
27 | try (ParticipantSession participantSession = new ParticipantSession(ledgerApiPort, userId)) {
28 | acceptOffer(participantSession);
29 | }
30 | }
31 |
32 | private static void acceptOffer(ParticipantSession participantSession) throws IOException {
33 | logger.info("BUYER: Fetching contract-id of owned IOU");
34 | StateServiceGrpc.StateServiceBlockingStub stateClient = participantSession.getStateService();
35 |
36 | Long ledgerEnd = stateClient.getLedgerEnd(StateServiceOuterClass.GetLedgerEndRequest.newBuilder().build()).getOffset();
37 |
38 | EventFormat eventFormat = IOU.contractFilter().eventFormat(Optional.of(Set.of(participantSession.getPartyId())));
39 |
40 | IOU.ContractId iouCid =
41 | new IOU.ContractId(
42 | stateClient
43 | .getActiveContracts(new GetActiveContractsRequest(eventFormat, ledgerEnd).toProto())
44 | .next()
45 | .getActiveContract()
46 | .getCreatedEvent()
47 | .getContractId());
48 |
49 | logger.info("BUYER: Reading shared disclosed contracts");
50 | DisclosedContract offer = Common.readDisclosedContract(Common.OFFER_DISCLOSED_CONTRACT_FILE);
51 | DisclosedContract priceQuotation =
52 | Common.readDisclosedContract(Common.PRICE_QUOTATION_DISCLOSED_CONTRACT_FILE);
53 | DisclosedContract stock = Common.readDisclosedContract(Common.STOCK_DISCLOSED_CONTRACT_FILE);
54 |
55 | List disclosedContracts = new java.util.ArrayList<>();
56 | disclosedContracts.add(priceQuotation);
57 | disclosedContracts.add(offer);
58 | disclosedContracts.add(stock);
59 |
60 | List exerciseAcceptOfferCommand =
61 | new Offer.ContractId(validateContractId(offer.contractId, "offer"))
62 | .exerciseOffer_Accept(
63 | new PriceQuotation.ContractId(validateContractId(priceQuotation.contractId, "priceQuotation")),
64 | participantSession.getPartyId(),
65 | iouCid)
66 | .commands();
67 |
68 | CommandsSubmission commandsSubmission =
69 | CommandsSubmission.create(
70 | Common.APP_ID, UUID.randomUUID().toString(), Optional.empty(), exerciseAcceptOfferCommand)
71 | .withWorkflowId("Buyer-buy-stock")
72 | .withDisclosedContracts(disclosedContracts)
73 | .withActAs(participantSession.getPartyId());
74 |
75 | logger.info("BUYER: Submitting command for offer acceptance");
76 | participantSession
77 | .getCommandService()
78 | .submitAndWait(SubmitAndWaitRequest.toProto(commandsSubmission));
79 |
80 | logger.info("BUYER: Success");
81 | }
82 |
83 | private static String validateContractId(Optional contractId, String where) {
84 | return contractId
85 | .orElseThrow(
86 | () -> new RuntimeException(String.format("contractId not set for %s", where)));
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/StockExchange/src/main/java/examples/stockexchange/parties/Seller.java:
--------------------------------------------------------------------------------
1 | package examples.stockexchange.parties;
2 |
3 | import com.daml.ledger.api.v2.StateServiceGrpc;
4 | import com.daml.ledger.api.v2.StateServiceOuterClass;
5 | import com.daml.ledger.javaapi.data.*;
6 | import examples.codegen.stockexchange.IOU;
7 | import examples.codegen.stockexchange.Offer;
8 | import examples.codegen.stockexchange.Stock;
9 | import examples.stockexchange.Common;
10 | import examples.stockexchange.ParticipantSession;
11 | import java.io.IOException;
12 | import java.util.*;
13 |
14 | import org.slf4j.Logger;
15 | import org.slf4j.LoggerFactory;
16 |
17 | public class Seller {
18 | private static final Logger logger = LoggerFactory.getLogger(Seller.class);
19 |
20 | public static void main(String[] args) throws Exception {
21 | logger.info("SELLER: Initializing");
22 |
23 | if (args.length < 3)
24 | throw new IllegalArgumentException(
25 | "Arguments: ");
26 |
27 | int ledgerApiPort = Integer.parseInt(args[0]);
28 | String userId = args[1];
29 | String stockExchangePartyId = args[2];
30 |
31 | try (ParticipantSession participantSession = new ParticipantSession(ledgerApiPort, userId)) {
32 | announceStockSaleOffer(stockExchangePartyId, participantSession);
33 | }
34 | }
35 |
36 | private static void announceStockSaleOffer(
37 | String stockExchangePartyId, ParticipantSession participantSession) throws IOException {
38 | logger.info("SELLER: Fetching contract-id of owned Stock");
39 |
40 | StateServiceGrpc.StateServiceBlockingStub stateClient = participantSession.getStateService();
41 |
42 | Long ledgerEnd = stateClient.getLedgerEnd(StateServiceOuterClass.GetLedgerEndRequest.newBuilder().build()).getOffset();
43 |
44 | EventFormat eventFormat = Stock.contractFilter().eventFormat(Optional.of(Set.of(participantSession.getPartyId())));
45 |
46 | Stock.ContractId stockCid =
47 | new Stock.ContractId(
48 | stateClient
49 | .getActiveContracts(new GetActiveContractsRequest(eventFormat, ledgerEnd).toProto())
50 | .next()
51 | .getActiveContract()
52 | .getCreatedEvent()
53 | .getContractId());
54 |
55 | List createOfferCommand =
56 | new Offer(participantSession.getPartyId(), stockExchangePartyId, stockCid)
57 | .create()
58 | .commands();
59 |
60 | CommandsSubmission commandsSubmission =
61 | CommandsSubmission.create(Common.APP_ID, UUID.randomUUID().toString(), Optional.empty(), createOfferCommand)
62 | .withWorkflowId("Seller-Offer")
63 | .withActAs(participantSession.getPartyId());
64 |
65 | logger.info("SELLER: Creating on-ledger Offer for selling owned Stock");
66 | participantSession
67 | .getCommandService()
68 | .submitAndWait(SubmitAndWaitRequest.toProto(commandsSubmission));
69 |
70 | logger.info("SELLER: Fetching Stock disclosed contract for sharing");
71 | DisclosedContract stockDisclosedContract =
72 | Common.fetchContractForDisclosure(
73 | participantSession.getStateService(),
74 | participantSession.getPartyId(),
75 | Stock.contractFilter());
76 |
77 | logger.info("SELLER: Fetching Offer disclosed contract for sharing");
78 | DisclosedContract offerDisclosedContract =
79 | Common.fetchContractForDisclosure(
80 | participantSession.getStateService(),
81 | participantSession.getPartyId(),
82 | Offer.contractFilter());
83 |
84 | logger.info("SELLER: Sharing Stock disclosed contract");
85 | Common.shareDisclosedContract(stockDisclosedContract, Common.STOCK_DISCLOSED_CONTRACT_FILE);
86 |
87 | logger.info("SELLER: Sharing Offer disclosed contract");
88 | Common.shareDisclosedContract(offerDisclosedContract, Common.OFFER_DISCLOSED_CONTRACT_FILE);
89 |
90 | logger.info("SELLER: Done");
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/StockExchange/src/main/java/examples/stockexchange/parties/StockExchange.java:
--------------------------------------------------------------------------------
1 | package examples.stockexchange.parties;
2 |
3 | import static examples.stockexchange.Common.APP_ID;
4 | import static examples.stockexchange.Common.fetchContractForDisclosure;
5 |
6 | import com.daml.ledger.javaapi.data.*;
7 | import examples.codegen.stockexchange.PriceQuotation;
8 | import examples.codegen.stockexchange.Stock;
9 | import examples.stockexchange.Common;
10 | import examples.stockexchange.ParticipantSession;
11 | import java.io.IOException;
12 | import java.util.List;
13 | import java.util.Optional;
14 | import java.util.Set;
15 | import java.util.UUID;
16 | import org.slf4j.Logger;
17 | import org.slf4j.LoggerFactory;
18 |
19 | public class StockExchange {
20 | private static final Logger logger = LoggerFactory.getLogger(StockExchange.class);
21 |
22 | public static void main(String[] args) throws Exception {
23 | logger.info("STOCK_EXCHANGE: Initializing");
24 |
25 | if (args.length < 5)
26 | throw new IllegalArgumentException(
27 | "Arguments: ");
28 | int ledgerApiPort = Integer.parseInt(args[0]);
29 | String userId = args[1];
30 | String sellerPartyId = args[2];
31 | String issuedStockName = args[3];
32 | long issuedStockPriceQuotation = Long.parseLong(args[4]);
33 |
34 | try (ParticipantSession participantSession = new ParticipantSession(ledgerApiPort, userId)) {
35 | issueStockAndPriceQuotation(
36 | sellerPartyId, issuedStockName, issuedStockPriceQuotation, participantSession);
37 | }
38 | }
39 |
40 | private static void issueStockAndPriceQuotation(
41 | String sellerPartyId,
42 | String issuedStockName,
43 | long issuedStockPriceQuotation,
44 | ParticipantSession participantSession)
45 | throws IOException {
46 | List createStockCommand =
47 | new Stock(participantSession.getPartyId(), sellerPartyId, issuedStockName)
48 | .create()
49 | .commands();
50 |
51 | CommandsSubmission issueStockSubmission =
52 | CommandsSubmission.create(APP_ID, UUID.randomUUID().toString(), Optional.empty(), createStockCommand)
53 | .withWorkflowId("Stock-issue")
54 | .withActAs(participantSession.getPartyId());
55 |
56 | logger.info("STOCK_EXCHANGE: Issuing stock with name {} to {}", issuedStockName, sellerPartyId);
57 | participantSession
58 | .getCommandService()
59 | .submitAndWait(SubmitAndWaitRequest.toProto(issueStockSubmission));
60 |
61 | List createPriceQuotationCommand =
62 | new PriceQuotation(
63 | participantSession.getPartyId(), issuedStockName, issuedStockPriceQuotation)
64 | .create()
65 | .commands();
66 |
67 | CommandsSubmission emitPriceQuotationSubmission =
68 | CommandsSubmission.create(APP_ID, UUID.randomUUID().toString(), Optional.empty(), createPriceQuotationCommand)
69 | .withWorkflowId("PriceQuotation-issue")
70 | .withActAs(participantSession.getPartyId());
71 |
72 | TransactionFormat transactionFormat = PriceQuotation.contractFilter().transactionFormat(Optional.of(Set.of(participantSession.getPartyId())));
73 |
74 | logger.info(
75 | "STOCK_EXCHANGE: Emitting price quotation for {} at value {}",
76 | issuedStockName,
77 | issuedStockPriceQuotation);
78 | participantSession
79 | .getCommandService()
80 | .submitAndWaitForTransaction(new SubmitAndWaitForTransactionRequest(emitPriceQuotationSubmission, transactionFormat).toProto())
81 | .getTransaction()
82 | .getEvents(0)
83 | .getCreated()
84 | .getContractId();
85 |
86 | logger.info(
87 | "STOCK_EXCHANGE: Fetching PriceQuotation for stock with name {} for disclosure",
88 | issuedStockName);
89 | DisclosedContract priceQuotationDisclosedContract =
90 | fetchContractForDisclosure(
91 | participantSession.getStateService(),
92 | participantSession.getPartyId(),
93 | PriceQuotation.contractFilter());
94 |
95 | logger.info("STOCK_EXCHANGE: Sharing PriceQuotation disclosed contract");
96 | Common.shareDisclosedContract(
97 | priceQuotationDisclosedContract, Common.PRICE_QUOTATION_DISCLOSED_CONTRACT_FILE);
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/PingPong/src/main/java/examples/pingpong/codegen/PingPongCodegenMain.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | // SPDX-License-Identifier: Apache-2.0
3 |
4 | package examples.pingpong.codegen;
5 |
6 | import com.daml.ledger.api.v2.CommandSubmissionServiceGrpc;
7 | import com.daml.ledger.api.v2.CommandSubmissionServiceGrpc.CommandSubmissionServiceFutureStub;
8 | import com.daml.ledger.api.v2.admin.UserManagementServiceGrpc;
9 | import com.daml.ledger.api.v2.admin.UserManagementServiceGrpc.UserManagementServiceBlockingStub;
10 | import com.daml.ledger.api.v2.admin.UserManagementServiceOuterClass.GetUserRequest;
11 | import com.daml.ledger.api.v2.admin.UserManagementServiceOuterClass.GetUserResponse;
12 | import com.daml.ledger.javaapi.data.Command;
13 | import com.daml.ledger.javaapi.data.CommandsSubmission;
14 | import com.daml.ledger.javaapi.data.SubmitRequest;
15 | import examples.pingpong.codegen.pingpong.Ping;
16 | import io.grpc.ManagedChannel;
17 | import io.grpc.ManagedChannelBuilder;
18 |
19 | import java.util.List;
20 | import java.util.Optional;
21 | import java.util.UUID;
22 |
23 | public class PingPongCodegenMain {
24 |
25 | // application id used for sending commands
26 | public static final String APP_ID = "PingPongCodegenApp";
27 |
28 | // constants for referring to the users with access to the parties
29 | public static final String ALICE_USER = "alice";
30 | public static final String BOB_USER = "bob";
31 |
32 | public static void main(String[] args) {
33 | // Extract host and port from arguments
34 | if (args.length < 2) {
35 | System.err.println("Usage: HOST PORT [NUM_INITIAL_CONTRACTS]");
36 | System.exit(-1);
37 | }
38 | String host = args[0];
39 | int port = Integer.parseInt(args[1]);
40 |
41 | // each party will create this number of initial Ping contracts
42 | int numInitialContracts = args.length == 3 ? Integer.parseInt(args[2]) : 10;
43 |
44 | // Initialize a plaintext gRPC channel
45 | ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
46 |
47 | // fetch the party IDs that got created in the Daml init script
48 | String aliceParty = fetchPartyId(channel, ALICE_USER);
49 | String bobParty = fetchPartyId(channel, BOB_USER);
50 |
51 | // initialize the ping pong processors for Alice and Bob
52 | PingPongProcessor aliceProcessor = new PingPongProcessor(aliceParty, channel);
53 | PingPongProcessor bobProcessor = new PingPongProcessor(bobParty, channel);
54 |
55 | // start the processors asynchronously
56 | aliceProcessor.runIndefinitely();
57 | bobProcessor.runIndefinitely();
58 |
59 | // send the initial commands for both parties
60 | createInitialContracts(channel, aliceParty, bobParty, numInitialContracts);
61 | createInitialContracts(channel, bobParty, aliceParty, numInitialContracts);
62 |
63 |
64 | try {
65 | // wait a couple of seconds for the processing to finish
66 | Thread.sleep(15000);
67 | System.exit(0);
68 | } catch (InterruptedException e) {
69 | e.printStackTrace();
70 | }
71 | }
72 |
73 | /**
74 | * Creates numContracts number of Ping contracts. The sender is used as the submitting party.
75 | *
76 | * @param channel the gRPC channel to use for services
77 | * @param sender the party that sends the initial Ping contract
78 | * @param receiver the party that receives the initial Ping contract
79 | * @param numContracts the number of initial contracts to create
80 | */
81 | private static void createInitialContracts(ManagedChannel channel, String sender, String receiver, int numContracts) {
82 | CommandSubmissionServiceFutureStub submissionService = CommandSubmissionServiceGrpc.newFutureStub(channel);
83 |
84 | for (int i = 0; i < numContracts; i++) {
85 | // command that creates the initial Ping contract with the required parameters according to the model
86 | List createCommands = Ping.create(sender, receiver, 0L).commands();
87 |
88 | // wrap the create command in a command submission
89 | CommandsSubmission commandsSubmission = CommandsSubmission.create(
90 | APP_ID,
91 | UUID.randomUUID().toString(),
92 | Optional.empty(),
93 | createCommands)
94 | .withActAs(List.of(sender))
95 | .withReadAs(List.of(sender))
96 | .withWorkflowId(String.format("Ping-%s-%d", sender, i));
97 |
98 | // convert the command submission to a proto data structure
99 | final var request = SubmitRequest.toProto(commandsSubmission);
100 | // asynchronously send the request
101 | submissionService.submit(request);
102 | }
103 | }
104 |
105 | private static String fetchPartyId(ManagedChannel channel, String userId) {
106 | UserManagementServiceBlockingStub userManagementService = UserManagementServiceGrpc.newBlockingStub(channel);
107 | GetUserResponse getUserResponse = userManagementService.getUser(GetUserRequest.newBuilder().setUserId(userId).build());
108 | return getUserResponse.getUser().getPrimaryParty();
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/PingPong/src/main/java/examples/pingpong/grpc/PingPongGrpcMain.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | // SPDX-License-Identifier: Apache-2.0
3 |
4 | package examples.pingpong.grpc;
5 |
6 | import java.util.Optional;
7 | import java.util.UUID;
8 |
9 | import com.daml.ledger.api.v2.CommandSubmissionServiceGrpc;
10 | import com.daml.ledger.api.v2.CommandSubmissionServiceGrpc.CommandSubmissionServiceFutureStub;
11 | import com.daml.ledger.api.v2.CommandSubmissionServiceOuterClass.SubmitRequest;
12 | import com.daml.ledger.api.v2.CommandsOuterClass.Command;
13 | import com.daml.ledger.api.v2.CommandsOuterClass.Commands;
14 | import com.daml.ledger.api.v2.CommandsOuterClass.CreateCommand;
15 | import com.daml.ledger.api.v2.ValueOuterClass.Identifier;
16 | import com.daml.ledger.api.v2.ValueOuterClass.Record;
17 | import com.daml.ledger.api.v2.ValueOuterClass.RecordField;
18 | import com.daml.ledger.api.v2.ValueOuterClass.Value;
19 | import com.daml.ledger.api.v2.admin.UserManagementServiceGrpc;
20 | import com.daml.ledger.api.v2.admin.UserManagementServiceGrpc.UserManagementServiceBlockingStub;
21 | import com.daml.ledger.api.v2.admin.UserManagementServiceOuterClass.GetUserRequest;
22 | import com.daml.ledger.api.v2.admin.UserManagementServiceOuterClass.GetUserResponse;
23 |
24 | import io.grpc.ManagedChannel;
25 | import io.grpc.ManagedChannelBuilder;
26 |
27 | public class PingPongGrpcMain {
28 |
29 | // application id used for sending commands
30 | public static final String APP_ID = "PingPongGrpcApp";
31 |
32 | // constants for referring to the users with access to the parties
33 | public static final String ALICE_USER = "alice";
34 | public static final String BOB_USER = "bob";
35 |
36 | public static void main(String[] args) {
37 | // Extract host and port from arguments
38 | if (args.length < 2) {
39 | System.err.println("Usage: HOST PORT [NUM_INITIAL_CONTRACTS]");
40 | System.exit(-1);
41 | }
42 | String host = args[0];
43 | int port = Integer.parseInt(args[1]);
44 |
45 | // each party will create this number of initial Ping contracts
46 | int numInitialContracts = args.length == 3 ? Integer.parseInt(args[2]) : 10;
47 |
48 | // Initialize a plaintext gRPC channel
49 | ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
50 |
51 | // fetch the party IDs that got created in the Daml init script
52 | String aliceParty = fetchPartyId(channel, ALICE_USER);
53 | String bobParty = fetchPartyId(channel, BOB_USER);
54 |
55 | String packageId = Optional.ofNullable(System.getProperty("package.id"))
56 | .orElseThrow(() -> new RuntimeException("package.id must be specified via sys properties"));
57 |
58 | Identifier pingIdentifier = Identifier.newBuilder()
59 | .setPackageId(packageId)
60 | .setModuleName("PingPong")
61 | .setEntityName("Ping")
62 | .build();
63 | Identifier pongIdentifier = Identifier.newBuilder()
64 | .setPackageId(packageId)
65 | .setModuleName("PingPong")
66 | .setEntityName("Pong")
67 | .build();
68 |
69 | // initialize the ping pong processors for Alice and Bob
70 | PingPongProcessor aliceProcessor = new PingPongProcessor(aliceParty, channel, pingIdentifier, pongIdentifier);
71 | PingPongProcessor bobProcessor = new PingPongProcessor(bobParty, channel, pingIdentifier, pongIdentifier);
72 |
73 | // start the processors asynchronously
74 | aliceProcessor.runIndefinitely();
75 | bobProcessor.runIndefinitely();
76 |
77 | // send the initial commands for both parties
78 | createInitialContracts(channel, aliceParty, bobParty, pingIdentifier, numInitialContracts);
79 | createInitialContracts(channel, bobParty, aliceParty, pingIdentifier, numInitialContracts);
80 |
81 |
82 | try {
83 | // wait a couple of seconds for the processing to finish
84 | Thread.sleep(15000);
85 | System.exit(0);
86 | } catch (InterruptedException e) {
87 | e.printStackTrace();
88 | }
89 | }
90 |
91 | /**
92 | * Creates numContracts number of Ping contracts. The sender is used as the submitting party.
93 | *
94 | * @param channel the gRPC channel to use for services
95 | * @param sender the party that sends the initial Ping contract
96 | * @param receiver the party that receives the initial Ping contract
97 | * @param pingIdentifier the PingPong.Ping template identifier
98 | * @param numContracts the number of initial contracts to create
99 | */
100 | private static void createInitialContracts(ManagedChannel channel, String sender, String receiver, Identifier pingIdentifier, int numContracts) {
101 | CommandSubmissionServiceFutureStub submissionService = CommandSubmissionServiceGrpc.newFutureStub(channel);
102 |
103 | for (int i = 0; i < numContracts; i++) {
104 | // command that creates the initial Ping contract with the required parameters according to the model
105 | Command createCommand = Command.newBuilder().setCreate(
106 | CreateCommand.newBuilder()
107 | .setTemplateId(pingIdentifier)
108 | .setCreateArguments(
109 | Record.newBuilder()
110 | // the identifier for a template's record is the same as the identifier for the template
111 | .setRecordId(pingIdentifier)
112 | .addFields(RecordField.newBuilder().setLabel("sender").setValue(Value.newBuilder().setParty(sender)))
113 | .addFields(RecordField.newBuilder().setLabel("receiver").setValue(Value.newBuilder().setParty(receiver)))
114 | .addFields(RecordField.newBuilder().setLabel("count").setValue(Value.newBuilder().setInt64(0)))
115 | )
116 | ).build();
117 |
118 |
119 | SubmitRequest submitRequest = SubmitRequest.newBuilder().setCommands(Commands.newBuilder()
120 | .setCommandId(UUID.randomUUID().toString())
121 | .setWorkflowId(String.format("Ping-%s-%d", sender, i))
122 | .addActAs(sender)
123 | .setUserId(APP_ID)
124 | .addCommands(createCommand)
125 | ).build();
126 |
127 | // asynchronously send the commands
128 | submissionService.submit(submitRequest);
129 | }
130 | }
131 |
132 | private static String fetchPartyId(ManagedChannel channel, String userId) {
133 | UserManagementServiceBlockingStub userManagementService = UserManagementServiceGrpc.newBlockingStub(channel);
134 | GetUserResponse getUserResponse = userManagementService.getUser(GetUserRequest.newBuilder().setUserId(userId).build());
135 | return getUserResponse.getUser().getPrimaryParty();
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/PingPong/src/main/java/examples/pingpong/codegen/PingPongProcessor.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | // SPDX-License-Identifier: Apache-2.0
3 |
4 | package examples.pingpong.codegen;
5 |
6 | import com.daml.ledger.api.v2.*;
7 | import com.daml.ledger.api.v2.CommandSubmissionServiceGrpc.CommandSubmissionServiceBlockingStub;
8 | import com.daml.ledger.api.v2.EventOuterClass.Event;
9 | import com.daml.ledger.api.v2.TransactionOuterClass.Transaction;
10 | import com.daml.ledger.api.v2.UpdateServiceGrpc.UpdateServiceStub;
11 | import com.daml.ledger.api.v2.UpdateServiceOuterClass.GetUpdatesResponse;
12 | import com.daml.ledger.javaapi.data.*;
13 | import com.daml.ledger.javaapi.data.codegen.Contract;
14 | import com.daml.ledger.javaapi.data.codegen.ContractCompanion;
15 | import com.daml.ledger.javaapi.data.codegen.Exercised;
16 | import com.daml.ledger.javaapi.data.codegen.Update;
17 | import examples.pingpong.codegen.pingpong.Ping;
18 | import examples.pingpong.codegen.pingpong.Pong;
19 | import io.grpc.ManagedChannel;
20 | import io.grpc.stub.StreamObserver;
21 |
22 | import java.util.*;
23 | import java.util.function.Function;
24 | import java.util.stream.Collectors;
25 | import java.util.stream.Stream;
26 |
27 | /**
28 | * This class subscribes to the stream of transactions for a given party and reacts to Ping or Pong contracts.
29 | */
30 | public class PingPongProcessor {
31 |
32 | private final String party;
33 |
34 | private final UpdateServiceStub transactionService;
35 | private final CommandSubmissionServiceBlockingStub submissionService;
36 |
37 | private final Identifier pingIdentifier;
38 | private final Identifier pongIdentifier;
39 |
40 | public PingPongProcessor(String party, ManagedChannel channel) {
41 | this.party = party;
42 | this.transactionService = UpdateServiceGrpc.newStub(channel);
43 | this.submissionService = CommandSubmissionServiceGrpc.newBlockingStub(channel);
44 | this.pingIdentifier = Ping.TEMPLATE_ID;
45 | this.pongIdentifier = Pong.TEMPLATE_ID;
46 | }
47 |
48 | public void runIndefinitely() {
49 | // restrict the subscription to ping and pong template types through an inclusive filter
50 | final var inclusiveFilter = new CumulativeFilter(
51 | Collections.emptyMap(),
52 | Map.of(pingIdentifier, Filter.Template.HIDE_CREATED_EVENT_BLOB, pongIdentifier, Filter.Template.HIDE_CREATED_EVENT_BLOB),
53 | Optional.empty());
54 | // specify inclusive filter for the party attached to this processor
55 | final var eventFormat = new EventFormat(
56 | Map.of(party, inclusiveFilter),
57 | Optional.empty(),
58 | true
59 | );
60 | final var includeTransactions = new TransactionFormat(
61 | eventFormat,
62 | TransactionShape.ACS_DELTA
63 | );
64 | final var updateFormat = new UpdateFormat(
65 | Optional.of(includeTransactions),
66 | Optional.empty(),
67 | Optional.empty()
68 | );
69 | // assemble the request for the transaction stream
70 | final var GetUpdatesRequest = new GetUpdatesRequest(
71 | 0L,
72 | Optional.empty(),
73 | updateFormat
74 | );
75 |
76 | // this StreamObserver reacts to transactions and prints a message if an error occurs or the stream gets closed
77 | StreamObserver transactionObserver = new StreamObserver<>() {
78 | @Override
79 | public void onNext(GetUpdatesResponse value) {
80 | if(value.hasTransaction())
81 | processTransaction(value.getTransaction());
82 | }
83 |
84 | @Override
85 | public void onError(Throwable t) {
86 | System.err.printf("%s encountered an error while processing transactions!\n", party);
87 | t.printStackTrace();
88 | }
89 |
90 | @Override
91 | public void onCompleted() {
92 | System.out.printf("%s's transactions stream completed.\n", party);
93 | }
94 | };
95 | System.out.printf("%s starts reading transactions.\n", party);
96 | transactionService.getUpdates(GetUpdatesRequest.toProto(), transactionObserver);
97 | }
98 |
99 | /**
100 | * Processes a transaction and sends the resulting commands to the Command Submission Service
101 | *
102 | * @param tx the Transaction to process
103 | */
104 | private void processTransaction(Transaction tx) {
105 | List commands = tx.getEventsList().stream()
106 | .filter(Event::hasCreated).map(Event::getCreated)
107 | .flatMap(e -> processEvent(tx.getWorkflowId(), e))
108 | .collect(Collectors.toList());
109 |
110 | if (!commands.isEmpty()) {
111 | CommandsSubmission commandsSubmission = CommandsSubmission.create(
112 | PingPongCodegenMain.APP_ID,
113 | UUID.randomUUID().toString(),
114 | Optional.empty(),
115 | commands)
116 | .withActAs(List.of(party))
117 | .withReadAs(List.of(party))
118 | .withWorkflowId(tx.getWorkflowId());
119 | submissionService.submit(SubmitRequest.toProto(commandsSubmission));
120 | }
121 | }
122 |
123 | /**
124 | * For each {@link CreatedEvent} where the receiver is
125 | * the current party, exercise the Pong choice of Ping contracts, or the Ping
126 | * choice of Pong contracts.
127 | *
128 | * @param workflowId the workflow the event is part of
129 | * @param protoEvent the {@link CreatedEvent} to process
130 | * @return an empty Stream if this event doesn't trigger any action for this {@link PingPongProcessor}'s
131 | * party
132 | */
133 | private Stream processEvent(String workflowId, EventOuterClass.CreatedEvent protoEvent) {
134 | String templateName = protoEvent.getTemplateId().getEntityName();
135 | Map fields = protoEvent
136 | .getCreateArguments()
137 | .getFieldsList()
138 | .stream()
139 | .collect(Collectors.toMap(ValueOuterClass.RecordField::getLabel, ValueOuterClass.RecordField::getValue));
140 |
141 | // check that this party is set as the receiver of the contract
142 | boolean thisPartyIsReceiver = fields.get("receiver").getParty().equals(party);
143 |
144 | if (!thisPartyIsReceiver) return Stream.empty();
145 |
146 | String contractId = protoEvent.getContractId();
147 | boolean isPing = templateName.equals(pingIdentifier.getEntityName());
148 | String choice = isPing ? "RespondPong" : "RespondPing";
149 |
150 | Long count = fields.get("count").getInt64();
151 | System.out.printf("%s is exercising %s on %s in workflow %s at count %d\n", party, choice, contractId, workflowId, count);
152 |
153 | final var event = CreatedEvent.fromProto(protoEvent);
154 |
155 | return Stream.concat(
156 | processPingPong(
157 | Ping.COMPANION,
158 | Ping.Exercises::exerciseRespondPong,
159 | event),
160 | processPingPong(
161 | Pong.COMPANION,
162 | Pong.Exercises::exerciseRespondPing,
163 | event)
164 | );
165 | }
166 |
167 | private , Id, Data>
168 | Stream processPingPong(
169 | ContractCompanion companion,
170 | Function>> createUpdate,
171 | CreatedEvent event) {
172 | if (!event.getTemplateId().getEntityName().equals(companion.TEMPLATE_ID.getEntityName()))
173 | return Stream.empty();
174 | Ct ct = companion.fromCreatedEvent(event);
175 | Update> update = createUpdate.apply(ct.id);
176 | return update.commands().stream();
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/PingPong/src/main/java/examples/pingpong/grpc/PingPongProcessor.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2 | // SPDX-License-Identifier: Apache-2.0
3 |
4 | package examples.pingpong.grpc;
5 |
6 | import com.daml.ledger.api.v2.CommandSubmissionServiceGrpc;
7 | import com.daml.ledger.api.v2.CommandSubmissionServiceGrpc.CommandSubmissionServiceBlockingStub;
8 | import com.daml.ledger.api.v2.CommandSubmissionServiceOuterClass.SubmitRequest;
9 | import com.daml.ledger.api.v2.CommandsOuterClass.Command;
10 | import com.daml.ledger.api.v2.CommandsOuterClass.Commands;
11 | import com.daml.ledger.api.v2.CommandsOuterClass.ExerciseCommand;
12 | import com.daml.ledger.api.v2.EventOuterClass.CreatedEvent;
13 | import com.daml.ledger.api.v2.EventOuterClass.Event;
14 | import com.daml.ledger.api.v2.TransactionFilterOuterClass.CumulativeFilter;
15 | import com.daml.ledger.api.v2.TransactionFilterOuterClass.EventFormat;
16 | import com.daml.ledger.api.v2.TransactionFilterOuterClass.Filters;
17 | import com.daml.ledger.api.v2.TransactionFilterOuterClass.TemplateFilter;
18 | import com.daml.ledger.api.v2.TransactionFilterOuterClass.TransactionFormat;
19 | import com.daml.ledger.api.v2.TransactionFilterOuterClass.TransactionShape;
20 | import com.daml.ledger.api.v2.TransactionFilterOuterClass.UpdateFormat;
21 | import com.daml.ledger.api.v2.TransactionOuterClass.Transaction;
22 | import com.daml.ledger.api.v2.UpdateServiceGrpc;
23 | import com.daml.ledger.api.v2.UpdateServiceGrpc.UpdateServiceStub;
24 | import com.daml.ledger.api.v2.UpdateServiceOuterClass.GetUpdatesRequest;
25 | import com.daml.ledger.api.v2.UpdateServiceOuterClass.GetUpdatesResponse;
26 | import com.daml.ledger.api.v2.ValueOuterClass.Identifier;
27 | import com.daml.ledger.api.v2.ValueOuterClass.Record;
28 | import com.daml.ledger.api.v2.ValueOuterClass.RecordField;
29 | import com.daml.ledger.api.v2.ValueOuterClass.Value;
30 | import io.grpc.ManagedChannel;
31 | import io.grpc.stub.StreamObserver;
32 |
33 | import java.util.List;
34 | import java.util.Map;
35 | import java.util.UUID;
36 | import java.util.stream.Collectors;
37 | import java.util.stream.Stream;
38 |
39 | /**
40 | * This class subscribes to the stream of transactions for a given party and reacts to Ping or Pong contracts.
41 | */
42 | public class PingPongProcessor {
43 |
44 | private final String party;
45 |
46 | private final UpdateServiceStub transactionService;
47 | private final CommandSubmissionServiceBlockingStub submissionService;
48 |
49 | private final Identifier pingIdentifier;
50 | private final Identifier pongIdentifier;
51 |
52 | public PingPongProcessor(String party, ManagedChannel channel, Identifier pingIdentifier, Identifier pongIdentifier) {
53 | this.party = party;
54 | this.transactionService = UpdateServiceGrpc.newStub(channel);
55 | this.submissionService = CommandSubmissionServiceGrpc.newBlockingStub(channel);
56 | this.pingIdentifier = pingIdentifier;
57 | this.pongIdentifier = pongIdentifier;
58 | }
59 |
60 | private CumulativeFilter newTemplate(Identifier identifier){
61 | return CumulativeFilter
62 | .newBuilder()
63 | .setTemplateFilter(
64 | TemplateFilter
65 | .newBuilder()
66 | .setTemplateId(identifier)
67 | .build()
68 | )
69 | .build();
70 | }
71 |
72 | public void runIndefinitely() {
73 | // restrict the subscription to ping and pong template types through an inclusive filter
74 | final var eventFormat = EventFormat.newBuilder()
75 | .setVerbose(true)
76 | .putFiltersByParty(
77 | party,
78 | Filters
79 | .newBuilder()
80 | .addCumulative(newTemplate(pingIdentifier))
81 | .addCumulative(newTemplate(pongIdentifier))
82 | .build(
83 | )).build();
84 | final var transactionFormat = TransactionFormat.newBuilder()
85 | .setEventFormat(eventFormat)
86 | .setTransactionShape(TransactionShape.TRANSACTION_SHAPE_ACS_DELTA)
87 | .build();
88 | final var updateFormat = UpdateFormat.newBuilder()
89 | .setIncludeTransactions(transactionFormat)
90 | .build();
91 | // assemble the request for the transaction stream
92 | GetUpdatesRequest transactionsRequest = GetUpdatesRequest.newBuilder()
93 | .setBeginExclusive(0L)
94 | .setUpdateFormat(updateFormat)
95 | .build();
96 |
97 | // this StreamObserver reacts to transactions and prints a message if an error occurs or the stream gets closed
98 | StreamObserver transactionObserver = new StreamObserver() {
99 | @Override
100 | public void onNext(GetUpdatesResponse value) {
101 | if(value.hasTransaction()) processTransaction(value.getTransaction());
102 | }
103 |
104 | @Override
105 | public void onError(Throwable t) {
106 | System.err.printf("%s encountered an error while processing transactions!\n", party);
107 | t.printStackTrace();
108 | }
109 |
110 | @Override
111 | public void onCompleted() {
112 | System.out.printf("%s's transactions stream completed.\n", party);
113 | }
114 | };
115 | System.out.printf("%s starts reading transactions.\n", party);
116 | transactionService.getUpdates(transactionsRequest, transactionObserver);
117 | }
118 |
119 | /**
120 | * Processes a transaction and sends the resulting commands to the Command Submission Service
121 | *
122 | * @param tx the Transaction to process
123 | */
124 | private void processTransaction(Transaction tx) {
125 | List commands = tx.getEventsList().stream()
126 | .filter(Event::hasCreated).map(Event::getCreated)
127 | .flatMap(e -> processEvent(tx.getWorkflowId(), e))
128 | .collect(Collectors.toList());
129 |
130 | if (!commands.isEmpty()) {
131 | SubmitRequest request = SubmitRequest.newBuilder()
132 | .setCommands(Commands.newBuilder()
133 | .setCommandId(UUID.randomUUID().toString())
134 | .setWorkflowId(tx.getWorkflowId())
135 | .addActAs(party)
136 | .setUserId(PingPongGrpcMain.APP_ID)
137 | .addAllCommands(commands)
138 | .build())
139 | .build();
140 | submissionService.submit(request);
141 | }
142 | }
143 |
144 | /**
145 | * For each {@link CreatedEvent} where the receiver is
146 | * the current party, exercise the Pong choice of Ping contracts, or the Ping
147 | * choice of Pong contracts.
148 | *
149 | * @param workflowId the workflow the event is part of
150 | * @param event the {@link CreatedEvent} to process
151 | * @return an empty Stream if this event doesn't trigger any action for this {@link PingPongProcessor}'s
152 | * party
153 | */
154 | private Stream processEvent(String workflowId, CreatedEvent event) {
155 | Identifier template = event.getTemplateId();
156 |
157 | boolean isPingPongModule = template.getModuleName().equals(pingIdentifier.getModuleName());
158 |
159 | boolean isPing = template.getEntityName().equals(pingIdentifier.getEntityName());
160 | boolean isPong = template.getEntityName().equals(pongIdentifier.getEntityName());
161 |
162 | if (!isPingPongModule || !isPing && !isPong) return Stream.empty();
163 |
164 | Map fields = event
165 | .getCreateArguments()
166 | .getFieldsList()
167 | .stream()
168 | .collect(Collectors.toMap(RecordField::getLabel, RecordField::getValue));
169 |
170 | // check that this party is set as the receiver of the contract
171 | boolean thisPartyIsReceiver = fields.get("receiver").getParty().equals(party);
172 |
173 | if (!thisPartyIsReceiver) return Stream.empty();
174 |
175 | String contractId = event.getContractId();
176 | String choice = isPing ? "RespondPong" : "RespondPing";
177 |
178 | Long count = fields.get("count").getInt64();
179 | System.out.printf("%s is exercising %s on %s in workflow %s at count %d\n", party, choice, contractId, workflowId, count);
180 |
181 | // assemble the exercise command
182 | Command cmd = Command
183 | .newBuilder()
184 | .setExercise(ExerciseCommand
185 | .newBuilder()
186 | .setTemplateId(template)
187 | .setContractId(contractId)
188 | .setChoice(choice)
189 | .setChoiceArgument(Value.newBuilder().setRecord(Record.getDefaultInstance())))
190 | .build();
191 |
192 | return Stream.of(cmd);
193 | }
194 | }
195 |
--------------------------------------------------------------------------------
/PingPong/README.rst:
--------------------------------------------------------------------------------
1 | Java Bindings Ping-Pong Example
2 | -------------------------------
3 |
4 | ::
5 |
6 | Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
7 | SPDX-License-Identifier: Apache-2.0.0
8 |
9 |
10 | This is an example of how a Java application would use the `Java Binding library `_ to connect to and exercise a DAML model running on a ledger. Since there are three levels of interface available, this example builds a similar application with all three levels.
11 |
12 | The application is a simple ``PingPong`` application, which consists of:
13 |
14 | - a DAML model with two contract templates, ``Ping`` and ``Pong``
15 | - two parties, ``Alice`` and ``Bob``
16 |
17 | The logic of the application is the following:
18 |
19 | #. The application injects a contract of type ``Ping`` for ``Alice``.
20 | #. ``Alice`` sees this contract and exercises the consuming choice ``RespondPong`` to create a contract
21 | of type ``Pong`` for ``Bob``.
22 | #. ``Bob`` sees this contract and exercises the consuming choice ``RespondPing`` to create a contract
23 | of type ``Ping`` for ``Alice``.
24 | #. Points 1 and 2 are repeated until the maximum number of contracts defined in the DAML is
25 | reached.
26 |
27 | Setting Up the Example Projects
28 | -------------------------------
29 |
30 | To set a project up:
31 |
32 | #. If you do not have it already, install the DAML SDK by running::
33 |
34 | curl -sSL https://get.digitalasset.com/install/install.sh | sh -s 3.4.9
35 |
36 | #. Use the start script for starting a ledger & the java application:
37 |
38 | ./start.sh
39 |
40 | * examples.pingpong.grpc.PingPongGrpcMain
41 | * examples.pingpong.codegen.PingPongCodegenMain
42 |
43 | depending on which example you wish to run. The script will take care of stopping an already running sandbox & start a fresh one on every call.
44 |
45 | Example Project -- Ping Pong with gRPC Bindings
46 | -----------------------------------------------
47 |
48 | The code for this example is in the package `examples.pingpong.grpc `_.
49 |
50 | PingPongGrpcMain.java
51 | =====================
52 |
53 | The entry point for the Java code is the main class `PingPongGrpcMain `_. Look at this class to see:
54 |
55 | - how to connect to and interact with the DAML Ledger via the Java Binding library
56 | - how to use the gRPC layer to build an automation for both parties.
57 |
58 | The main function:
59 |
60 | - creates an instance of a ``ManagedChannel`` connecting to an existing ledger
61 | - fetches the ledgerID and packageId from the ledger
62 | - creates ``Identifiers`` for the Ping and Pong templates
63 | - creates and starts instances of `PingPongProcessor `_ that contain the logic of the automation
64 | - injects the initial contracts to start the process
65 |
66 | PingPongProcessor.java
67 | ======================
68 |
69 | The core of the application is the method `PingPongProcessor.runIndefinitely() `_.
70 |
71 | This method retrieves a gRPC streaming endpoint using the ``GetUpdatesRequest`` request, and then creates a ``StreamObserver``, providing implementations of the ``onNext``, ``onError`` and ``onComplete`` observer methods. ``gRPC`` layer arranges that these methods receive stream events asynchronously.
72 |
73 | The method `onNext `_ is the main driver, extracting the transaction list from each ``GetUpdatesResponse``, and passing in to ``processTransaction()`` for processing. This method, and the method ``processTransaction()`` implements the application logic.
74 |
75 | `processTransaction() `_ extracts all creation events from the the transaction and passes them to ``processEvent()``. This produces a list of commands to be sent to the ledger to further the workflow, and these are packages up in a ``Commands`` request and sent to the ledger.
76 |
77 | `processEvent() `_ takes a transaction event and turns it into a stream of commands to be sent back to the ledger. To do this, it examines the event for the correct package and template (it's a create of a ``Ping`` or ``Pong`` template) and then looks at the receiving part to decide if this processor should respond. If so, an exercise command for the correct choice is created and returned in a ``Stream``.
78 |
79 | In all other cases, an empty ``Stream`` is returned, indication no action is required.
80 |
81 | Output
82 | ======
83 |
84 | The application prints statements similar to these:
85 |
86 | .. code-block:: text
87 |
88 | Bob is exercising RespondPong on #1:0 in workflow Ping-Alice-1 at count 0
89 | Alice is exercising RespondPing on #344:1 in workflow Ping-Alice-7 at count 9
90 |
91 | The first line shows that:
92 |
93 | - ``Bob`` is exercising the ``RespondPong`` choice on the contract with ID ``#1:0`` for the workflow ``Ping-Alice-1``.
94 | - Count ``0`` means that this is the first choice after the initial ``Ping`` contract.
95 | - The workflow ID ``Ping-Alice-1`` conveys that this is the workflow triggered by the second initial ``Ping``
96 | contract that was created by ``Alice``.
97 |
98 | The second line is analogous to the first one.
99 |
100 | Example Project -- Ping Pong with Generated Java Data Layer
101 | -----------------------------------------------------------
102 |
103 | The code for this example is in the package `examples.pingpong.codegen `_.
104 |
105 | PingPongCodegenMain.java
106 | ========================
107 |
108 | The entry point for the Java code is the main class `PingPongCodegenMain `_. Look at this class to see:
109 |
110 | - how to connect to and interact with the DAML Ledger via the Java Binding library
111 | - how to use the gRPC layer to build an automation for both parties.
112 | - how to streamline interactions with the ledger types by using auto generated data layer.
113 |
114 | The main function:
115 |
116 | - creates an instance of a ``ManagedChannel`` connecting to an existing ledger
117 | - fetches the ledgerID and packageId from the ledger
118 | - creates ``Identifiers`` for the Ping and Pong templates
119 | - creates and starts instances of `PingPongProcessor `_ that contain the logic of the automation
120 | - injects the initial contracts to start the process
121 |
122 | PingPongProcessor.java
123 | ======================
124 |
125 | The core of the application is the method `PingPongProcessor.runIndefinitely() `_.
126 |
127 | This method retrieves a gRPC streaming endpoint using the ``GetUpdatesRequest`` request, and then creates a ``StreamObserver``, providing implementations of the ``onNext``, ``onError`` and ``onComplete`` observer methods. ``gRPC`` layer arranges that these methods receive stream events asynchronously.
128 |
129 | The method `onNext `_ is the main driver, extracting the transaction list from each ``GetUpdatesResponse``, and passing in to ``processTransaction()`` for processing. This method, and the method ``processTransaction()`` implements the application logic.
130 |
131 | `processTransaction() `_ extracts all creation events from the the transaction and passes them to ``processEvent()``. This produces a list of commands to be sent to the ledger to further the workflow, and these are packages up in a ``Commands`` request and sent to the ledger.
132 |
133 | `processEvent() `_ takes a transaction event and turns it into a stream of commands to be sent back to the ledger. To do this, it examines the event for the correct package and template (it's a create of a ``Ping`` or ``Pong`` template) and then looks at the receiving part to decide if this processor should respond. If so, an exercise command for the correct choice is created and returned in a ``Stream``.
134 |
135 | In all other cases, an empty ``Stream`` is returned, indication no action is required.
136 |
137 | Output
138 | ======
139 |
140 | The application prints statements similar to these:
141 |
142 | .. code-block:: text
143 |
144 | Bob is exercising RespondPong on #1:0 in workflow Ping-Alice-1 at count 0
145 | Alice is exercising RespondPing on #344:1 in workflow Ping-Alice-7 at count 9
146 |
147 | The first line shows that:
148 |
149 | - ``Bob`` is exercising the ``RespondPong`` choice on the contract with ID ``#1:0`` for the workflow ``Ping-Alice-1``.
150 | - Count ``0`` means that this is the first choice after the initial ``Ping`` contract.
151 | - The workflow ID ``Ping-Alice-1`` conveys that this is the workflow triggered by the second initial ``Ping``
152 | contract that was created by ``Alice``.
153 |
154 | The second line is analogous to the first one.
155 |
156 | The Generated Data Layer
157 | ========================
158 |
159 | The ``codegen`` variant of the client application is similar to its ``grpc`` counterpart. Both are written in
160 | a traditional imperative style. What sets them apart is the usage of the generated data layer in the former.
161 | This layer simplifies construction of the ledger api calls and the analysis of the return values.
162 |
163 | - ``PingPongCodegenMain.createInitialContracts`` creates a strongly typed instance of a Ping contract and then embeds it in an equally strongly typed ``CommandsSubmission``. Then, it uses the built in ``toProto`` methods to convert the request into a wire-ready ``protobuf`` structure.
164 | - ``PingPongProcessor.runIndefinitely`` creates a per party inclusive filter by invoking a series of class constructors. Contrast this with the intricate process of defining a filter in the analogous method in the ``grpc`` variant of the application.
165 | - ``PingPongProcessor.processEvent`` starts off by extracting common data fields from the ``grpc`` version of the received events, to be later used for logging purposes. Events are then converted to the corresponding data layer format and passed to the individual template handlers.
166 | - ``PingPongProcessor.processPingPong`` creates a strongly typed representation of the daml contracts by means of the daml contract companions. A strongly typed instance can be used to create a command representing a desired choice exercise.
167 | - ``PingPongProcessor.processTransaction`` is responsible for creating a ledger request enveloping the choice exercises and submitting it to the ledger.
168 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2020 Digital Asset (Switzerland) GmbH and/or its affiliates
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------