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.
loading...
Generate a minimum distributionβ
Next, create an instance to manage Kandel strategies (kandelStrategies
), and load the recommended configuration for the market.
loading...
With this, you can generate a distribution with the minimum recommended amount of liquidity to avoid %%density|density%% issues by:
- Creating a generator
- Calculating minimums per offer
- Calculating the distribution for the given price parameters of
minPrice: 900
,maxPrice: 1100
, andpriceRatio: 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.
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).
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.
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 choseliquiditySharing
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.
loading...
Mint test tokensβ
If you are running on a testnet, then you can mint test tokens to send to the Kandel instance.
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|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!
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.
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.
loading...