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
333 changes: 276 additions & 57 deletions docs/Cargo.lock

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions docs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ miden-client = { version = "0.14", features = ["testing", "tonic"] }
miden-client-sqlite-store = { version = "0.14", package = "miden-client-sqlite-store" }
miden-protocol = { version = "0.14" }
rand = { version = "0.9" }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
tokio = { version = "1.48", features = ["rt-multi-thread", "net", "macros", "fs"] }
rand_chacha = "0.9.0"
1 change: 1 addition & 0 deletions docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
#![doc = include_str!("rust-client/creating_notes_in_masm_tutorial.md")]
#![doc = include_str!("rust-client/delegated_proving_tutorial.md")]
#![doc = include_str!("rust-client/network_transactions_tutorial.md")]
#![doc = include_str!("rust-client/oracle_tutorial.md")]
163 changes: 54 additions & 109 deletions docs/src/rust-client/counter_contract_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ miden-client = { version = "0.14", features = ["testing", "tonic"] }
miden-client-sqlite-store = { version = "0.14", package = "miden-client-sqlite-store" }
miden-protocol = { version = "0.14" }
rand = { version = "0.9" }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
tokio = { version = "1.46", features = ["rt-multi-thread", "net", "macros", "fs"] }
rand_chacha = "0.9.0"
```

### Set up your `src/main.rs` file
Expand All @@ -54,50 +51,23 @@ In the previous section, we explained how to instantiate the Miden client. We ca
Copy and paste the following code into your `src/main.rs` file:

```rust no_run
use miden_client::auth::NoAuth;
use miden_client::transaction::TransactionKernel;
use rand::RngCore;
use std::{fs, path::Path, sync::Arc};
use std::{path::PathBuf, sync::Arc};

