feat(ws): push each swap as it settles on NEAR Intents
`ServerMessage::IntentsSwap` carries the trade exactly as /intents/swaps returns it — one conversion, `intents::to_swap`, serves both, so the route and the push cannot describe the same trade differently. Only trades read within LIVE_WITHIN_BLOCKS (~a minute) of NEAR's final tip are pushed. The backfill never broadcasts, and the threshold is what keeps an upward catch-up after a restart from announcing an hour of history as news — the confusion the wormhole pulse shipped once already. The market panel re-reads its figures on each push rather than adjusting a copy: a VWAP over the latest N trades drops its oldest as it gains one. Verified by replaying the trade at NEAR 218,261,895 with the threshold opened locally and catching the frame on /v1/ws. Refs #27 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015pkwBsR54NRmZyU4QVhvfj
This commit is contained in:
@@ -22,6 +22,7 @@ import { useEffect, useState } from 'react'
|
||||
import type { IntentsPrice } from '../api/generated/IntentsPrice'
|
||||
import { RequestFailed, fetchIntentsPrice } from '../api/rest'
|
||||
import { ago, height as fmtHeight, price as fmtPrice, tokens } from '../lib/format'
|
||||
import { useObserverInstance } from '../lib/observer-context'
|
||||
import { Field } from './Field'
|
||||
|
||||
/** A multi-token id short enough for a label: `nep141:zec.omft.near` → `zec.omft.near`. */
|
||||
@@ -42,10 +43,17 @@ export function IntentsPanel({
|
||||
const [data, setData] = useState<IntentsPrice | null>(null)
|
||||
const [absent, setAbsent] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
// Bumped by every pushed swap. The figures are re-read rather than adjusted
|
||||
// here: a VWAP over the latest N trades loses its oldest trade as it gains a
|
||||
// new one, and arithmetic on a copy drifts from the server's answer while
|
||||
// looking authoritative — the reason the wormhole stage re-reads its stock.
|
||||
const [settled, setSettled] = useState(0)
|
||||
const observer = useObserverInstance()
|
||||
|
||||
useEffect(() => observer.onIntentsSwap(() => setSettled((n) => n + 1)), [observer])
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController()
|
||||
setData(null)
|
||||
setAbsent(false)
|
||||
setError(null)
|
||||
fetchIntentsPrice(chain, controller.signal)
|
||||
@@ -56,7 +64,10 @@ export function IntentsPanel({
|
||||
else setError(e instanceof RequestFailed ? e.message : 'Could not reach the observer.')
|
||||
})
|
||||
return () => controller.abort()
|
||||
}, [chain])
|
||||
}, [chain, settled])
|
||||
|
||||
// A different chain's figures must never stand in for this one's while it loads.
|
||||
useEffect(() => setData(null), [chain])
|
||||
|
||||
if (absent) return null
|
||||
|
||||
|
||||
Reference in New Issue
Block a user