Major SDK Components

Overview

The Dexible SDK repository is broken into several packages or modules. But when you create an SDK instance, it has several sub-layers that are useful for constructing orders, getting quotes, or querying the API.

Dexible SDK makes heavy use of ethers.js. Many of the arguments or Typescript interfaces expect ethers object types as input. We recommend you familiarize yourself with the ethers docs if you are not sure what the code in this documentation refers to. https://docs.ethers.io/v5/

Installation

Currently, the SDK is implemented in Typescript, so installation is simple for JavaScript projects. Future support for python is under development.

yarn add dexible-sdk

SDK

The top-most component is the SDK object. The SDK can be constructed with or without a ethers Signer instance. Omitting a signer will limit certain functionality within the SDK related to order submission.

Here is an example of creating an SDK instance:

import {SDK} from 'dexible-sdk';
import {ethers} from 'ethers';

const provider = new ethers.providers.InfuraProvider(1, process.env.INFURA_PROJECT_ID);
const signer = new ethers.Wallet(process.env.WALLET, provider);

// create a fully featured SDK with signer
const sdk = await SDK.create({
   signer,
   provider
});

// create limited SDK without signer
const sdk = await SDK.create({
   provider,
});

In this code snippet, we first create an ethers InfuraProvider instance. We use a wallet key found in the environment to create a new ethers Wallet instance. Then we create the SDK instance using the wallet (signer). The SDK will detect the current network from the supplied provider.

Dexible currently supports the following networks:

  • Avalanche Mainnet

  • Binance Mainnet

  • Ethereum Mainnet

  • Ethereum Ropsten

  • Fantom Mainnet (Opera)

  • Polygon Mainnet

SDK in a Browser

The SDK can be used in a browser but requires a web3 provider, such as MetaMask or WalletConnect, and login support using JavaScript Web Tokens (JWT).

Using Web3 Provider

In a browser environment, you will likely have access to MetaMask or some other browser-based wallet. Here is an example to setup the signer you need for the SDK instead of a local wallet key:

const provider = new ethers.providers.Web3Provider(web3.currentProvider);
const signer = provider.getSigner();

JWT Handler

To create an SDK instance in the browser, you supply a handler that can store and retrieve a previously-created JWT. The interface looks like this:

export default interface IJWTHandler {
    storeToken: (token:string, expiration: number) => Promise<void>;
    readToken: () => Promise<string|null>;
}

The handler is given a token to store and is asked to retrieve a previously-stored token when needed. In most cases, you can store the token in localStorage in the browser or keep in memory if you don't want to store the token anywhere permanent.

Once you have a handler implementation, you can create an SDK instance with:

...
let sdk = await SDK.instance({
   network: "ethereum",
   chainId: 1,
   signer,
   jwtHandler: new JWTHolder()
});

Here the JWTHolder just holds the token in memory. It's implementation might look like this:

class JWTHolder implements IJWTHandler {
    token: string | null;

    constructor() {
        this.token = null;
    }

    //called by SDK when it needs to communicate to API. here we try 
    //pulling any previous token from local storage
    readToken = async ():Promise<string|null>  => {
        if(!this.token) {
           this.token = localStorage.getItem("DexibleJWT");
        }
        return this.token;
    }

    //called when SDK wants you to store the token for re-use
    storeToken = async (token: string, expiration: number):Promise<void> => {
        this.token = token;
        localStorage.setItem("DexibleJWT", token);
    }
}

Last updated