Skip to content

Commit 6d63958

Browse files
authored
docs(pro): move ExampleConsumer lazer address into constructor (#3874)
Stores the pyth-lazer-stellar verifier's contract address at deploy time via __constructor instead of taking it as an argument on every update_price call. Matches the shipped example at pyth-examples/lazer/stellar and the standard Soroban pattern where one-time deployment configuration lives in instance storage. Co-authored-by: Jayant Krishnamurthy <541339+jayantk@users.noreply.github.com>
1 parent c5f702e commit 6d63958

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

  • apps/developer-hub/content/docs/price-feeds/pro/integrate-as-consumer

apps/developer-hub/content/docs/price-feeds/pro/integrate-as-consumer/stellar.mdx

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,31 @@ Then verify from inside your own contract:
6565
#![no_std]
6666

6767
use pyth_lazer_stellar_sdk::{ParseError, PythLazerClient, VerifiedPayload};
68-
use soroban_sdk::{contract, contractimpl, Address, Bytes, Env};
68+
use soroban_sdk::{contract, contractimpl, contracttype, Address, Bytes, Env};
6969

7070
#[contract]
7171
pub struct ExampleConsumer;
7272

73+
#[contracttype]
74+
pub enum DataKey {
75+
Lazer,
76+
}
77+
7378
#[contractimpl]
7479
impl ExampleConsumer {
80+
/// Runs once at deploy time. Stores the deployed `pyth-lazer-stellar`
81+
/// verifier's contract address so every subsequent `update_price` call
82+
/// uses the same trusted verifier.
83+
pub fn __constructor(env: Env, lazer: Address) {
84+
env.storage().instance().set(&DataKey::Lazer, &lazer);
85+
}
86+
7587
/// Receives a signed Lazer update and asks `pyth-lazer-stellar` to verify
7688
/// it. Returns a typed `VerifiedPayload` (which derefs to the parsed
7789
/// `Update`); traps if the signature is bad, returns `ParseError` if the
7890
/// verified payload bytes are malformed.
79-
pub fn update_price(env: Env, lazer: Address, payload: Bytes) -> Result<(), ParseError> {
91+
pub fn update_price(env: Env, payload: Bytes) -> Result<(), ParseError> {
92+
let lazer: Address = env.storage().instance().get(&DataKey::Lazer).unwrap();
8093
let update: VerifiedPayload =
8194
PythLazerClient::new(&env, &lazer).verify_update(&payload)?;
8295
// `update.feeds`, `update.timestamp`, `update.channel` are accessible
@@ -111,12 +124,22 @@ pub struct StoredPrice {
111124
pub timestamp_us: u64,
112125
}
113126

127+
#[contracttype]
128+
pub enum DataKey {
129+
Lazer,
130+
}
131+
114132
#[contract]
115133
pub struct ExampleConsumer;
116134

117135
#[contractimpl]
118136
impl ExampleConsumer {
119-
pub fn update_price(env: Env, lazer: Address, payload: Bytes) -> Result<StoredPrice, ParseError> {
137+
pub fn __constructor(env: Env, lazer: Address) {
138+
env.storage().instance().set(&DataKey::Lazer, &lazer);
139+
}
140+
141+
pub fn update_price(env: Env, payload: Bytes) -> Result<StoredPrice, ParseError> {
142+
let lazer: Address = env.storage().instance().get(&DataKey::Lazer).unwrap();
120143
// Verify + parse in one call.
121144
let update = PythLazerClient::new(&env, &lazer).verify_update(&payload)?;
122145

@@ -139,12 +162,13 @@ impl ExampleConsumer {
139162
Every property your contract reads must be requested in the Step 1 subscription — feed properties not in the subscription decode to `None`, so an unguarded `.expect(...)` will trap on-chain. Signed integer properties (`price`, `best_bid_price`, `best_ask_price`, `confidence`, `funding_rate`, `ema_price`, `ema_confidence`) are decoded as `Option<i64>`. Per-feed `feed_update_timestamp` (microseconds since epoch, requested as `feedUpdateTimestamp`) is the right field for freshness checks; the top-level `update.timestamp` is the payload-level timestamp. See the [Pyth Pro payload reference](/price-feeds/pro/payload-reference) for the complete wire format.
140163

141164
<Callout type="info">
142-
Submit the transaction from your off-chain client using
165+
Pass the deployed [`pyth-lazer-stellar` contract id](/price-feeds/pro/contract-addresses#stellar)
166+
as the `lazer` constructor argument at deploy time — e.g.
167+
`stellar contract deploy --wasm <path> -- --lazer <verifier-contract-id>`.
168+
Then submit price updates from your off-chain client using
143169
[`@stellar/stellar-sdk`](https://www.npmjs.com/package/@stellar/stellar-sdk),
144-
passing the binary update bytes as the `update` argument to
145-
`ExampleConsumer::update_price` (and the deployed
146-
[`pyth-lazer-stellar` contract id](/price-feeds/pro/contract-addresses#stellar)
147-
as `lazer`).
170+
passing just the binary update bytes as the `payload` argument to
171+
`ExampleConsumer::update_price`.
148172
</Callout>
149173

150174
</Step>

0 commit comments

Comments
 (0)