Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6743c54
feat(scenarios): add the Pyth Pro receiver price feed template
92Infinitus92 Oct 16, 2026
69b0a9e
ci: claude code review
failfmi Oct 7, 2026
3dfacb1
fix(scenarios): name the Pyth Pro collection in its load errors
92Infinitus92 Oct 19, 2026
5891b4e
refactor(scenarios): name the receiver template Pyth Core Compatible …
92Infinitus92 Oct 29, 2026
ac0be67
Merge feat/scenarios/override/pyth-pro-support into feat/scenarios/py…
92Infinitus92 Oct 29, 2026
691c529
fix(scenarios): scale the confidence with the price in the Pyth Core …
92Infinitus92 Oct 29, 2026
c9b77fc
Merge feat/scenarios/override/pyth-pro-support into feat/scenarios/py…
92Infinitus92 Nov 2, 2026
0df9576
docs(scenarios): call rec5 the Pyth Core receiver
92Infinitus92 Nov 3, 2026
78e23a4
Merge upstream/main into feat/scenarios/pyth-pro-receiver
92Infinitus92 Nov 6, 2026
8886ae1
feat(scenarios): keep Pyth price feeds published with keep-alive
92Infinitus92 Nov 9, 2026
3ef1659
feat(scenarios): let an override opt out of keep-alive
92Infinitus92 Nov 9, 2026
e15c9a0
ci: add Claude pull request reviews
92Infinitus92 Nov 9, 2026
bafeba3
feat(scenarios): keep every local account of a kind alive, and stop o…
92Infinitus92 Nov 9, 2026
49d1aec
Merge feat/scenarios/keep-alive-toggle into wip/pyth-core-compatible
92Infinitus92 Nov 9, 2026
ebea6b0
feat(scenarios): keep every loaded Pyth price of a receiver published…
92Infinitus92 Nov 9, 2026
660d37b
fix(scenarios): restart every keep-alive binding of an account when k…
92Infinitus92 Nov 10, 2026
7c1dbf7
docs(scenarios): say in each price template that it keeps every loade…
92Infinitus92 Nov 10, 2026
26d3eb0
Merge feat/scenarios/keep-alive-toggle into wip/pyth-core-compatible
92Infinitus92 Nov 10, 2026
8706557
docs(scenarios): tell the Pyth Core template's reader which protocols…
92Infinitus92 Nov 10, 2026
7cd2a68
docs(scenarios): explain how to keep one Pyth price stale while the o…
92Infinitus92 Nov 10, 2026
5fcded7
docs(scenarios): say that an override with keepAlive false starts no …
92Infinitus92 Nov 10, 2026
8a2d644
docs(scenarios): a Pyth publish_time or posted_slot value holds only …
92Infinitus92 Nov 10, 2026
6553998
Merge upstream/main into feat/scenarios/keep-alive-toggle
92Infinitus92 Nov 20, 2026
695043f
feat(scenarios): let an override pass its own keep-alive imports
92Infinitus92 Nov 20, 2026
57d4aff
refactor(scenarios): keep upstream keep-alive and let an override tur…
92Infinitus92 Nov 20, 2026
1e755e9
docs(scenarios): one README for the Pyth receivers
92Infinitus92 Nov 20, 2026
15d18b0
Merge feat/scenarios/keep-alive-toggle into feat/scenarios/pyth-pro-r…
92Infinitus92 Nov 20, 2026
baf6c73
refactor(scenarios): keep each Pyth feed fresh through its own override
92Infinitus92 Nov 20, 2026
1f07b7b
test(scenarios): move the Pyth tests to tests/protocols
92Infinitus92 Nov 20, 2026
a9edc73
ci: remove the Claude pull request review
92Infinitus92 Dec 4, 2026
6f9fc65
Merge remote-tracking branch 'upstream/main' into wip/pyth-core-compa…
92Infinitus92 Dec 4, 2026
fe43d3b
fix(scenarios): tag the Pyth Core Compatible template clock-sensitive…
92Infinitus92 Dec 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/core/src/scenarios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For Surfpool to understand how to serialize and deserialize account data, it nee
Protocols that are natively supported by Surfpool will have their IDLs included by default.

