App interface

The snippets below are taken from a simple lending tutorial.

When you execute the move-to-ts command from inside your Move project, it automatically generates the typescript files under project-path/build/typescript. The top-level index.ts contains an App class which gives you access to all the features of the TypeScript SDK, including:

  • fetching state from chain

  • executing move functions (using App interface)

  • executing move functions (using TypeScript methods)

Creating App object

import { App } from 'path-to-generated-ts-folder';
import { AptosClient } from 'aptos';

async function act() {
  const client = new AptosClient(...);
  const app = new App(client);
}

Loading resources from fullnodes

User and LendingProtocol are resource structs defined in the lend2 module. For all resource structs, you will be able to fetch them by using:

The generated loadX method would actually try to load all the key-value pairs of any IterableTable contained within the resource. To disable this additional loading behavior, you can pass in another boolean variable,

Do note that if the resource struct contains Table members, you need to explicitly pass in a false value to ask the loader to not loadFullState. This is because there isn't an easy way to enumerate all key-value pairs of a Table. And we require you to pass in the additional false to acknowledge that the struct's state is only partially loaded.

Execute Move functions (arbitrary)

There are 2 approaches to execute arbitrary move functions from TypeScript:

  1. Synchronous execution by preloading resources

  2. Asynchronous execution

Both require you to place the #[app] attribute on the function you want to execute, for example:

The synchronous approach requires you to manually load the needed resources. Once the needed resources are loaded, you may repeatedly call the synchronous function and obtain the return value immediately.

The asynchronous approach requires you to run move-to-ts with the `-a` flag, and can handle state loading for you automatically by making asynchronous calls. This returns the most up-to-date result, but every call to the async function may involve some delay due to network access.

Execute Move functions (methods)

move-to-ts can also generate TypeScript methods for if you use the #[method] attribute

We note that user_get_limits is a Move function with the following signature:

To add user_get_limits as a method into the generated TypeScript User class, you need to do the following:

  • Make sure the first parameter of user_get_limits is of type &User

  • add a #[method] attribute to User's Move struct declaration, like below:

If you need to attach multiple methods, just include more function names in the method attribute, and separate the function names by comma:

Last updated