diff --git a/docs/src/miden-bank/04-note-scripts.md b/docs/src/miden-bank/04-note-scripts.md index 14a2d63..f362854 100644 --- a/docs/src/miden-bank/04-note-scripts.md +++ b/docs/src/miden-bank/04-note-scripts.md @@ -28,7 +28,7 @@ Part 3: Part 4: ┌──────────────────┐ ┌──────────────────┐ │ Bank (complete) │ │ Bank (complete) │ │ ─────────────────│ │ ─────────────────│ -│ + deposit() │ │ + deposit() │ +│ + bank_deposit() │ │ + bank_deposit() │ │ + withdraw() │ │ + withdraw() │ └──────────────────┘ └──────────────────┘ ▲ @@ -154,14 +154,14 @@ impl DepositNote { // Deposit each asset into the bank for asset in assets { - account.deposit(depositor, asset); + account.bank_deposit(depositor, asset); } } } ``` :::info Cross-Component Calls -The `#[account(bank_account::Bank)] pub struct Wallet;` declaration and the `account.deposit(...)` call use Miden's cross-component binding system. The `#[account(...)]` macro wraps the consuming account so the note can call the bank's `Bank` methods directly. We'll explain exactly how this works in [Part 5: Cross-Component Calls](./cross-component-calls). For now, just know that building `bank-account` first generates the WIT files that `deposit-note` binds against. +The `#[account(bank_account::Bank)] pub struct Wallet;` declaration and the `account.bank_deposit(...)` call use Miden's cross-component binding system. The `#[account(...)]` macro wraps the consuming account so the note can call the bank's `Bank` methods directly. We'll explain exactly how this works in [Part 5: Cross-Component Calls](./cross-component-calls). For now, just know that building `bank-account` first generates the WIT files that `deposit-note` binds against. ::: ### The #[note] and #[note_script] Attributes @@ -258,9 +258,9 @@ The Miden compiler prints non-fatal `ERROR` lines about `MAST` serialization on 3. Note script runs depositor = get_sender() → User's AccountId assets = get_assets() → [100 tokens] - account.deposit(depositor, 100 tokens) + account.bank_deposit(depositor, 100 tokens) -4. Bank's deposit() method executes +4. Bank's bank_deposit() method executes - Validates initialization and amount - Updates balance: balances[User] += 100 - Adds asset to vault @@ -546,7 +546,7 @@ impl DepositNote { // Deposit each asset into the bank for asset in assets { - account.deposit(depositor, asset); + account.bank_deposit(depositor, asset); } } } @@ -557,7 +557,7 @@ impl DepositNote { ## Key Takeaways 1. **`#[note]`** marks the struct and impl block, with **`#[note_script]`** on the entry point method `fn run(self, _arg: Word, account: &mut Wallet)` -2. **`#[account(bank_account::Bank)] pub struct Wallet;`** wraps the consuming account so the note can call the bank's methods via `account.deposit(...)` +2. **`#[account(bank_account::Bank)] pub struct Wallet;`** wraps the consuming account so the note can call the bank's methods via `account.bank_deposit(...)` 3. **`active_note::get_sender()`** returns who created the note 4. **`active_note::get_assets()`** returns assets attached to the note 5. **`active_note::get_storage()`** returns parameterized data @@ -573,4 +573,4 @@ See the complete note script implementations: ## Next Steps -Now that you understand note scripts, let's learn how they call account methods in [Part 5: Cross-Component Calls](./cross-component-calls). +Now that you understand note scripts, let's learn how they call account methods in [Part 5: Cross-Component Calls](./cross-component-calls). \ No newline at end of file diff --git a/docs/src/miden-bank/05-cross-component-calls.md b/docs/src/miden-bank/05-cross-component-calls.md index d8a6e39..3cf4095 100644 --- a/docs/src/miden-bank/05-cross-component-calls.md +++ b/docs/src/miden-bank/05-cross-component-calls.md @@ -19,7 +19,7 @@ By the end of this section, you will have: ## Building on Part 4 -In Part 4, you wrote `account.deposit(depositor, asset)` in the deposit note. But how does that call actually work? This part explains the binding system: +In Part 4, you wrote `account.bank_deposit(depositor, asset)` in the deposit note. But how does that call actually work? This part explains the binding system: ```text ┌────────────────────────────────────────────────────────────┐ @@ -28,7 +28,7 @@ In Part 4, you wrote `account.deposit(depositor, asset)` in the deposit note. Bu │ │ │ bank-account/ │ │ └── src/lib.rs miden build │ -│ fn deposit() ─────────────▶ generated-wit/ │ +│ fn bank_deposit() ─────────────▶ generated-wit/ │ │ fn withdraw() miden-bank-account.wit │ │ │ ┌───────────────────────────┐ │ @@ -37,7 +37,7 @@ In Part 4, you wrote `account.deposit(depositor, asset)` in the deposit note. Bu │ └── src/lib.rs │ │ │ #[account(bank_account::Bank)] │ │ │ pub struct Wallet; │ │ -│ account.deposit(...) ────────────▶ calls via binding│ +│ account.bank_deposit(...) ─────────▶ calls via binding│ │ │ └────────────────────────────────────────────────────────────┘ ``` @@ -90,7 +90,7 @@ For our bank: - `bank_account` - The package name (derived from `bank-account` with underscores) - `Bank` - The component trait whose methods are exposed on the wrapper -The macro reads the bank account's generated WIT and generates a `Wallet` type whose methods (`deposit`, `withdraw`, `initialize`, `get_depositor_balance`) call into the bank component across the component boundary. +The macro reads the bank account's generated WIT and generates a `Wallet` type whose methods (`bank_deposit`, `withdraw`, `initialize`, `get_depositor_balance`) call into the bank component across the component boundary. ## Calling Account Methods @@ -112,7 +112,7 @@ impl DepositNote { // Deposit each asset into the bank for asset in assets { - account.deposit(depositor, asset); + account.bank_deposit(depositor, asset); } } } @@ -191,7 +191,7 @@ trait Bank { // EXPORTED: Available through bindings fn initialize(&mut self); fn get_depositor_balance(&self, depositor: AccountId, asset: Asset) -> Felt; - fn deposit(&mut self, depositor: AccountId, deposit_asset: Asset); + fn bank_deposit(&mut self, depositor: AccountId, deposit_asset: Asset); fn withdraw(&mut self, withdraw_asset: Asset, serial_num: Word, tag: Felt, note_type: Felt); } ``` @@ -221,7 +221,7 @@ interface bank-account { initialize: func(); get-depositor-balance: func(depositor: account-id, asset: asset) -> felt; - deposit: func(depositor: account-id, deposit-asset: asset); + bank-deposit: func(depositor: account-id, deposit-asset: asset); withdraw: func(withdraw-asset: asset, serial-num: word, tag: felt, note-type: felt); } ``` @@ -265,7 +265,7 @@ miden-bank-account.wit -These files enable the deposit note's `#[account(bank_account::Bank)]` wrapper to call `account.deposit()`. +These files enable the deposit note's `#[account(bank_account::Bank)]` wrapper to call `account.bank_deposit()`. ## Common Issues @@ -285,7 +285,7 @@ error: cannot find module `bindings` ### "Method not found" Error ``` -error: no method named `deposit` found +error: no method named `bank_deposit` found ``` **Cause**: The method isn't declared on the `#[component] trait Bank`. Only trait methods are exported through bindings. diff --git a/examples/miden-bank/contracts/bank-account/src/lib.rs b/examples/miden-bank/contracts/bank-account/src/lib.rs index 2715184..a21fdf5 100644 --- a/examples/miden-bank/contracts/bank-account/src/lib.rs +++ b/examples/miden-bank/contracts/bank-account/src/lib.rs @@ -91,6 +91,9 @@ trait Bank { /// Deposit an asset into the bank for a specific depositor. /// + /// Named `bank_deposit` to avoid colliding with the built-in wallet `deposit` + /// method when FPI bindings are generated. + /// /// The asset is added to the bank's vault and the depositor's /// balance is updated in the mapping. /// @@ -103,7 +106,7 @@ trait Bank { /// Panics if the deposit amount exceeds `MAX_DEPOSIT_AMOUNT`. /// Panics if the resulting balance would exceed `MAX_BALANCE` (u64 overflow). /// Panics if the bank has not been initialized. - fn deposit(&mut self, depositor: AccountId, deposit_asset: Asset); + fn bank_deposit(&mut self, depositor: AccountId, deposit_asset: Asset); /// Withdraw assets back to the depositor. /// @@ -153,7 +156,7 @@ impl Bank for BankStorage { self.balances.get(key) } - fn deposit(&mut self, depositor: AccountId, deposit_asset: Asset) { + fn bank_deposit(&mut self, depositor: AccountId, deposit_asset: Asset) { // Ensure the bank is initialized before accepting deposits self.require_initialized(); @@ -220,7 +223,7 @@ impl Bank for BankStorage { // bound to the note metadata, so it cannot be spoofed by a malicious caller. let depositor = active_note::get_sender(); - // Verify this is a fungible asset — see `deposit()` for the rationale. + // Verify this is a fungible asset — see `bank_deposit()` for the rationale. assert!( withdraw_asset.value[1].as_canonical_u64() == 0, "Only fungible assets are supported" @@ -332,4 +335,4 @@ impl BankStorage { // Add the asset to the output note output_note::add_asset(*asset, note_idx); } -} +} \ No newline at end of file diff --git a/examples/miden-bank/contracts/deposit-note/src/lib.rs b/examples/miden-bank/contracts/deposit-note/src/lib.rs index 5f82e3c..0ff9bc5 100644 --- a/examples/miden-bank/contracts/deposit-note/src/lib.rs +++ b/examples/miden-bank/contracts/deposit-note/src/lib.rs @@ -18,7 +18,7 @@ pub struct Wallet; /// 1. Note is created by a user with fungible assets attached /// 2. Bank account consumes this note /// 3. Note script reads the sender (depositor) and assets -/// 4. For each asset, calls `account.deposit(depositor, asset)` +/// 4. For each asset, calls `account.bank_deposit(depositor, asset)` /// 5. Bank receives the asset and updates the depositor's balance /// /// # Note Inputs @@ -38,7 +38,7 @@ impl DepositNote { // Deposit each asset into the bank for asset in assets { - account.deposit(depositor, asset); + account.bank_deposit(depositor, asset); } } }