Token

In most cases, your code will need to interact with tokens. The SDK makes it easy to find metadata about tokens, including your wallet's balance and current Dexible spend allowance, using the "tokens" sub-component of the SDK instance.

It is very important that you perform a lookup on each token you intend to use. The lookup will verify that the token address you're using is valid. After lookup, then you can "verify" the token with Dexible to make sure Dexible will allow you to trade the token.

Looking up Tokens

The following code looks up a token by its address and uses the additional balance and spend allowance information to check errors or increase spending.

//WETH token metadata
let tokenIn = sdk.token.lookup("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
let orderInputAmount = ethers.utils.parseUnits("20", tokenIn.decimals);
if(tokenIn.balance && tokenIn.balance.lt(orderInputAmount)) {
    throw new Error("I don't have enough funds to trade!");
}
if(tokenIn.allowance.lt(orderInputAmount)) {
    await sdk.token.increaseSpending({
        token: tokenIn,
        amount: orderInputAmount.mul(10) //or whatever you want >= orderInputAmount
    });
}

Increase Spending

The example above illustrated how easy it is to increase spending allowance for a token using the SDK. As mentioned earlier, we recommend increasing spending to a level you feel comfortable with but at least enough to cover the cost of the order input amount.

Verifying Tokens

As mentioned earlier, tokens must pass a verification check before they can be used in an order. The SDK provides a simple way to check whether a token you want to trade is acceptable.

let ok = await sdk.token.verify(tokenIn.address);
if(!ok || ok.error) {
   //cannot trade the token
   throw new Error("Invalid input token");
}

The verify function will return a boolean or error depending on the result of evaluation. The error may give you clues as to why the token is unacceptable. We debated whether to put the verification directly in the token lookup function; but we decided the two functions have two different objectives: looking up token metadata and verifying that you can use the token for trading.

Last updated