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.
JavaScript / Typescript
Python
1
yarn add dexible-sdk
Copied!
1
pip install dexible
Copied!

SDK

The top-most component is the SDK object. Here is an example of creating an SDK instance:
JavaScript
Python
1
import {SDK} from 'dexible-sdk';
2
import {ethers} from 'ethers';
3
​
4
let provider = new ethers.providers.InfuraProvider(1, process.env.INFURA_PROJECT_ID);
5
let signer = new ethers.Wallet(process.env.WALLET, provider);
6
​
7
let sdk = await SDK.create({
8
signer,
9
network: "ethereum",
10
chainId: 1
11
});
Copied!
1
import dexible
2
import web3
3
​
4
hexkey = "YOUR_WALLET_KEY_HEX"
5
provider = web3.Web3.HTTPProvider("https://mainnet.infura.io/v3/<YOUR_INFURA_PROJECT_ID>")
6
account = web3.Account.from_key(web3.Web3.toBytes(hexstr=hexkey))
7
​
8
sdk = dexible.DexibleSDK(provider=provider, account=account, chain_id=1)
9
​
Copied!
In this code snippet, we first create an ethers Infura provider 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) and tell the SDK what network and which chain in that network we want to interact with. Currently, Dexible only supports the "ethereum" network; future versions will support other networks such as "polygon", "arbitrum" and "optimistic". The chainId is the ID assigned to the chain you want to interact with within the network. "1" in this case refers to Ethereum mainnet. Other networks have their own IDs (the Kovan testnet, for example, uses 42, etc).

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:
1
const provider = new ethers.providers.Web3Provider(web3.currentProvider);
2
const signer = provider.getSigner();
Copied!

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:
1
export default interface IJWTHandler {
2
storeToken: (token:string, expiration: number) => Promise<void>;
3
readToken: () => Promise<string|null>;
4
}
Copied!
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:
1
...
2
let sdk = await SDK.instance({
3
network: "ethereum",
4
chainId: 1,
5
signer,
6
jwtHandler: new JWTHolder()
7
});
Copied!
Here the JWTHolder just holds the token in memory. It's implementation might look like this:
1
class JWTHolder implements IJWTHandler {
2
token: string | null;
3
​
4
constructor() {
5
this.token = null;
6
}
7
​
8
//called by SDK when it needs to communicate to API. here we try
9
//pulling any previous token from local storage
10
readToken = async ():Promise<string|null> => {
11
if(!this.token) {
12
this.token = localStorage.getItem("DexibleJWT");
13
}
14
return this.token;
15
}
16
​
17
//called when SDK wants you to store the token for re-use
18
storeToken = async (token: string, expiration: number):Promise<void> => {
19
this.token = token;
20
localStorage.setItem("DexibleJWT", token);
21
}
22
}
Copied!
​
Last modified 3mo ago