diff --git a/crates/core/src/scenarios/README.md b/crates/core/src/scenarios/README.md index 2ab38d0b6..d241da744 100644 --- a/crates/core/src/scenarios/README.md +++ b/crates/core/src/scenarios/README.md @@ -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) diff --git a/crates/core/src/scenarios/protocols/pyth/README.md b/crates/core/src/scenarios/protocols/pyth/README.md new file mode 100644 index 000000000..f7b55a714 --- /dev/null +++ b/crates/core/src/scenarios/protocols/pyth/README.md @@ -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": ""}`. + +### 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": ""}`. + +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: +price_message.ema_conf: +``` + +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. diff --git a/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/idl.json b/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/idl.json new file mode 100644 index 000000000..c9b8013ae --- /dev/null +++ b/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/idl.json @@ -0,0 +1,1002 @@ +{ + "address": "rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp", + "metadata": { + "name": "pyth_solana_receiver", + "version": "0.2.1", + "spec": "0.1.0" + }, + "instructions": [ + { + "name": "initialize", + "discriminator": [ + 175, + 175, + 109, + 31, + 13, + 152, + 155, + 237 + ], + "accounts": [ + { + "name": "payer", + "writable": true, + "signer": true + }, + { + "name": "config", + "writable": true + }, + { + "name": "system_program" + } + ], + "args": [ + { + "name": "initial_config", + "type": { + "defined": { + "name": "Config" + } + } + } + ] + }, + { + "name": "request_governance_authority_transfer", + "discriminator": [ + 92, + 18, + 67, + 156, + 27, + 151, + 183, + 224 + ], + "accounts": [ + { + "name": "payer", + "signer": true + }, + { + "name": "config", + "writable": true + } + ], + "args": [ + { + "name": "target_governance_authority", + "type": "pubkey" + } + ] + }, + { + "name": "cancel_governance_authority_transfer", + "discriminator": [ + 39, + 93, + 70, + 137, + 137, + 90, + 248, + 154 + ], + "accounts": [ + { + "name": "payer", + "signer": true + }, + { + "name": "config", + "writable": true + } + ], + "args": [] + }, + { + "name": "accept_governance_authority_transfer", + "discriminator": [ + 254, + 39, + 222, + 79, + 64, + 217, + 205, + 127 + ], + "accounts": [ + { + "name": "payer", + "signer": true + }, + { + "name": "config", + "writable": true + } + ], + "args": [] + }, + { + "name": "set_data_sources", + "discriminator": [ + 107, + 73, + 15, + 119, + 195, + 116, + 91, + 210 + ], + "accounts": [ + { + "name": "payer", + "signer": true + }, + { + "name": "config", + "writable": true + } + ], + "args": [ + { + "name": "valid_data_sources", + "type": { + "vec": { + "defined": { + "name": "DataSource" + } + } + } + } + ] + }, + { + "name": "set_fee", + "discriminator": [ + 18, + 154, + 24, + 18, + 237, + 214, + 19, + 80 + ], + "accounts": [ + { + "name": "payer", + "signer": true + }, + { + "name": "config", + "writable": true + } + ], + "args": [ + { + "name": "single_update_fee_in_lamports", + "type": "u64" + } + ] + }, + { + "name": "set_wormhole_address", + "discriminator": [ + 154, + 174, + 252, + 157, + 91, + 215, + 179, + 156 + ], + "accounts": [ + { + "name": "payer", + "signer": true + }, + { + "name": "config", + "writable": true + } + ], + "args": [ + { + "name": "wormhole", + "type": "pubkey" + } + ] + }, + { + "name": "set_minimum_signatures", + "discriminator": [ + 5, + 210, + 206, + 124, + 43, + 68, + 104, + 149 + ], + "accounts": [ + { + "name": "payer", + "signer": true + }, + { + "name": "config", + "writable": true + } + ], + "args": [ + { + "name": "minimum_signatures", + "type": "u8" + } + ] + }, + { + "name": "post_update_atomic", + "docs": [ + "Post a price update using a VAA and a MerklePriceUpdate.", + "This function allows you to post a price update in a single transaction.", + "Compared to `post_update`, it only checks whatever signatures are present in the provided VAA and doesn't fail if the number of signatures is lower than the Wormhole quorum of two thirds of the guardians.", + "The number of signatures that were in the VAA is stored in the `VerificationLevel` of the `PriceUpdateV2` account.", + "", + "We recommend using `post_update_atomic` with 5 signatures. This is close to the maximum signatures you can verify in one transaction without exceeding the transaction size limit.", + "", + "# Warning", + "", + "Using partially verified price updates is dangerous, as it lowers the threshold of guardians that need to collude to produce a malicious price update." + ], + "discriminator": [ + 49, + 172, + 84, + 192, + 175, + 180, + 52, + 234 + ], + "accounts": [ + { + "name": "payer", + "writable": true, + "signer": true + }, + { + "name": "guardian_set", + "docs": [ + "Instead we do the same steps in deserialize_guardian_set_checked." + ] + }, + { + "name": "config" + }, + { + "name": "treasury", + "writable": true + }, + { + "name": "price_update_account", + "docs": [ + "The constraint is such that either the price_update_account is uninitialized or the write_authority is the write_authority.", + "Pubkey::default() is the SystemProgram on Solana and it can't sign so it's impossible that price_update_account.write_authority == Pubkey::default() once the account is initialized" + ], + "writable": true, + "signer": true + }, + { + "name": "system_program" + }, + { + "name": "write_authority", + "signer": true + } + ], + "args": [ + { + "name": "params", + "type": { + "defined": { + "name": "PostUpdateAtomicParams" + } + } + } + ] + }, + { + "name": "post_update", + "docs": [ + "Post a price update using an encoded_vaa account and a MerklePriceUpdate calldata.", + "This should be called after the client has already verified the Vaa via the Wormhole contract.", + "Check out target_chains/solana/cli/src/main.rs for an example of how to do this." + ], + "discriminator": [ + 133, + 95, + 207, + 175, + 11, + 79, + 118, + 44 + ], + "accounts": [ + { + "name": "payer", + "writable": true, + "signer": true + }, + { + "name": "encoded_vaa" + }, + { + "name": "config" + }, + { + "name": "treasury", + "writable": true + }, + { + "name": "price_update_account", + "docs": [ + "The constraint is such that either the price_update_account is uninitialized or the write_authority is the write_authority.", + "Pubkey::default() is the SystemProgram on Solana and it can't sign so it's impossible that price_update_account.write_authority == Pubkey::default() once the account is initialized" + ], + "writable": true, + "signer": true + }, + { + "name": "system_program" + }, + { + "name": "write_authority", + "signer": true + } + ], + "args": [ + { + "name": "params", + "type": { + "defined": { + "name": "PostUpdateParams" + } + } + } + ] + }, + { + "name": "post_twap_update", + "docs": [ + "Post a TWAP (time weighted average price) update for a given time window.", + "This should be called after the client has already verified the VAAs via the Wormhole contract.", + "Check out target_chains/solana/cli/src/main.rs for an example of how to do this." + ], + "discriminator": [ + 232, + 176, + 212, + 105, + 69, + 121, + 18, + 30 + ], + "accounts": [ + { + "name": "payer", + "writable": true, + "signer": true + }, + { + "name": "start_encoded_vaa" + }, + { + "name": "end_encoded_vaa" + }, + { + "name": "config" + }, + { + "name": "treasury", + "writable": true + }, + { + "name": "twap_update_account", + "docs": [ + "The constraint is such that either the price_update_account is uninitialized or the write_authority is the write_authority.", + "Pubkey::default() is the SystemProgram on Solana and it can't sign so it's impossible that price_update_account.write_authority == Pubkey::default() once the account is initialized" + ], + "writable": true, + "signer": true + }, + { + "name": "system_program" + }, + { + "name": "write_authority", + "signer": true + } + ], + "args": [ + { + "name": "params", + "type": { + "defined": { + "name": "PostTwapUpdateParams" + } + } + } + ] + }, + { + "name": "reclaim_rent", + "discriminator": [ + 218, + 200, + 19, + 197, + 227, + 89, + 192, + 22 + ], + "accounts": [ + { + "name": "payer", + "writable": true, + "signer": true + }, + { + "name": "price_update_account", + "writable": true + } + ], + "args": [] + }, + { + "name": "reclaim_twap_rent", + "discriminator": [ + 84, + 3, + 32, + 238, + 108, + 217, + 135, + 39 + ], + "accounts": [ + { + "name": "payer", + "writable": true, + "signer": true + }, + { + "name": "twap_update_account", + "writable": true + } + ], + "args": [] + } + ], + "accounts": [ + { + "name": "Config", + "discriminator": [ + 155, + 12, + 170, + 224, + 30, + 250, + 204, + 130 + ] + }, + { + "name": "PriceUpdateV2", + "discriminator": [ + 34, + 241, + 35, + 99, + 157, + 126, + 244, + 205 + ] + }, + { + "name": "TwapUpdate", + "discriminator": [ + 104, + 192, + 188, + 72, + 246, + 166, + 12, + 81 + ] + } + ], + "errors": [ + { + "code": 6000, + "name": "InvalidWormholeMessage", + "msg": "Received an invalid wormhole message" + }, + { + "code": 6001, + "name": "DeserializeMessageFailed", + "msg": "An error occurred when deserializing the message" + }, + { + "code": 6002, + "name": "InvalidPriceUpdate", + "msg": "Received an invalid price update" + }, + { + "code": 6003, + "name": "UnsupportedMessageType", + "msg": "This type of message is not supported currently" + }, + { + "code": 6004, + "name": "InvalidDataSource", + "msg": "The tuple emitter chain, emitter doesn't match one of the valid data sources." + }, + { + "code": 6005, + "name": "InsufficientFunds", + "msg": "Funds are insufficient to pay the receiving fee" + }, + { + "code": 6006, + "name": "FeedIdMismatch", + "msg": "Cannot calculate TWAP, end slot must be greater than start slot" + }, + { + "code": 6007, + "name": "ExponentMismatch", + "msg": "The start and end messages must have the same feed ID" + }, + { + "code": 6008, + "name": "InvalidTwapSlots", + "msg": "The start and end messages must have the same exponent" + }, + { + "code": 6009, + "name": "InvalidTwapStartMessage", + "msg": "Start message is not the first update for its timestamp" + }, + { + "code": 6010, + "name": "InvalidTwapEndMessage", + "msg": "End message is not the first update for its timestamp" + }, + { + "code": 6011, + "name": "TwapCalculationOverflow", + "msg": "Overflow in TWAP calculation" + }, + { + "code": 6012, + "name": "WrongWriteAuthority", + "msg": "This signer can't write to price update account" + }, + { + "code": 6013, + "name": "WrongVaaOwner", + "msg": "The posted VAA account has the wrong owner." + }, + { + "code": 6014, + "name": "DeserializeVaaFailed", + "msg": "An error occurred when deserializing the VAA." + }, + { + "code": 6015, + "name": "InsufficientGuardianSignatures", + "msg": "The number of guardian signatures is below the minimum" + }, + { + "code": 6016, + "name": "InvalidVaaVersion", + "msg": "Invalid VAA version" + }, + { + "code": 6017, + "name": "GuardianSetMismatch", + "msg": "Guardian set version in the VAA doesn't match the guardian set passed" + }, + { + "code": 6018, + "name": "InvalidGuardianOrder", + "msg": "Guardian signature indices must be increasing" + }, + { + "code": 6019, + "name": "InvalidGuardianIndex", + "msg": "Guardian index exceeds the number of guardians in the set" + }, + { + "code": 6020, + "name": "InvalidSignature", + "msg": "A VAA signature is invalid" + }, + { + "code": 6021, + "name": "InvalidGuardianKeyRecovery", + "msg": "The recovered guardian public key doesn't match the guardian set" + }, + { + "code": 6022, + "name": "WrongGuardianSetOwner", + "msg": "The guardian set account is owned by the wrong program" + }, + { + "code": 6023, + "name": "InvalidGuardianSetPda", + "msg": "The Guardian Set account doesn't match the PDA derivation" + }, + { + "code": 6024, + "name": "GuardianSetExpired", + "msg": "The Guardian Set is expired" + }, + { + "code": 6025, + "name": "GovernanceAuthorityMismatch", + "msg": "The signer is not authorized to perform this governance action" + }, + { + "code": 6026, + "name": "TargetGovernanceAuthorityMismatch", + "msg": "The signer is not authorized to accept the governance authority" + }, + { + "code": 6027, + "name": "NonexistentGovernanceAuthorityTransferRequest", + "msg": "The governance authority needs to request a transfer first" + }, + { + "code": 6028, + "name": "ZeroMinimumSignatures", + "msg": "The minimum number of signatures should be at least 1" + } + ], + "types": [ + { + "name": "PriceFeedMessage", + "type": { + "kind": "struct", + "fields": [ + { + "name": "feed_id", + "type": { + "array": [ + "u8", + 32 + ] + } + }, + { + "name": "price", + "type": "i64" + }, + { + "name": "conf", + "type": "u64" + }, + { + "name": "exponent", + "type": "i32" + }, + { + "name": "publish_time", + "type": "i64" + }, + { + "name": "prev_publish_time", + "type": "i64" + }, + { + "name": "ema_price", + "type": "i64" + }, + { + "name": "ema_conf", + "type": "u64" + } + ] + } + }, + { + "name": "TwapPrice", + "docs": [ + "The time weighted average price & conf for a feed over the window [start_time, end_time].", + "This type is used to persist the calculated TWAP in TwapUpdate accounts on Solana." + ], + "type": { + "kind": "struct", + "fields": [ + { + "name": "feed_id", + "type": { + "array": [ + "u8", + 32 + ] + } + }, + { + "name": "start_time", + "type": "i64" + }, + { + "name": "end_time", + "type": "i64" + }, + { + "name": "price", + "type": "i64" + }, + { + "name": "conf", + "type": "u64" + }, + { + "name": "exponent", + "type": "i32" + }, + { + "name": "down_slots_ratio", + "docs": [ + "Ratio out of 1_000_000, where a value of 1_000_000 represents", + "all slots were missed and 0 represents no slots were missed." + ], + "type": "u32" + } + ] + } + }, + { + "name": "MerklePriceUpdate", + "type": { + "kind": "struct", + "fields": [ + { + "name": "message", + "type": "bytes" + }, + { + "name": "proof", + "type": { + "vec": { + "array": [ + "u8", + 20 + ] + } + } + } + ] + } + }, + { + "name": "DataSource", + "type": { + "kind": "struct", + "fields": [ + { + "name": "chain", + "type": "u16" + }, + { + "name": "emitter", + "type": "pubkey" + } + ] + } + }, + { + "name": "PostUpdateAtomicParams", + "type": { + "kind": "struct", + "fields": [ + { + "name": "vaa", + "type": "bytes" + }, + { + "name": "merkle_price_update", + "type": { + "defined": { + "name": "MerklePriceUpdate" + } + } + }, + { + "name": "treasury_id", + "type": "u8" + } + ] + } + }, + { + "name": "PostUpdateParams", + "type": { + "kind": "struct", + "fields": [ + { + "name": "merkle_price_update", + "type": { + "defined": { + "name": "MerklePriceUpdate" + } + } + }, + { + "name": "treasury_id", + "type": "u8" + } + ] + } + }, + { + "name": "PostTwapUpdateParams", + "type": { + "kind": "struct", + "fields": [ + { + "name": "start_merkle_price_update", + "type": { + "defined": { + "name": "MerklePriceUpdate" + } + } + }, + { + "name": "end_merkle_price_update", + "type": { + "defined": { + "name": "MerklePriceUpdate" + } + } + }, + { + "name": "treasury_id", + "type": "u8" + } + ] + } + }, + { + "name": "VerificationLevel", + "docs": [ + "* This enum represents how many guardian signatures were checked for a Pythnet price update\n * If full, guardian quorum has been attained\n * If partial, at least config.minimum signatures have been verified, but in the case config.minimum_signatures changes in the future we also include the number of signatures that were checked" + ], + "type": { + "kind": "enum", + "variants": [ + { + "name": "Partial", + "fields": [ + { + "name": "num_signatures", + "type": "u8" + } + ] + }, + { + "name": "Full" + } + ] + } + }, + { + "name": "Config", + "type": { + "kind": "struct", + "fields": [ + { + "name": "governance_authority", + "type": "pubkey" + }, + { + "name": "target_governance_authority", + "type": { + "option": "pubkey" + } + }, + { + "name": "wormhole", + "type": "pubkey" + }, + { + "name": "valid_data_sources", + "type": { + "vec": { + "defined": { + "name": "DataSource" + } + } + } + }, + { + "name": "single_update_fee_in_lamports", + "type": "u64" + }, + { + "name": "minimum_signatures", + "type": "u8" + } + ] + } + }, + { + "name": "PriceUpdateV2", + "type": { + "kind": "struct", + "fields": [ + { + "name": "write_authority", + "type": "pubkey" + }, + { + "name": "verification_level", + "type": { + "defined": { + "name": "VerificationLevel" + } + } + }, + { + "name": "price_message", + "type": { + "defined": { + "name": "PriceFeedMessage" + } + } + }, + { + "name": "posted_slot", + "type": "u64" + } + ] + } + }, + { + "name": "TwapUpdate", + "type": { + "kind": "struct", + "fields": [ + { + "name": "write_authority", + "type": "pubkey" + }, + { + "name": "twap", + "type": { + "defined": { + "name": "TwapPrice" + } + } + } + ] + } + } + ] +} \ No newline at end of file diff --git a/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/keep-alive.yaml b/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/keep-alive.yaml new file mode 100644 index 000000000..854ba956a --- /dev/null +++ b/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/keep-alive.yaml @@ -0,0 +1,17 @@ +protocol: Pyth +version: v2 +account_type: PriceUpdateV2 +idl_file_path: idl.json + +templates: + - id: pyth-core-compatible-price-publication + name: Price publication + description: The Pyth publisher republishing a price + idl_account_name: PriceUpdateV2 + address: + type: pubkey + properties: + - path: price_message.publish_time + description: Unix seconds the price was published at, checked against the Clock + - path: posted_slot + description: Slot the price was posted in, for consumers that measure freshness in slots diff --git a/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/overrides.yaml b/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/overrides.yaml new file mode 100644 index 000000000..0b3b99927 --- /dev/null +++ b/crates/core/src/scenarios/protocols/pyth/core-compatible/v2/overrides.yaml @@ -0,0 +1,91 @@ +protocol: Pyth +version: v2 +account_type: PriceUpdateV2 +idl_file_path: idl.json + +tags: + - oracle + - price-feed + - defi + - clock-sensitive + +keep_alive: + - template: pyth-core-compatible-price-publication + address: { type: override } + values: { price_message.publish_time: $unix_s, posted_slot: $slot } + +templates: + - id: pyth-core-compatible-price-feed-v2 + name: Override Pyth Core Compatible Price Account + description: Override a price account of the Pyth Core Compatible receiver, found by address; keeps that account published + idl_account_name: PriceUpdateV2 + properties: + - path: price_message.price + label: Price + description: Price as a scaled integer - multiply the USD value by 10^-exponent + - path: price_message.conf + label: Confidence + description: Uncertainty band around the price, in the same scale as price + - path: price_message.exponent + label: Exponent + description: Decimal exponent, usually -8 (-10 on some feeds). Rescales every price field + - path: price_message.publish_time + label: Publish Time + description: 'Unix seconds of this update, interpreted against the Surfnet clock. Keep-alive writes the Clock time every slot, so a value set here holds only in an override with "keepAlive": []' + - path: price_message.prev_publish_time + label: Previous Publish Time + description: Unix seconds of the preceding update + - path: price_message.ema_price + label: EMA Price + description: Exponential moving average price, in the same scale as price + - path: price_message.ema_conf + label: EMA Confidence + description: Exponential moving average confidence, in the same scale as conf + - path: posted_slot + label: Posted Slot + description: 'Slot the update was posted at, for consumers that measure freshness in slots. Keep-alive writes the current slot every slot, so a value set here holds only in an override with "keepAlive": []' + address: + type: pubkey + llm_context: | + FINDING THE PRICE ACCOUNT: + Pyth runs two receivers on Solana with the same feed ids at different addresses, and a + consumer reads only one. This template writes price accounts owned by the Pyth Core + Compatible receiver rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp; for the Pyth Core + receiver rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ use pyth-price-feed-v2. + 1. Take the price account from the account that reads it: the consumer's protocol templates + say which field names it. A price account the user gives is used as is. + 2. Read it with getAccountInfo, encoding "jsonParsed". Its owner must be + rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp; price_message.exponent gives the scale. + 3. Put it in the override's account as {"pubkey":""}. + + Set fetchBeforeUse: true to pull the account from upstream first; false only for a later + override that builds on state an earlier override in the same scenario prepared. + + PRICE: price_message.price = usd_price x 10^-exponent, so with exponent -8, $145.00 is + 14500000000. ema_price, conf and ema_conf use the same scale. 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. + + THE PRICE STAYS PUBLISHED. From the slot this override plays until the network is reset, + Surfpool republishes the price account it writes every slot, as the Pyth publisher does on + mainnet: publish_time becomes the Surfnet clock's unix seconds and posted_slot the slot, also + after a time travel. Price, conf and the EMA fields keep their values, and a value set for + publish_time or posted_slot does not hold. + + EVERY FEED A TRANSACTION READS: a price account no override writes is not republished and + ages against the Surfnet clock. A liquidation reads every feed of the borrower, so add one + override of this template per price account the transaction reads, in the same scenario and + slot. On an account whose price must not move, set only price_message.publish_time: any + value works, because keep-alive rewrites it in the same slot. + + STALE PRICE: a price account left without an override ages from the start. To let one age + later, give a later override of it "keepAlive": [] and a value, such as its current price. + From that slot on nothing republishes it, and a publish_time or posted_slot set in that + override holds. + + EXAMPLE - "SOL drops to $20" on an account with exponent -8, live price p and ema_price q: + price_message.price: 2000000000 + price_message.ema_price: 2000000000 + price_message.conf: the live conf x 2000000000 / p + price_message.ema_conf: the live ema_conf x 2000000000 / q diff --git a/crates/core/src/scenarios/protocols/pyth/v2/keep-alive.yaml b/crates/core/src/scenarios/protocols/pyth/v2/keep-alive.yaml new file mode 100644 index 000000000..b435dfe68 --- /dev/null +++ b/crates/core/src/scenarios/protocols/pyth/v2/keep-alive.yaml @@ -0,0 +1,17 @@ +protocol: Pyth +version: v2 +account_type: PriceUpdateV2 +idl_file_path: idl.json + +templates: + - id: pyth-price-publication + name: Price publication + description: The Pyth publisher republishing a price + idl_account_name: PriceUpdateV2 + address: + type: pubkey + properties: + - path: price_message.publish_time + description: Unix seconds the price was published at, checked against the Clock + - path: posted_slot + description: Slot the price was posted in, for consumers that measure freshness in slots diff --git a/crates/core/src/scenarios/protocols/pyth/v2/overrides.yaml b/crates/core/src/scenarios/protocols/pyth/v2/overrides.yaml index bdc65802d..49bf77223 100644 --- a/crates/core/src/scenarios/protocols/pyth/v2/overrides.yaml +++ b/crates/core/src/scenarios/protocols/pyth/v2/overrides.yaml @@ -9,6 +9,11 @@ tags: - defi - clock-sensitive +keep_alive: + - template: pyth-price-publication + address: { type: override } + values: { price_message.publish_time: $unix_s, posted_slot: $slot } + constants: price_feed: label: Price Feed @@ -194,7 +199,7 @@ constants: templates: - id: pyth-price-feed-v2 name: Override Price Feed - description: Override any Pyth price feed with custom price data + description: Override any Pyth price feed with custom price data; keeps that feed published idl_account_name: PriceUpdateV2 properties: - path: feed_id @@ -212,7 +217,7 @@ templates: description: Decimal exponent, normally -8 and -10 on some feeds. Rescales every price field - path: price_message.publish_time label: Publish Time - description: Unix seconds of this update, interpreted against the Surfnet clock + description: 'Unix seconds of this update, interpreted against the Surfnet clock. Keep-alive writes the Clock time every slot, so a value set here holds only in an override with "keepAlive": []' - path: price_message.prev_publish_time label: Previous Publish Time description: Unix seconds of the preceding update @@ -224,11 +229,17 @@ templates: description: Exponential moving average confidence, in the same scale as conf - path: posted_slot label: Posted Slot - description: Slot the update was posted at, for consumers that measure freshness in slots + description: 'Slot the update was posted at, for consumers that measure freshness in slots. Keep-alive writes the current slot every slot, so a value set here holds only in an override with "keepAlive": []' llm_context: | CRITICAL: Always set fetchBeforeUse: true for Pyth price feeds! This fetches the current account data from mainnet before applying your price override. + WHICH RECEIVER: this template writes price accounts of the Pyth Core receiver + rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ, which Save and Kamino read. Project 0 reads the + Pyth Core Compatible receiver instead: for a Project 0 price use + pyth-core-compatible-price-feed-v2. When a protocol's own templates name the price account it + reads, follow them. + HOW TO USE THIS TEMPLATE: 1. Select the correct price feed from the `feed_id` constant options (e.g., SOL/USD, BTC/USD) 2. Check the feed's `llm_context` field for its decimal precision (most use 8, some use 10) @@ -255,31 +266,25 @@ templates: unless the scenario is specifically about an exponent error. If you do change it, restate every scaled field to match. - price_message.ema_price and price_message.ema_conf use the same scales as price and conf. - - posted_slot is a slot number, not a timestamp. Some consumers measure freshness in slots - rather than in seconds. - TIME FIELDS - READ BEFORE SETTING publish_time: - publish_time and prev_publish_time are unix seconds interpreted against the SURFNET clock, not - the real-world clock. The two can differ by an arbitrary amount, so a timestamp taken from the - real current time will look far in the future inside the simulation and will NOT produce a - stale price. + THE PRICE STAYS PUBLISHED. From the slot this override plays until the network is reset, + Surfpool republishes the price account it writes every slot, as the Pyth publisher does on + mainnet: publish_time becomes the Surfnet clock's unix seconds and posted_slot the slot, also + after a time travel. Price, conf and the EMA fields keep their values, and a value set for + publish_time or posted_slot does not hold. - Do not try to backdate the timestamp either - the Surfnet clock only moves forward. To build a - stale-price scenario, make the price fresh and then advance time past the consumer's limit: - 1. surfnet_pauseClock so the elapsed time is exact rather than dependent on slot production. - It returns an EpochInfo, which carries no timestamp - step 2 is how you read the time. - 2. read the Surfnet clock with getAccountInfo on - SysvarC1ock11111111111111111111111111111111 using the jsonParsed encoding, and take - result.value.data.parsed.info.unixTimestamp, which is in SECONDS. Set publish_time to - that value, which makes the price fresh. - 3. surfnet_timeTravel forward by more than the consumer's staleness limit. Its - absoluteTimestamp is in MILLISECONDS, so multiply the value from step 2 by 1000 before - adding your offset. - 4. send the transaction - the consumer now sees an aged price and should reject it + EVERY FEED A TRANSACTION READS: a price account no override writes is not republished and + ages against the Surfnet clock. A liquidation reads every feed of the borrower, so add one + override of this template per feed the transaction reads, 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. feed_id alone writes nothing. - UNITS WARNING for step 3: surfnet_timeTravel's absoluteTimestamp is in MILLISECONDS, while - publish_time and the Clock sysvar are in SECONDS. Multiply by 1000 when passing a value read - from the clock, or the call fails with a "past timestamp" error. + STALE PRICE: a feed left without an override ages 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, and a publish_time or posted_slot set in that override + holds. 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. address: type: pda program_id: pythWSnswVUd12oZpeFP8e9CVaEqJg25g1Vtc2biRsT diff --git a/crates/core/src/scenarios/registry.rs b/crates/core/src/scenarios/registry.rs index dcc7866da..5ff8ec897 100644 --- a/crates/core/src/scenarios/registry.rs +++ b/crates/core/src/scenarios/registry.rs @@ -10,6 +10,13 @@ use surfpool_types::{ pub const PYTH_V2_IDL_CONTENT: &str = include_str!("./protocols/pyth/v2/idl.json"); pub const PYTH_V2_OVERRIDES_CONTENT: &str = include_str!("./protocols/pyth/v2/overrides.yaml"); +pub const PYTH_V2_KEEP_ALIVE_CONTENT: &str = include_str!("./protocols/pyth/v2/keep-alive.yaml"); +pub const PYTH_CORE_COMPATIBLE_V2_IDL_CONTENT: &str = + include_str!("./protocols/pyth/core-compatible/v2/idl.json"); +pub const PYTH_CORE_COMPATIBLE_V2_OVERRIDES_CONTENT: &str = + include_str!("./protocols/pyth/core-compatible/v2/overrides.yaml"); +pub const PYTH_CORE_COMPATIBLE_V2_KEEP_ALIVE_CONTENT: &str = + include_str!("./protocols/pyth/core-compatible/v2/keep-alive.yaml"); pub const CHAINLINK_V1_IDL_CONTENT: &str = include_str!("./protocols/chainlink/v1/idl.json"); pub const CHAINLINK_V1_OVERRIDES_CONTENT: &str = @@ -278,6 +285,20 @@ impl TemplateRegistry { pub fn load_pyth_overrides(&mut self) { self.load_protocol_overrides(PYTH_V2_IDL_CONTENT, PYTH_V2_OVERRIDES_CONTENT, "pyth"); + let idl = serde_json::from_str(PYTH_V2_IDL_CONTENT).expect("pyth idl"); + self.load_keep_alive(Some(idl), PYTH_V2_KEEP_ALIVE_CONTENT, "pyth"); + self.load_protocol_overrides( + PYTH_CORE_COMPATIBLE_V2_IDL_CONTENT, + PYTH_CORE_COMPATIBLE_V2_OVERRIDES_CONTENT, + "pyth-core-compatible", + ); + let idl = serde_json::from_str(PYTH_CORE_COMPATIBLE_V2_IDL_CONTENT) + .expect("pyth core compatible idl"); + self.load_keep_alive( + Some(idl), + PYTH_CORE_COMPATIBLE_V2_KEEP_ALIVE_CONTENT, + "pyth-core-compatible", + ); } pub fn load_chainlink_overrides(&mut self) { @@ -934,18 +955,19 @@ templates: fn test_registry_loads_all_protocols() { let registry = TemplateRegistry::new(); - // Pyth (1) + Chainlink (1) + Jupiter (1) + Raydium CLMM (3) + Raydium AMM v4 (3) + // Pyth (2) + Chainlink (1) + Jupiter (1) + Raydium CLMM (3) + Raydium AMM v4 (3) // + Raydium CPMM (2) + Meteora (DLMM 1, DAMM v2 2) // + Kamino (Lend 17, Scope 3, Farms 5, Swap 2, Vault 5, Liquidity 4 = 36) // + Whirlpool (2) + SPL Token (2) + Pump (2) + PumpSwap (3) + GoonFi (4) + HumidiFi (3) - // + SolFi (4) + BisonFi (3) + Phoenix Eternal (19) + OpenBook v2 (2) = 94 + // + SolFi (4) + BisonFi (3) + Phoenix Eternal (19) + OpenBook v2 (2) = 95 assert_eq!( registry.count(), - 94, - "Registry should load 94 templates total" + 95, + "Registry should load 95 templates total" ); assert!(registry.contains("pyth-price-feed-v2")); + assert!(registry.contains("pyth-core-compatible-price-feed-v2")); assert!(registry.contains("chainlink-data-feed")); @@ -1520,7 +1542,7 @@ templates: [] let registry = TemplateRegistry::new(); let pyth_templates = registry.by_protocol("Pyth"); - assert_eq!(pyth_templates.len(), 1, "Should have 1 Pyth template"); + assert_eq!(pyth_templates.len(), 2, "Should have 2 Pyth templates"); let chainlink_templates = registry.by_protocol("Chainlink"); assert_eq!( @@ -1671,8 +1693,8 @@ templates: [] let oracle_templates = registry.by_tags(&[vec!["oracle".to_string()]].concat()); assert_eq!( oracle_templates.len(), - 7, - "Should find 7 oracle templates (Pyth + Chainlink + 3 Kamino Scope + 1 GoonFi + 1 SolFi)" + 8, + "Should find 8 oracle templates (2 Pyth + Chainlink + 3 Kamino Scope + 1 GoonFi + 1 SolFi)" ); let rewards_templates = registry.by_tags(&[vec!["rewards".to_string()]].concat()); @@ -1716,12 +1738,15 @@ templates: [] !openbook.is_empty(), "the OpenBook v2 templates are registered" ); - for template in std::iter::once("pyth-price-feed-v2").chain( - phoenix - .iter() - .chain(openbook.iter()) - .map(|template| template.id.as_str()), - ) { + for template in ["pyth-price-feed-v2", "pyth-core-compatible-price-feed-v2"] + .into_iter() + .chain( + phoenix + .iter() + .chain(openbook.iter()) + .map(|template| template.id.as_str()), + ) + { assert!( tagged.contains(&template), "{template} must be clock-sensitive" @@ -1751,6 +1776,7 @@ templates: [] assert!(ids.contains(&"raydium-amm-pool-state".to_string())); assert!(ids.contains(&"jupiter-token-ledger-override".to_string())); assert!(ids.contains(&"pyth-price-feed-v2".to_string())); + assert!(ids.contains(&"pyth-core-compatible-price-feed-v2".to_string())); assert!(ids.contains(&"chainlink-data-feed".to_string())); assert!(ids.contains(&"meteora-dlmm-pool-state".to_string())); assert!(ids.contains(&"kamino-reserve-state".to_string())); @@ -1989,6 +2015,66 @@ templates: [] assert_eq!(&forged[224..240], &[0; 16]); } + #[test] + fn test_pyth_core_compatible_price_feed_takes_a_caller_selected_account() { + let registry = TemplateRegistry::new(); + let template = registry.get("pyth-core-compatible-price-feed-v2").unwrap(); + + assert_eq!( + template.idl().address, + "rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp" + ); + assert_eq!( + template.address, + AccountAddress::Pubkey(String::new()), + "the price account must be chosen by the caller" + ); + assert!( + template.constants.is_empty(), + "the template must not embed a feed catalog" + ); + assert!( + template.llm_context.as_deref().is_some_and( + |context| context.contains("rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp") + ), + "the template must say which receiver owns its accounts" + ); + } + + #[test] + fn pyth_price_feeds_keep_their_price_published() { + let registry = TemplateRegistry::new(); + for (id, publication) in [ + ("pyth-price-feed-v2", "pyth-price-publication"), + ( + "pyth-core-compatible-price-feed-v2", + "pyth-core-compatible-price-publication", + ), + ] { + let template = registry.get(id).unwrap(); + let [import] = template.keep_alive.as_slice() else { + panic!("{id} must keep its price published"); + }; + assert_eq!(import.template, publication, "{id}"); + assert_eq!( + import.address, + surfpool_types::KeepAliveAddress::Override, + "{id} must keep the price account it writes published" + ); + let mut paths: Vec<&str> = import.values.keys().map(String::as_str).collect(); + paths.sort(); + assert_eq!( + paths, + ["posted_slot", "price_message.publish_time"], + "{id} must leave the scenario's price alone" + ); + assert!( + !registry.contains(publication), + "{publication} is never listed" + ); + } + } + #[test] fn test_get_pda_seed_references() { use surfpool_types::AccountAddress; diff --git a/crates/core/src/tests/protocols/mod.rs b/crates/core/src/tests/protocols/mod.rs index cacf9ef24..eff948746 100644 --- a/crates/core/src/tests/protocols/mod.rs +++ b/crates/core/src/tests/protocols/mod.rs @@ -9,5 +9,6 @@ //! A protocol's module is declared here and nowhere else; the filter above then covers it. pub mod meteora; +pub mod pyth; pub mod raydium; pub mod whirlpool; diff --git a/crates/core/src/tests/protocols/pyth/mod.rs b/crates/core/src/tests/protocols/pyth/mod.rs new file mode 100644 index 000000000..8bd0b748f --- /dev/null +++ b/crates/core/src/tests/protocols/pyth/mod.rs @@ -0,0 +1,300 @@ +//! Pyth integration tests. +//! +//! These fetch a real price feed from mainnet, so they need a network connection and are compiled +//! only behind a feature: +//! +//! ```text +//! 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. + +use std::collections::HashMap; + +use solana_clock::Clock; +use solana_commitment_config::CommitmentConfig; +use solana_pubkey::Pubkey; +use surfpool_types::{AccountAddress, OverrideInstance, Scenario}; + +use crate::{ + scenarios::TemplateRegistry, + surfnet::{ + GetAccountResult, locker::SurfnetSvmLocker, remote::SurfnetRemoteClient, svm::SurfnetSvm, + }, +}; + +const RPC_URL_ENV: &str = "SURFPOOL_TEST_RPC_URL"; +const DEFAULT_RPC_URL: &str = "https://api.mainnet-beta.solana.com"; + +const PYTH_CORE_RECEIVER: &str = "rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ"; +const CORE_SOL_USD_FEED: &str = "7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE"; +const PYTH_CORE_COMPATIBLE_RECEIVER: &str = "rec2HHDDnjLfj4kE7VyEtFA1HPGQLK33259532cRyHp"; +const CORE_COMPATIBLE_SOL_USD_FEED: &str = "7AviUf9nL62mcxNbQGKm4nKDQnPjswo6c5MX4D57HmyE"; +const SOL_USD_FEED_ID: &str = "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d"; + +async fn fetch(address: &str) -> solana_account::Account { + let client = SurfnetRemoteClient::new( + std::env::var(RPC_URL_ENV).unwrap_or_else(|_| DEFAULT_RPC_URL.to_string()), + ); + match client + .get_multiple_accounts( + &[Pubkey::from_str_const(address)], + CommitmentConfig::confirmed(), + ) + .await + .unwrap_or_else(|e| panic!("failed to fetch {address} from mainnet: {e}")) + .remove(0) + { + GetAccountResult::FoundAccount(_, account, _) + | GetAccountResult::FoundCoupledAccount((_, account), _, _) => account, + GetAccountResult::None(_) => { + panic!("{address} no longer exists on mainnet; the test needs a new address") + } + } +} + +/// Runs the template through the scenario path on the account the caller names, so the IDL comes +/// from the account's owner. +#[tokio::test(flavor = "multi_thread")] +async fn core_compatible_price_feed_touches_only_price_fields() { + let live = fetch(CORE_COMPATIBLE_SOL_USD_FEED).await; + assert_eq!(live.owner.to_string(), PYTH_CORE_COMPATIBLE_RECEIVER); + // A Partial verification level shifts every offset below by one. + assert_eq!(live.data[40], 1, "verification level should be Full"); + let feed_id = hex::decode(SOL_USD_FEED_ID.trim_start_matches("0x")).unwrap(); + assert_eq!(&live.data[41..73], feed_id.as_slice(), "feed_id at 41"); + + let registry = TemplateRegistry::new(); + let template = registry.get("pyth-core-compatible-price-feed-v2").unwrap(); + + // (path, offset, width, value). Each value differs from the live one, so a skipped write fails. + let rows: [(&str, usize, usize, i64); 8] = [ + ("price_message.price", 73, 8, 9_100_000_000_000), + ("price_message.conf", 81, 8, 500_000_000), + ("price_message.exponent", 89, 4, -10), + ("price_message.publish_time", 93, 8, 1_800_000_000), + ("price_message.prev_publish_time", 101, 8, 1_799_999_940), + ("price_message.ema_price", 109, 8, 9_050_000_000_000), + ("price_message.ema_conf", 117, 8, 450_000_000), + ("posted_slot", 125, 8, 500_000_000), + ]; + for path in template.property_paths() { + assert!( + rows.iter().any(|(row, ..)| *row == path), + "{path} has no row" + ); + } + + let target = Pubkey::from_str_const(CORE_COMPATIBLE_SOL_USD_FEED); + let (svm, _events_rx, _geyser_rx) = SurfnetSvm::default(); + let identity = svm + .get_forged_account_data(&target, &live.data, template.idl(), &HashMap::new()) + .unwrap(); + assert_eq!(identity, live.data, "no-op round trip changed bytes"); + + let values: HashMap = rows + .iter() + .map(|(path, _, _, value)| (path.to_string(), serde_json::json!(value))) + .collect(); + + let locker = SurfnetSvmLocker::new(svm); + locker.with_svm_writer(|svm| svm.set_account(&target, live.clone()).unwrap()); + let mut scenario = Scenario::new( + "Pyth Core Compatible SOL/USD".to_string(), + "Move every price field of the live feed".to_string(), + ); + scenario.add_override( + OverrideInstance::new( + template.id.clone(), + 0, + AccountAddress::Pubkey(CORE_COMPATIBLE_SOL_USD_FEED.to_string()), + ) + .with_values(values), + ); + locker.register_scenario(scenario, Some(100)).unwrap(); + locker + .materialize_overrides_for_slot(&None, 100) + .await + .unwrap(); + + let (forged, clock) = locker.with_svm_reader(|svm| { + ( + svm.get_account(&target) + .unwrap() + .expect("the feed should exist"), + svm.inner.get_sysvar::(), + ) + }); + assert_eq!(forged.owner, live.owner); + assert_eq!(forged.lamports, live.lamports); + assert_eq!(forged.data.len(), live.data.len()); + + let diffs: Vec = (0..live.data.len()) + .filter(|&i| forged.data[i] != live.data[i]) + .collect(); + let written = |i: &usize| { + rows.iter() + .any(|(_, offset, width, _)| (*offset..offset + width).contains(i)) + }; + assert!( + !diffs.is_empty() && diffs.iter().all(written), + "changed {diffs:?}" + ); + for (path, offset, width, value) in rows { + // Keep-alive republishes the price after the override plays. + let value = match path { + "price_message.publish_time" => clock.unix_timestamp, + "posted_slot" => clock.slot as i64, + _ => value, + }; + assert_eq!( + &forged.data[offset..offset + width], + &value.to_le_bytes()[..width], + "{path}" + ); + } +} + +/// Keep-alive republishes each price account an override writes every slot, as the Pyth publisher +/// does on mainnet, time travels included: one whose price moved, and one whose override set only +/// `publish_time`. Only the publication fields move with the Clock: each price stays as the +/// scenario left it. A price account no override writes is never republished, and one whose later +/// override sets `keepAlive: []` is not republished from that slot on. +#[tokio::test(flavor = "multi_thread")] +async fn each_override_keeps_the_price_account_it_writes_published() { + const PRICE: i64 = 2_000_000_000; + const STOP_PRICE: i64 = 1_000_000_000; + const STOP_SLOT: u64 = 2; + for (template_id, address, receiver) in [ + ("pyth-price-feed-v2", CORE_SOL_USD_FEED, PYTH_CORE_RECEIVER), + ( + "pyth-core-compatible-price-feed-v2", + CORE_COMPATIBLE_SOL_USD_FEED, + PYTH_CORE_COMPATIBLE_RECEIVER, + ), + ] { + let live = fetch(address).await; + assert_eq!(live.owner.to_string(), receiver); + // A Partial verification level shifts every offset below by one. + assert_eq!( + live.data[40], 1, + "{address} verification level should be Full" + ); + let word = + |data: &[u8], offset: usize| <[u8; 8]>::try_from(&data[offset..offset + 8]).unwrap(); + let live_price = i64::from_le_bytes(word(&live.data, 73)); + + let moved = Pubkey::from_str_const(address); + let published = Pubkey::new_unique(); + let unnamed = Pubkey::new_unique(); + let stopped = Pubkey::new_unique(); + let (svm, _events_rx, _geyser_rx) = SurfnetSvm::default(); + let locker = SurfnetSvmLocker::new(svm); + locker.with_svm_writer(|svm| { + for address in [moved, published, unnamed, stopped] { + svm.set_account(&address, live.clone()).unwrap(); + } + }); + let override_of = |address: &Pubkey, slot: u64, path: &str, value: i64| { + OverrideInstance::new( + template_id.to_string(), + slot, + AccountAddress::Pubkey(address.to_string()), + ) + .with_values(HashMap::from([( + path.to_string(), + serde_json::json!(value), + )])) + }; + let mut scenario = Scenario::new( + "Pyth SOL at $20".to_string(), + "One override per price account".to_string(), + ); + scenario.add_override(override_of(&moved, 0, "price_message.price", PRICE)); + // Any value: keep-alive rewrites it in the same slot. + scenario.add_override(override_of(&published, 0, "price_message.publish_time", 1)); + scenario.add_override(override_of(&stopped, 0, "price_message.price", PRICE)); + let mut stop = override_of(&stopped, STOP_SLOT, "price_message.price", STOP_PRICE); + stop.keep_alive = Some(serde_json::json!([])); + scenario.add_override(stop); + locker.register_scenario(scenario, None).unwrap(); + + let read = |address: &Pubkey| { + locker.with_svm_reader(|svm| { + ( + svm.get_account(address).unwrap().unwrap().data, + svm.inner.get_sysvar::(), + ) + }) + }; + let assert_published = |when: &str, with_stopped: bool| { + let mut kept = vec![ + (moved, "moved", PRICE), + (published, "publish_time only", live_price), + ]; + if with_stopped { + kept.push((stopped, "stopped later", PRICE)); + } + for (address, name, price) in kept { + let (data, clock) = read(&address); + let what = format!("{name} {when}"); + assert_eq!( + i64::from_le_bytes(word(&data, 93)), + clock.unix_timestamp, + "{template_id} {what}: publish_time" + ); + assert_eq!( + u64::from_le_bytes(word(&data, 125)), + clock.slot, + "{template_id} {what}: posted_slot" + ); + assert_eq!( + i64::from_le_bytes(word(&data, 73)), + price, + "{template_id} {what}" + ); + let diffs: Vec = (0..live.data.len()) + .filter(|&i| data[i] != live.data[i]) + .collect(); + assert!( + diffs.iter().all(|i| [73..81, 93..101, 125..133] + .iter() + .any(|written| written.contains(i))), + "{template_id} {what}: changed {diffs:?}" + ); + } + assert_eq!(read(&unnamed).0, live.data, "{template_id} unnamed {when}"); + }; + + for slot in 0..STOP_SLOT { + locker.confirm_current_block(&None).await.unwrap(); + assert_published(&format!("relative slot {slot}"), true); + } + let mut aged = read(&stopped).0; + aged[73..81].copy_from_slice(&STOP_PRICE.to_le_bytes()); + for slot in STOP_SLOT..=STOP_SLOT + 1 { + locker.confirm_current_block(&None).await.unwrap(); + let when = format!("relative slot {slot}"); + assert_published(&when, false); + assert_eq!(read(&stopped).0, aged, "{template_id} stopped {when}"); + } + let clock = locker.with_svm_reader(|svm| svm.inner.get_sysvar::()); + locker + .apply_time_travel( + Clock { + slot: clock.slot + 1_000, + unix_timestamp: clock.unix_timestamp + 3_600, + ..clock + }, + &None, + ) + .await + .unwrap(); + assert_published("after a time travel", false); + assert_eq!( + read(&stopped).0, + aged, + "{template_id} stopped after a time travel" + ); + } +}