Skip to content
Draft
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
26 changes: 26 additions & 0 deletions examples/consume-asset-script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "consume-asset-script"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

# Mark this as its own workspace to prevent workspace inference from parent
[workspace]

[dependencies]
miden = "0.10.0"

# Miden dependencies for cargo-miden build/linking
[package.metadata.miden.dependencies]
"miden:basic-wallet" = { path = "../basic-wallet" }

[package.metadata.component.target.dependencies]
"miden:basic-wallet" = { path = "../basic-wallet/target/generated-wit/" }

[package.metadata.component]
package = "miden:consume-asset-script"

[package.metadata.miden]
project-kind = "transaction-script"
29 changes: 29 additions & 0 deletions examples/consume-asset-script/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![no_std]
#![feature(alloc_error_handler)]

#[macro_use]
extern crate alloc;

use crate::bindings::Account;
use alloc::vec;
use miden::{intrinsics::advice::adv_push_mapvaln, *};

#[tx_script]
fn run(arg: Word, account: &mut Account) {
let num_felts = adv_push_mapvaln(arg.clone());
let num_felts_u64 = num_felts.as_u64();

let num_assets = num_felts_u64 / 4;
let num_words = Felt::from_u64_unchecked(num_felts_u64 / 4);

// Load all words at once, verified against the commitment (RPO hash)
let data = adv_load_preimage(num_words, arg);

// Receive assets
for i in 0..num_assets {
let off = (i * 4) as usize;
let asset_word = Word::new([data[off], data[off + 1], data[off + 2], data[off + 3]]);

account.receive_asset(Asset::new(asset_word));
}
}
7 changes: 7 additions & 0 deletions examples/swapp-note/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build]
target = "wasm32-wasip2"

[target.wasm32-wasip2]
# Force-enable `cfg(miden)` for Miden-VM-targeted builds (including editor/LSP workflows).
rustflags = ["--cfg", "miden"]

29 changes: 29 additions & 0 deletions examples/swapp-note/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "swapp-note"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

# Mark this as its own workspace to prevent workspace inference from parent
[workspace]

[dependencies]
miden = "0.10.0"


# Miden dependencies for cargo-miden build/linking
[package.metadata.miden.dependencies]
"miden:basic-wallet" = { path = "../basic-wallet" }


[package.metadata.component.target.dependencies]
"miden:basic-wallet" = { path = "../basic-wallet/target/generated-wit/" }


[package.metadata.component]
package = "miden:swapp-note"

[package.metadata.miden]
project-kind = "note-script"
36 changes: 36 additions & 0 deletions examples/swapp-note/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SWAPp Note

A Miden note script implementing a partially-fillable swap (PSWAP) for decentralized exchange functionality.

## How It Works

A creator locks an **offered asset** in the note and specifies a **requested asset** and amount. A consumer can fill the swap fully or partially by providing some or all of the requested asset.

- **Proportional exchange** — Output amounts are calculated proportionally to the input.
- **Partial fills** — If only partially filled, a remainder SWAPp note is automatically created with the leftover offered asset.
- **Surplus capture** — Solvers can earn spread in cross-swap scenarios via a surplus P2ID note.
- **Self-cancellation** — The note creator can consume their own note to reclaim assets.

## Note Inputs (set at creation)

| Index | Field |
|-------|-------|
| 0-3 | Requested asset (id prefix, id suffix, padding, total amount) |
| 4-5 | Creator account ID (prefix, suffix) |
| 6 | Note type |
| 7 | Tag |

## Note Args (provided by consumer)

| Index | Field |
|-------|-------|
| 0 | `input_amount` — amount of requested asset provided |
| 1 | `inflight_amount` — inflight requested asset amount |
| 2 | `surplus_amount` — offered asset surplus for solver |
| 3 | `consumer_p2id_tag` — P2ID tag for surplus note |

## Build

```sh
cargo miden build --manifest-path contracts/swapp-note/Cargo.toml --release
```
5 changes: 5 additions & 0 deletions examples/swapp-note/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "nightly-2025-12-10"
components = ["rustfmt", "rust-src", "clippy"]
targets = ["wasm32-wasip2"]
profile = "minimal"
Loading