# Getting all choices for a given Party or Contract

**URL:** <https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830>\
**Category:** App Development\
**Tags:** react\
**Created:** [July 4, 2022, 2:15pm UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830 "2022-07-04T14:15:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Freitas](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/joao_freitas/32/3352_2.png) [@Joao\_Freitas](https://forum.canton.network/u/Joao_Freitas)\
**Post date:** [July 4, 2022, 2:15pm UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/1 "2022-07-04T14:15:53Z")

</div>

Hi everyone,

is there any way that on React Js app we can fetch all the possible choices a given party or contract can exercise? Just like you guys have on navigator?

 ![Screenshot 2022-07-04 at 15.12.58](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/d/de7c656baf375fb2bf3cee9703feabfcdae1dd59.png)

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [July 5, 2022, 7:29am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/2 "2022-07-05T07:29:03Z")

</div>

What the Navigator does is fetching packages, interpreting its contents (which includes the signatures for all templates and choices) and using those to allow to have a generic interface to the underlying ledger.

The HTTP JSON API Service and the React.js bindings (which work against the former) are not meant for “metaprogramming” and don’t expose facilities for “introspection”. However, you might be able to piggy back on the TypeScript codegen and leverage the dynamic aspects of JS to inspect the generated code and retrieve the available choices. Mostly thinking out loud, but it might work.

---

<div class="post-metadata">

**Author:** ![Joao\_Freitas](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/joao_freitas/32/3352_2.png) [@Joao\_Freitas](https://forum.canton.network/u/Joao_Freitas)\
**Post date:** [July 5, 2022, 8:02am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/3 "2022-07-05T08:02:47Z")

</div>

Thanks 🙂 I will take a look on both options.

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [July 5, 2022, 9:10am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/4 "2022-07-05T09:10:47Z")

</div>

You can also do what the Navigator does in the background: Decode the Daml package and interpret its contents.

You can download the Daml package (DALF) you need [using the JSON API](https://docs.daml.com/json-api/index.html#download-a-dalf-package). As documented [here](https://docs.daml.com/daml/intro/9_Dependencies.html#dar-dalf-daml-lf-and-the-engine), DALF packages are just binary encoded protobuf messages from the Daml-LF schema.

You can use [protobuf.js](https://github.com/protobufjs/protobuf.js) to decode DALFs, thought to make it comfortable, you’ll want to load the proto schema files, which you can download on [GitHub releases](https://github.com/digital-asset/daml/releases). You may need to hack around a bit to deal with relative vs absolute paths in the proto files and rename the `constructor` field in `Enum` since protobuf.js doesn’t deal with it, but once you put it all together, you can get your hands on the payload as a JS object, and then access template, choice, and type information dynamically.

A PoC grade way of getting at the package info here:

```haskell
<head>
    <script src="https://cdn.rawgit.com/dcodeIO/protobuf.js/6.11.3/dist/protobuf.js"></script>
</head>
<body>
    <h1>Daml Archive Reader</h1>
    <p></p>

    <script>
        document.getElementsByTagName('p')[0].innerText = "foo";

        function withArchive(path, callback) {
            var req = new XMLHttpRequest();
            req.open("GET", path, true);
            req.responseType = 'arraybuffer';
            req.onload = function(e) {
                if (this.status == 200) {
                    callback(new Uint8Array(this.response)); 
                }
            }
            req.send();
        }
        
        protobuf.load("protos-2.2.0/com/daml/daml_lf_1_14/daml_lf.proto", function(err, root) {
            if (err)
                throw err;

            withArchive(".daml/dist/create-daml-app-0.1.0-1f477a11f7fd9914437b909e89ea15775e705e182d2296dfa60fb0414fb0b74c/create-daml-app-0.1.0-1f477a11f7fd9914437b909e89ea15775e705e182d2296dfa60fb0414fb0b74c.dalf", function(buffer) {
                // Get the archive
                var Archive = root.lookupType("daml_lf_1_13.Archive");
                var archive = Archive.decode(buffer);
                // Get the payload

                var Payload = root.lookupType("daml_lf_1_13.ArchivePayload");
                var payload = Payload.decode(archive.payload);

                document.getElementsByTagName('p')[0].innerText = JSON.stringify(payload);
            })
        });

    </script>
</body>

```

---

<div class="post-metadata">

**Author:** ![Joao\_Freitas](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/joao_freitas/32/3352_2.png) [@Joao\_Freitas](https://forum.canton.network/u/Joao_Freitas)\
**Post date:** [July 5, 2022, 9:23am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/5 "2022-07-05T09:23:00Z")

</div>

Thanks for the helpful insights [bernhard](https://discuss.daml.com/u/bernhard) 🙂 .  
I will leave a feedback here after trying this approach! 🙂

---

<div class="post-metadata">

**Author:** ![Illia.Milevskiy](https://avatars.discourse-cdn.com/v4/letter/i/85e7bf/32.png) [@Illia.Milevskiy](https://forum.canton.network/u/Illia.Milevskiy)\
**Post date:** [August 16, 2022, 10:07am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/6 "2022-08-16T10:07:01Z")

</div>

Thank you for your advice!

I was success to get all data as you recommended. But my problem is - I got all data, and it takes a lot of time to get it. So I’m looking for a solution, How can I pick up just needed pieces of data? I thought that I could get just one package and decode it. I use this doc to get it. But the response I receive with .bin resolution, and I can’t decode it properly. Can you advise how I can get just the needed piece of data from daml?  
[https://docs.daml.com/json-api/index.html?\_ga=2.229756572.1649263458.1659520863-497201012.1642427044&\_gl=1\*hdodvm\*\_ga\*NDk3MjAxMDEyLjE2NDI0MjcwNDQ.\*\_ga\_GVK9ZHZSMR\*MTY1OTUzMjE2NC4zLjEuMTY1OTUzNDA1Mi4w#download-a-dalf-package](https://docs.daml.com/json-api/index.html?_ga=2.229756572.1649263458.1659520863-497201012.1642427044&_gl=1*hdodvm*_ga*NDk3MjAxMDEyLjE2NDI0MjcwNDQ.*_ga_GVK9ZHZSMR*MTY1OTUzMjE2NC4zLjEuMTY1OTUzNDA1Mi4w#download-a-dalf-package)

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [August 16, 2022, 5:17pm UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/7 "2022-08-16T17:17:44Z")

</div>

You cannot fetch the data below the granularity of a package. If you need it to be more efficient, build a client-side cache to give you access to that information in a more efficient way. Depending on your requirements that could be an in-memory cache or at some point, you want to back that cache by a database.

---

<div class="post-metadata">

**Author:** ![Illia.Milevskiy](https://avatars.discourse-cdn.com/v4/letter/i/85e7bf/32.png) [@Illia.Milevskiy](https://forum.canton.network/u/Illia.Milevskiy)\
**Post date:** [August 22, 2022, 12:14pm UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/8 "2022-08-22T12:14:36Z")

</div>

That’s what I try to do,  
I do a call to get some specific package and then try to unarchive with protobuf, the same as in Bernhard’s example.

```haskell
        function withArchive(path, callback) {
            var req = new XMLHttpRequest();
            req.open("GET", path, true);
            req.responseType = 'arraybuffer';
            req.setRequestHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwczovL2RhbWwuY29tL2xlZGdlci1hcGkiOnsibGVkZ2VySWQiOiJ4YmZpcnN0LWxlZGdlci1zYW5kYm94IiwiYXBwbGljYXRpb25JZCI6InhiZmlyc3QtbGVkZ2VyLXNhbmRib3giLCJyZWFkQXMiOlsiUHVibGljIl19fQ.7hTOBSYv2POOJjXNLScE3p1ebH4ZMpsYM2a58YWNJM0")
            req.onload = function(e) {
                if (this.status == 200) {
                    callback(new Uint8Array(this.response)); 
                }
            }
            req.send();
        }

```

```haskell
protobuf.load("protos-1.17.1/com/daml/daml_lf_1_14/daml_lf.proto", function(err, root) {
            console.log("test")
            if (err)
                throw err;

            // withArchive(".daml/dist/xbfirst-ledger-1.0.0/xbfirst-ledger-1.0.0-bfb772d261fbf18c9fbbed3450e626422b3dde16c840a70e10266df83262cd87/xbfirst-ledger-1.0.0-bfb772d261fbf18c9fbbed3450e626422b3dde16c840a70e10266df83262cd87.dalf", function(buffer) {
                withArchive("http://localhost:7576/v1/packages/cc348d369011362a5190fe96dd1f0dfbc697fdfd10e382b9e9666f0da05961b7", function(buffer) {
                // Get the archive
                var Archive = root.lookupType("daml_lf_1_13.Archive");
                var archive = Archive.decode(buffer);
                // Get the payload

                var Payload = root.lookupType("daml_lf_1_13.ArchivePayload");
                var payload = Payload.decode(archive.payload);

                document.getElementsByTagName('p')[0].innerText = JSON.stringify(payload);
            })
        });

```

The problem is in Bernhard’s example. We specify a direct path to local package with dalf resolution. But when I try to get some package with API request, I get a response with .bin resolution, and protobuf can’t decode it. How can I get the package with dalf resolution via API request?

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [August 22, 2022, 1:16pm UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/9 "2022-08-22T13:16:20Z")

</div>

What errors are you getting, it works just fine for me.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [August 22, 2022, 1:35pm UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/10 "2022-08-22T13:35:10Z")

</div>

The package endpoint gives you directly the `ArchivePayload` without the `Archive` wrapper so try skipping the part where you decode just the Archive and directly decode what you get from the endpoint to the payload.

---

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [August 24, 2022, 9:23am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/11 "2022-08-24T09:23:14Z")

</div>

Maybe it’s not an important detail for you, but I want to draw your attention to the fact that Navigator actually doesn’t fulfill the “for a given party” part of your requirement. When you log in to Navigator as a certain party, you can see all the choices which are available on a certain contract, not just those ones where the logged-in party is the controller. If the logged-in party tries to exercise a random choice displayed by Navigator, the submission can fail.

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [August 24, 2022, 9:35am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/12 "2022-08-24T09:35:19Z")

</div>

The question “Is there an input value `args` such that party `p` can call choice `c` with argument `args`?” is in general undecidable, even when we pay attention only to the `controller` clause.

```haskell
template Foo
  with
    p : Party
    someHash : Text
  where
    signatory p

    choice GoodLuck : ()
      with
        args : Text
      controller (if sha256 args == someHash then [p] else [])
      do
        return () 

```

You could use some heuristics to exclude choices that _obviously_ won’t work like

```haskell
    choice Uncallable : ()
      controller ([] : [Party])
      do
        return () 

```

And you can do that client-side. When you parse the Daml-LF, you get access to the controller expression. You could exclude all choices where that value is a single party or a list of parties, which isn’t a subset of the rights you hold in your claim.

---

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [August 24, 2022, 9:37am UTC](https://forum.canton.network/t/getting-all-choices-for-a-given-party-or-contract/4830/13 "2022-08-24T09:37:43Z")

</div>

Yes, that’s right.
