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
16 changes: 8 additions & 8 deletions docs/src/miden-bank/04-note-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Part 3: Part 4:
┌──────────────────┐ ┌──────────────────┐
│ Bank (complete) │ │ Bank (complete) │
│ ─────────────────│ │ ─────────────────│
│ + deposit() │ │ + deposit()
│ + bank_deposit() │ │ + bank_deposit()
│ + withdraw() │ │ + withdraw() │
└──────────────────┘ └──────────────────┘
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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
Expand All @@ -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).
18 changes: 9 additions & 9 deletions docs/src/miden-bank/05-cross-component-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
┌────────────────────────────────────────────────────────────┐
Expand All @@ -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
│ │
│ ┌───────────────────────────┐ │
Expand All @@ -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│
│ │
└────────────────────────────────────────────────────────────┘
```
Expand Down Expand Up @@ -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

Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
```
Expand Down Expand Up @@ -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);
}
```
Expand Down Expand Up @@ -265,7 +265,7 @@ miden-bank-account.wit

</details>

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

Expand All @@ -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.
Expand Down
11 changes: 7 additions & 4 deletions examples/miden-bank/contracts/bank-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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.
///
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -332,4 +335,4 @@ impl BankStorage {
// Add the asset to the output note
output_note::add_asset(*asset, note_idx);
}
}
}
4 changes: 2 additions & 2 deletions examples/miden-bank/contracts/deposit-note/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}