Skip to content

Add high-level types in Miden SDK on a case-by-case basis #479

@greenhat

Description

@greenhat

Following up on the #436 (comment)

The original idea was to have two sets of types.

Low-level type in miden-base-sys crate:

struct CoreAsset {
  inner: Word
}

And a high-level in miden-base crate:

enum Asset {
  Fungible(FungibleAsset)
  NonFungible(NonFungibleAsset)
}

struct FungibleAsset {
    faucet_id: AccountId,
    amount: u64,
}

struct NonFungibleAsset(Word);

For every type that we want to use in the component API, we need to define it in the WIT format, which limits us to the WIT type system. Although we are remapping the WIT types to our custom Rust type, it should have the same shape as the WIT type since the lifting/lowering of the types are not customizable (yet).

This makes me wonder if we would be better off with just the low-level types and exposing all the functionality of high-level types via the low-level type's methods. We can still define additional "helper" types in Rust land without mentioning them in WIT, as long as they are not used in the component API.

Pros:

  1. No need to convert to/from low-level types (Word) when calling MASM code (stdlib, tx kernel);
  2. Cheaper passing through the component API due to simpler lifting/lowering;
  3. Only one set of types;

Cons:

  1. Some overhead when accessing the fields in the inner Word;
  2. Field access only via getters/setters;

If we choose to go with only the low-level types, then we need to rename CoreAsset to Asset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions