Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 11 additions & 32 deletions docs/src/rust-client/oracle_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@ use miden_client::{
component::AccountComponentMetadata, AccountBuilder, AccountComponent, AccountId,
AccountType, StorageMapKey, StorageSlot, StorageSlotName,
},
assembly::{
CodeBuilder, DefaultSourceManager, Module, ModuleKind, Path as AssemblyPath,
},
assembly::CodeBuilder,
auth::NoAuth,
builder::ClientBuilder,
keystore::FilesystemKeyStore,
rpc::{
domain::account::AccountStorageRequirements,
Endpoint, GrpcClient,
},
transaction::{ForeignAccount, TransactionKernel, TransactionRequestBuilder},
transaction::{ForeignAccount, TransactionRequestBuilder},
Client, ClientError, Felt, Word, ZERO,
};
use miden_client_sqlite_store::ClientBuilderSqliteExt;
use rand::RngCore;
use std::{fs, path::Path, sync::Arc};
use std::sync::Arc;

/// Import the oracle + its publishers and return the ForeignAccount list
/// Due to Pragma's decentralized oracle architecture, we need to get the
Expand Down Expand Up @@ -149,21 +147,6 @@ pub async fn get_oracle_foreign_accounts(
Ok(foreign_accounts)
}

fn create_library(
library_path: &str,
source_code: &str,
) -> Result<Arc<miden_client::assembly::Library>, Box<dyn std::error::Error>> {
let source_manager = Arc::new(DefaultSourceManager::default());
let assembler = TransactionKernel::assembler_with_source_manager(source_manager.clone());
let module = Module::parser(ModuleKind::Library).parse_str(
AssemblyPath::new(library_path),
source_code,
source_manager,
)?;
let library = assembler.assemble_library([module])?;
Ok(library)
}

#[tokio::main]
async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -218,13 +201,14 @@ async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
// Create Oracle Reader contract
// -------------------------------------------------------------------------
let contract_code =
fs::read_to_string(Path::new("../masm/accounts/oracle_reader.masm")).unwrap();
// Compile-time inclusion keeps the example independent of the directory
// from which the binary is launched.
let contract_code = include_str!("../masm/accounts/oracle_reader.masm");

let contract_slot_name =
StorageSlotName::new("miden::tutorials::oracle_reader").expect("valid slot name");
let contract_component_code = CodeBuilder::new()
.compile_component_code("external_contract::oracle_reader", &contract_code)
.compile_component_code("external_contract::oracle_reader", contract_code)
.unwrap();
let contract_component = AccountComponent::new(
contract_component_code,
Expand Down Expand Up @@ -254,18 +238,13 @@ async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
// Build the script that calls our `get_price` procedure
// -------------------------------------------------------------------------
let script_path = Path::new("../masm/scripts/oracle_reader_script.masm");
let script_code = fs::read_to_string(script_path).unwrap();

let library_path = "external_contract::oracle_reader";
let account_component_lib =
create_library(library_path, &contract_code).unwrap();
let script_code = include_str!("../masm/scripts/oracle_reader_script.masm");

let tx_script = client
.code_builder()
.with_dynamically_linked_library(&account_component_lib)
.with_linked_module("external_contract::oracle_reader", contract_code)
.unwrap()
.compile_tx_script(&script_code)
.compile_tx_script(script_code)
.unwrap();

let tx_increment_request = TransactionRequestBuilder::new()
Expand Down Expand Up @@ -455,4 +434,4 @@ This defaults to Pragma's current testnet oracle. To read from a different deplo

### Continue learning

Next tutorial: [How to Use Unauthenticated Notes](./unauthenticated_note_how_to.md)
Next tutorial: [How to Use Unauthenticated Notes](./unauthenticated_note_how_to.md)
41 changes: 10 additions & 31 deletions rust-client/src/bin/oracle_data_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ use miden_client::{
component::AccountComponentMetadata, AccountBuilder, AccountComponent, AccountId,
AccountType, StorageMapKey, StorageSlot, StorageSlotName,
},
assembly::{
CodeBuilder, DefaultSourceManager, Module, ModuleKind, Path as AssemblyPath,
},
assembly::CodeBuilder,
auth::NoAuth,
builder::ClientBuilder,
keystore::FilesystemKeyStore,
rpc::{
domain::account::AccountStorageRequirements,
Endpoint, GrpcClient,
},
transaction::{ForeignAccount, TransactionKernel, TransactionRequestBuilder},
transaction::{ForeignAccount, TransactionRequestBuilder},
Client, ClientError, Felt, Word, ZERO,
};
use miden_client_sqlite_store::ClientBuilderSqliteExt;
use rand::RngCore;
use std::{fs, path::Path, sync::Arc};
use std::sync::Arc;

/// Import the oracle + its publishers and return the ForeignAccount list
/// Due to Pragma's decentralized oracle architecture, we need to get the
Expand Down Expand Up @@ -95,21 +93,6 @@ pub async fn get_oracle_foreign_accounts(
Ok(foreign_accounts)
}

fn create_library(
library_path: &str,
source_code: &str,
) -> Result<Arc<miden_client::assembly::Library>, Box<dyn std::error::Error>> {
let source_manager = Arc::new(DefaultSourceManager::default());
let assembler = TransactionKernel::assembler_with_source_manager(source_manager.clone());
let module = Module::parser(ModuleKind::Library).parse_str(
AssemblyPath::new(library_path),
source_code,
source_manager,
)?;
let library = assembler.assemble_library([module])?;
Ok(library)
}

#[tokio::main]
async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -164,13 +147,14 @@ async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
// Create Oracle Reader contract
// -------------------------------------------------------------------------
let contract_code =
fs::read_to_string(Path::new("../masm/accounts/oracle_reader.masm")).unwrap();
// Compile-time inclusion keeps the example independent of the directory
// from which the binary is launched.
let contract_code = include_str!("../../../masm/accounts/oracle_reader.masm");

let contract_slot_name =
StorageSlotName::new("miden::tutorials::oracle_reader").expect("valid slot name");
let contract_component_code = CodeBuilder::new()
.compile_component_code("external_contract::oracle_reader", &contract_code)
.compile_component_code("external_contract::oracle_reader", contract_code)
.unwrap();
let contract_component = AccountComponent::new(
contract_component_code,
Expand Down Expand Up @@ -200,18 +184,13 @@ async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
// Build the script that calls our `get_price` procedure
// -------------------------------------------------------------------------
let script_path = Path::new("../masm/scripts/oracle_reader_script.masm");
let script_code = fs::read_to_string(script_path).unwrap();

let library_path = "external_contract::oracle_reader";
let account_component_lib =
create_library(library_path, &contract_code).unwrap();
let script_code = include_str!("../../../masm/scripts/oracle_reader_script.masm");

let tx_script = client
.code_builder()
.with_dynamically_linked_library(&account_component_lib)
.with_linked_module("external_contract::oracle_reader", contract_code)
.unwrap()
.compile_tx_script(&script_code)
.compile_tx_script(script_code)
.unwrap();

let tx_increment_request = TransactionRequestBuilder::new()
Expand Down