Codegen attributes

#[cmd]

When move-to-ts is invoked with the -c (--gen-cli) flag, it will output a cli.ts (invoked with yarn cli) that contains one command for every entry function that's decorated with the #[cmd] attribute.

Example in Move:

#[cmd(desc=b"Add new token into registry")]
public entry fun add_token_script<TokenType>(
    admin: &signer,
    name: vector<u8>,
    symbol: vector<u8>,
    description: vector<u8>,
    decimals: u8,
    logo_url: vector<u8>,
    project_url: vector<u8>,
) acquires TokenRegistry {
    ...
}

Generated CLI:

$ yarn cli
Usage: move-ts-cli [options] [command]

Options:
  -c, --config <path>                  path to your aptos config.yml (generated with "aptos init")
  -p, --profile <PROFILE>              aptos config profile to use (default: "default")
  -h, --help                           display help for command

Commands:
  coin_registry:add-token-script <TYPE_TokenType> <name> <symbol> <description> <decimals> <logo_url> <project_url>  
                                       Add new token into registry

#[method]

The #[method] attribute allows you to attach specific Move functions to specified Move resource types, and use them from TypeScript as ordinary class methods.

Example in Move (taken from Econia)

    #[method(
        book_orders_sdk,
        book_price_levels_sdk,
        get_orders_sdk,
        simulate_swap_sdk
    )]
    /// An order book for the given market
    struct OrderBook<phantom B, phantom Q, phantom E> has key {
        ...
    }

    
    /// Calculate expected result of swap against an `OrderBook`.
    fun simulate_swap_sdk<B, Q, E>(
        order_book_ref_mut: &mut OrderBook<B, Q, E>,
        style: bool,
        coins_in: u64
    ): (
        u64,
        u64
    ) {
        ...
    }

Usage in TypeScript:

// first fetch OrderBook resource from chain
const orderBook = await app.loadOrderBook(...);
// performs computation using fetched state
const [quoteReceived, basePaid] = orderBook.simulate_swap_sdk(true, u64(100000));

#[app]

The #[app] attribute, used on functions, tells move-to-ts to include a particular function in the App interface generated. It allows you to call arbitrary move functions from TypeScript.

Example (taken from lending tutorial):

#[app]
public fun global_get_user_limits(user: address): (bool, u64, u64) acquires User, LendingProtocol {
    let user = borrow_global<User>(user);
    let protocol = borrow_global<LendingProtocol>(@hippo_tutorial);
    user_get_limits(user, protocol)
}

When the #[app] attribute is applied on global_get_user_limits, the following interface is exposed from the App interface:

app_global_get_user_limits(
  user: HexString,
) {
  return global_get_user_limits_(user, this.cache);
}

#[query]

The #[query] attribute allows you to:

  1. Execute arbitrary computation in Move under simulation mode using realtime onchain data

  2. Obtain a specified output from fullnode as serialized return value

The targetted use case is to allow frontends/arb bots to obtain realtime quotes from fullnodes directly. We are reworking the interface of this query feature to make it simpler and more general (hint: we'll use script transactions!). For a contrived example of the existing, and somewhat inconvenient, interface, please refer to this.

Last updated