// Cloudflare Workers entry (wrangler deploy). Vars come from wrangler.toml [vars].
// publicBaseHandler: requests from the api.youraitools.co proxy (allowlisted X-Forwarded-Host + this Worker's
// X-Forwarded-Prefix) get an app whose public URLs are https://api.youraitools.co/<prefix>; all other requests get
// the unchanged workers.dev app. See publicbase.js.
// scheduled (cron, every 5 min): refresh the smart-money positions snapshot into the SnapStore Durable Object.
import { createApp } from './app.js';
import { DEFAULT_ENABLED } from './networks.js';
import { configFromEnv } from './config.js';
import { watchdog } from './deadline.js';
import { Stats } from './stats.js';
import { publicBaseHandler } from './publicbase.js';
import { x402TrustResponse } from './x402trust.js';
import { headAsGet } from './head.js';
import { makeStore, refresh } from './store.js';
export { StatsCounter } from './counter-do.js'; // usage counters (SQLite Durable Object, no KV writes)
export { SnapStore } from './snapshot-do.js'; // positions snapshot + cohort + 26 h per-coin history (SQLite DO)
const snapStub = (env) => env.SNAP.get(env.SNAP.idFromName('global'));
let handler;
export default {
// watchdog: a last-resort 503 (never charged) instead of a hung request; see deadline.js for the budgets.
fetch: watchdog((request, env, ctx) => {
const trust = x402TrustResponse(request, env); // x402-trust.com domain proof (public key)
if (trust) return trust;
if (!handler) {
const stats = new Stats(undefined, { counter: env.COUNTER }); // no KV at all; one counter batch shared by both app instances
const store = makeStore(() => snapStub(env));
handler = publicBaseHandler(env, (publicUrl) => createApp({ ...configFromEnv(env), enabledNetworks: env.ENABLED_NETWORKS ?? DEFAULT_ENABLED, solanaPayTo: env.SOLANA_PAY_TO || undefined, stats, store, publicUrl }));
}
return headAsGet(handler)(request, env, ctx); // HEAD = GET without body (see head.js)
}),
async scheduled(event, env, ctx) {
const r = await refresh(snapStub(env), { now: Date.now() });
if (!r.ok) console.warn(`smartperps refresh: ${JSON.stringify(r)}`);
},
};
agentgate-source: code mirror on Cloudflare Pages. All repos