Skip to main content

Deploy Kandel strategy

This tutorial covers how to deploy a Kandel strategy from a developer standpoint. For more information about Kandel, see the Kandel documentation.

Prerequisites​

  • The tutorial assumes knowledge of JavaScript
  • Follow preparation to create a new tutorial folder.
  • Make sure to use a chain where Mangrove is live - you can find all live addresses for Mangrove here
  • For a more simple tutorial to get acquainted with Mangrove, we recommend Deploy a simple offer

Start local node​

Before proceeding, let's import the environment variables made as part of the preparation.

source .env

Start Foundry's local node anvil to test things locally, with $RPC_URL coming from .env and pointing, for instance, to the Polygon Mumbai testnet.

anvil --fork-url $RPC_URL

Import and connect​

Start up node in a new terminal and issue the following code which performs the initial setup of loading the .env you added in preparation, importing the Mangrove SDK, and connecting the SDK to the local node for a specific market.

examples/tutorials/deploy-kandel.js
loading...

Generate a minimum distribution​

Next, create an instance to manage Kandel strategies (kandelStrategies), and load the recommended configuration for the market.

examples/tutorials/deploy-kandel.js
loading...

With this, you can generate a distribution with the minimum recommended amount of liquidity to avoid density issues by:

  • Creating a generator
  • Calculating minimums per offer
  • Calculating the distribution for the given price parameters of minPrice: 900, maxPrice: 1100, and priceRatio: 1.01.

See the API documentation for calculateMinimumDistribution for more details on other distributionParams. In our example here, midPrice: 1100 is used to set the current price, and decide which offers become bids and which become asks.

examples/tutorials/deploy-kandel.js
loading...

The last three lines should output something similar to the following (actual volumes may differ due to different configuration for the market):

Number of price points: 21
Minimum base volume: 0.0021
Minimum quote volume: 1.9063

πŸ’‘ The minimums depend on the price; if the price range is changed, then the minimums should be re-checked.

Generate desired distribution​

Based on the minimum volumes we calculated, we can select a desired distribution with volumes above these values. Here we use 3 for base (WETH) and 3000 for quote (USDC).

examples/tutorials/deploy-kandel.js
loading...

Note the final log which shows all the bids and asks that will be created, including those that will initially have no liquidity (0 gives) but serve as dual offers of the other side of the book.

Deploy Kandel instance​

Now, you can use the seeder to sow a Kandel instance for a given seed, and retrieve a kandelInstance to manage the deployed instance.

examples/tutorials/deploy-kandel.js
loading...

A brief explanation on the above seed parameters:

  • onAave indicates whether or not the liquidity to be used by Kandel is sitting on AAVE - here, it is not the case (it will be fetched from a wallet)
  • market: this is the WETH/USDC pair that we previously chose
  • liquiditySharing indicates whether you are using shared liquidity or not (SDK only, not available via the UI). This refers to the concept amplified liquidity.

Approve transfers​

The kandelInstance has functions for approving transfers for the base and quote tokens. This is required for the Kandel strategy to be able to transfer tokens from the wallet when depositing funds.

examples/tutorials/deploy-kandel.js
loading...

Mint test tokens​

If you are running on a testnet, then you can mint test tokens to send to the Kandel instance.

examples/tutorials/deploy-kandel.js
loading...

Populate offers for the distribution​

Now that our Kandel instance is deployed, we can populate the offers for the distribution. This will create offers for the base and quote tokens, and deposit the required amounts of tokens into the Kandel instance.

The offers also need a provision, hence here the default that we are using can be inspected.

πŸ’‘ The population can span multiple transactions due to gas limits. After this step, the Kandel offers are deployed and are ready to be taken!

examples/tutorials/deploy-kandel.js
loading...

Manage existing Kandel instance​

Later on, you can also manage an existing Kandel instance. For example, you might want to inspect the status of your offers. For this, the farm can be used to retrieve Kandel instances you own based on events from the seeder.

examples/tutorials/deploy-kandel.js
loading...

Close Kandel strategy and withdraw funds​

At some point, you might want to close your Kandel strategy (for instance due to price movements). This can be easily done with the retractAndWithdraw function. It will withdraw all funds (both tokens and provision) from the Kandel instance and retract all offers.

examples/tutorials/deploy-kandel.js
loading...