Typescript SDK

Typescript SDK

This documentation provides an overview and a guide on how to use 01 typescript sdk.

SDK repository

Typescript SDK itself

Examples repository

Examples of the sdk usecases.

Zamm Arb Bot Repository

Zamm Arb Bot which uses typescript sdk.

Loading the environment

This is a simple way to load anchor wallet from Keypair

const keypair = getKeypairFromSecretKey(SECRET_KEY)
const wallet: Wallet = walletFromKeyPair(keypair)

Here is an easy way to load zoUser which loads all the necessary components related to Zero One Exchange:

const zoUser = await ZoUser.load(wallet, CLUSTER, {
    withRealm: false,
    commitment: COMMITMENT,
    skipPreflight: SKIP_PREFLIGHT,
    rpcUrl: RPC_URL

Key fields

All the key fields are accessible through zoUser:

const program = zoUser.program
const provider = zoUser.program
const connection = zoUser.connection

Additionally, one can easily access:

  • margin - account tracking user's margin and balances
  • control - account tracking user's positions and orders
  • state - account tracking exchange's balances and markets
  • cache - account tracking oracle prices
const margin = zoUser.margin
const control = zoUser.control
const state = zoUser.state
const cache = zoUser.cache

Num class

Note zero one uses Num class to represent numbers. It allows to easily get number, decimal, or BN from the stored balance, simplifying arithmetics, by storing number of decimals

  • when one passes number or Decimal, Num assumes that one passes in big(i.e. SOL)
  • when one passes BN, Num assumes that one passes in smoll(i.e. lamports) Examples below are identical in their value
const oneSolFromNumber = new Num(1, 9)
const oneSolFromDecimal = new Num(new Decimal(1.0), 9)
const oneSolFromBN = new Num(new BN(1_000_000_000), 9)

Examples below will work identically with oneSolFromNumber and oneSolFromDecimal

const decimalFromNum = oneSolFromBN.decimal returns new Decimal(1.0)
const numberFromNum = oneSolFromBN.number  // returns 1
const bnFromNum = oneSolFromBN.bn //returns new BN(1_000_000_000)