Token

In most cases, your code will need to interact with tokens. The SDK makes it easy to verify that the token is usable and that Dexible smart contract has been granted the appropriate spend allowance.

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 are using is valid. You can easily "verify" the token with Dexible to make sure Dexible will allow you to trade the token.

Check Token Validity

The following code uses Dexible to verify that the token is usable on the platform.

//WETH token metadata
const weth:IERC20Token = {
   address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
   decimals: 18,
   chainId: 1
}
let {supported,reason} = await sdk.exchange.supportsToken(weth);
if(!supported) {
   throw new Error("Cannot use token on Dexible due to " + reason);
}

let [balance,spend] =  await sdk.getBalanceAndSpendAllowance(weth, traderAddress); 

let orderInputAmount = ethers.utils.parseUnits("20", tokenIn.decimals);
if(balance.lt(orderInputAmount)) {
    throw new Error("I don't have enough funds to trade!");
}
if(spend.lt(orderInputAmount)) {
    //assumes SDK has a signer
    await sdk.approveSpend(
        weth,
        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.

Last updated