Skip to content

Commit 7c7ea4f

Browse files
authored
fix: point the oracle tutorial at Pragma's Miden v0.15 oracle (#209)
1 parent 59a8b73 commit 7c7ea4f

3 files changed

Lines changed: 35 additions & 19 deletions

File tree

docs/src/rust-client/oracle_tutorial.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub async fn get_oracle_foreign_accounts(
111111
let publisher_word = storage
112112
.get_map_item(&publishers_slot, key)
113113
.expect("publisher entry missing from oracle storage");
114+
// The publisher id word is laid out as [prefix, suffix, 0, 0].
114115
AccountId::new_unchecked([publisher_word[0], publisher_word[1]])
115116
})
116117
.collect();
@@ -190,9 +191,13 @@ async fn main() -> Result<(), ClientError> {
190191
// -------------------------------------------------------------------------
191192
// Get all foreign accounts for oracle data
192193
// -------------------------------------------------------------------------
194+
// Defaults to Pragma's current Miden v0.15 testnet oracle; pass a different
195+
// bech32 id as the first CLI argument to point at another deployment. Pragma's
196+
// addresses change between testnet iterations, so check their README
197+
// (https://github.com/astraly-labs/pragma-miden) if this feed stops resolving.
193198
let oracle_bech32 = std::env::args()
194199
.nth(1)
195-
.expect("Usage: oracle_data_query <ORACLE_BECH32_ID>");
200+
.unwrap_or_else(|| "mtst1apadf2szkxqkcyt7x2znuggv9qkhccam".to_string());
196201
let (_, oracle_account_id) = AccountId::from_bech32(&oracle_bech32).unwrap();
197202

198203
// BTC/USD is identified by the faucet ID pair `1:0` (prefix 1, suffix 0).
@@ -322,16 +327,19 @@ The import `miden::tx` contains the `tx::execute_foreign_procedure` which we wil
322327
#### Here's a breakdown of what the `get_price` procedure does:
323328

324329
1. Pushes the 16 foreign procedure inputs that `tx::execute_foreign_procedure` requires. The first four are the arguments to `get_median` — the BTC/USD faucet ID prefix `1`, suffix `0`, an `amount` of `0`, and a trailing `0` — and the remaining twelve are zero padding.
325-
2. Pushes `0xd1aa2a8b38ccf58f37bb7aa490a8154c1cf89c537144ab23bd1111f13e5a28e8` onto the stack, which is the procedure root of the `get_median` procedure in the oracle.
330+
2. Pushes `0xaa3a12d4e9de2dad37c50dba93809b9c17226d512e642d3d620c77088a85da71` onto the stack, which is the procedure root of the `get_median` procedure in the oracle.
326331
3. Pushes the Pragma oracle account ID prefix and suffix.
327332
4. Calls `tx::execute_foreign_procedure`, which invokes the `get_median` procedure via foreign procedure invocation. `get_median` returns `[is_tracked, median_price, amount]` on the stack.
328333

329334
Inside of the `masm/accounts/` directory, create the `oracle_reader.masm` file:
330335

331336
```masm
332337
# The oracle account ID, procedure hash, and pair ID below reference
333-
# Pragma's testnet deployment (https://github.com/astraly-labs/pragma-miden).
334-
# If Pragma redeploys their oracle, these values must be updated.
338+
# Pragma's Miden v0.15 testnet deployment (https://github.com/astraly-labs/pragma-miden).
339+
# Pragma's addresses change between testnet iterations (their README is the
340+
# source of truth), so if the oracle is redeployed these values must be updated:
341+
# the oracle account id, the `get_median` procedure root, and (if a different
342+
# feed) the faucet pair id.
335343
336344
use miden::protocol::tx
337345
@@ -349,11 +357,11 @@ pub proc get_price
349357
# => [pair_prefix, pair_suffix, amount, 0, PAD(12)]
350358
351359
# This is the procedure root of the `get_median` procedure
352-
push.0xd1aa2a8b38ccf58f37bb7aa490a8154c1cf89c537144ab23bd1111f13e5a28e8
360+
push.0xaa3a12d4e9de2dad37c50dba93809b9c17226d512e642d3d620c77088a85da71
353361
# => [GET_MEDIAN_HASH, FOREIGN_INPUTS(16)]
354362
355363
# The Pragma oracle account id: prefix then suffix, leaving suffix on top
356-
push.17041133956008732928.1562038061251555584
364+
push.8850886096234572817.9093477099503364096
357365
# => [oracle_id_suffix, oracle_id_prefix, GET_MEDIAN_HASH, FOREIGN_INPUTS(16)]
358366
359367
exec.tx::execute_foreign_procedure
@@ -393,11 +401,11 @@ cargo run --release
393401
The output of our program will look something like this:
394402

395403
```text
396-
Latest block: 806773
397-
Oracle accountId prefix: V0(AccountIdPrefixV0 { prefix: 17041133956008732928 }) suffix: 1562038061251555584
398-
Stack state before step 11449:
404+
Latest block: 489876
405+
Oracle accountId prefix: V1(AccountIdPrefixV1 { prefix: 8850886096234572817 }) suffix: 9093477099503364096
406+
Stack state before step 6324:
399407
├── 0: 1
400-
├── 1: 76307450000
408+
├── 1: 6439689500000
401409
├── 2: 0
402410
├── 3: 0
403411
├── 4: 0
@@ -429,21 +437,21 @@ Stack state before step 11449:
429437
├── 30: 0
430438
└── 31: 0
431439
432-
View transaction on MidenScan: https://testnet.midenscan.com/tx/0x28dbb2ea1270884701e8f4875032db675ab9dfff13c3caaa5e53adfcf56e383b
440+
View transaction on MidenScan: https://testnet.midenscan.com/tx/0xbf6048faa60c72c43ab5645d937d02d0d768ee4fdad3c64b736789fdf0ef6a44
433441
```
434442

435-
The `get_median` procedure leaves three values on the stack. Index `0` holds `is_tracked``1` when Pragma tracks the requested pair. Index `1` holds the median price; in the output above it is `76307450000`, which is `$76307.45` once the 6 decimal places are applied. Index `2` holds the `amount` value that was passed into the call. Pragma publishes several price feeds on testnet; this tutorial reads the `BTC/USD` feed.
443+
The `get_median` procedure leaves three values on the stack. Index `0` holds `is_tracked``1` when Pragma tracks the requested pair. Index `1` holds the median price as a raw fixed-point integer; in the output above it is `6439689500000`. The number of decimal places is defined by Pragma's feed (BTC/USD is published with 8 decimals), so this reading is about `$64,396.89`. Index `2` holds the `amount` value that was passed into the call. Pragma publishes several price feeds on testnet; this tutorial reads the `BTC/USD` feed.
436444

437445
### Running the tutorial
438446

439447
To run this tutorial end-to-end, navigate to the `rust-client` directory in the [miden-tutorials](https://github.com/0xMiden/miden-tutorials/) repository and run:
440448

441449
```bash
442450
cd rust-client
443-
cargo run --release --bin oracle_data_query -- <ORACLE_BECH32_ID>
451+
cargo run --release --bin oracle_data_query
444452
```
445453

446-
where `<ORACLE_BECH32_ID>` is Pragma's deployed oracle account ID on testnet.
454+
This defaults to Pragma's current testnet oracle. To read from a different deployment, pass its bech32 account ID as an argument: `cargo run --release --bin oracle_data_query -- <ORACLE_BECH32_ID>`.
447455

448456
### Continue learning
449457

masm/accounts/oracle_reader.masm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# The oracle account ID, procedure hash, and pair ID below reference
2-
# Pragma's testnet deployment (https://github.com/astraly-labs/pragma-miden).
3-
# If Pragma redeploys their oracle, these values must be updated.
2+
# Pragma's Miden v0.15 testnet deployment (https://github.com/astraly-labs/pragma-miden).
3+
# Pragma's addresses change between testnet iterations (their README is the
4+
# source of truth), so if the oracle is redeployed these values must be updated:
5+
# the oracle account id, the `get_median` procedure root, and (if a different
6+
# feed) the faucet pair id.
47

58
use miden::protocol::tx
69

@@ -18,11 +21,11 @@ pub proc get_price
1821
# => [pair_prefix, pair_suffix, amount, 0, PAD(12)]
1922

2023
# This is the procedure root of the `get_median` procedure
21-
push.0xd1aa2a8b38ccf58f37bb7aa490a8154c1cf89c537144ab23bd1111f13e5a28e8
24+
push.0xaa3a12d4e9de2dad37c50dba93809b9c17226d512e642d3d620c77088a85da71
2225
# => [GET_MEDIAN_HASH, FOREIGN_INPUTS(16)]
2326

2427
# The Pragma oracle account id: prefix then suffix, leaving suffix on top
25-
push.17041133956008732928.1562038061251555584
28+
push.8850886096234572817.9093477099503364096
2629
# => [oracle_id_suffix, oracle_id_prefix, GET_MEDIAN_HASH, FOREIGN_INPUTS(16)]
2730

2831
exec.tx::execute_foreign_procedure

rust-client/src/bin/oracle_data_query.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub async fn get_oracle_foreign_accounts(
5757
let publisher_word = storage
5858
.get_map_item(&publishers_slot, key)
5959
.expect("publisher entry missing from oracle storage");
60+
// The publisher id word is laid out as [prefix, suffix, 0, 0].
6061
AccountId::new_unchecked([publisher_word[0], publisher_word[1]])
6162
})
6263
.collect();
@@ -136,9 +137,13 @@ async fn main() -> Result<(), ClientError> {
136137
// -------------------------------------------------------------------------
137138
// Get all foreign accounts for oracle data
138139
// -------------------------------------------------------------------------
140+
// Defaults to Pragma's current Miden v0.15 testnet oracle; pass a different
141+
// bech32 id as the first CLI argument to point at another deployment. Pragma's
142+
// addresses change between testnet iterations, so check their README
143+
// (https://github.com/astraly-labs/pragma-miden) if this feed stops resolving.
139144
let oracle_bech32 = std::env::args()
140145
.nth(1)
141-
.expect("Usage: oracle_data_query <ORACLE_BECH32_ID>");
146+
.unwrap_or_else(|| "mtst1apadf2szkxqkcyt7x2znuggv9qkhccam".to_string());
142147
let (_, oracle_account_id) = AccountId::from_bech32(&oracle_bech32).unwrap();
143148

144149
// BTC/USD is identified by the faucet ID pair `1:0` (prefix 1, suffix 0).

0 commit comments

Comments
 (0)