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
9 changes: 5 additions & 4 deletions masm/scripts/reader_script.masm
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use external_contract::count_reader_contract
use external_contract::counter_contract
use miden::core::sys

begin
padw padw padw padw
# => [pad(16)]

push.{get_count_proc_hash}
# => [GET_COUNT_HASH, pad(16)]
procref.counter_contract::get_count
# => [GET_COUNT_ROOT, pad(16)]

push.{account_id_prefix}
# => [account_id_prefix, GET_COUNT_HASH, pad(16)]
# => [account_id_prefix, GET_COUNT_ROOT, pad(16)]

push.{account_id_suffix}
# => [account_id_suffix, account_id_prefix, GET_COUNT_HASH, pad(16)]
# => [account_id_suffix, account_id_prefix, GET_COUNT_ROOT, pad(16)]

call.count_reader_contract::copy_count
# => []
Expand Down
31 changes: 7 additions & 24 deletions rust-client/src/bin/counter_contract_fpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,14 @@ async fn main() -> Result<(), ClientError> {
// -------------------------------------------------------------------------
println!("\n[STEP 3] Call counter contract with FPI from count reader contract");

// Link the local counter module so the MASM script can resolve
// `procref.counter_contract::get_count` directly.
let counter_contract_code = include_str!("../../../masm/accounts/counter.masm");

// Compile the counter as a component (same path as the deploy binary) to get
// the correct procedure root that matches the on-chain MAST.
let counter_component_code = client
.code_builder()
.compile_component_code("external_contract::counter_contract", counter_contract_code)
.unwrap();
let counter_component = AccountComponent::new(
counter_component_code,
vec![],
AccountComponentMetadata::new("external_contract::counter_contract"),
)
.unwrap();

let get_count_root = counter_component
.component_code()
.as_library()
.get_procedure_root_by_path("external_contract::counter_contract::get_count")
.expect("get_count export not found");
let get_count_hash = format!("{}", get_count_root);

println!("get_count hash: {:?}", get_count_hash);
println!("counter id prefix: {:?}", counter_contract_id.prefix());
println!("counter id suffix: {:?}", counter_contract_id.suffix());

let script_code = include_str!("../../../masm/scripts/reader_script.masm")
.replace("{get_count_proc_hash}", &get_count_hash)
.replace(
"{account_id_suffix}",
&counter_contract_id.suffix().as_canonical_u64().to_string(),
Expand All @@ -157,12 +137,15 @@ async fn main() -> Result<(), ClientError> {
&u64::from(counter_contract_id.prefix()).to_string(),
);

// Link the count reader contract code into the same `CodeBuilder` chain
// that compiles the script.
// Link both modules into the same `CodeBuilder` chain that compiles the
// script. This lets MASM resolve the `procref` without formatting a
// procedure digest into the source string at runtime.
let tx_script = client
.code_builder()
.with_linked_module("external_contract::count_reader_contract", count_reader_code)
.unwrap()
.with_linked_module("external_contract::counter_contract", counter_contract_code)
.unwrap()
.compile_tx_script(script_code.as_str())
.unwrap();

Expand Down