Skip to content

feat(studio): add Velocity scenario support - #37

Open
92Infinitus92 wants to merge 5 commits into
feat/scenarios/override/velocity-supportfrom
feat/velocity-support
Open

92Infinitus92 wants to merge 5 commits into
feat/scenarios/override/velocity-supportfrom
feat/velocity-support

Conversation

@92Infinitus92

Copy link
Copy Markdown
Collaborator

Adds Velocity to the scenario editor and the Market move preset.

  • The preset lists Velocity's perp markets, takes a typed market address too, and moves that market's oracle price, posted at the current slot with its confidence scaled with the price.
  • The Market move dialog and its adapter interface are shared code; the Manifest and Jupiter Perps PRs carry the same files.
  • Adds the Velocity icon, the protocol entry and one AI chip.

Tests: vitest, tsc (only the two existing vitest config errors), lint and prettier pass.

Backend: LimeChain/surfpool#50

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 conf value 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.yml has 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_slot and publish_time are absolute values read when the scenario is created, and fetchBeforeUse: false.
    • If someone plays the scenario later, the move is measured from an old price, not "−X% from now".
    • posted_slot and publish_time will 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*.ts and 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 readMarket is pending for a pasted address, selectedMarket is undefined. The combobox goes blank and shows no loading indicator until the read finishes.
  • Possible blank labels: toMarket shows an empty label for a market whose name bytes are empty or all NUL. Fall back to truncateAddress(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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • conf has no bounds check. price is clamped through moveValue, but conf is 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 fetchBeforeUse is false, playing the scenario later writes a creation-time posted_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 passes ANTHROPIC_API_KEY to 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 error is reset. If that's intended, fine; otherwise reset them in handleClose.

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants