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)

Usage in TypeScript:

#[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):

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

#[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