# Enriched Daml finance Batch by wrapping a daml-finance Batch contract

**URL:** <https://forum.canton.network/t/enriched-daml-finance-batch-by-wrapping-a-daml-finance-batch-contract/6936>\
**Category:** App Development\
**Tags:** daml-finance\
**Created:** [November 20, 2023, 12:10pm UTC](https://forum.canton.network/t/enriched-daml-finance-batch-by-wrapping-a-daml-finance-batch-contract/6936 "2023-11-20T12:10:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvelasco.intellecteu](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/jvelasco.intellecteu/32/7305_2.png) [@jvelasco.intellecteu](https://forum.canton.network/u/jvelasco.intellecteu)\
**Post date:** [November 20, 2023, 12:10pm UTC](https://forum.canton.network/t/enriched-daml-finance-batch-by-wrapping-a-daml-finance-batch-contract/6936/1 "2023-11-20T12:10:10Z")

</div>

Hello,

We have an use case where we would like to **enrich** the daml-finance `Batch` **with an aging feature**. The logic is simple: if the **settler** tries to settle the `Batch` and any of the instructions have not been allocated/approved, we don’t want to fail. Instead, we want to increment an Int field `age`.

Our initial idea was to create a new interface `AgedBatch` with a extra field `age` in the view, and a new `Settle'` choice with the signature below. So in case something is wrong, a new `AgedBatch` with `age` incremented is created.

`choice Settle' : Either (ContractId AgedBatch) [ContractId Holding.I]`

Regarding the implementation, we basically would copy/paste from daml-finance codebase.  
This would work, but we are considering a second approach.

The idea is to have a template which wraps a daml-finance Batch contract like that:

```haskell
template AgedBatch
  with
    settler : Party
    age : Int
    batchCid : ContractId Batch.I
  where
    signatory settler

    choice Settle' : Either (ContractId AgedBatch) [ContractId Holding.I]
      controller settler
      do
        try
          Right <$> exercise batchCid Batch.Settle with
            actors = singleton settler
        catch
          GeneralError _msg -> Left <$> create this with counter = this.counter + 1          
          -- add more exceptions here

```

But the downside in the above model is that we cannot prevent the **settler** to settle using the **wrapped batchCid** instead of the choice `Settle'` in the enriched Batch.

Any ideas?

Thanks!  
Jose Velasco

---

<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:** [November 20, 2023, 4:50pm UTC](https://forum.canton.network/t/enriched-daml-finance-batch-by-wrapping-a-daml-finance-batch-contract/6936/2 "2023-11-20T16:50:13Z")

</div>

Just an idea: Don’t store a `ContractId Batch.I`, but a `Batch` - the non-interface version. Then in your `try`, do a `create` and an `exercise`. That way you can also make `AgedBatch` implement the `Batch.I` interface and use it interchangeably with the standard `Batch`.

```haskell
template AgedBatch
  with
    settler : Party
    age : Int
    batch : Batch
  where
    signatory settler

    interface instance Batch.I for AgedBatch where
      view = view (toInterface @Batch.I batch)
      settle s = 
        try do
          cid <- create batch
          exercise (toInterfaceContractId @Batch.I cid) s
        catch
          GeneralError _msg -> do
            create this with age = age + 1 
            return []         
          -- add more exceptions here
      cancel c = do
        cid <- create batch
        exercise (toInterfaceContractId @Batch.I cid) c

```

You incur a cost of one extra create, but functionally it’ll do what you want. If you don’t want to incur that, you can copy&paste the `settle` and `cancel` implementations from default `Batch`.

---

<div class="post-metadata">

**Author:** ![jvelasco.intellecteu](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/jvelasco.intellecteu/32/7305_2.png) [@jvelasco.intellecteu](https://forum.canton.network/u/jvelasco.intellecteu)\
**Post date:** [November 20, 2023, 5:09pm UTC](https://forum.canton.network/t/enriched-daml-finance-batch-by-wrapping-a-daml-finance-batch-contract/6936/3 "2023-11-20T17:09:49Z")

</div>

> [@bernhard](#):
>
> ```haskell
> try do
> cid <- create batch
> exercise (toInterfaceContractId @Batch.I cid) s
> 
> ```

Thanks @bernhard  
I see your point but if I create a Batch and is settled immediately it will fail because the instructions associated need to be allocated/approved in a separate process. Maybe I’m missing something…

---

<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:** [November 21, 2023, 8:13am UTC](https://forum.canton.network/t/enriched-daml-finance-batch-by-wrapping-a-daml-finance-batch-contract/6936/4 "2023-11-21T08:13:18Z")

</div>

Your `AgedBatch` takes place of the original `Batch` here. Because it also implements the `Batch.I` interface, you can use it interchangeably. The instructions would be created/allocated/approved in a separate process as with a normal `Batch`. The only difference here is that the `Settle` choice does something different: if settlement fails, it increments the age.

If this doesn’t make sense, I most likely misunderstood what you are trying to accomplish.

---

<div class="post-metadata">

**Author:** ![jvelasco.intellecteu](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/jvelasco.intellecteu/32/7305_2.png) [@jvelasco.intellecteu](https://forum.canton.network/u/jvelasco.intellecteu)\
**Post date:** [November 21, 2023, 8:34am UTC](https://forum.canton.network/t/enriched-daml-finance-batch-by-wrapping-a-daml-finance-batch-contract/6936/5 "2023-11-21T08:34:06Z")

</div>

Thanks for the clarification @bernhard  
It makes totally sense.
