Accessing test data within Scripts

Hi @Alexander_Bernauer,

The answer is contained in the documentation page of Daml Script, but it did take me a few minutes to re-puzzle it out, so I definitely agree it could be more explicit. The mechanism is actually really simple: the input file is used to define the argument to the script function. Here is a complete example. First, Main.daml:

module Main where

import Daml.Script

data InputLine = InputLine with name: Text, age: Int
  deriving (Show, Eq)

setup : [InputLine] -> Script Int
setup input = script do
  return $ sum (map (.age) input)

You can now run (after starting a sandbox with daml sandbox in another terminal):

$ daml build
Compiling t to a DAR.
Created .daml/dist/t-0.0.1.dar
$ RUN="daml script --dar .daml/dist/t-0.0.1.dar --script-name Main:setup --ledger-host localhost --ledger-port 6865 --output-file out.json --input-file"
$ $RUN <(echo '[{"name": "Alice", "age": 23}]')
$ cat out.json 
23
$ $RUN <(echo '[{"name": "Alice", "age": 23}, {"name": "Bob", "age": 25}]')
$ cat out.json 
48
$ 

So the piping is fairly straightforward: --input-file is the argument given to the function named as --script-name, and --output-file contains the return value of said function.

3 Likes