# ValueDecoder for tuple and contract IDs

**URL:** https://forum.canton.network/t/valuedecoder-for-tuple-and-contract-ids/6747
**Category:** App Development
**Tags:** codegen, java
**Created:** [July 12, 2023, 5:57am UTC](https://forum.canton.network/t/valuedecoder-for-tuple-and-contract-ids/6747 "2023-07-12T05:57:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![huw](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/huw/32/2343_2.png) [@huw](https://forum.canton.network/u/huw)
#### Post date: [July 12, 2023, 5:57am UTC](https://forum.canton.network/t/valuedecoder-for-tuple-and-contract-ids/6747/1 "2023-07-12T05:57:15Z")

</div>

Hi. I am using the java bindings. I need to convert the `Value` returned by the ledger API in the exercise result for this [choice](https://github.com/digital-asset/daml-finance/blob/1a58c1df3776c918c98cd2333b1e12ed0e99df1c/src/main/daml/Daml/Finance/Interface/Settlement/Factory.daml#L45) into the correct type - `Tuple2<Batch.ContractId, List<Instruction.ContractId>>`. I assume I will need to use something like `Tuple2.valueDecoder(?, ?)` but not sure what the arguments should be.

---

<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: [July 12, 2023, 7:34am UTC](https://forum.canton.network/t/valuedecoder-for-tuple-and-contract-ids/6747/2 "2023-07-12T07:34:15Z")

</div>

`Tuple2` takes two type parameters for the respective values in your tuple. The arguments to the `Tuple2.valueDecoder` are the decoders for those types. So if you have a `Tuple2<String, Party>` you need to pass value decoders for string and party.

---

<div class="post-metadata">

### Author: ![huw](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/huw/32/2343_2.png) [@huw](https://forum.canton.network/u/huw)
#### Post date: [July 12, 2023, 7:37am UTC](https://forum.canton.network/t/valuedecoder-for-tuple-and-contract-ids/6747/3 "2023-07-12T07:37:34Z")

</div>

Yes but how can I get value decoders for ContractId types mentioned above?

---

<div class="post-metadata">

### Author: ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)
#### Post date: [July 12, 2023, 3:12pm UTC](https://forum.canton.network/t/valuedecoder-for-tuple-and-contract-ids/6747/4 "2023-07-12T15:12:44Z")

</div>

As you noticed [over here](https://discuss.daml.com/t/java-bindings-example-seems-a-little-out-of-date/6748), using the new strongly-typed command submission methods is the best way to get a choice exercise result decoded for you.

Otherwise, if you look at the code generated for `CHOICE_Instruct`, you can see how the return-type decoder used in the aforementioned command submission methods is assembled. With regard to concrete contract ID types, it should look something like

```haskell
value -> new Batch.ContractId(
  value.asContractId()
    .orElseThrow(() -> new IllegalArgumentException("Batch"))
    .getValue())

```

Concrete contract-IDs are surely a missing piece of the combinator-library puzzle for assembling value decoders, but given the above pattern you can define a general-purpose decoder like so:

```haskell
  // use like concreteContractIdValueDecoder(Batch.COMPANION)
  public static final <ContractType, Id>
      ValueDecoder<Id> concreteContractIdValueDecoder(
          ContractTypeCompanion<?, Id, ContractType, ?> companion) {
    return value ->
        companion.toContractId(
            new ContractId<>(
                value
                    .asContractId()
                    .orElseThrow(
                        () ->
                            new IllegalArgumentException(
                                companion.TEMPLATE_ID.getEntityName() + ".ContractId"))
                    .getValue()));
  }

```
