14/Build
Run your own desk
Copy .env.example to .env at the repo root, install with pnpm, create three HCS topics, set up the ENS name, then run the gateway, the agent and the web app. Without keys the agent still runs read-only on live mainnet data, which is the safest way to start.
Requirements
Node 22 or newer, pnpm 9 (the workspace pins pnpm@9.15.9), and Foundry if you want to build or deploy the Uniswap hook.
Commands
cp .env.example .env # the agent, gateway and scripts load the root .env
pnpm install
pnpm build && pnpm test # 20 kernel + 16 agent + 5 gateway + 29 plugin + 17 ENS tests
pnpm test:contracts # 15 Foundry tests for the v4 hook
pnpm init:topics # prints HCS_*_TOPIC_ID and NEXT_PUBLIC_HCS_*_TOPIC_ID lines
pnpm dev:gateway # :4020 x402 seller, discovery, guarantees
pnpm dev:agent # the autonomous loop
pnpm dev:web # :3000 landing page, /app and /docs
pnpm agent:once # one cycle, then exit
pnpm quote # paid toxicity quote from the CLI
pnpm demo:buyer # the counterparty buyer agentGoing live on mainnet
- Hedera account. Create a mainnet account for the agent, fund it with a little HBAR and USDC
0.0.456858(associate the token), and setHEDERA_OPERATOR_IDandHEDERA_OPERATOR_KEY. - HCS topics. Run
pnpm init:topicsand paste the printed ids into.env. - ENS. Register the parent name, then run
ens:setup,ens:register-erc8004(putERC8004_AGENT_IDin.env),ens:publishandens:delegate grant. See ENS identity and mandate. - SaucerSwap. Set
SAUCERSWAP_API_KEY. LeaveLIVE_EXECUTION=falsefor a dry run first. - x402. The defaults point at the Blocky402 mainnet facilitator; set
BLOCKY402_API_KEYif it asks for one, and setINTERNAL_SYNC_SECRET. - Uniswap hook (optional). Deploy it (see Uniswap v4 LVR Shield), then set
UNISWAP_V4_HOOK_ADDRESSandHOOK_ORACLE_PRIVATE_KEY.
Environment variables
Defaults are the values in code when the variable is unset, which sometimes differ from .env.example.
| Variable | Default | What it does |
|---|---|---|
HEDERA_NETWORK | mainnet | The network the agent trades and publishes on. Any value containing testnet selects testnet |
HEDERA_OPERATOR_ID | unset | The desk's account. Unset: the agent is read-only (no HCS, no swaps, no balances, so no treasury) and the gateway pays to 0.0.0 with HCS off |
HEDERA_OPERATOR_KEY | unset | Its key: ECDSA hex (with or without 0x) or DER |
HEDERA_MIRROR_URL | https://mainnet.mirrornode.hedera.com | Mirror Node for the agent's balance and fee reads |
SIGNAL_NETWORK | mainnet | Where market signals come from. Keep mainnet even when trading on testnet |
HCS_DECISION_TOPIC_ID | unset | Decision, execution, mode and agent-card messages. Unset: not published |
HCS_PNL_TOPIC_ID | unset | Ledger, clearance and refund messages |
HCS_TRACK_TOPIC_ID | the decision topic | Predictions and scores |
| Variable | Default | What it does |
|---|---|---|
LIVE_EXECUTION | false | Only true broadcasts swaps; otherwise every trade is a dry run |
SAUCERSWAP_API_KEY | unset | Needed by saucer-swap-plugin; without it live swaps are blocked |
MAX_TRADE_USD | 5 | Hard per-trade cap on top of the ENS mandate |
MIN_TRADE_USD | 0.5 | Smaller rebalances are skipped as dust |
HBAR_FEE_RESERVE | 5 | HBAR never sold, kept for fees |
POLL_INTERVAL_MS | 60000 | Cycle length, times the survival cadence. The gateway also uses it to judge agentOnline |
SHUTDOWN_FLOOR_USD | 1 | Below this treasury value the agent stops spending |
PREDICTION_INTERVAL_MS | 900000 | One forecast every 15 minutes |
PREDICTION_HORIZON_MS | 3600000 | Each forecast is scored after one hour |
GAS_FLUSH_INTERVAL_MS | 600000 | How often the agent books its batched HCS fees |
PARITY_POOL_ID | saucerswap-hbar-usdc-v2 | Pool id checked against the mandate's allowlist |
INITIAL_RANGE_HALF_WIDTH_TICKS | 480 | Starting target range before the agent picks one |
AGENT_STATE_FILE | .parity/agent-state.json | Agent memory: ledger, forecasts, executions, range |
OPENAI_API_KEY | unset | Turns on the LLM strategist. Unset: quant-only |
OPENAI_BASE_URL | https://api.openai.com/v1 | Any OpenAI-compatible endpoint |
OPENAI_MODEL | gpt-4o-mini | Model name |
LLM_CALL_COST_USD | 0.0025 | Booked per call as an off-chain cost |
| Variable | Default | What it does |
|---|---|---|
X402_NETWORK | hedera:mainnet | Payment network and the preset for pool and USDC ids |
BLOCKY402_FACILITATOR_URL | https://api.blocky402.com | Facilitator (testnet default https://api.testnet.blocky402.com) |
BLOCKY402_API_KEY | unset | Sent as X-Api-Key on verify, settle and supported |
X402_GATEWAY_PORT | 4020 | Listen port |
X402_GATEWAY_URL | http://localhost:4020 | Where the agent pushes, and the public base URL in the agent cards |
X402_BASE_PRICE_USD | 0.01 | Base of the price formula |
INTERNAL_SYNC_SECRET | unset | Shared secret for /internal/snapshot. Unset: only loopback callers can push |
BUILDER_ACCOUNT_IDS | unset | Comma-separated accounts excluded from revenue, on top of the operator |
CLEARANCE_TTL_MS | 120000 | How long a clearance can be executed and claimed |
CLEARANCE_MAX_NOTIONAL_USD | 1000 | Largest trade the guarantee covers |
GATEWAY_STATE_FILE | .parity/gateway-state.json | Persisted books |
| Variable | Default | What it does |
|---|---|---|
ENS_CHAIN | mainnet | mainnet or sepolia |
ENS_RPC_URL | https://ethereum-rpc.publicnode.com | Ethereum RPC (Sepolia default https://ethereum-sepolia-rpc.publicnode.com) |
ENS_SUBNAME | unset | The agent's ENS name (ENS_NAME also works). Unset: mandate unavailable |
ENS_MANDATE_READ_MODE | live | fallback gives an always-active local mandate, for development only |
ENS_MANDATE_MAX_STALENESS_MS | 300000 | How long a cached read survives an RPC outage |
ENS_REQUIRE_EAC_DELEGATION | false | true also requires ENS_AGENT_ADDRESS to hold the parity.* delegation |
ENS_OWNER_PRIVATE_KEY | unset | Owner key for the setup and publish scripts |
ENS_AGENT_PRIVATE_KEY | unset | Agent key: signs its ENSIP-28 consent and --as-agent pauses |
HEDERA_EVM_PRIVATE_KEY | unset | ECDSA key so the Hedera account can be listed under ENSIP-28 |
ENS_HEDERA_NETWORK | hedera:mainnet | Hedera chain used in ENSIP-11/28/31 records |
ERC8004_CHAIN_ID | 1 | Registry chain; 296 for Hedera testnet, 11155111 for Sepolia |
ERC8004_REGISTRY_ADDRESS | per chain | Override the IdentityRegistry address |
ERC8004_AGENT_ID | unset | Required. Without it the ENSIP-25 check fails and the desk stays paused |
ERC8004_AGENT_URI | ${X402_GATEWAY_URL}/agent.json | Registered agent URI |
ERC8004_PRIVATE_KEY | ENS_OWNER_PRIVATE_KEY | Signs the registration |
PARITY_MAX_NOTIONAL_USD | 1000 (.env.example: 20) | Published to parity.maxNotionalUsd |
PARITY_MAX_SLIPPAGE_BPS | 60 | Published to parity.maxSlippageBps |
PARITY_MAX_LTV_BPS | 6500 | Published to parity.maxLtvBps |
PARITY_MIN_REBALANCE_EV_USD | 0.01 | Published to parity.minRebalanceEvUsd |
PARITY_ALLOWED_POOLS | saucerswap-hbar-usdc-v2 | Published to parity.allowedPools |
PARITY_PAYMENT_HTS_TOKEN_IDS | 0.0.456858 | Tokens in the ENSIP-31 payment preference |
PARITY_WEB_URL, PARITY_MCP_URL, PARITY_A2A_URL | unset | Optional ENSIP-26 endpoints, also listed in /agent.json |
| Variable | Default | What it does |
|---|---|---|
UNISWAP_V4_HOOK_ADDRESS | unset | The deployed hook. Unset: the publisher is disabled |
HOOK_ORACLE_PRIVATE_KEY | unset | The hook's guardianOracle (or owner) key |
HOOK_CHAIN_ID | 130 | Unichain; also 1, 1301, 11155111 |
HOOK_RPC_URL | viem default | Needed only for other chains |
HOOK_MIN_SCORE_DELTA | 5 | Smallest score move worth a transaction |
HOOK_HEARTBEAT_MS | 600000 | Rewrite at least this often; must beat the hook's staleness window |
| Variable | Default | What it does |
|---|---|---|
BUYER_ACCOUNT_ID, BUYER_PRIVATE_KEY | unset | The plugin's buyer demo account |
PARITY_GATEWAY_URL | unset | Gateway the buyer demo calls (.env.example: http://localhost:4020) |
X402_BUYER_ACCOUNT_ID, X402_BUYER_PRIVATE_KEY | the operator account | The pnpm quote CLI's payer |
NEXT_PUBLIC_GATEWAY_URL | http://localhost:4020 | Gateway the web app reads |
NEXT_PUBLIC_MIRROR_URL | public mainnet mirror | Mirror Node for browser-side HCS reads when /status does not name one |
NEXT_PUBLIC_HCS_DECISION_TOPIC_ID (and _PNL_, _TRACK_) | unset | Topic ids to use when /status has none |
NEXT_PUBLIC_SITE_URL | http://localhost:3000 | Base URL for metadata |
NEXT_PUBLIC_REPO_URL | unset | Shows a GitHub link in the docs top bar |
Mainnet and testnet
Everything defaults to mainnet. To run against testnet, set HEDERA_NETWORK=testnet, X402_NETWORK=hedera:testnet and ENS_HEDERA_NETWORK=hedera:testnet. The facilitator only defaults to https://api.testnet.blocky402.com when BLOCKY402_FACILITATOR_URL is unset, and .env.example sets it to the mainnet URL, so change that line too. Keep SIGNAL_NETWORK=mainnet: signals should come from a liquid venue even when the desk trades on testnet.
| Preset | Mainnet | Testnet |
|---|---|---|
| SaucerSwap V2 HBAR/USD pool | 0.0.3964804 | 0.0.9283328 |
| Pool USD token | 0.0.456858 | 0.0.5449 |
| x402 USDC | 0.0.456858 | 0.0.429274 |
| WHBAR | 0.0.1456986 | 0.0.15058 |
| Mirror Node | https://mainnet.mirrornode.hedera.com | https://testnet.mirrornode.hedera.com |
| SaucerSwap API | https://api.saucerswap.finance | https://test-api.saucerswap.finance |
Things that will bite you
The desk stays paused
The ENS read fails closed. Without ERC8004_AGENT_ID and a matching ENSIP-25 record, every cycle ends in PAUSE_ON_MANDATE, every clearance is refused and the hook is paused. That is by design. For local work only, set ENS_MANDATE_READ_MODE=fallback; ensSource then reads local-fallback.
Paid routes fail before the facilitator answers
The x402 middleware syncs with the facilitator on the first paid request and retries until it answers. If the facilitator is unreachable, paid routes fail until it is back. Turning the sync off is not a workaround: the server then has no supported payment kinds and answers 500.
Mainnet settlement needs a Blocky402 key
The mainnet facilitator may require an X-Api-Key of the form b402_ plus 64 hex characters to settle. Set BLOCKY402_API_KEY.
Swaps are blocked without SAUCERSWAP_API_KEY
The SaucerSwap plugin needs it to resolve tokens. With LIVE_EXECUTION=true and no key, each approved trade is recorded as blocked.
Nothing trades until you say so
LIVE_EXECUTION defaults to false: approved trades are recorded as dry runs ("Prepared SELL_HBAR ...") with the modelled cost, and nothing is broadcast.
Paid routes answer 503 at first
Until the agent pushes its first snapshot the gateway is in bootstrap and refuses paid calls without charging. Start the agent, or check /health for agentOnline.
The web app does not read the root .env
Next.js loads env files from apps/web, not from the repo root, so NEXT_PUBLIC_* values in the root .env do not reach the browser. Put them in apps/web/.env.local or export them before pnpm dev:web.
Your own payments do not count
The operator account and BUILDER_ACCOUNT_IDS are excluded from revenue. The pnpm quote CLI falls back to the operator account as payer, so use X402_BUYER_* or BUYER_* keys from another account to test real revenue. The CLI also defaults X402_NETWORK to testnet when it is unset.
Repository map
| Path | What is there |
|---|---|
packages/quant-kernel | Pure math: volatility, toxicity, LVR, risk kernel, forecasts, pool math, guarantee, pricing, treasury |
apps/agent | The loop (index.ts), feeds, strategist, executor, hook publisher, init:topics script |
apps/x402-gateway | Hono server (app.ts), clearances, state store, HCS publisher, Mirror Node fetchers |
apps/web | Next.js: landing page, /app dashboard, /docs |
packages/parity-plugin | Hedera Agent Kit plugin, ParityClient, buyer-agent demo |
packages/client-cli | Minimal x402 buyer CLI (pnpm quote) |
contracts/ens | ENS mandate reader, profile publishing, delegation, fleet, ERC-8004 registration |
contracts/uniswap-v4 | ParityLvrShieldHook, deploy and seed scripts, Foundry tests |
docs/ | API.md and ENS.md |