Developer FAQs

  • Updated

ℹ️ Don't see your question here? Come to our Discord and look for the #🏗coding-chat channel!

Is there an API?

There is no API in the regular sense. Perpetual Protocol uses The Graph API to provide data, and protocol functions can be called using tools that interact directly with the on-chain smart contracts.

How do I obtain a value for liquidity?

First call

Orderbook.getOpenOrder(address trader,
        address baseToken,
        int24 lowerTick,
        int24 upperTick


and the function will return a struct:

struct Info {
        uint128 liquidity;
        int24 lowerTick;
        int24 upperTick;
        uint256 lastFeeGrowthInsideX128;
        int256 lastTwPremiumGrowthInsideX96;
        int256 lastTwPremiumGrowthBelowX96;
        int256 lastTwPremiumDivBySqrtPriceGrowthInsideX96;
        uint256 baseDebt;
        uint256 quoteDebt;

How is Net Return calculated?

\[net return = impermanent loss + pending fees for this liquidity\]

To get this number programmatically, you need to get your impermanent position size

impermanentPosition = accountBalance.getTotalPositionSize() - accountBalance.getTakerPositionSize()

and the open notional

impermanentOpenNotional = accountBalance.getTotalOpenNotional() - accountBalance.getTakerOpenNotional()

then you can get

impermanentLoss = impermanentPosition * indexPrice + impermanentOpenNotional

next get pending fees

pendingFees = accountBalance.getPnlAndPendingFee()

now you can calculate net return

netReturn = pendingFees + impermanentLoss

How are base and quote calculated?

We can calculate the base/quote amount in a liquidity at any given time as long as we know the current price at that time.

p_c = current price
p_u = upper price of the liquidity
p_l = lower price of the liquidity
L = liquidity amount

There are 3 cases: 1. current price is in the range:

base amount = (1/√p_c - 1/√p_u) * L
quote amount = (√p_c - √p_l) * L

2. current price is above the upper price, only quote token in the liquidity:

quote amount = (√p_u - √p_l) * L

3. current price is below the lower price, only base toke in the liquidity:

base amount = (1/√p_l - 1/√p_u) * L