> For the complete documentation index, see [llms.txt](https://docs.predy.finance/predy-v5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.predy.finance/predy-v5/product-recipe.md).

# Product Recipe

How to provide Perpetual Futures Trade

## Under developing Page

The following describes how the flow actually takes place when the user enters each value such as $$a\_{perp}=ETH \ amount$$ and $$a\_{squart} = Squart \ amount$$.

#### How to make Squart(√ETH)

First, the Uniswap v3 LP position shall be minted with the following Tokens:

$$required\_{eth}=\frac{a\_{squart}}{2}(\frac{1}{\sqrt{p}}-\frac{1}{\sqrt{p\_b}})$$

$$required\_{usdc}=\frac{a\_{squart}}{2}(\sqrt{p}-\sqrt{p\_a})$$ &#x20;

where, $$m= Margin$$, $$p= ETH \ price$$, $$P\_a = Lower Price\_{range}$$ and $$P\_a = Higer Price\_{range}$$.&#x20;

In addition, add the following Token to retrieve Squart(√ETH).

$$offset\_{eth}=\frac{a\_{squart}}{2}\frac{1}{\sqrt{p\_b}}$$

$$offset\_{usdc}=\frac{a\_{squart}}{2}\sqrt{p\_a}$$

Therefore, the total Token amount of each of the above is required. See this [Paper](https://www.notion.so/Creating-x-Perpetual-Future-Contract-from-Uniswap-V3-LP-Position-ad8db3e9b2c040f8865be2b8a4eae6fc) for detailed instructions.&#x20;

#### Trading (entry position creation)

First, Exchange ETH and USDC with Uniswap.

$$SwappedForSquart=p'\*(required\_{eth} + offset\_{eth})$$

$$entry\_{perp}=p'\*a\_{perp}$$

$$entry\_{squart}=required\_{usdc}+offset\_{usdc}+SwappedForSquart$$

where, $$p' = ETH \ Price\_{trade}$$ and The unit on the left side of the formula is the value (unit price x quantity).

#### Position's Value and Vault's Value

$$v\_p=p \* a\_{perp} - entry\_{perp} + \sqrt{p} \* a\_{squart} - entry\_{squart}$$

$$VaultValue = v\_p + m$$

#### Asset and Debt Concept

Treat the following as an ASSET for positive cases and a DEBT for negative cases.

$$asset\_{eth} = a\_{perp} + offset\_{eth}$$

$$asset\_{usdc}=-entry\_{perp}-entry\_{squart}+offset\_{usdc}$$

As shown in this [Paper](https://www.notion.so/Creating-x-Perpetual-Future-Contract-from-Uniswap-V3-LP-Position-ad8db3e9b2c040f8865be2b8a4eae6fc), the OFFSET changes depending on the relocation of the range.

#### Reallocate Position

Protocol temporarily undertakes the liability when it relocates in place of User. This is expressed as Reallocation. The new debt needed at this time, or the debt to be returned, is as follows:

$$Required\_{ETH}=\frac{TotalSquartAmount}{2}(\frac{1}{\sqrt{p\_{a\_{prev}}}}-\frac{1}{\sqrt{p\_{a\_{current}}}})$$

$$Required\_{USDC}= \frac{TotalSquartAmount}{2}(\sqrt{p\_{b\_{prev}}}-\sqrt{p\_{b\_{current}}})$$

On the next trade, the debt is transferred to the trader by updating the offset.

$$offset\_{eth} ← offset\_{eth} + \frac{a\_{squart}}{2}(\frac{1}{\sqrt{p\_{a\_{prev}}}}-\frac{1}{\sqrt{p\_{a\_{current}}}})$$

$$offset\_{usdc} ← offset\_{usdc} + \frac{a\_{squart}}{2}(\sqrt{p\_{b\_{prev}}}-\sqrt{p\_{b\_{current}}})$$

The interest payments that occur while the protocol is shouldering the debt accumulate as ReallocationFeeGrowth.

```
protocolReallocationPositionUSDC += required USDC for reallocation
protocolReallocationPositionETH += required ETH for reallocation
ReallocationFeeGrowthETH += protocolReallocationPositionETH * (SupplyInterestGrowthETH - lastInterestGrowthETH)
ReallocationFeeGrowthUSDC += protocolReallocationPositionUSDC * (SupplyInterestGrowthUSDC - lastInterestGrowthUSDC)
```

#### Fee&#x20;

This is expressed as a premium when considered as Option and as an interest rate when considered as Lending. The User earns Fee income from the Asset and pays a Fee on the Debt.

```
// 
let net_eth_interest
let net_usdc_interest

if(asset_{eth} >= 0) {
 net_eth_interest = asset_{eth}*(SupplyInterestGrowthETH - lastInterestGrowthETH)
} else {
 net_eth_interest = asset_{eth}*(BorrowInterestGrowthETH - lastInterestGrowthETH)
}

if(asset_{usdc} >= 0) {
 net_usdc_interest += asset_{usdc}*(SupplyInterestGrowthUSDC - lastInterestGrowthUSDC)
} else {
 net_usdc_interest += asset_{usdc}*(BorrowInterestGrowthUSDC - lastInterestGrowthUSDC)
}

if(a_{squart} >= 0) {
 net_usdc_interest += a_{squart}*(SupplyPremiumGrowth - lastPremiumGrowth)
 net_eth_interest += a_{squart}*TradeFeeETH
 net_usdc_interest += a_{squart}*TradeFeeUSDC
} else {
 net_usdc_interest += a_{squart}*(BorrowPremiumGrowth - lastPremiumGrowth)
}

if(a_{squart} > 0) {
  net_usdc_interest += a_{squart} * (ReallocationFeeGrowthUSDC - lastReallocationFeeGrowthUSDC)
  net_eth_interest += a_{squart} * (ReallocationFeeGrowthETH - lastReallocationFeeGrowthETH)
}

net_interest = net_usdc_interest + net_eth_interest * p
```

#### Debt Value

```
// 
let debtValue
if(asset_{eth} < 0) {
 debtValue += -asset_{eth} * price_{eth}
}
if(asset_{usdc} < 0) {
 debtValue += -asset_{usdc}
}
```

The settlement penalty is 0.05% of this debtValue.

#### Min Deposit

$$minValueWithinRange =min(v(pR), v(\frac{p}{R}))$$

$$minDeposit = PositionValue - minValueWithinRange$$

Where, R = risk params and it sets as 1.2.

#### Liquidation Price

Find √X satisfy $$v\_x = minDeposit$$

$$\sqrt{x\_1}=\frac{(-a\_{squart}+\sqrt{a\_{squart}^2-a\_{perp}\*(-entry\_{perp}-entry\_{perp}+m)}) \* \sqrt{R}}{2a\_{perp}}$$

$$\sqrt{x\_2}=\frac{(-a\_{squart}-\sqrt{a\_{squart}^2-a\_{perp}\*(-entry\_{perp}-entry\_{perp}+m)})}{2a\_{perp} \* \sqrt{R}}$$

#### Margin Available

$$Vault Value = Margin + Position Value$$

$$Margin Utilizing = Min. Deposit$$

$$MarginAvailable = VaultValue - Margin Utilizing$$

$$Withdraw-ableMargin=min(MarginAvailable, Margin)$$
