Perp v2
Perp v2
  • About Perpetual Protocol
    • Social Links
    • Governance
    • v2 Era Ecosystem
    • PERP Token
  • Terms of Service
  • General
    • Wallets
    • Transfering Assets
    • Deposit & Withdrawal
    • Perpetual + Uniswap
    • FAQs
    • Guides
    • Legacy Reward Programs
    • Security
  • Hot Tub
  • Trading on Perp v2
    • How It Works
    • UI Overview
    • Perpetual Futures Basics
    • Trading Quick-start
    • Opening and Closing Positions
    • Account Value & More
    • Funding Payments
    • Order Types
    • Trading Fees and Gas Fees
    • Managing Risk
    • Multi-collateral
    • More docs
  • Providing Liquidity
    • Basics
    • Introduction to Maker UI
    • Add / Remove Liquidity
    • Estimated Fees and Rewards APR
    • More on LPing
    • Tools for LPs
  • Perp v2 Specs
  • Developer Docs
    • Developer FAQs
    • Source Code
    • Protocol Attributes
    • Contract guide
      • AccountBalance
      • BaseToken
      • ClearingHouse
      • CollateralManager
      • DelegateApproval
      • Exchange
      • InsuranceFund
      • MarketRegistry
      • OrderBook
      • QuoteToken
      • Vault
      • VirtualToken
    • Interface Contracts
      • IAccountBalance
      • IBaseToken
      • IClearingHouse
      • IClearingHouseConfig
      • IClearingHouseConfigEvent
      • ICollateralManager
      • IDelegateApproval
      • IERC20Metadata
      • IExchange
      • IIndexPrice
      • IInsuranceFund
      • IMarketRegistry
      • IOrderBook
      • IVault
      • IVirtualToken
    • Integration guide
    • Dev tools
Powered by GitBook
On this page
  • Is there an API?
  • How do I get quotes for a given asset?
  • How do I obtain a value for liquidity?
  • How is Net Return calculated?
  • How are base and quote calculated?

Was this helpful?

  1. Developer Docs

Developer FAQs

Last updated 1 year ago

Was this helpful?

Content Out of Date

This content is not maintained and refers to an out-of-date version of Perpetual Protocol.

For the latest documentation, see

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.

Useful subgraphs

How do I get quotes for a given asset?

AKA how much vUSD will I pay for 1 vETH and similar questions.

You can get quotes using our quoter contract:

Example code:

export class QuoterService {
    constructor(private readonly contractProxyFactory: ContractProxyFactory) {}
    async getPerpAveragePrice(baseToken: string, amountType: AmountType, amount: Big) {
        const quoterProxy = await this.contractProxyFactory.createQuoterProxy()
        const side = amount.gt(0) ? Side.LONG : Side.SHORT
        const { isBaseToQuote, isExactInput } = convertSideAndAmountType(side, amountType)
        const swapResp = await quoterProxy.quote({
            baseToken,
            isBaseToQuote,
            isExactInput,
            amount: toWei(amount.abs()),
            sqrtPriceLimitX96: Big(0),
        })
        return swapResp.exchangedPositionNotional.div(swapResp.exchangedPositionSize).abs()
    }
}

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?

netReturn=impermanentLoss+pendingFeesForThisLiquiditynet Return = impermanent Loss + pending Fees For This LiquiditynetReturn=impermanentLoss+pendingFeesForThisLiquidity

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

For more info about `isBaseToQuote` and other values, see the.

https://docs.perp.com
Perpetual Protocol v2
Uniswap Optimism
0xE99d0FBe203E3697285166f599d8E57f8e335ed0
dev docs