Custom Algos

Dexible can accept custom order types so long as they extend the BaseSwap class. We recommend you look at the source code for other swaps before creating your own. Every algo requires the gasPolicy and slippage policies. Beyond that, the Limit, StopLimit, and TWAP algos have specific policy requirements as well (namely, LimitPrice, StopPrice, and BoundedDelay). If you truly want to start from scratch, you can apply policies to a Market algo to express to Dexible how you want the order to execute.

PriceRange Market Order

Suppose you want to simply specify a price range for an order. Unlike limit, you want to execute if the price is above or below a certain price point (often called price 'bands'). Here is what you can do:

import {
  Price, 
  Dexible, 
  GasCostPolicy, 
  SlippagePolicy, 
  PriceBoundsPolicy,
  BaseSwap,
  Market,
  units
} from 'dexible-sdk';

class MySwapType extends BaseSwap {
   constructor(props: BaseSwapConfig) {
       super(props, "MySwapType");
   }
   
   toAlgo() {
       let policies = [
         new GasCostPolicy({
            gasType: 'relative',
            deviation: 0
         }),
         new SlippagePolicy({
            amount: .5
         }),
         new PriceBoundsPolicy({
            basePrice: new Price({
               inToken: tokenIn,
               outToken: tokenOut,
               inUnits: 1,
               outUnits: 2500
            }),
            lowerBoundPercent: 2,
            upperBoundPercent: 2 
         })
      ];
   
      return new Market({
         policies
      });
   }
}

const main = async () => {
   //create Dexible instance since we'll need it later
   let sdk = new Dexible(...);
   
   let tokenIn = weth; //assumed full IERC20Token instance
   let tokenOut = matic; //same
   
    let swap = new MySwapType({
       tokenIn,
       tokenOut,
       amountIn: units.inBNUnits("18000", 18)
    });   
    ...
}

main();

Here we simply create a custom swap type "market" order, it will not behave like a market order because we've added specific policies that tell Dexible how to execute. It will restrict the execution to a specific price band.

Order Matters

The order of the policies array is the exact order of evaluation within Dexible's infrastructure. The first non-passing policy will stop evaluation for the order. In certain cases, the policy will force a cancel. We recommend putting those policies ahead of others to short circuit evaluation.

Extending Policies

We will add more policy types as we expand Dexible. If you find that you need a policy that is missing, please contact us and we will evaluate your use case and add policies accordingly.

Last updated