The Graph API

  • Updated

Perpetual Protocol does not provide an official API. Instead it relies on services such as The Graph. This is to ensure the maximum distribution and decentralization possible at this point in the project's development.

The Graph is intended as a data collection, and may not be realtime.

Important Links

Definitions

đźš§ Definitions will be added as they are requested. Please come to our Discord and request in in the #coding-chat channel!

FundingUpdated

  • Updates the global funding params which are used to calculate each trader's funding payment
  • Updated per timestamp

FundingPaymentSettled

  • Settles a trader's funding payment into owedRealizedPnl
  • Calculated before executing openPosition, closePosition, addLiquidity, removeLiquidity.

dailyFundingRate

  • Shows funding rate from past 24h
  • app.perp.com shows dailyFundingRate / 24 * 100

exchangedPositionNotional

  • Shows value of a trade in USD
  • Includes non-fee generating swaps, so aggregate fees will be less than 0.1% of this value

Examples

See more examples in the “playground” at The Graph link above.

image

Query latest Funding Rate

{
fundingUpdateds(orderBy: blockNumberLogIndex, orderDirection: desc) {
id
baseToken
dailyFundingRate
timestamp
}
}

Query your funding payment history

{
trader(id: "0x9c1e0c67aa30c063f341885b12cb81cc94613fc7") {
positions(where:{baseToken:"0x8c835dfaa34e2ae61775e80ee29e2c724c6ae2bb"}){
fundingPayment
timestamp
}
}
}

Query Maker Position Change

{
  liquidityChangeds(
    where:{maker:"0x0000000your0address"},
    orderBy: blockNumberLogIndex, 
    orderDirection: desc
  ) {
    id
    txHash
    maker
    baseToken
    quoteToken
    lowerTick
    upperTick
    base
    quote
    liquidity
    quoteFee
    blockNumber
    timestamp
  }
}

Query Liquidation

{
  positionLiquidateds(
    where:{trader:"0x0000000your0address"}
) {
    txHash
    trader
    baseToken
    liquidator
    liquidationFee
    positionSizeAbs
    positionNotionalAbs
    blockNumber
    timestamp
  }
}

Query prices

{
positionChangeds(
orderBy: blockNumberLogIndex,
orderDirection: desc,
where:
{
baseToken:"0x8C835DFaA34e2AE61775e80EE29E2c724c6AE2BB"
}
)
{
swappedPrice
entryPriceAfter
marketPriceAfter
timestamp
}
}