# Move-to-TypeScript Transpiler

```typescript
const {client, account} = ...;

// Load auto-generated App
const app = new App(client).hippo_tutorial.lend2;

// load User and LendingProtocol struct from chain
const user = await app.loadUser(account.address());
const protocol = await app.loadLendingProtocol(app.moduleAddress, false);

// call user_get_limits to compute some info about user's state
const [isUserHealthy, totalBorrow, totalDeposit] = user.user_get_limits(protocol);
console.log(isUserHealthy, totalBorrow, totalDeposit);

// make a withdrawal
await app.withdraw(account, u64(1000000), [app.FakeBTC.getTag()]);
```

With `move-to-ts`([github](https://github.com/hippospace/move-to-ts/)),  you can:

* Interact with your Aptos contract using auto-generated TypeScript SDK
* Interact with your Aptos contract using auto-generated CLI utility
* Execute all your Move unit tests from TypeScript, and obtain better debugging information

The snippets below are taken from a simple [lending tutorial](https://github.com/hippospace/tutorial-lending).

### Installation

```bash
cargo install --git https://github.com/hippospace/move-to-ts.git
```

### Generate TypeScript SDK

```bash
# From your Move package root folder
# Use this step to fetch dependencies
aptos move compile
# Generate SDK to build/typescript folder
move-to-ts
```

### Interact with contract using generated SDK

```typescript
const {client, account} = ...;

// Load auto-generated App
const app = new App(client).hippo_tutorial.lend2;

// load User and LendingProtocol struct from chain
const user = await app.loadUser(account.address());
const protocol = await app.loadLendingProtocol(app.moduleAddress, false);

// call user_get_limits to compute some info about user's state
const [isUserHealthy, totalBorrow, totalDeposit] = user.user_get_limits(protocol);
console.log(isUserHealthy, totalBorrow, totalDeposit);

// make a withdrawal
await app.withdraw(account, u64(1000000), [app.FakeBTC.getTag()]);
```

### Interact with contract using generated CLI utility

When you add the `#[cmd]` attribute to your Move contract's `public entry` function, `move-to-ts` can help you automatically generate the corresponding CLI commandline invocation utility.

For example, for the public entry function `admin_add_pool` taken from our lending tutorial:

```
#[cmd(desc=b"Create a new lending pool (admin-only)")]
public entry fun admin_add_pool<CoinType>(admin: &signer, initial_price: u64) acquires LendingProtocol {
```

The auto-generated CLI utility has the following usage:

```
$ yarn cli

  Usage: yarn cli [options] [command]

  Move TS CLI generated by move-to-ts

  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:
    lend2:admin-add-pool <TYPE_CoinType> <initial_price>  Create a new lending pool (admin-only)
    lend2:admin-init                                      Initialize protocol information (admin-only)
    lend2:admin-update-price <TYPE_CoinType> <price>      Update price of a particular coin (admin-only)
    lend2:borrow <TYPE_CoinType> <amount>                 Borrow from the CoinType pool. May fail if user exceeds
                                                          borrow limit.
    ...
```

### Debug Move unit tests

```
// generate TS SDK with unit tests included
move-to-ts -t -n package_name
cd build/typescript

// installs dependency
yarn install
// build project
yarn build
// runs all unit tests
yarn test
// run unit tests from specific file
yarn test move_file_name
```

Currently it is probably easier to debug move unit tests via `move-to-ts` than it is using the native Move toolchain, because when a unit test throw error, its stack trace is printed by default and you can very quickly locate where the error has happened.
