Repository navigation
feat(studio): add Velocity scenario support - #37
92Infinitus92 wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Adds a shared Market move dialog and adapter interface, plus a Velocity adapter that lists perp markets with getProgramAccounts, reads the PythLazer oracle together with the Clock sysvar, and writes a moved price, conf, posted_slot and publish_time. It also adds the Velocity icon, the protocol entry and an AI chip.
Overall the code is clean and well tested. Price math stays in bigint, stale async results are guarded and errors are surfaced to the user. I found no blocking bugs, only small points:
- The
confvalue is never range-checked and uses a different rounding rule from the price (see the inline comment). - The template id is written out twice in the Velocity adapter.
- When the protocol changes, the previous adapter's market list is shown for one render before it reloads.
- The PR also carries an unrelated commit,
ci: claude code review, which adds.github/workflows/claude-review.yml. Confirm that belongs in this PR; it would fit better in its own PR. - The preset card says "Move an order book, perps or oracle price", but only Velocity, a perps oracle, ships in this PR. That's fine if the Manifest and Jupiter Perps PRs land too, but the copy overpromises if this one merges alone.
| const { account, slot, unixTimestamp } = await readWithClock(rpcUrl, market.account); | ||
| const oracle = dataView(expectAccount(account, market.account, ORACLE)); | ||
| const price = moveValue(oracle.getBigInt64(8, true), move, I64_MAX); | ||
| const conf = (oracle.getBigUint64(40, true) * move.numerator) / move.denominator; |
There was a problem hiding this comment.
conf doesn't go through moveValue, so it's never range-checked. A large upward move can push it past u64 and the override would carry a value that can't be stored. It is also rounded down here, while price is rounded half up. Consider conf === 0n ? 0n : moveValue(conf, move), or add an explicit u64 bound check.
| const conf = (oracle.getBigUint64(40, true) * move.numerator) / move.denominator; | ||
| return [ | ||
| { | ||
| templateId: 'velocity-oracle-price', |
There was a problem hiding this comment.
Nit: the template id literal is repeated from line 44. Pulling it into a TEMPLATE_ID constant would stop the two copies from drifting apart.
| price: toScenarioNumber(price.toString()), | ||
| conf: toScenarioNumber(conf.toString()), | ||
| posted_slot: toScenarioNumber(slot.toString()), | ||
| publish_time: toScenarioNumber((unixTimestamp * BigInt(1000000)).toString()), |
There was a problem hiding this comment.
Nit: publish_time is microseconds here (Pyth Lazer convention), but the Clock only has second resolution, so the value always ends in 000000. A short comment noting the unit would help readers who expect seconds.
| useEffect(() => { | ||
| if (!open || !adapter || templates === null) return; | ||
| let cancelled = false; | ||
| setMarkets(null); |
There was a problem hiding this comment.
setMarkets(null) only runs after the render that switches the adapter. For that one render, markets still holds the previous protocol's list and selectedMarket falls back to markets?.[0]. That market is from the wrong protocol. Clear markets in handleProtocolSelect too, or key the list by protocol, so a stale market is never selectable.
There was a problem hiding this comment.
Summary
This PR adds Velocity to the scenario editor and adds a shared Market move preset. The preset includes a dialog, an adapter interface, RPC and byte-decoding helpers in market-move.ts, and a Velocity adapter that rewrites a PythLazer oracle's price, conf, posted_slot and publish_time. The changes are clean and well tested: request IDs guard against races, the BigInt math gives exact rounding, i64 values pass through toScenarioNumber without losing precision, and the account owner, discriminator and length checks are sensible.
Issues and suggestions
- Scope:
.github/workflows/claude-review.ymlhas nothing to do with Velocity. Please move it to its own PR so it can be reviewed and reverted separately. - The scenario is a snapshot taken at creation time:
price,posted_slotandpublish_timeare absolute values read when the scenario is created, andfetchBeforeUse: false.- If someone plays the scenario later, the move is measured from an old price, not "−X% from now".
posted_slotandpublish_timewill be old too, so Velocity's staleness checks may reject the oracle.- Either document this in the dialog or scenario description, or have the backend set the slot and time when the scenario plays.
- Shared code across three PRs: the description says the Manifest and Jupiter Perps PRs carry the same
market-move*.tsand dialog files. Expect merge conflicts or drift between the copies. Consider landing the shared code once, by itself, and rebasing the adapter PRs on top of it. - No loading state for a typed address: while
readMarketis pending for a pasted address,selectedMarketisundefined. The combobox goes blank and shows no loading indicator until the read finishes. - Possible blank labels:
toMarketshows an empty label for a market whose name bytes are empty or all NUL. Fall back totruncateAddress(address).
| const { account, slot, unixTimestamp } = await readWithClock(rpcUrl, market.account); | ||
| const oracle = dataView(expectAccount(account, market.account, ORACLE)); | ||
| const price = moveValue(oracle.getBigInt64(8, true), move, I64_MAX); | ||
| const conf = (oracle.getBigUint64(40, true) * move.numerator) / move.denominator; |
There was a problem hiding this comment.
conf is scaled without any range check, unlike price, which goes through moveValue(..., I64_MAX). A large upward move can push conf past u64 max (or i64, depending on how the template serializes it), and the backend would then get a value it can't write. It also truncates where price rounds half up. Consider moveValue(conf, move), or at least clamping it. Note that moveValue throws on 0, so a zero conf needs special handling.
| values: { | ||
| price: toScenarioNumber(price.toString()), | ||
| conf: toScenarioNumber(conf.toString()), | ||
| posted_slot: toScenarioNumber(slot.toString()), |
There was a problem hiding this comment.
posted_slot and publish_time are fixed when the scenario is created. With fetchBeforeUse: false, playing the scenario later writes an oracle that is many slots old, which Velocity's staleness check may reject. Unless the backend re-stamps these values at play time, consider computing them at play time or telling the user in the dialog or description.
|
|
||
| const toMarket = (address: string, data: Uint8Array): MarketMoveMarket => ({ | ||
| label: new TextDecoder().decode(data.subarray(424, 456)).replace(/[\s\0]+$/, ''), | ||
| value: address, |
There was a problem hiding this comment.
If the 32-byte name is empty or all NUL, the label is '' and the option is blank in the picker. Fall back to truncateAddress(address) when the trimmed name is empty.
| if (!option || !adapter) return; | ||
| const address = option.value; | ||
| const requestId = ++marketRequestRef.current; | ||
| setMarket(address); |
There was a problem hiding this comment.
For a typed address, market is set right away, but selectedMarket stays undefined until readMarket resolves. The combobox clears and shows no 'Reading market…' feedback in the meantime. Consider tracking a pending flag and showing it in the placeholder or helper text.
There was a problem hiding this comment.
Overview
This PR adds a shared Market move preset: a dialog, an adapter interface, and RPC and byte-parsing helpers in market-move.ts. Velocity is the first adapter. It lists the program's PerpMarket accounts, accepts a typed market address, and writes an override for the market's Pyth Lazer oracle that changes price, conf, posted_slot and publish_time.
The code is clean and well tested. The parsing is exact: bigint fractions and lossless numbers. The dialog guards against stale requests with a request-id ref and cancelled flags.
Main points
confhas no bounds check.priceis clamped throughmoveValue, butconfis not, so a large upward move can produce a value that doesn't fit in a u64. See the inline comment.- The oracle timestamps are fixed when the scenario is created. Because
fetchBeforeUseis false, playing the scenario later writes a creation-timeposted_slot/publish_time. Velocity's staleness check may then reject the oracle. See the inline comment. - An unrelated CI change is bundled in.
.github/workflows/claude-review.yml(commit 5974991) has nothing to do with Velocity support and isn't mentioned in the PR description. Consider moving it to its own PR so it can be reviewed separately. It needs its own review because it passesANTHROPIC_API_KEYto PR runs and interpolates the PR title and body into the prompt. - Dialog state survives close and reopen. On reopen, the previous protocol, market, custom market and move are kept. Only
erroris reset. If that's intended, fine; otherwise reset them inhandleClose.
Overall the PR is in good shape; the conf bound check is the main fix needed.
| const { account, slot, unixTimestamp } = await readWithClock(rpcUrl, market.account); | ||
| const oracle = dataView(expectAccount(account, market.account, ORACLE)); | ||
| const price = moveValue(oracle.getBigInt64(8, true), move, I64_MAX); | ||
| const conf = (oracle.getBigUint64(40, true) * move.numerator) / move.denominator; |
There was a problem hiding this comment.
conf is scaled without a bounds check, while price goes through moveValue(..., I64_MAX). With a large upward move such as +100000%, the result can exceed u64::MAX. The override then fails late in the backend, or the value gets truncated. The result is also rounded down here, but price rounds half up.
Consider a shared helper that rounds the same way and checks against U64_MAX, but still allows 0, because a zero conf is legitimate and moveValue would throw on it.
| values: { | ||
| price: toScenarioNumber(price.toString()), | ||
| conf: toScenarioNumber(conf.toString()), | ||
| posted_slot: toScenarioNumber(slot.toString()), |
There was a problem hiding this comment.
posted_slot and publish_time are taken from the Clock at the moment the scenario is created. With fetchBeforeUse: false and scenarioRelativeSlot: 0, playing the scenario later (for example, from the editor a few minutes on) writes an oracle that is already old. Velocity's staleness check may then reject it.
If the template or backend can fill these in at play time (for example, a "current slot" sentinel), that would be more robust. If not, it's worth saying in the dialog copy that the scenario should be played right away.
|
|
||
| const toMarket = (address: string, data: Uint8Array): MarketMoveMarket => ({ | ||
| label: new TextDecoder().decode(data.subarray(424, 456)).replace(/[\s\0]+$/, ''), | ||
| value: address, |
There was a problem hiding this comment.
A market whose name bytes are empty or all NUL decodes to an empty label. That gives a blank option in the picker and an override labelled Velocity oracle price …. Consider falling back to truncateAddress(address) when the decoded name is empty.
Adds Velocity to the scenario editor and the Market move preset.
Tests: vitest, tsc (only the two existing vitest config errors), lint and prettier pass.
Backend: LimeChain/surfpool#50