11---
2- title : ' Foreign Procedure Invocation'
2+ title : " Foreign Procedure Invocation"
33sidebar_position : 7
44---
55
@@ -76,9 +76,9 @@ This tutorial assumes you have a basic understanding of Miden assembly and compl
7676Add the following code to the ` app/page.tsx ` file. This code defines the main page of our web application:
7777
7878``` tsx
79- ' use client' ;
80- import { useState } from ' react' ;
81- import { foreignProcedureInvocation } from ' ../lib/foreignProcedureInvocation' ;
79+ " use client" ;
80+ import { useState } from " react" ;
81+ import { foreignProcedureInvocation } from " ../lib/foreignProcedureInvocation" ;
8282
8383export default function Home() {
8484 const [isFPIRunning, setIsFPIRunning] = useState (false );
@@ -101,8 +101,8 @@ export default function Home() {
101101 className = " w-full px-6 py-3 text-lg cursor-pointer bg-transparent border-2 border-orange-600 text-white rounded-lg transition-all hover:bg-orange-600 hover:text-white"
102102 >
103103 { isFPIRunning
104- ? ' Working...'
105- : ' Foreign Procedure Invocation Tutorial' }
104+ ? " Working..."
105+ : " Foreign Procedure Invocation Tutorial" }
106106 </button >
107107 </div >
108108 </div >
@@ -125,8 +125,8 @@ Copy and paste the following code into the `lib/foreignProcedureInvocation.ts` f
125125``` ts
126126// lib/foreignProcedureInvocation.ts
127127export async function foreignProcedureInvocation(): Promise <void > {
128- if (typeof window === ' undefined' ) {
129- console .warn (' foreignProcedureInvocation() can only run in the browser' );
128+ if (typeof window === " undefined" ) {
129+ console .warn (" foreignProcedureInvocation() can only run in the browser" );
130130 return ;
131131 }
132132
@@ -144,16 +144,16 @@ export async function foreignProcedureInvocation(): Promise<void> {
144144 AccountStorageRequirements,
145145 WebClient,
146146 AccountStorageMode,
147- } = await import (' @demox-labs/miden-sdk' );
147+ } = await import (" @demox-labs/miden-sdk" );
148148
149- const nodeEndpoint = ' https://rpc.testnet.miden.io' ;
149+ const nodeEndpoint = " https://rpc.testnet.miden.io" ;
150150 const client = await WebClient .createClient (nodeEndpoint );
151- console .log (' Current block number: ' , (await client .syncState ()).blockNum ());
151+ console .log (" Current block number: " , (await client .syncState ()).blockNum ());
152152
153153 // -------------------------------------------------------------------------
154154 // STEP 1: Create the Count Reader Contract
155155 // -------------------------------------------------------------------------
156- console .log (' \n [STEP 1] Creating count reader contract.' );
156+ console .log (" \n [STEP 1] Creating count reader contract." );
157157
158158 // Count reader contract code in Miden Assembly (exactly from count_reader.masm)
159159 const countReaderCode = `
@@ -166,7 +166,7 @@ export async function foreignProcedureInvocation(): Promise<void> {
166166 export.copy_count
167167 exec.tx::execute_foreign_procedure
168168 # => [count]
169-
169+
170170 push.0
171171 # [index, count]
172172
@@ -204,9 +204,9 @@ export async function foreignProcedureInvocation(): Promise<void> {
204204 await client .syncState ();
205205
206206 // Create the count reader contract account (using available WebClient API)
207- console .log (' Creating count reader contract account...' );
207+ console .log (" Creating count reader contract account..." );
208208 console .log (
209- ' Count reader contract ID:' ,
209+ " Count reader contract ID:" ,
210210 countReaderContract .account .id ().toString (),
211211 );
212212
@@ -215,11 +215,11 @@ export async function foreignProcedureInvocation(): Promise<void> {
215215 // -------------------------------------------------------------------------
216216 // STEP 2: Build & Get State of the Counter Contract
217217 // -------------------------------------------------------------------------
218- console .log (' \n [STEP 2] Building counter contract from public state' );
218+ console .log (" \n [STEP 2] Building counter contract from public state" );
219219
220220 // Define the Counter Contract account id from counter contract deploy (same as Rust)
221221 const counterContractId = AccountId .fromHex (
222- ' 0xe59d8cd3c9ff2a0055da0b83ed6432' ,
222+ " 0xe59d8cd3c9ff2a0055da0b83ed6432" ,
223223 );
224224
225225 // Import the counter contract
@@ -233,15 +233,15 @@ export async function foreignProcedureInvocation(): Promise<void> {
233233 }
234234 }
235235 console .log (
236- ' Account storage slot 0:' ,
236+ " Account storage slot 0:" ,
237237 counterContractAccount .storage ().getItem (0 )?.toHex (),
238238 );
239239
240240 // -------------------------------------------------------------------------
241241 // STEP 3: Call the Counter Contract via Foreign Procedure Invocation (FPI)
242242 // -------------------------------------------------------------------------
243243 console .log (
244- ' \n [STEP 3] Call counter contract with FPI from count reader contract' ,
244+ " \n [STEP 3] Call counter contract with FPI from count reader contract" ,
245245 );
246246
247247 // Counter contract code (exactly from counter.masm)
@@ -299,7 +299,7 @@ export async function foreignProcedureInvocation(): Promise<void> {
299299 ).withSupportsAllTypes ();
300300
301301 const getCountProcHash =
302- counterContractComponent .getProcedureHash (' get_count' );
302+ counterContractComponent .getProcedureHash (" get_count" );
303303
304304 // Build the script that calls the count reader contract (exactly from reader_script.masm with replacements)
305305 const fpiScriptCode = `
@@ -327,11 +327,11 @@ export async function foreignProcedureInvocation(): Promise<void> {
327327
328328 // Create the library for the count reader contract
329329 const countReaderLib = builder .buildLibrary (
330- ' external_contract::count_reader_contract' ,
330+ " external_contract::count_reader_contract" ,
331331 countReaderCode ,
332332 );
333333 builder .linkDynamicLibrary (countReaderLib );
334-
334+ .
335335 // Compile the transaction script with the count reader library
336336 const txScript = builder .compileTxScript (fpiScriptCode );
337337
@@ -355,7 +355,7 @@ export async function foreignProcedureInvocation(): Promise<void> {
355355 );
356356
357357 console .log (
358- ' View transaction on MidenScan: https://testnet.midenscan.com/tx/' +
358+ " View transaction on MidenScan: https://testnet.midenscan.com/tx/" +
359359 txResult .toHex (),
360360 );
361361
@@ -366,15 +366,15 @@ export async function foreignProcedureInvocation(): Promise<void> {
366366 counterContractAccount .id (),
367367 );
368368 console .log (
369- ' counter contract storage:' ,
369+ " counter contract storage:" ,
370370 updatedCounterContract ?.storage ().getItem (0 )?.toHex (),
371371 );
372372
373373 const updatedCountReaderContract = await client .getAccount (
374374 countReaderContract .account .id (),
375375 );
376376 console .log (
377- ' count reader contract storage:' ,
377+ " count reader contract storage:" ,
378378 updatedCountReaderContract ?.storage ().getItem (0 )?.toHex (),
379379 );
380380
@@ -383,19 +383,19 @@ export async function foreignProcedureInvocation(): Promise<void> {
383383 if (countReaderStorage ) {
384384 const countValue = Number (
385385 BigInt (
386- ' 0x ' +
386+ " 0x " +
387387 countReaderStorage
388388 .toHex ()
389389 .slice (- 16 )
390390 .match (/ .. / g )!
391391 .reverse ()
392- .join (' ' ),
392+ .join (" " ),
393393 ),
394394 );
395- console .log (' Count copied via Foreign Procedure Invocation:' , countValue );
395+ console .log (" Count copied via Foreign Procedure Invocation:" , countValue );
396396 }
397397
398- console .log (' \n Foreign Procedure Invocation Transaction completed!' );
398+ console .log (" \n Foreign Procedure Invocation Transaction completed!" );
399399}
400400```
401401
@@ -462,7 +462,7 @@ use.std::sys
462462export.copy_count
463463 exec.tx::execute_foreign_procedure
464464 # => [count]
465-
465+
466466 push.0
467467 # [index, count]
468468
@@ -526,7 +526,7 @@ This script:
526526In the WebClient, we get the procedure hash using the [ ` getProcedureHash ` ] ( https://github.com/0xMiden/miden-tutorials/blob/b281dea26ab0946e1c0aa68d4ea30e15765d456b/web-client/lib/foreignProcedureInvocation.ts#L178 ) method:
527527
528528``` ts
529- let getCountProcHash = counterContractComponent .getProcedureHash (' get_count' );
529+ let getCountProcHash = counterContractComponent .getProcedureHash (" get_count" );
530530```
531531
532532### Foreign Accounts
@@ -551,7 +551,7 @@ We create a library for the count reader contract so our transaction script can
551551
552552``` ts
553553const countReaderLib = builder .buildLibrary (
554- ' external_contract::count_reader_contract' ,
554+ " external_contract::count_reader_contract" ,
555555 countReaderCode ,
556556);
557557builder .linkDynamicLibrary (countReaderLib );
@@ -590,7 +590,7 @@ The Miden webclient stores account and note data in the browser. If you get erro
590590 await indexedDB .deleteDatabase (db .name );
591591 console .log (` Deleted database: ${ db .name } ` );
592592 }
593- console .log (' All databases deleted.' );
593+ console .log (" All databases deleted." );
594594})();
595595```
596596
0 commit comments