Typescript

In order to use the Typescript Interface generation feature of Factori, you will need to have npm installed on your machine.

The Typescript SDK can be found in the src/typescript_sdk subfolder of your Factori project directory.

It is generated by activating the --typescript option of the factori import kt1 or factori import michelson commands, as seen below:

factori import kt1 <dir> KT1...XXX --network mainnet --name my_contract --typescript

<dir> is the working directory (it may be relative, such as .), and KT1...XXX is the address of the contract.

will create the directory <dir>/src/typescript_sdk where the following files and directories will appear:

  • src:
    • functolib.ts: A library of types and functions used by all imported interfaces
    • my_contract_code.json: the Micheline code of your contract (useful for deployment)
    • my_contract_interface.ts: the Typescript interface to your contract, described below.
  • public (empty folder for now)
  • package.json (javascript config file)
  • tsconfig.json (typescript config file)

The command also creates a Makefile with useful commands such as:

make ts-deps

which will install all needed typescript dependencies, and

make ts

which will compile your interface, but also any scenario which you may find yourself writing inside <dir>/src/typescript_sdk/. For instance, if you create a <dir>/src/typescript_sdk/scenario.ts, you can run:

make ts
node <dir>/src/dist/scenario.js

Description of the interface file

The interface file (my_contract_interface.ts using the convention from above) consists of both

  1. Types
  2. Functions

The types describe the contract's storage as well as all the input types of the contract's entrypoints (and any intermediate types which may need to be defined in the process). The functions are (mainly) of three kinds:

  1. A deploy function deploy_my_contract;
  2. Calling functions for each entrypoint, of the form call_<entrypoint_name>.
  3. Utility functions for manipulating types, so that in principle, you never have to use Micheline or Michelson directly:
    • Encoding functions from a type to Micheline (<type name>_encode);
    • Decoding functions from Micheline to a type (<type name>_decode);
    • Random generation of elements of the type (<type name>_generator);
    • (technically not a function, but rather a constant): the type itself expressed in Micheline; this is useful e.g. for querying big maps (<type name>_micheline).

Note about Taquito

Factori's Typescript generation uses the awesome Taquito library. Please note that although there are many similarities between the kind of storage that Taquito accepts for a given contract and that generated by Factori, they are not the same and it is not advised to mix them. Taquito generates a dynamic interface (you generally can't look at it until you have retrieved a storage and looked at it), while Factori generates a static interface (you know its exact structure in advance).