Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC account code compilation (WIT version) #72

Closed
greenhat opened this issue Nov 21, 2023 · 1 comment · Fixed by #79
Closed

PoC account code compilation (WIT version) #72

greenhat opened this issue Nov 21, 2023 · 1 comment · Fixed by #79
Assignees
Labels
feature New feature or request

Comments

@greenhat
Copy link
Contributor

greenhat commented Nov 21, 2023

Following the discussion at #28 (comment)

The goal is to try to compile the following code to the MASM below and see what parts we are missing:

use miden::{
    account,
    asset::Asset,
    note::Recipient,
    tx,
};

#[miden_account]
pub struct MyWallet;

#[miden_account]
impl MyWallet {
    pub fn recieve_asset(&mut self, asset: Asset) {
        account::add_asset(asset);
    }

    pub fn send_asset(&mut self, asset: Asset, recipient: Recipient) {
        account::remove_asset(asset);
        tx::create_note(asset, recipient);
    }
}

The above code is expected to be compiled close to the following (manually written) code:

use.miden::sat::account
use.miden::sat::tx

#! Adds the provided asset to the current account.
export.receive_asset
    exec.account::add_asset
    padw swapw dropw
end

#! Creates a note which sends the specified asset out of the current account 
#! to the specified recipient.
export.send_asset.1
    exec.account::remove_asset
    # => [ASSET, tag, RECIPIENT, ...]

    # insert 8 ZEROs into the stack right after recipient; we temporarily store one of the
    # elements of ASSET in memory to make stack manipulation easier
    push.0 swap loc_store.0 padw push.0.0.0 swapdw loc_load.0
    # => [ASSET, tag, RECIPIENT, ZERO, ZERO, ...]

    exec.tx::create_note
    # => [note_ptr, ZERO, ZERO, ...]
end

The things to check are:

  • Mark the MyWallet struct as a Miden account (export marker trait in WIT vs. doc comment attribute). Keep in mind that helper methods exported in the account are to be execd in notes/tx scripts.
  • Try to define the Asset as a resource in WIT;

EDIT: updated to reflect the switch to WIT.

@greenhat greenhat added the feature New feature or request label Nov 21, 2023
@greenhat greenhat self-assigned this Nov 21, 2023
@bobbinth
Copy link
Contributor

Not sure how it might affect things, but a potentially better code on the Rust side for this would be:

use miden::{
    account,
    asset::Asset,
    note::Recipient,
    tx,
};

#[miden_account]
pub struct MyWallet;

#[miden_account]
impl MyWallet {
    pub fn recieve_asset(&mut self, asset: Asset) {
        account::add_asset(asset);
    }

    pub fn send_asset(&mut self, asset: Asset, recipient: Recipient) {
        let asset = account::remove_asset(asset); // this line is different
        tx::create_note(asset, recipient);
    }
}

This is semantically different from the version in the original post only if Asset is not Copy. So, I guess one question we should settle on is whether Asset should be Copy on the Rust side. My initial thinking is that probably it should not be, but I also haven't thought through the implications.

@greenhat greenhat linked a pull request Nov 23, 2023 that will close this issue
@greenhat greenhat changed the title PoC account code compilation PoC account code compilation (WIT version) Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
2 participants