Skip to content
Discussions/App Development/Round amount with strategiesForum ↗

Round amount with strategies

App Development3 posts345 viewsLast activity Dec 2021
RU
rui.genesisOP
Dec 2021

Hi community,

Could you help me to understand if somehow it’s possible to have different strategies for example to round a value.
I leave you the example bellow to be easier to understand what I would like to do. In this example each company
can create a Product contract which applies a round “strategy” to Subscription amount, according with each company preferences.

For example in Product 1, released by company 1, they want to use floor “strategy” to round amounts,
but company 2 wants to use ceil “strategy” to round amounts.

That’s I would like to implement with roundAmount function.
Somehow pass the strategy name and grab its corresponding fn in a dynamic way.
Is it possible to have it using DAML ? For example with a Map Text FN ? Or using another ways ?

template Product
    with
        code: Text
        round_strategy: Text
        issuer: Party
    where
        signatory issuer
    
        controller issuer can
           nonconsuming Product_Propose_Subscription : ContractId SubscriptionProposal
                with
                    holder: Party
                    amount_quote: Decimal
                do
                    create SubscriptionProposal with
                        code = "some bussiness code"
                        product_code = code
                        amount = roundAmount amount_quote round_strategy
                        issuer
                        holder


template SubscriptionProposal
    with
        code: Text
        product_code: Text
        amount: Int
        issuer: Party
        holder: Party
    where
        signatory issuer



roundAmount: Decimal -> Text -> Int
roundAmount amount strategy = 
    let strategyFn = getRoundStrategyFn strategy
    strategyFn amount

GA
Gary_Verhaegen
Dec 2021

If you know of all of the strategies in advance (i.e. at template definition time), you can have a simple enum on the wire and use that.

If you want a more open-ended set of options, you’ll need to invent your own mini-language and write an interpreter for it in Daml. It’s not very difficult (if you keep to an abstract syntax and skip over all the parsing stuff) for a simple arithmetic language. See this post for a more in-depth example.

Edit: This discussion may also be relevant.

RU
rui.genesis
Dec 2021

@Gary_Verhaegen Thank you for you suggestions. I believe this enum approach will do.

← Back to Discussions