# An editable contributor payout round, decided by a real Raxol.Payments.Ledger and # rendered by Raxol. Mock settlement: no signer, provider, wallet or settlement endpoint. # Edit PayoutRound.Config below, then rerun: sh start.sh payouts # (or, from raxol-agent/packages/raxol_payments: mix run --no-start --no-compile ) defmodule PayoutRound.Config do alias Raxol.Payments.SpendingPolicy # The rules every payment in this round must pass. def policy do %SpendingPolicy{ per_request_max: Decimal.new(30), session_max: Decimal.new(60), lifetime_max: Decimal.new(1000), currency: "USDC" } end # {contributor, whole USDC, what for}, decided in this order. def payouts do [ {"ada", 25, "parser fix"}, {"grace", 35, "release tooling"}, {"linus", 30, "ci speedup"}, {"margaret", 20, "docs pass"} ] end end # Keep Raxol's local data inside this disposable project. Set inside the VM, never # exported by the launcher, so no version manager shim sees a redirected XDG. project = Path.dirname(__ENV__.file) System.put_env(%{ "XDG_DATA_HOME" => Path.join(project, ".data"), "XDG_CONFIG_HOME" => Path.join(project, ".config") }) Logger.configure(level: :error) Application.put_env(:raxol, :startup_mode, :minimal) # No source watcher: it needs inotify-tools in a container and nothing here reloads. Application.put_env(:raxol, :code_reloader, false) {:ok, _} = Application.ensure_all_started(:raxol) defmodule PayoutRound do # `use Raxol.Agent` imports the view DSL, so helpers here avoid names like `label`. use Raxol.Agent alias Raxol.Payments.Ledger alias PayoutRound.Config @ledger :payout_ledger @agent "payout-round" @meter_width 24 def init(_), do: %{rows: [], spent: 0} def update({:pay, who, amount, why}, state) do decision = case Ledger.try_spend(@ledger, @agent, Decimal.new(amount), Config.policy(), %{to: who}) do :ok -> :paid {:over_limit, limit} -> {:refused, limit} end spent = if decision == :paid, do: state.spent + amount, else: state.spent {%{state | rows: state.rows ++ [{who, amount, why, decision}], spent: spent}, []} end def view(model) do policy = Config.policy() budget = Decimal.to_integer(policy.session_max) box style: %{border: :single, padding: 1, width: 72, height: 9 + length(model.rows)} do column style: %{gap: 0} do [ text("Contributor payout round (mock settlement, no signer)", style: [:bold]), text( "policy: #{policy.per_request_max} #{policy.currency} per payment, " <> "#{budget} #{policy.currency} per session" ), text(""), text(row_text("contributor", "USDC", "for", "decision"), style: [:dim]) ] ++ Enum.map(model.rows, fn {who, amount, why, decision} -> text(row_text(who, Integer.to_string(amount), why, decision_text(decision))) end) ++ [ text(""), text("session budget [#{meter(model.spent, budget)}] #{model.spent}/#{budget} USDC") ] end end end @doc "One plain line for logs and report.json." def summary(model) do paid = Enum.count(model.rows, fn {_, _, _, decision} -> decision == :paid end) budget = Decimal.to_integer(Config.policy().session_max) "payout round: #{paid} paid, #{length(model.rows) - paid} refused, " <> "session #{model.spent}/#{budget} USDC" end defp meter(spent, budget) do filled = min(div(spent * @meter_width, max(budget, 1)), @meter_width) String.duplicate("#", filled) <> String.duplicate(".", @meter_width - filled) end defp row_text(who, amount, why, decision) do String.pad_trailing(who, 13) <> String.pad_leading(amount, 5) <> " " <> String.pad_trailing(why, 20) <> decision end defp decision_text(:paid), do: "paid (mock)" defp decision_text({:refused, :per_request}), do: "REFUSED over #{Config.policy().per_request_max} cap" defp decision_text({:refused, :session}), do: "REFUSED budget spent" defp decision_text({:refused, :lifetime}), do: "REFUSED lifetime cap" defp decision_text({:refused, other}), do: "REFUSED: #{other}" end {:ok, _} = Raxol.Payments.Ledger.start_link(name: :payout_ledger) unless Process.whereis(Raxol.Headless), do: Raxol.Headless.start_link() {:ok, id} = Raxol.Headless.start(PayoutRound, id: "payouts", subscriptions: false) for {who, amount, why} <- PayoutRound.Config.payouts() do :ok = Raxol.Headless.send_message(id, {:pay, who, amount, why}) end {:ok, screen} = Raxol.Headless.screenshot(id) {:ok, model} = Raxol.Headless.get_model(id) IO.puts(String.trim_trailing(screen)) IO.puts(PayoutRound.summary(model))