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

from the repo rootbash
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 agent

Going live on mainnet

  1. Hedera account. Create a mainnet account for the agent, fund it with a little HBAR and USDC 0.0.456858 (associate the token), and set HEDERA_OPERATOR_ID and HEDERA_OPERATOR_KEY.
  2. HCS topics. Run pnpm init:topics and paste the printed ids into .env.
  3. ENS. Register the parent name, then run ens:setup, ens:register-erc8004 (put ERC8004_AGENT_ID in .env), ens:publish and ens:delegate grant. See ENS identity and mandate.
  4. SaucerSwap. Set SAUCERSWAP_API_KEY. Leave LIVE_EXECUTION=false for a dry run first.
  5. x402. The defaults point at the Blocky402 mainnet facilitator; set BLOCKY402_API_KEY if it asks for one, and set INTERNAL_SYNC_SECRET.
  6. Uniswap hook (optional). Deploy it (see Uniswap v4 LVR Shield), then set UNISWAP_V4_HOOK_ADDRESS and HOOK_ORACLE_PRIVATE_KEY.

Environment variables

Defaults are the values in code when the variable is unset, which sometimes differ from .env.example.

Hedera and HCS
VariableDefaultWhat it does
HEDERA_NETWORKmainnetThe network the agent trades and publishes on. Any value containing testnet selects testnet
HEDERA_OPERATOR_IDunsetThe 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_KEYunsetIts key: ECDSA hex (with or without 0x) or DER
HEDERA_MIRROR_URLhttps://mainnet.mirrornode.hedera.comMirror Node for the agent's balance and fee reads
SIGNAL_NETWORKmainnetWhere market signals come from. Keep mainnet even when trading on testnet
HCS_DECISION_TOPIC_IDunsetDecision, execution, mode and agent-card messages. Unset: not published
HCS_PNL_TOPIC_IDunsetLedger, clearance and refund messages
HCS_TRACK_TOPIC_IDthe decision topicPredictions and scores
Agent and execution
VariableDefaultWhat it does
LIVE_EXECUTIONfalseOnly true broadcasts swaps; otherwise every trade is a dry run
SAUCERSWAP_API_KEYunsetNeeded by saucer-swap-plugin; without it live swaps are blocked
MAX_TRADE_USD5Hard per-trade cap on top of the ENS mandate
MIN_TRADE_USD0.5Smaller rebalances are skipped as dust
HBAR_FEE_RESERVE5HBAR never sold, kept for fees
POLL_INTERVAL_MS60000Cycle length, times the survival cadence. The gateway also uses it to judge agentOnline
SHUTDOWN_FLOOR_USD1Below this treasury value the agent stops spending
PREDICTION_INTERVAL_MS900000One forecast every 15 minutes
PREDICTION_HORIZON_MS3600000Each forecast is scored after one hour
GAS_FLUSH_INTERVAL_MS600000How often the agent books its batched HCS fees
PARITY_POOL_IDsaucerswap-hbar-usdc-v2Pool id checked against the mandate's allowlist
INITIAL_RANGE_HALF_WIDTH_TICKS480Starting target range before the agent picks one
AGENT_STATE_FILE.parity/agent-state.jsonAgent memory: ledger, forecasts, executions, range
OPENAI_API_KEYunsetTurns on the LLM strategist. Unset: quant-only
OPENAI_BASE_URLhttps://api.openai.com/v1Any OpenAI-compatible endpoint
OPENAI_MODELgpt-4o-miniModel name
LLM_CALL_COST_USD0.0025Booked per call as an off-chain cost
x402 gateway
VariableDefaultWhat it does
X402_NETWORKhedera:mainnetPayment network and the preset for pool and USDC ids
BLOCKY402_FACILITATOR_URLhttps://api.blocky402.comFacilitator (testnet default https://api.testnet.blocky402.com)
BLOCKY402_API_KEYunsetSent as X-Api-Key on verify, settle and supported
X402_GATEWAY_PORT4020Listen port
X402_GATEWAY_URLhttp://localhost:4020Where the agent pushes, and the public base URL in the agent cards
X402_BASE_PRICE_USD0.01Base of the price formula
INTERNAL_SYNC_SECRETunsetShared secret for /internal/snapshot. Unset: only loopback callers can push
BUILDER_ACCOUNT_IDSunsetComma-separated accounts excluded from revenue, on top of the operator
CLEARANCE_TTL_MS120000How long a clearance can be executed and claimed
CLEARANCE_MAX_NOTIONAL_USD1000Largest trade the guarantee covers
GATEWAY_STATE_FILE.parity/gateway-state.jsonPersisted books
ENS and ERC-8004
VariableDefaultWhat it does
ENS_CHAINmainnetmainnet or sepolia
ENS_RPC_URLhttps://ethereum-rpc.publicnode.comEthereum RPC (Sepolia default https://ethereum-sepolia-rpc.publicnode.com)
ENS_SUBNAMEunsetThe agent's ENS name (ENS_NAME also works). Unset: mandate unavailable
ENS_MANDATE_READ_MODElivefallback gives an always-active local mandate, for development only
ENS_MANDATE_MAX_STALENESS_MS300000How long a cached read survives an RPC outage
ENS_REQUIRE_EAC_DELEGATIONfalsetrue also requires ENS_AGENT_ADDRESS to hold the parity.* delegation
ENS_OWNER_PRIVATE_KEYunsetOwner key for the setup and publish scripts
ENS_AGENT_PRIVATE_KEYunsetAgent key: signs its ENSIP-28 consent and --as-agent pauses
HEDERA_EVM_PRIVATE_KEYunsetECDSA key so the Hedera account can be listed under ENSIP-28
ENS_HEDERA_NETWORKhedera:mainnetHedera chain used in ENSIP-11/28/31 records
ERC8004_CHAIN_ID1Registry chain; 296 for Hedera testnet, 11155111 for Sepolia
ERC8004_REGISTRY_ADDRESSper chainOverride the IdentityRegistry address
ERC8004_AGENT_IDunsetRequired. Without it the ENSIP-25 check fails and the desk stays paused
ERC8004_AGENT_URI${X402_GATEWAY_URL}/agent.jsonRegistered agent URI
ERC8004_PRIVATE_KEYENS_OWNER_PRIVATE_KEYSigns the registration
PARITY_MAX_NOTIONAL_USD1000 (.env.example: 20)Published to parity.maxNotionalUsd
PARITY_MAX_SLIPPAGE_BPS60Published to parity.maxSlippageBps
PARITY_MAX_LTV_BPS6500Published to parity.maxLtvBps
PARITY_MIN_REBALANCE_EV_USD0.01Published to parity.minRebalanceEvUsd
PARITY_ALLOWED_POOLSsaucerswap-hbar-usdc-v2Published to parity.allowedPools
PARITY_PAYMENT_HTS_TOKEN_IDS0.0.456858Tokens in the ENSIP-31 payment preference
PARITY_WEB_URL, PARITY_MCP_URL, PARITY_A2A_URLunsetOptional ENSIP-26 endpoints, also listed in /agent.json
Uniswap v4 hook publisher
VariableDefaultWhat it does
UNISWAP_V4_HOOK_ADDRESSunsetThe deployed hook. Unset: the publisher is disabled
HOOK_ORACLE_PRIVATE_KEYunsetThe hook's guardianOracle (or owner) key
HOOK_CHAIN_ID130Unichain; also 1, 1301, 11155111
HOOK_RPC_URLviem defaultNeeded only for other chains
HOOK_MIN_SCORE_DELTA5Smallest score move worth a transaction
HOOK_HEARTBEAT_MS600000Rewrite at least this often; must beat the hook's staleness window
Buyers and the web app
VariableDefaultWhat it does
BUYER_ACCOUNT_ID, BUYER_PRIVATE_KEYunsetThe plugin's buyer demo account
PARITY_GATEWAY_URLunsetGateway the buyer demo calls (.env.example: http://localhost:4020)
X402_BUYER_ACCOUNT_ID, X402_BUYER_PRIVATE_KEYthe operator accountThe pnpm quote CLI's payer
NEXT_PUBLIC_GATEWAY_URLhttp://localhost:4020Gateway the web app reads
NEXT_PUBLIC_MIRROR_URLpublic mainnet mirrorMirror Node for browser-side HCS reads when /status does not name one
NEXT_PUBLIC_HCS_DECISION_TOPIC_ID (and _PNL_, _TRACK_)unsetTopic ids to use when /status has none
NEXT_PUBLIC_SITE_URLhttp://localhost:3000Base URL for metadata
NEXT_PUBLIC_REPO_URLunsetShows 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.

PresetMainnetTestnet
SaucerSwap V2 HBAR/USD pool0.0.39648040.0.9283328
Pool USD token0.0.4568580.0.5449
x402 USDC0.0.4568580.0.429274
WHBAR0.0.14569860.0.15058
Mirror Nodehttps://mainnet.mirrornode.hedera.comhttps://testnet.mirrornode.hedera.com
SaucerSwap APIhttps://api.saucerswap.financehttps://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

PathWhat is there
packages/quant-kernelPure math: volatility, toxicity, LVR, risk kernel, forecasts, pool math, guarantee, pricing, treasury
apps/agentThe loop (index.ts), feeds, strategist, executor, hook publisher, init:topics script
apps/x402-gatewayHono server (app.ts), clearances, state store, HCS publisher, Mirror Node fetchers
apps/webNext.js: landing page, /app dashboard, /docs
packages/parity-pluginHedera Agent Kit plugin, ParityClient, buyer-agent demo
packages/client-cliMinimal x402 buyer CLI (pnpm quote)
contracts/ensENS mandate reader, profile publishing, delegation, fleet, ERC-8004 registration
contracts/uniswap-v4ParityLvrShieldHook, deploy and seed scripts, Foundry tests
docs/API.md and ENS.md