**Currently supported protocols:**
- **Pyth v2** - Price oracle with 4 price feed templates (SOL/USD, BTC/USD, ETH/BTC, ETH/USD)
- **Pyth v2** - Price oracle with a price feed template for the Pyth Core receiver (`pyth-price-feed-v2`) and one for the Pyth Core Compatible receiver (`pyth-core-compatible-price-feed-v2`); both keep the price they write published. See [protocols/pyth/README.md](./protocols/pyth/README.md)
- **Chainlink** - Data feeds of the Store program, with a template for a feed's latest round. See [protocols/chainlink/README.md](./protocols/chainlink/README.md)
- **Jupiter v6** - DEX aggregator with TokenLedger manipulation template
- **Kamino** – Lending (v1.27.0), Scope oracle, Farms, Swap/LIMO, Earn vaults and Liquidity, across six programs. See [protocols/kamino/README.md](./protocols/kamino/README.md)
Expand Down
227 changes: 227 additions & 0 deletions crates/core/src/scenarios/protocols/pyth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
# Pyth

Pyth runs two price receivers on Solana, both deployments of the `pyth_solana_receiver` program, as
listed on [Pyth's Solana contract addresses](https://docs.pyth.network/price-feeds/contract-addresses/solana).
Surfpool bundles an IDL, an override template and a keep-alive for each:

| Receiver | Program | Folder | Template |
|---|---|---|---|
| Pyth Core | `rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ` | `v2/` | `pyth-price-feed-v2` |
| Pyth Core Compatible | `rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp` | `core-compatible/v2/` | `pyth-core-compatible-price-feed-v2` |

They hold separate price accounts for the same feeds, and a consumer reads the accounts of one of
them. Take the price account from the consumer's own account that names it.

For how scenarios work in general see the [scenarios README](../../README.md).

# Template index

**Pyth Core** · `rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ` · `v2/`

| Template | Overrides |
|---|---|
| `pyth-price-feed-v2` | the price account of a feed picked from a list |

**Pyth Core Compatible** · `rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp` · `core-compatible/v2/`

| Template | Overrides |
|---|---|
| `pyth-core-compatible-price-feed-v2` | a price account of this receiver, given by address |

Both templates write the same fields of a `PriceUpdateV2` account: `price_message.price`,
`price_message.conf`, `price_message.exponent`, `price_message.publish_time`,
`price_message.prev_publish_time`, `price_message.ema_price`, `price_message.ema_conf` and
`posted_slot`. `pyth-price-feed-v2` also takes `feed_id`, which picks the account and is not
written. Both templates import a [keep-alive](#keep-alive).

## Picking a price account

Set `fetchBeforeUse: true` to pull the price account from upstream first.

### Pyth Core

Pick the feed in the template's **Price Feed** list, the `price_feed` constant in
`v2/overrides.yaml`. Its value is the feed id, and the template derives the price account from it:
a PDA of `pythWSnswVUd12oZpeFP8e9CVaEqJg25g1Vtc2biRsT` with the seeds shard `0` as a little-endian
`u16` and the 32-byte feed id. For SOL/USD, feed id
`0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d`, that is
`7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE`, an account owned by the Pyth Core receiver.

For a feed the list does not have, name the price account in the override's account as
`{"pubkey": "<price account>"}`.

### Pyth Core Compatible

This template has no feed list. It writes the account the override names:

1. Take the price account from the consumer's account that names it.
2. Check that its owner is `rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp`. For an account owned by
the Pyth Core receiver, use `pyth-price-feed-v2`.
3. Put it in the override's account as `{"pubkey": "<price account>"}`.

The SOL/USD price account of this receiver is `7AviUf9nL62mcxNbQGKm4nKDQnPjswo6c5MX4D57HmyE`.

## Number formats

Every price field is a scaled integer. The scale is the account's own `price_message.exponent`,
normally `-8` and `-10` on some feeds.

| You'll see | It means | Example |
|---|---|---|
| `price_message.price` | the USD price x 10^-exponent | $145.00 at exponent `-8` → `14500000000` |
| `price_message.conf` | the uncertainty band around the price, in the same scale | $2 at exponent `-8` → `200000000` |
| `price_message.ema_price`, `price_message.ema_conf` | moving averages of price and conf, in the same scale | |
| `price_message.exponent` | the decimal exponent; it rescales every price field | `-8` |
| `price_message.publish_time`, `price_message.prev_publish_time` | Unix seconds | |
| `posted_slot` | the slot the update was posted at | |

More examples at exponent `-8`: $97,000 BTC → `9700000000000`, $3,500 ETH → `350000000000`. At
exponent `-10` (BONK, PEPE and SHIB in the Pyth Core list): $0.00002 BONK → `200000`, $0.000012
PEPE → `120000`.

Leave `price_message.exponent` alone unless the scenario is about an exponent error. If you change it,
restate every scaled field to match.

# Keep-alive

Nobody publishes Pyth prices on a surfnet, so both templates import a
[keep-alive](../../README.md#keep-alive) for the price account an override writes: from the slot
the override plays until the network is reset, Surfpool writes the Clock's unix seconds to
`price_message.publish_time` and the slot to `posted_slot` every slot, time travels included. The
price, confidence and EMA fields keep their values. A price account no override writes is not
republished and ages against the Surfnet clock.

Each `overrides.yaml` imports, at its top level, the publication template of its folder's
`keep-alive.yaml`:

| Template | Keep-alive template |
|---|---|
| `pyth-price-feed-v2` | `pyth-price-publication` |
| `pyth-core-compatible-price-feed-v2` | `pyth-core-compatible-price-publication` |

```yaml
keep_alive:
- template: pyth-price-publication
address: { type: override }
values: { price_message.publish_time: $unix_s, posted_slot: $slot }
```

The keep-alive templates are never offered in Studio or MCP. Because keep-alive rewrites
`price_message.publish_time` and `posted_slot` every slot, a value an override sets for them holds
only in an override with `"keepAlive": []`.

# Recipes

## Drop a price

"SOL drops to $20" on an account with exponent `-8`, live price `p` and live EMA price `q`:

```
price_message.price: 2000000000
price_message.ema_price: 2000000000
price_message.conf: <live conf x 2000000000 / p>
price_message.ema_conf: <live ema_conf x 2000000000 / q>
```

Move `ema_price` with `price`: consumers use the EMA for some checks and the spot price for others.
Scale `conf` and `ema_conf` by the same factor as the price: consumers refuse a price whose
confidence is too wide a share of it, so a large drop that keeps the live `conf` can be refused.

## Keep every feed a transaction reads fresh

A transaction can read more feeds than the scenario moves: a liquidation reads every feed of the
borrower. Add one override of the receiver's template per such feed, in the same scenario and slot.
On a feed whose price must not move, set only `price_message.publish_time`. Any value works,
because keep-alive rewrites it in the same slot. For `pyth-price-feed-v2`, `feed_id` alone writes
nothing.

## Let a feed go stale

- A feed left without an override ages against the Surfnet clock from the start.
- To let a feed age later, give a later override of it `"keepAlive": []` and a value, such as its
current price. From that slot on nothing republishes it.

To make it stale at once, `surfnet_timeTravel` past the consumer's limit. Its `absoluteTimestamp`
is in milliseconds, while `publish_time` and the Clock sysvar are in seconds.

# Troubleshooting

| Symptom | Fix |
|---|---|
| The consumer still reads the old price | The override wrote the other receiver's account. Check the price account's owner and use that receiver's template |
| A large price drop is refused | Scale `conf` and `ema_conf` with the price. See [Drop a price](#drop-a-price) |
| A `publish_time` or `posted_slot` value did not hold | Keep-alive rewrites both every slot. A value holds only in an override with `"keepAlive": []` |
| Other feeds the transaction reads turn stale | Keep-alive republishes only the price accounts overrides write. See [Keep every feed a transaction reads fresh](#keep-every-feed-a-transaction-reads-fresh) |
| A feed meant to be stale is still fresh after `surfnet_timeTravel` | `absoluteTimestamp` is in milliseconds, and `publish_time` is in seconds |
| Prices are off by a power of ten after an exponent change | `exponent` rescales every price field. Restate `price`, `conf`, `ema_price` and `ema_conf` to match |

# Files

| File | Holds |
|---|---|
| `v2/overrides.yaml` | the `pyth-price-feed-v2` template and its `price_feed` list |
| `v2/idl.json` | the Pyth Core IDL |
| `v2/keep-alive.yaml` | the `pyth-price-publication` keep-alive template |
| `v2/generate-price-feeds.js` | the script that regenerates the `price_feed` list |
| `core-compatible/v2/overrides.yaml` | the `pyth-core-compatible-price-feed-v2` template |
| `core-compatible/v2/idl.json` | the Pyth Core Compatible IDL |
| `core-compatible/v2/keep-alive.yaml` | the `pyth-core-compatible-price-publication` keep-alive template |

[`registry.rs`](../../registry.rs) compiles these files in. `load_pyth_overrides` loads both
template collections under the protocol `Pyth`, each with the IDL beside it, and both keep-alive
files.

## Regenerating the Pyth Core feed list

`v2/generate-price-feeds.js` fetches the feeds from the Pyth Hermes API
(`https://hermes.pyth.network/v2/price_feeds`) and rewrites the `price_feed` options in
`v2/overrides.yaml`. From `v2/`:

```
node generate-price-feeds.js
```

It keeps the `Crypto` feeds quoted in USD whose base is in the script's `POPULAR_SYMBOLS`, plus
ETH/BTC, and puts SOL, BTC, ETH, USDC and USDT first. The "(N decimals)" in each option's
description is the feed's exponent without its sign, read from
`https://hermes.pyth.network/v2/updates/price/latest`, or 8 when Hermes returns none. The script
expects the `constants` block to end right before `templates:`.

## Tests

The Pyth integration tests in `crates/core/src/tests/protocols/pyth/` fetch real price accounts from
mainnet, so they need a network connection and are compiled only behind a feature:

```
cargo test -p surfpool-core --features integration-tests tests::protocols::pyth
```

Set `SURFPOOL_TEST_RPC_URL` to use a private endpoint if the public one rate-limits. The unit tests
in `registry.rs` check the Pyth Core address derivation and both keep-alive imports without a
network.

# IDL

## Pyth Core

`v2/idl.json` is an Anchor IDL with the address `rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ`. It
has no instructions and defines one account, `PriceUpdateV2`, with the types `PriceFeedMessage`,
`PriceUpdateV2` and `VerificationLevel`. Its `PriceUpdateV2` account has the same discriminator and
fields as the one in the Pyth Core Compatible IDL.

| IDL | sha256 |
|---|---|
| Bundled `v2/idl.json` | `93b29b6f60d6c50c39de3d3f075b0d9acf948633edef7234e6b1e2f9b397aa02` |

## Pyth Core Compatible

`core-compatible/v2/idl.json` is the IDL the program publishes in its Anchor IDL account
`raeUSn8gguFWVFs4iw3YathYM3mdb95W5Bfiu3tqE7F` (`pyth_solana_receiver` 0.2.1, legacy format, sha256
`9ec4718f66a71fc524ecdd533770ca5a8a3984f8e1fb603f01f5e31ff0ab0caa`), converted with
`anchor idl convert --program-id rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp` (anchor-cli 0.31.1,
output sha256 `29ab79ef705a3429b3ced2b1222e1b5efdcd4b40fed547177d31c16ebd8473fc`).

One change is made before the conversion: the account names `priceUpdateV2` and `twapUpdate` are
written `PriceUpdateV2` and `TwapUpdate`, as the program's Rust types are named. Anchor derives an
account's discriminator from its name, and only the capitalized names give the discriminator the
live price accounts carry.
Loading
Loading