-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy paththree-intel.js
More file actions
179 lines (167 loc) · 6.73 KB
/
Copy paththree-intel.js
File metadata and controls
179 lines (167 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// GET /api/x402/three-intel
//
// $THREE Town Oracle — $0.01 USDC per call on Solana or Base.
//
// The paid intel kiosk in the $THREE town (/play) buys from this endpoint:
// live $THREE market intel — price, 24 h change, market cap, liquidity,
// volume, and a bullish/bearish/neutral signal with a short rationale.
// Agents can call it directly too (it's cataloged in the bazaar), so the
// same oracle the town kiosk sells from is buyable by any x402 client.
//
// Response: { mint, symbol, price_usd, change_24h, market_cap_usd,
// liquidity_usd, volume_24h_usd, signal, headline, rationale,
// confidence, ts }
//
// Data is live: DexScreener public API (no key required). No mock path.
import { paidEndpoint } from '../_lib/x402-paid-endpoint.js';
import { buildBazaarSchema } from '../_lib/x402-spec.js';
import { installAccessControl } from '../_lib/x402/access-control.js';
import { withService } from '../_lib/x402/bazaar-helpers.js';
import { priceFor } from '../_lib/x402-prices.js';
import { env } from '../_lib/env.js';
const ROUTE = '/api/x402/three-intel';
const DESCRIPTION =
'$THREE Town Oracle — pay $0.01 USDC per call for live $THREE market intel: ' +
'price, 24 h change, market cap, liquidity, 24 h volume, and a ' +
'bullish / bearish / neutral signal with a two-sentence rationale. ' +
'Powered by live DexScreener data. This is the oracle behind the paid ' +
'intel kiosk in the $THREE town on three.ws/play.';
const INPUT_SCHEMA = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: {},
};
const OUTPUT_SCHEMA = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
required: ['mint', 'symbol', 'signal', 'headline', 'rationale', 'confidence', 'ts'],
properties: {
mint: { type: 'string' },
symbol: { type: 'string' },
price_usd: { type: ['number', 'null'] },
change_24h: { type: ['number', 'null'] },
market_cap_usd: { type: ['number', 'null'] },
liquidity_usd: { type: ['number', 'null'] },
volume_24h_usd: { type: ['number', 'null'] },
signal: { type: 'string', enum: ['bullish', 'bearish', 'neutral'] },
headline: { type: 'string' },
rationale: { type: 'string' },
confidence: { type: 'number', minimum: 0, maximum: 1 },
ts: { type: 'string', format: 'date-time' },
},
};
export const BAZAAR = {
description: DESCRIPTION,
useCases: ['$THREE market signal', 'in-world paid oracle', 'token intel'],
input: { type: 'query', example: {}, schema: INPUT_SCHEMA },
output: {
type: 'json',
example: {
mint: 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump',
symbol: 'THREE',
price_usd: 0.003685, change_24h: 12.4, market_cap_usd: 3685000,
liquidity_usd: 412000, volume_24h_usd: 1268079,
signal: 'bullish',
headline: 'THREE climbs +12.40% — moderate upside',
rationale: 'THREE gained +12.40% over 24 h with price at $0.003685. Volume is healthy against liquidity; momentum favors the upside.',
confidence: 0.86, ts: '2026-06-12T10:00:00Z',
},
},
schema: buildBazaarSchema({
method: 'GET',
queryParamsSchema: INPUT_SCHEMA,
outputSchema: OUTPUT_SCHEMA,
}),
};
export async function fetchThreeMarket(mint) {
const r = await fetch(`https://api.dexscreener.com/latest/dex/tokens/${mint}`, {
headers: { Accept: 'application/json' },
signal: AbortSignal.timeout(6000),
});
if (!r.ok) return null;
const data = await r.json();
const pairs = (data.pairs || []).filter((p) => p.chainId === 'solana');
if (!pairs.length) return null;
pairs.sort((a, b) => (b.liquidity?.usd ?? 0) - (a.liquidity?.usd ?? 0));
const p = pairs[0];
return {
price_usd: parseFloat(p.priceUsd) || null,
change_24h: p.priceChange?.h24 ?? null,
market_cap_usd: p.marketCap ?? p.fdv ?? null,
liquidity_usd: p.liquidity?.usd ?? null,
volume_24h_usd: p.volume?.h24 ?? null,
};
}
export function buildSignal({ price_usd, change_24h, volume_24h_usd, liquidity_usd }) {
const fmt = (n) => (n >= 100 ? n.toFixed(2) : n >= 1 ? n.toFixed(3) : n.toFixed(6));
const pStr = price_usd != null ? `$${fmt(price_usd)}` : '?';
const sign = change_24h >= 0 ? '+' : '';
const cStr = `${sign}${change_24h.toFixed(2)}%`;
// Volume/liquidity turnover colors the rationale: high turnover = conviction.
const turnover =
volume_24h_usd != null && liquidity_usd ? volume_24h_usd / liquidity_usd : null;
const flowLine =
turnover == null ? 'Flow data is thin; weigh the move accordingly.'
: turnover > 3 ? 'Volume is running hot against liquidity — high conviction behind the move.'
: turnover > 1 ? 'Volume is healthy against liquidity; participation is real.'
: 'Volume is light against liquidity; the move has limited backing so far.';
let signal, headline;
if (change_24h > 5) {
signal = 'bullish';
headline = `THREE surges ${cStr} in 24 h — strong momentum`;
} else if (change_24h > 1) {
signal = 'bullish';
headline = `THREE climbs ${cStr} — moderate upside`;
} else if (change_24h < -5) {
signal = 'bearish';
headline = `THREE drops ${Math.abs(change_24h).toFixed(2)}% — sellers in control`;
} else if (change_24h < -1) {
signal = 'bearish';
headline = `THREE slips ${cStr} — mild weakness`;
} else {
signal = 'neutral';
headline = `THREE flat at ${cStr} — consolidating at ${pStr}`;
}
const rationale = `THREE is ${change_24h >= 0 ? 'up' : 'down'} ${cStr} over 24 h, trading at ${pStr}. ${flowLine}`;
const confidence = Math.min(0.93, 0.64 + Math.min(Math.abs(change_24h) / 20, 0.29));
return { signal, headline, rationale, confidence };
}
export default paidEndpoint({
route: ROUTE,
method: 'GET',
priceAtomics: priceFor('three-intel', '10000'), // $0.01 USDC
networks: ['solana', 'base'],
description: DESCRIPTION,
bazaar: BAZAAR,
service: withService({
serviceName: '$THREE Town Oracle',
tags: ['three', 'market', 'signal', 'play', 'solana'],
}),
accessControl: installAccessControl({ requiredScope: 'x402:bypass' }),
async handler() {
const mint = env.THREE_TOKEN_MINT;
let live = null;
try { live = await fetchThreeMarket(mint); } catch { /* upstream hiccup */ }
if (!live || live.change_24h == null) {
// Live data is unavailable (DexScreener hiccup/rate-limit). This is a
// paid endpoint — never charge for a fabricated signal. Throw so the
// paidEndpoint wrapper returns a 503 BEFORE settlement runs; the buyer
// is not charged and can retry once live data is back.
throw Object.assign(new Error('live THREE market data is temporarily unavailable'), {
status: 503,
code: 'data_unavailable',
});
}
const { signal, headline, rationale, confidence } = buildSignal(live);
return {
mint,
symbol: 'THREE',
...live,
signal,
headline,
rationale,
confidence,
ts: new Date().toISOString(),
};
},
});