How to take user input and calculate individual formula

Hi Team,
I am currently working on a project where I need to pass user input from our middleware to DAML (Digital Asset Modeling Language) and subsequently solve a formula. Here is a simplified version of what I am trying to achieve:
value1 := user input
value2 := previous month value (user input from middleware)
value3 := user input
value4 := monthly input (user input from middleware)

total := value1 / (value1 + value2 + value3) * value4
All the input values are being received from users through the middleware and then passed to the DAML network. I am looking for guidance on how to efficiently handle this process and execute the formula in DAML.
If possible, could you please provide a solution or suggestions on how to implement this seamlessly? Your assistance will be highly valuable for the successful completion of this project.

I’m not sure I understand the challenge. Taking your question at face value, you can write a trivial function that takes 4 Decimal arguments and returns a Decimal.

totalValue : Decimal -> Decimal -> Decimal -> Decimal -> Decimal
totalValue value1 value2 value3 value4 = value1 / (value1 + value2 + value3) * value4

Does this answer your question? Or am I missing something?

In general Daml and Canton provide a world leading multi-party authorization platform, but to achieve this provides a very poor basis for RPC. Consequently, it will be important to consider if this entire feature should be done entirely in Daml. It probably will be worth splitting the functionality into a Daml component that exploits the unique advantages of Daml and Canton; and, an off-ledger component that handles the “RPC” aspect of the problem.

When I look at this description I divide the workflow into two key phases:

  1. Authorized Users attest to certain values (value1, value2, value3, value4)
  2. A system performs a calculation based on those values to arrive at a total

The second phase depends on knowning:

  1. The input values were provided by authenticated users, authorized to act as a role authorized to attest to their accuracy
  2. The input values have not been revised or amended since they were provided
  3. Any revision or amendment was performed by authenticated users, authorized to act …*

But the second phase itself is just a function, as @a_putkov has pointed out. While you can attach authorization to the ability to “view” the inputs to the function (the four values); you can’t limit the ability for anyone to evaluate a function — it’s just code.

This means you probably want to provide choices that allow authorized parties to submit the values, and those values will probably be stored as contracts signed by the authorizing parties—this covers dependency #1. Those contracts can probably be acted upon by choices that allow the values to be amended according to whatever business requirements about such things are agreed upon by the participants—this covers dependency #3.

Any code that can see the resulting contracts can then verify they were active at the appropriate point in time and calculate the “total”—this covers dependency #2.

The only time this final step would need to be done on the ledger is if you need some sort of shared consensus on precisely when that total was calculated, and to prove to all parties that need to observe the calculation that the values were in fact current at the time of calculation.

At that point the issue isn’t so much “I want an RPC call to Daml”, but rather “I need a shared consensus on the time and validity of the shared calculation”.

If you don’t have this requirement, then I would recommend running the final calculation as a PQS query on demand and restricting the Daml component to the provenance and authorization parts of the business requirement (phase 1).

(we can shorten “authenticated users, authorized to act as a role authorized to …” to “by an authorized Daml Party” — I only used the long form here to connect the Daml concept to the business one)

Hi Team,
I’m unsure about how to provide input values for this function. Could you please guide me on the proper way to input values through test Script file for value1 , value2 , value3 , and value4

totalValue : Decimal → Decimal → Decimal → Decimal → Decimal
totalValue value1 value2 value3 value4 = value1 / (value1 + value2 + value3) * value4

If the question is how you can call a function and pass the values of arguments to it, then like in any programming language you can specify the values of the arguments in the function call or you can assign the values to variables and use these variables when calling the function. Here’s a quick Daml Script example.

testTotalValue = script do
  let
    a=5.0
    b=2.0
    c=3.0
    d=4.0
  debug $ totalValue a b c d
  totalValue a b c d === 2.0

However, I suspect what you might be asking is how you can create a UI that prompts the user to enter the values to pass to the function call. If this is the question, then the answer is you can’t do it in Daml Script. Daml language is purposely insulated from the outside world. Using Daml one can only communicate with the ledger. Daml doesn’t provide any I/O operations, HTTP calls etc. If you need to collect user inputs from UI, you need to implement this in a ledger client application, which can then pass the values to Daml application deployed on the ledger by submitting a command that exercises a choice on a contract for example. This command submission can be implemented using the HTTP JSON API or the Ledger API. The Daml application would need to have a template with a choice that takes the values of the user inputs as arguments. Inside the body of the choice a Daml function that performs the required calculation can be called with the values of the function arguments taken from the arguments of the choice.
Does this help? Or have I misunderstood what you’re asking?