Skip to main content

Automated Strategies

Getting Started

In this guide, we will write a nodejs script that calls the defiedge strategy smart contract for atomizing the rebalance functionality. This is an example of a rebalance bot which checks the current price of token1 per token0 . If the current price of token1 per token0 is less than to the lower price or token1 per token0 price is greater than to the upper price then it should rebalance the strategy by calling DeFiedge strategy contract rebalance function

For Creating Rebalance bot Manager needs to integrate following APIs

Step 1: Swap Amount API.

Step 2: Precision calculation API.

Step 3: 1Inch Router API.

Step 4: You can integrate your own Back Tested API for ranges.

DeFiegde API Documentation :-

  • Swap Amount API :-

You can find DefiEdge API endpoint below :

  ​​https://api.defiedge.io/

Methods For Calling the API

  1. This method is used for getting the liquidity data from strategy

       Request :

       GET: matic/{{Input_Strategy_Address}}/liquidity

       Response :

 {"amount0":0,
"amount1":0,
"unusedAmount0":0.00096905,
"unusedAmount1":25.816053,
"amount0Total":0.00096905,
"amount1Total":25.816053,
"unusedAmount0BigNumber":{"type":"BigNumber","hex":"0x017a89"},
"unusedAmount1BigNumber":{"type":"BigNumber","hex":"0x0189ebf5"}
  1. This method is used for calculating the liquidity amount ratio with respect to ranges for swap

        Request :

       POST: /calculations/swap_amounts

       POST data:

Nodejs Example for above APIs

 //API used for calculating liquidity data of strategy by providing strategy address
let liquidity = await axios.get(`https://api.defiedge.io/matic/${CONFIG.STRATEGY_ADDRESS}/liquidity`);
console.log("liquidity: ", liquidity.data)

//API used for calculating swap amount via providing Post data

let swapAmounts = await axios.post("https://api.defiedge.io/calculations/swap_amounts", {
x: liquidity.data.amount0Total,
y: liquidity.data.amount1Total,
price: priceToken1PerToken0,
ranges: [[Number(newlowerPrice.toSignificant(6)),Number(newupperPrice.toSignificant(6)),1]],
f: fee
});
console.log("swapAmounts: ", swapAmounts.data)
var swapData;
if(swapAmounts.data.swapped_x == 0 && swapAmounts.data.swapped_y == 0){
swapData = "0x"
console.log("-no need for swap")
} else {
console.log("swap data")