use miden_client::{
address::NetworkId,
assembly::{
Assembler,
CodeBuilder,
DefaultSourceManager,
Module,
ModuleKind,
Path as AssemblyPath,
account::{
component::AccountComponentMetadata, AccountBuilder, AccountComponent, AccountStorageMode,
AccountType, StorageSlot, StorageSlotName,
},
address::NetworkId,
auth::NoAuth,
builder::ClientBuilder,
keystore::FilesystemKeyStore,
rpc::{Endpoint, GrpcClient},
transaction::TransactionRequestBuilder,
ClientError,
ClientError, Word,
};
use miden_client_sqlite_store::ClientBuilderSqliteExt;
use miden_client::{
account::{
component::AccountComponentMetadata, AccountBuilder, AccountComponent, AccountStorageMode,
AccountType, StorageSlot, StorageSlotName,
},
Word,
};

fn create_library(
library_path: &str,
source_code: &str,
) -> Result<std::sync::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 All @@ -107,10 +77,10 @@ async fn main() -> Result<(), ClientError> {
let rpc_client = Arc::new(GrpcClient::new(&endpoint, timeout_ms));

// Initialize keystore
let keystore_path = std::path::PathBuf::from("./keystore");
let keystore_path = PathBuf::from("./keystore");
let keystore = Arc::new(FilesystemKeyStore::new(keystore_path).unwrap());

let store_path = std::path::PathBuf::from("./store.sqlite3");
let store_path = PathBuf::from("./store.sqlite3");

let mut client = ClientBuilder::new()
.rpc(rpc_client)
Expand Down Expand Up @@ -256,15 +226,20 @@ To build the counter contract copy and paste the following code at the end of yo
// -------------------------------------------------------------------------
println!("\n[STEP 1] Creating counter contract.");

// Load the MASM file for the counter contract
let counter_path = Path::new("../masm/accounts/counter.masm");
let counter_code = fs::read_to_string(counter_path).unwrap();
// Load the MASM file for the counter contract. `include_str!` resolves at
// compile time relative to this source file, so the binary is independent of
// the working directory it is run from.
let counter_code = include_str!("../masm/accounts/counter.masm");

// Compile the account code into `AccountComponent` with one storage slot
// Compile the account code into `AccountComponent` with one storage slot.
// Using `client.code_builder()` makes the assembler share the client's
// persisted source manager, which keeps debug spans coherent for any
// libraries that link against this code later (see miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

let counter_slot_name =
StorageSlotName::new("miden::tutorials::counter").expect("valid slot name");
let component_code = CodeBuilder::new()
.compile_component_code("external_contract::counter_contract", &counter_code)
let component_code = client
.code_builder()
.compile_component_code("external_contract::counter_contract", counter_code)
.unwrap();
let counter_component = AccountComponent::new(
component_code,
Expand Down Expand Up @@ -324,21 +299,17 @@ Paste the following code at the end of your `src/main.rs` file:
println!("\n[STEP 2] Call Counter Contract With Script");

// Load the MASM script referencing the increment procedure
let script_path = Path::new("../masm/scripts/counter_script.masm");
let script_code = fs::read_to_string(script_path).unwrap();

// Create a library from the counter contract code
let account_component_lib = create_library(
"external_contract::counter_contract",
&counter_code,
)
.unwrap();
let script_code = include_str!("../masm/scripts/counter_script.masm");

// Compile the script with the counter contract code linked as a dynamic
// module on the same `CodeBuilder`. This shares the client's source
// manager between parsing and assembly, which is what miden-vm#2778
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

// requires to avoid panics when debug spans are reported.
let tx_script = client
.code_builder()
.with_dynamically_linked_library(&account_component_lib)
.with_linked_module("external_contract::counter_contract", counter_code)
.unwrap()
.compile_tx_script(&script_code)
.compile_tx_script(script_code)
.unwrap();

// Build a transaction request with the custom script
Expand Down Expand Up @@ -384,50 +355,23 @@ println!(
The final `src/main.rs` file should look like this:

```rust no_run
use miden_client::auth::NoAuth;
use miden_client::transaction::TransactionKernel;
use rand::RngCore;
use std::{fs, path::Path, sync::Arc};
use std::{path::PathBuf, sync::Arc};

use miden_client::{
address::NetworkId,
assembly::{
Assembler,
CodeBuilder,
DefaultSourceManager,
Module,
ModuleKind,
Path as AssemblyPath,
account::{
component::AccountComponentMetadata, AccountBuilder, AccountComponent, AccountStorageMode,
AccountType, StorageSlot, StorageSlotName,
},
address::NetworkId,
auth::NoAuth,
builder::ClientBuilder,
keystore::FilesystemKeyStore,
rpc::{Endpoint, GrpcClient},
transaction::TransactionRequestBuilder,
ClientError,
ClientError, Word,
};
use miden_client_sqlite_store::ClientBuilderSqliteExt;
use miden_client::{
account::{
component::AccountComponentMetadata, AccountBuilder, AccountComponent, AccountStorageMode,
AccountType, StorageSlot, StorageSlotName,
},
Word,
};

fn create_library(
library_path: &str,
source_code: &str,
) -> Result<std::sync::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 All @@ -437,10 +381,10 @@ async fn main() -> Result<(), ClientError> {
let rpc_client = Arc::new(GrpcClient::new(&endpoint, timeout_ms));

// Initialize keystore
let keystore_path = std::path::PathBuf::from("./keystore");
let keystore_path = PathBuf::from("./keystore");
let keystore = Arc::new(FilesystemKeyStore::new(keystore_path).unwrap());

let store_path = std::path::PathBuf::from("./store.sqlite3");
let store_path = PathBuf::from("./store.sqlite3");

let mut client = ClientBuilder::new()
.rpc(rpc_client)
Expand All @@ -458,15 +402,20 @@ async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
println!("\n[STEP 1] Creating counter contract.");

// Load the MASM file for the counter contract
let counter_path = Path::new("../masm/accounts/counter.masm");
let counter_code = fs::read_to_string(counter_path).unwrap();
// Load the MASM file for the counter contract. `include_str!` resolves at
// compile time relative to this source file, so the binary is independent
// of the working directory it is run from.
let counter_code = include_str!("../masm/accounts/counter.masm");

// Compile the account code into `AccountComponent` with one storage slot
// Compile the account code into `AccountComponent` with one storage slot.
// Using `client.code_builder()` makes the assembler share the client's
// persisted source manager, which keeps debug spans coherent for any
// libraries that link against this code later (see miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

let counter_slot_name =
StorageSlotName::new("miden::tutorials::counter").expect("valid slot name");
let component_code = CodeBuilder::new()
.compile_component_code("external_contract::counter_contract", &counter_code)
let component_code = client
.code_builder()
.compile_component_code("external_contract::counter_contract", counter_code)
.unwrap();
let counter_component = AccountComponent::new(
component_code,
Expand Down Expand Up @@ -503,21 +452,17 @@ async fn main() -> Result<(), ClientError> {
println!("\n[STEP 2] Call Counter Contract With Script");

// Load the MASM script referencing the increment procedure
let script_path = Path::new("../masm/scripts/counter_script.masm");
let script_code = fs::read_to_string(script_path).unwrap();

// Create a library from the counter contract code
let account_component_lib = create_library(
"external_contract::counter_contract",
&counter_code,
)
.unwrap();
let script_code = include_str!("../masm/scripts/counter_script.masm");

// Compile the script with the counter contract code linked as a dynamic
// module on the same `CodeBuilder`. This shares the client's source
// manager between parsing and assembly, which is what miden-vm#2778
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

// requires to avoid panics when debug spans are reported.
let tx_script = client
.code_builder()
.with_dynamically_linked_library(&account_component_lib)
.with_linked_module("external_contract::counter_contract", counter_code)
.unwrap()
.compile_tx_script(&script_code)
.compile_tx_script(script_code)
.unwrap();

// Build a transaction request with the custom script
Expand Down
15 changes: 6 additions & 9 deletions docs/src/rust-client/create_deploy_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ miden-client = { version = "0.14", features = ["testing", "tonic"] }
miden-client-sqlite-store = { version = "0.14", package = "miden-client-sqlite-store" }
miden-protocol = { version = "0.14" }
rand = { version = "0.9" }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
tokio = { version = "1.46", features = ["rt-multi-thread", "net", "macros", "fs"] }
rand_chacha = "0.9.0"
```

## Step 2: Initialize the client
Expand All @@ -72,7 +69,7 @@ Copy and paste the following code into your `src/main.rs` file.
```rust no_run
use miden_client::auth::{AuthSchemeId, AuthSingleSig};
use rand::RngCore;
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};
use tokio::time::Duration;

use miden_client::{
Expand Down Expand Up @@ -105,10 +102,10 @@ async fn main() -> Result<(), ClientError> {
let rpc_client = Arc::new(GrpcClient::new(&endpoint, timeout_ms));

// Initialize keystore
let keystore_path = std::path::PathBuf::from("./keystore");
let keystore_path = PathBuf::from("./keystore");
let keystore = Arc::new(FilesystemKeyStore::new(keystore_path).unwrap());

let store_path = std::path::PathBuf::from("./store.sqlite3");
let store_path = PathBuf::from("./store.sqlite3");

let mut client = ClientBuilder::new()
.rpc(rpc_client)
Expand Down Expand Up @@ -242,7 +239,7 @@ Your updated `main()` function in `src/main.rs` should look like this:
```rust no_run
use miden_client::auth::{AuthSchemeId, AuthSingleSig};
use rand::RngCore;
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};
use tokio::time::Duration;

use miden_client::{
Expand Down Expand Up @@ -275,10 +272,10 @@ async fn main() -> Result<(), ClientError> {
let rpc_client = Arc::new(GrpcClient::new(&endpoint, timeout_ms));

// Initialize keystore
let keystore_path = std::path::PathBuf::from("./keystore");
let keystore_path = PathBuf::from("./keystore");
let keystore = Arc::new(FilesystemKeyStore::new(keystore_path).unwrap());

let store_path = std::path::PathBuf::from("./store.sqlite3");
let store_path = PathBuf::from("./store.sqlite3");

let mut client = ClientBuilder::new()
.rpc(rpc_client)
Expand Down
Loading
Loading