How can I listen to the archival of a contract

G-d willing

Hello,
I want to write a small java application that tests a consumable choice of a contract. The Java application simply creates the contract and exercises its choice. And once it is exercised the contract should be archived. What is the best way to verify it?

Thanks,

1 Like
  1. Spin up a sandbox
  2. Have the Java application create the contract
    • retain the contract ID in your test
  3. Have the Java application exercise the choice on your contract
  4. Have your test fetch the ACS for the relevant template
  5. Inspect the result
  6. Assert that there is no contract with the ID you retained above

Another possibility is to have a choice that just fetches the contract and use that instead of asking for the ACS, but this means changing the Daml model for debugging purposes, which might not fit your intention.

Another possibility, if you application consumes the ACS already and keeps an up-to-date copy of it locally as its operational data store (ODS) you might want to use your application’s facilities to this end.

With that said, an important note: the Ledger API is thoroughly tested to ensure that consuming choice actually consume the contract on which they are defined. If by any chance you believe you might be testing the Ledger API rather than your application, I would recommend to “raise the abstraction level” of the test to make sure that the application-specific outcome is tested, rather than the Ledger API’s behavior (you wouldn’t test that a DELETE statement deletes a row on the database, you would ensure that the services that rely on that row being there behave as you would expect).

I 100% agree and know that as well. However, there is a great challenge with my specific case since by exercising the consuming choice, there are multiple Holding contracts that are created. And verifying that is a somewhat complicated.

There is an additional option to observe the archived event from a Java app using this library.
There is a function:
TreeEvent observeEvent(String party, MessageTester<TreeEvent> eventTester)
That one can use like this:

sandbox.getLedgerAdapter().observeEvent(ALICE_PARTY, ContractArchived.apply(MyTemplate.TEMPLATE_ID, contractId));

More details are in the README. The library works with daml > 2.0

1 Like