diff --git a/docs/src/rust-client/oracle_tutorial.md b/docs/src/rust-client/oracle_tutorial.md index 865d99b..27181e2 100644 --- a/docs/src/rust-client/oracle_tutorial.md +++ b/docs/src/rust-client/oracle_tutorial.md @@ -57,9 +57,7 @@ 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, @@ -67,12 +65,12 @@ use miden_client::{ 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 @@ -149,21 +147,6 @@ pub async fn get_oracle_foreign_accounts( Ok(foreign_accounts) } -fn create_library( - library_path: &str, - source_code: &str, -) -> Result, Box> { - 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> { // ------------------------------------------------------------------------- @@ -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, @@ -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() @@ -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) \ No newline at end of file diff --git a/rust-client/src/bin/oracle_data_query.rs b/rust-client/src/bin/oracle_data_query.rs index 2847274..8617a6c 100644 --- a/rust-client/src/bin/oracle_data_query.rs +++ b/rust-client/src/bin/oracle_data_query.rs @@ -3,9 +3,7 @@ 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, @@ -13,12 +11,12 @@ use miden_client::{ 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 @@ -95,21 +93,6 @@ pub async fn get_oracle_foreign_accounts( Ok(foreign_accounts) } -fn create_library( - library_path: &str, - source_code: &str, -) -> Result, Box> { - 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> { // ------------------------------------------------------------------------- @@ -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, @@ -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()