// The app as a deployment runs it: everything is read from the environment, and nothing is required to start. // With no variable at all it serves the rate card and the live figures and refuses everything else. // // RUNPOD_API_KEY the provider account that owns the machines. Real money: see the README. // SESSION_SECRET signs sessions; at least 32 characters // TREASURY_ADDRESS where USDC top-ups are sent on Elysium // BLOB_READ_WRITE_TOKEN the Vercel Blob store that holds the state (set by Vercel when the store is linked) // CRON_SECRET protects GET /v1/tick // Rentals open only when the first four are all set. Optional: // RELAYER_KEY a HyperEVM account that only pays inbox gas; without it the relayer carries reads only // MARGIN_BPS Idle's margin over the provider's price, in basis points (default 2000 = 20 %) // SITE_DOMAIN the host named in the sign-in message (default idle-elysium.vercel.app) // API_BASE the public address of this API, for the OpenAPI document (default https:///api) // ALLOWED_ORIGINS comma-separated origins for CORS (default: any) // POD_PREFIX how Idle names its machines on the provider account (default "idle") // CLAIM_MIN_BLOCK top-ups mined before this Elysium block are refused (set it if the state is ever reset) // EQUITY_HAIRCUT_BPS taken off the price of EQNVDA when it buys credits (default 100 = 1 %) // EQUITY_MAX_GAP_BPS EQNVDA is refused while its pool and the xyz:NVDA mark differ by more (default 300 = 3 %) // STATE_BLOB_PATH, ELYSIUM_RPC, HYPEREVM_RPC, INBOX_ADDRESS, USDC_ADDRESS, CHAIN_ID import { Contract, JsonRpcProvider, Wallet, getAddress } from "ethers"; import { createApp } from "./app.mjs"; import { ELYSIUM, makeMarket } from "./market.mjs"; import { ELYSIUM_CREDITS } from "./contract.mjs"; import { makeOffers } from "./offers.mjs"; import { blobStore } from "./store.mjs"; import { createRunpodProvider } from "./provider-runpod.mjs"; const read = (env, k, d = "") => (env[k] && String(env[k]).trim()) || d; /** A JSON-RPC call to one node. Throws the node's error object ({code, message, data}). */ export function rpcClient(url, f = fetch) { return async (method, params = []) => { const r = await f(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }), signal: AbortSignal.timeout(12_000) }); const j = await r.json(); if (j.error) throw j.error; return j.result; }; } const creditsAddress = (a) => (/^0x[0-9a-fA-F]{40}$/.test(a) ? a.toLowerCase() : null); export function configFromEnv(env = process.env) { const secret = read(env, "SESSION_SECRET"); const treasury = read(env, "TREASURY_ADDRESS"); const margin = Number(read(env, "MARGIN_BPS", "2000")); const bps = (k, d, max) => { const n = Number(read(env, k, String(d))); return Number.isInteger(n) && n >= 0 && n <= max ? n : d; }; return { chainId: Number(read(env, "CHAIN_ID", String(ELYSIUM.chainId))), usdc: getAddress(read(env, "USDC_ADDRESS", ELYSIUM.usdc)).toLowerCase(), inbox: getAddress(read(env, "INBOX_ADDRESS", ELYSIUM.inbox)), // A short secret is treated as no secret: sessions signed with it could be forged. sessionSecret: secret.length >= 32 ? secret : null, treasury: /^0x[0-9a-fA-F]{40}$/.test(treasury) ? treasury.toLowerCase() : null, cronSecret: read(env, "CRON_SECRET") || null, marginBps: Number.isInteger(margin) && margin >= 0 && margin <= 20_000 ? margin : 2000, domain: read(env, "SITE_DOMAIN", "idle-elysium.vercel.app"), apiBase: read(env, "API_BASE") || null, podPrefix: read(env, "POD_PREFIX", "idle"), claimMinBlock: Number(read(env, "CLAIM_MIN_BLOCK", "0")) || 0, eqnvda: ELYSIUM.eqnvda.toLowerCase(), // The credits contract on Elysium. CREDITS_ADDRESS=none hides it; another address replaces it. credits: creditsAddress(read(env, "CREDITS_ADDRESS", ELYSIUM_CREDITS.address)), equityHaircutBps: bps("EQUITY_HAIRCUT_BPS", 100, 9_000), equityMaxGapBps: bps("EQUITY_MAX_GAP_BPS", 300, 5_000), origins: read(env, "ALLOWED_ORIGINS").split(",").map((s) => s.trim()).filter(Boolean), }; } /** The app wired to the real world: Runpod, Vercel Blob, Elysium. Each piece is there only if its variable is. */ export function appFromEnv(env = process.env) { const cfg = configFromEnv(env); const elysiumRpc = read(env, "ELYSIUM_RPC", ELYSIUM.rpc); const chain = rpcClient(elysiumRpc); const runpodKey = read(env, "RUNPOD_API_KEY"); // The gas account is only built when a key is configured, and only on the first send. // A serverless host runs one request per instance at a time, so the nonce is read fresh for each post. let inbox; const key = read(env, "RELAYER_KEY"); const post = key ? async (raw) => { inbox ??= new Contract(cfg.inbox, ["function sendL2Message(bytes messageData) returns (uint256)"], new Wallet(key, new JsonRpcProvider(read(env, "HYPEREVM_RPC", ELYSIUM.hyperEvmRpc), undefined, { cacheTimeout: -1 }))); return (await inbox.sendL2Message("0x04" + raw.slice(2))).hash; // 0x04 marks a signed transaction } : null; return createApp(cfg, { market: makeMarket({ rpc: elysiumRpc, chainId: cfg.chainId, usdc: cfg.usdc }), // The market offers behind /v1/offers and /v1/route: two public reads, no key (offers.mjs). offers: makeOffers(), provider: runpodKey ? createRunpodProvider({ apiKey: runpodKey }) : null, store: read(env, "BLOB_READ_WRITE_TOKEN") ? blobStore({ path: read(env, "STATE_BLOB_PATH", "idle-gpu/state.json") }) : null, chain, post, log: (e) => console.log(JSON.stringify({ t: new Date().toISOString(), ...e })), }); }