Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(market-data): stop a remembered payload being timestamped as if it were just fetched

nirholas452cb38b6443
diff --git a/api/_lib/market-data/datapoints.js b/api/_lib/market-data/datapoints.js
index 5f2589404..91a030eaa 100644
--- a/api/_lib/market-data/datapoints.js
+++ b/api/_lib/market-data/datapoints.js
@@ -66,8 +66,11 @@ const _full = new Map(); // key → { value, expiresAt }
async function fullSet(key, url, slim) {
const now = Date.now();
const hit = _full.get(key);
- if (hit && hit.expiresAt > now) return hit.value;
- const { value } = await lastGood(
+ if (hit && hit.expiresAt > now) {
+ noteFetchedAt(hit.fetchedAt);
+ return hit.value;
+ }
+ const { value, stale, ageMs } = await lastGood(
`market-datapoints:${key}`,
async () => {
const raw = await fetchUpstreamJson(
@@ -79,10 +82,31 @@ async function fullSet(key, url, slim) {
},
{ maxAgeMs: 60 * 60_000 },
);
- _full.set(key, { value, expiresAt: Date.now() + TTL_MS });
+ // When the last-good tier answered, the payload is as old as `ageMs`, and a
+ // buyer paying for this datapoint has to be told that rather than handed a
+ // timestamp claiming it was fetched just now.
+ const fetchedAt = stale ? Date.now() - ageMs : Date.now();
+ _full.set(key, { value, expiresAt: Date.now() + TTL_MS, fetchedAt });
+ noteFetchedAt(fetchedAt);
return value;
}
+// The freshness of the upstream payload the CURRENT request read. A datapoint
+// is assembled from one family read, so recording it per request (rather than
+// threading a timestamp through every extractor) keeps `as_of` honest without
+// reshaping the family API. Reset by readDatapoint before the read it describes.
+let _readFetchedAt = null;
+
+/** Test seam: stand in for a family read that came from the last-good tier. */
+export function _noteFetchedAtForTest(at) {
+ noteFetchedAt(at);
+}
+
+function noteFetchedAt(at) {
+ if (at == null) return;
+ _readFetchedAt = _readFetchedAt == null ? at : Math.min(_readFetchedAt, at);
+}
+
// slug → slim protocol row, over the FULL ~6k-protocol DeFiLlama feed.
// Exported (with the other full-set accessors) for the free enumerator at
// /api/x402/d, which pages through the same cached id space.
@@ -647,8 +671,15 @@ export function parseDatapointPath(segments) {
// Fetch + extract one datapoint. The paid handler's whole job.
export async function readDatapoint({ family, familyDef, id, metric, metricDef }) {
+ _readFetchedAt = null;
const row = await familyDef.row(id);
const value = metricDef.extract(row);
+ // `as_of` is when the DATA was read from upstream, not when this response was
+ // assembled. Those were the same thing before the last-good tier existed;
+ // now they are not, and stamping the response time over an hour-old payload
+ // would sell a stale number as a live one.
+ const fetchedAt = _readFetchedAt ?? Date.now();
+ const staleMs = Date.now() - fetchedAt;
return {
family,
...(id != null ? { id } : {}),
@@ -656,7 +687,8 @@ export async function readDatapoint({ family, familyDef, id, metric, metricDef }
label: metricDef.label,
unit: metricDef.unit,
value: value === undefined ? null : value,
- as_of: new Date().toISOString(),
+ as_of: new Date(fetchedAt).toISOString(),
+ ...(staleMs > TTL_MS ? { stale: true, age_seconds: Math.round(staleMs / 1000) } : {}),
source: 'three.ws market-data',
};
}
diff --git a/api/pump/dashboard.js b/api/pump/dashboard.js
index 44685795e..674e73d6d 100644
--- a/api/pump/dashboard.js
+++ b/api/pump/dashboard.js
@@ -98,7 +98,11 @@ export default wrap(async (req, res) => {
? null
: {
value: market.price_usd,
- updateUnixTime: Math.floor(Date.now() / 1000),
+ // No `updateUnixTime` here. Birdeye's field means "when this price
+ // was observed", and the market chain can answer from a cached
+ // reading up to half an hour old, so stamping the response time
+ // would assert a freshness we cannot vouch for. `source` says which
+ // provider answered, which is a claim we can actually stand behind.
priceChange24h: market.price_change_24h,
// The dashboard has always rendered a market cap slot; Birdeye's
// price endpoint does not carry one, so it read "N/A" for every
diff --git a/tests/market-datapoints.test.js b/tests/market-datapoints.test.js
index b87e59d5e..6ba5a54e0 100644
--- a/tests/market-datapoints.test.js
+++ b/tests/market-datapoints.test.js
@@ -21,6 +21,8 @@ const {
datapointDescription,
parseDatapointPath,
datapointEndpointCount,
+ readDatapoint,
+ _noteFetchedAtForTest,
} = await import('../api/_lib/market-data/datapoints.js');
function expectThrow(fn, { status, code }) {
@@ -311,3 +313,37 @@ describe('dynamic datapoint route', () => {
expect(String(res.headers['cache-control'])).toContain('no-store');
});
});
+
+// A paid datapoint's `as_of` is when the DATA was read upstream, not when the
+// response was assembled. Those were the same thing until the last-good tier
+// landed under it; stamping the response time over an hour-old payload would
+// sell a stale number to a buyer as a live one.
+describe('readDatapoint freshness', () => {
+ it('reports as_of from the upstream read and flags a stale payload', async () => {
+ const fetchedAt = Date.now() - 45 * 60_000;
+ const familyDef = {
+ row: async () => {
+ _noteFetchedAtForTest(fetchedAt);
+ return { tvl: 123 };
+ },
+ metrics: {},
+ };
+ const metricDef = { label: 'TVL', unit: 'usd', extract: (r) => r.tvl };
+
+ const out = await readDatapoint({ family: 'protocol', familyDef, id: 'x', metric: 'tvl', metricDef });
+ expect(out.value).toBe(123);
+ expect(Date.parse(out.as_of)).toBeCloseTo(fetchedAt, -4);
+ expect(out.stale).toBe(true);
+ expect(out.age_seconds).toBeGreaterThan(600);
+ });
+
+ it('does not flag a fresh read, and stamps it now', async () => {
+ const familyDef = { row: async () => ({ tvl: 7 }), metrics: {} };
+ const metricDef = { label: 'TVL', unit: 'usd', extract: (r) => r.tvl };
+
+ const out = await readDatapoint({ family: 'protocol', familyDef, id: 'x', metric: 'tvl', metricDef });
+ expect(out.stale).toBeUndefined();
+ expect(out.age_seconds).toBeUndefined();
+ expect(Date.now() - Date.parse(out.as_of)).toBeLessThan(5_000);
+ });
+});