0xPolygonMiden / compiler

Compiler from MidenIR to Miden Assembly

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Examples of Miden projects in Rust

greenhat opened this issue · comments

Besides the developer's guide/book it is essential to provide some examples of Miden projects (accounts, note scripts, etc.) written in Rust.

I'd like to create a separate repo for them (rust-examples?). So that users could clone the repository, run the examples, tinker with them and use them as a starting point in their own projects.

The README in the new repo will provide an entry point starting with prerequisites, a list of examples, and how to build and run them. Plus, the links to the guide/book in the compiler repo.

Each example should be a standalone Rust project in its own folder. The folder should contain a README.md file with a brief description of the example, and instructions on how to build and run it.

I think that's a great idea! I'm guessing these would be different from rust-templates - right?

Regarding the name: I wonder if rust-examples is too generic for this. Maybe something like contract-examples would be better?

Btw, as the first example, we could probably put a Rust equivalent of BasicWallet in there.

I'm guessing these would be different from rust-templates - right?

Yes, that's correct. rust-templates is intended to be a collection of templates for creating new Miden projects via the cargo miden new command. So when user runs cargo miden new my_wallet our extension downloads the folder from the repo and fills my_wallet in the Cargo.toml.
We provide a hello-world type of source code in the src/lib.rs file, so in a way this is also an example, and I'm intended to use as an entry point into our ecosystem by heavy use of comments with links, extra source code, etc. But the new repo would be a collection of more complex examples that demonstrate how to use the Miden SDK in Rust.

Regarding the name: I wonder if rust-examples is too generic for this. Maybe something like contract-examples would be better?

Sounds great! contract here is more specific and gives a better idea of what the examples are about. I was thinking that we would want to also put the non rollup specific examples in the future as well, but we can always create a new repo for that. Omitting the rust prefix also makes sense, since the Rust is the only high-level language we support at the moment.

Btw, as the first example, we could probably put a Rust equivalent of BasicWallet in there.

Yep, my thoughts exactly, it would be the first one. Here is how BasicWallet Rust code curruntly looks like in tests -

impl Guest for Component {
fn receive_asset(asset: CoreAsset) {
add_asset(asset);
}
fn send_asset(asset: CoreAsset, tag: Tag, recipient: Recipient) {
let asset = remove_asset(asset);
create_note(asset, tag, recipient);
}
}

It would look be a bit different for Alpha (no mod bindings, explicit #[no_mangle] attributes, etc.) since we will not have WIT support until the Beta release.

contract-examples repo created!

Here is how BasicWallet Rust code currently looks like in tests

Looks great! We may want to hide things like setting the allocator, panic handler etc. behind some macro. Similarly, we may want to shorten import paths - e.g., miden::tx::create_note instead of bindings::miden::base::tx::create_note. But no need to do this now - we can do it further down the road.