Numscript, Formance's DSL for moving money, for Elixir.
It runs the official interpreter (github.com/formancehq/numscript, MIT),
compiled to WebAssembly, inside 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.
Summary
Functions
Parses the script. Returns the variables it expects (name => type). On a
syntax error, Error.errors lists every error found.
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).
Variables in the interpreter's format (all strings): {asset, integer}
becomes "asset integer", integers become strings.
Feature flags known to the bundled interpreter (all experimental).
Version of github.com/formancehq/numscript the bundled WASM was built from.
Runs the script. Options
Types
Functions
@spec check(String.t()) :: {:ok, %{vars: %{required(String.t()) => 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.
@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).
Variables in the interpreter's format (all strings): {asset, integer}
becomes "asset integer", integers become strings.
Feature flags known to the bundled interpreter (all experimental).
@spec interpreter_version() :: {:ok, String.t()} | {:error, Numscript.Error.t()}
Version of github.com/formancehq/numscript the bundled WASM was built from.
@spec run(String.t(), vars(), keyword()) :: {:ok, Numscript.Result.t()} | {:error, Numscript.Error.t()}
Runs the script. Options:
:balances- a balances map, orfn 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)