Leverage Token based on MCDEX Perpetual (MAI3)

It is straightforward to design and implement a Defi leverage token protocol based on MCDEX Perpetual Protocol:

Mint (Buy)

  1. User deposits collateral tokens(e.g., USDC) to the protocol;
  2. The protocol deposits the collateral tokens into its margin account in MCDEX perpetual;
  3. The protocol opens a position with the target leverage(e.g., 3X) of MCDEX perpetual;
  4. The protocol sends the share token(leverage token) to the user;

Redeem (Sell)

  1. User sends leverage token to the protocol;
  2. The protocol closes a portion of its position of MCDEX perpetual;
  3. The protocol withdraw some collateral tokens from its MCDEX perpetual margin account;
  4. The protocol sends the collateral token to the users;

Rebalance

When the underlying perpetual position has PNL, the effective leverage of the leverage token is not the same as the original target leverage. In this case, the protocol shall increase/decrease its position size if it has profit/loss to make the effective leverage equal/near to the target leverage again.

The leverage token protocol shall introduce some strategies to rebalance its position:

  1. The protocol MAY rebalance at some fixed frequency, for example, once per day;
  2. The protocol MAY rebalance when the effective leverage is out of some range of the target leverage, for example, ±25%;

Besides, because the rebalance may trade a large size, the protocol should split the rebalance into smaller trades and reduce the slippage.

Community Development

I suggest the developers in our community complete the design and implement this product. MCDEX DAO should incentive such project because it will increase the use cases of MCDEX perpetual.

2 Likes

Here is an example of the leveraged token implementation.

2 Likes

This is cool!
Took an initial look. Looks like while calculating the cost and income, the fees (lpFee,operatorFee,vaultFee) are not being considered. It should be considered, right @jie
I can make a PR for the calculation of fees unless I am missing something

It is a good point.

queryTradeWithAMM() should have included fee. We will fix queryTradeWithAMM(). Sorry for the unstable of the interface.

1 Like

Yeah, that would be great!
Where should I look for this change?
In the dev branch right?

Since you are taking suggestions I had one more.
Right now using queryTradeWithAMM() method we can know how much collateral will be required when the underlying asset amount (tradeAmount) is given

Could you also add a method that returns the underlying asset amount when the collateral amount and leverage is given?

Otherwise what happens is the whoever is building on top of MCDEX (devs like me :upside_down_face:) needs to recreate the whole equation that queryTradeWithAMM() uses but in reverse

It’s a good catch! queryTradeWithAMM should return trading price, fees, and cost (trader should deposit if cost > 0 and withdraw if cost < 0) just like a normal trade. It will be fixed in the dev branch first.

The second suggestion is a little difficult to implement. Because trading price (f(x)) is a function of trading amount (x), and you want to specify collateral (c). This is basically an equation like:

Find an x that satisfies f(x) * x / leverage = c.

One possible solution is to search for x using binary search. It is possibly a feasible solution because calculation gas cost is much lower than storage gas cost on Arbitrum, but we still need to verify that. Since binary search is more like a tool function, we may not integrate it in the main contract but may add a tool contract.

I agree.
The second suggestion makes sense to implement in a helper contract, not in the mcdex core contracts.
Thanks @tc-mcdex

I am trying to understand what exactly the f(x) is. I am using AMMDesign and contract source code to recreate it.
Are there any other resources I should be looking at?
I see that in the mcdex frontend, there is an option to input the collateral amount which gets converted into the underlying asset amount (I think it assumes 1x leverage). Can you share how are you calculating it there?

Hi, the f(x) is formula 7 and 15 of the document.

The frontend function is implemented here: computeAMMTradeAmountByMargin. It uses bisect search.

1 Like

@yashnaman We are going to replace queryTrade with this new version. Could you check if this modification is useful?

mai-protocol-v3/Reader.sol at dev · mcdexio/mai-protocol-v3 (github.com)

1 Like