# `Numscript`
[🔗](https://github.com/Hivebyte-Inc/numscript/blob/v0.2.0/lib/numscript.ex#L1)

[Numscript](https://docs.formance.com/numscript/), Formance's DSL for moving
money, for Elixir.

It runs the official interpreter (`github.com/formancehq/numscript`, MIT),
compiled to WebAssembly, inside [Wasmex](https://hex.pm/packages/wasmex). The
module is compiled once at boot and kept warm in a small pool of instances,
so a call takes about 2 ms.

This library keeps no state: your ledger owns the balances.

    script = """
    vars { monetary $amount }
    send $amount (
      source = @revenue allowing unbounded overdraft
      destination = { 30% to @savings  remaining to @checking }
    )
    """

    {:ok, %Numscript.Result{postings: postings}} =
      Numscript.run(script, %{"amount" => {"USD/2", 100_000}})

## Balances

The interpreter only knows the balances you give it; a missing account has
zero. Pass a map, `%{"account" => %{"USD/2" => 1000}}`, or a function. The
function receives the script's dependencies (`[{account, asset}]`, sorted) and
returns the map. That is the place to lock the accounts
(`SELECT ... FOR UPDATE`, in the order given) and read their balances, inside
the same database transaction that will write the postings.

## Variables

Strings are passed as they are (`"USD/2 1000"`, `"users:001"`, `"30%"`).
`{asset, integer}` is accepted for monetary values and integers for numbers.

## Experimental features

Some constructs of the interpreter sit behind feature flags (`features/0`),
for example account interpolation (`@users:$id`). They work, but Formance
may still change them: pass the flags you use in `run/3`'s `:features`.

# `balances`

```elixir
@type balances() :: %{optional(String.t()) =&gt; %{optional(String.t()) =&gt; integer()}}
```

# `dependency`

```elixir
@type dependency() :: {account :: String.t(), asset :: String.t()}
```

# `vars`

```elixir
@type vars() :: %{
  optional(String.t() | atom()) =&gt;
    String.t() | integer() | {String.t(), integer()}
}
```

# `check`

```elixir
@spec check(String.t()) ::
  {:ok, %{vars: %{required(String.t()) =&gt; String.t()}}}
  | {:error, Numscript.Error.t()}
```

Parses the script. Returns the variables it expects (`name => type`). On a
syntax error, `Error.errors` lists every error found.

# `dependencies`

```elixir
@spec dependencies(String.t(), vars(), balances()) ::
  {:ok, %{reads: [dependency()], writes: [dependency()]}}
  | {:error, Numscript.Error.t()}
```

Accounts and assets the script reads (`reads`, its sources) and writes
(`writes`). Lock them before running, always in the same order (the lists
are sorted).

# `encode_vars`

```elixir
@spec encode_vars(vars()) :: %{required(String.t()) =&gt; String.t()}
```

Variables in the interpreter's format (all strings): `{asset, integer}`
becomes `"asset integer"`, integers become strings.

# `features`

Feature flags known to the bundled interpreter (all experimental).

# `interpreter_version`

```elixir
@spec interpreter_version() :: {:ok, String.t()} | {:error, Numscript.Error.t()}
```

Version of `github.com/formancehq/numscript` the bundled WASM was built from.

# `run`

```elixir
@spec run(String.t(), vars(), keyword()) ::
  {:ok, Numscript.Result.t()} | {:error, Numscript.Error.t()}
```

Runs the script. Options:

  * `:balances` - a balances map, or `fn dependencies -> map end` (see the moduledoc)
  * `:meta` - account metadata, `%{"account" => %{"key" => "value"}}`
  * `:features` - experimental feature flags to enable (`features/0`)
  * `:timeout` - in ms (default 5000)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
