# Major SDK Components

### Overview

The Dexible SDK V2.0 has been dramatically simplified from its previous versions.

{% hint style="info" %}
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/>
{% endhint %}

### Installation

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

{% tabs %}
{% tab title="JavaScript / Typescript" %}

```
yarn add dexible-sdk
```

{% endtab %}
{% endtabs %}

### Dexible

The top-most component is the Dexible class. 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. Because we are now living in a multi-chain world, Dexible requires a "IWeb3Factory" implementation. The web3 factory is responsible for giving Dexible an instance of an ethers provider based on a specified chain id. If you use Infura for RPC services, there is an InfuraWeb3Factory in the SDK that simply takes the infura key and uses that to connect.&#x20;

&#x20;Here is an example of creating a Dexible instance:

{% tabs %}
{% tab title="JavaScript" %}

```javascript
import {Dexible, InfuraWeb3Factory} from 'dexible-sdk';
import {ethers} from 'ethers';

const signer = new ethers.Wallet(process.env.WALLET, provider);
//dexible instance with a signing wallet for order management
const sdk = new Dexible({
    signer,
    web3Factory: new InfuraWeb3Factory(process.env.INFURA_KEY)
});

// create limited Dexible instance without signer
const sdk = new Dexible({
   web3Factory: new InfuraWeb3Factory(process.env.INFURA_KEY)
});
```

{% endtab %}
{% endtabs %}

In this code snippet, we use the InfuraWeb3Factory provided by the SDK as the web3 factory implementation. We use a wallet key found in the environment to create a new ethers `Walle`t instance. Then we create the Dexible instance using the wallet (signer) and factory.

Dexible currently supports the following networks:

* Avalanche Mainnet
* Binance Mainnet
* Ethereum Mainnet
* Ethereum Goerli
* Fantom Mainnet (Opera)
* Polygon Mainnet
* Arbitrum Mainnet

### SDK in a Browser

The SDK can be used in a browser but requires a web3 factory that interacts with a browser-supported wallet such as MetaMask or WalletConnect. Most wallet implementations support requests to change network, etc. Dexible mainly uses the IWeb3Factory implementation you provide to get the correct network connection when looking up token details and balance information. Dexible in the browser should also use Dexible's JWT support by providing an IJWTHandler implementation. This is a carryover from V1.0.

#### 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:

```javascript
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.&#x20;

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

```
...
let sdk = new Dexible({
   signer,
   jwtHandler: new JWTHolder(),
   web3Factory: myWeb3Factory
});
```

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);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dexible.gitbook.io/dexible-sdk/major-sdk-components.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
