Skip to main content

Reader

The Reader contract provides convenience functions for retrieving information such as markets, positions, and pricing data.

This page focuses on the most commonly used read helpers. For the full public surface, see Reader.sol.

Examples below assume you already have contract instances such as reader, dataStore, and referralStorage, plus any required market or price structs.

Market list

Use Reader.getMarkets to retrieve a paginated list of all markets. Each market is represented by a Market.Props struct.

Reader.getMarkets

function getMarkets(DataStore dataStore, uint256 start, uint256 end)
external view returns (Market.Props[] memory)

Example (TypeScript / ethers):

const markets = await reader.getMarkets(dataStore.address, 0, 20);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
startuint256Start index for pagination
enduint256End index for pagination

Returns: Array of Market.Props.

Market.Props

FieldTypeDescription
marketTokenaddressAddress of the GM token for this market
indexTokenaddressAddress of the index token (for example, ETH for the ETH/USD market)
longTokenaddressAddress of the long collateral token
shortTokenaddressAddress of the short collateral token

Detailed market list

Use Reader.getMarketInfoList to retrieve markets with additional runtime data including borrowing rates, funding rates, and whether the market is disabled.

Reader.getMarketInfoList

function getMarketInfoList(
DataStore dataStore,
MarketUtils.MarketPrices[] memory marketPricesList,
uint256 start,
uint256 end
) external view returns (ReaderUtils.MarketInfo[] memory)

Example (TypeScript / ethers):

const marketInfoList = await reader.getMarketInfoList(dataStore.address, marketPricesList, 0, 20);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketPricesListMarketUtils.MarketPrices[]Current prices for each market, in the same order as the markets being queried
startuint256Start index for pagination
enduint256End index for pagination

Returns: Array of ReaderUtils.MarketInfo, which includes the market props, borrowing rates, funding rates, and a flag indicating whether the market is disabled.

MarketUtils.MarketPrices

FieldTypeDescription
indexTokenPricePrice.PropsCurrent price range for the index token
longTokenPricePrice.PropsCurrent price range for the long token
shortTokenPricePrice.PropsCurrent price range for the short token

Price.Props

FieldTypeDescription
minuint256Minimum (bid) price
maxuint256Maximum (ask) price

Getting GM token price

Use Reader.getMarketTokenPrice to retrieve the current GM token price along with detailed pool information: pool value, PnL, token amounts, borrowing fees, and the impact pool.

Reader.getMarketTokenPrice

function getMarketTokenPrice(
DataStore dataStore,
Market.Props memory market,
Price.Props memory indexTokenPrice,
Price.Props memory longTokenPrice,
Price.Props memory shortTokenPrice,
bytes32 pnlFactorType,
bool maximize
) external view returns (int256, MarketPoolValueInfo.Props memory)

Example (TypeScript / ethers):

const [gmPrice, poolInfo] = await reader.getMarketTokenPrice(
dataStore.address,
market,
indexTokenPrice,
longTokenPrice,
shortTokenPrice,
pnlFactorType,
true
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsMarket data struct (see Market list)
indexTokenPricePrice.PropsCurrent price range for the index token
longTokenPricePrice.PropsCurrent price range for the long token
shortTokenPricePrice.PropsCurrent price range for the short token
pnlFactorTypebytes32Which PnL factor cap to apply. Use Keys.MAX_PNL_FACTOR_FOR_TRADERS for trading contexts, Keys.MAX_PNL_FACTOR_FOR_DEPOSITS for deposit simulation, or Keys.MAX_PNL_FACTOR_FOR_WITHDRAWALS for withdrawal simulation.
maximizeboolPass true to use maximum prices (gives the higher GM token price), false to use minimum prices

Returns: A tuple of (int256 price, MarketPoolValueInfo.Props poolInfo) where price is the GM token price and poolInfo contains pool value, PnL, token amounts, borrowing fees, and the impact pool balance.

Position list

Use Reader.getAccountPositions to retrieve all open positions for a given account. The function returns raw position data without fee or pricing calculations.

Reader.getAccountPositions

function getAccountPositions(
DataStore dataStore,
address account,
uint256 start,
uint256 end
) external view returns (Position.Props[] memory)

Example (TypeScript / ethers):

const positions = await reader.getAccountPositions(dataStore.address, account.address, 0, 20);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
accountaddressThe account address to retrieve positions for
startuint256Start index for pagination
enduint256End index for pagination

Returns: Array of Position.Props, each containing the raw position state (size, collateral, entry price, and so on).

Detailed position list

Use Reader.getAccountPositionInfoList to retrieve all open positions for a given account with full fee and pricing context. This function uses the account address and market list directly rather than position keys.

note

Reader.getAccountPositionInfoList uses an account plus market list rather than explicit positionKeys. If you need to query specific keys directly, use Reader.getPositionInfoList.

Reader.getAccountPositionInfoList

function getAccountPositionInfoList(
DataStore dataStore,
IReferralStorage referralStorage,
address account,
address[] memory markets,
MarketUtils.MarketPrices[] memory marketPrices,
address uiFeeReceiver,
uint256 start,
uint256 end
) external view returns (ReaderPositionUtils.PositionInfo[] memory)

Example (TypeScript / ethers):

const positionInfoList = await reader.getAccountPositionInfoList(
dataStore.address,
referralStorage.address,
account.address,
markets,
marketPrices,
ethers.constants.AddressZero,
0,
20
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
referralStorageaddressAddress of the ReferralStorage contract
accountaddressThe account address to retrieve positions for
marketsaddress[]Array of perp market addresses covering every position you expect to retrieve. Must contain only perpetual markets (not swap-only markets).
marketPricesMarketUtils.MarketPrices[]Current prices for each market in the same order as markets. If a returned position's market is missing from this list, the call reverts.
uiFeeReceiveraddressAddress of the UI fee receiver used in fee calculations. Pass the zero address if not applicable.
startuint256Start index for pagination
enduint256End index for pagination

Returns: Array of ReaderPositionUtils.PositionInfo, each including the position, fees, execution price, and PnL.

Position information

Use Reader.getPositionInfo to retrieve full details for a single position, including fees, execution price, and PnL.

Reader.getPositionInfo

function getPositionInfo(
DataStore dataStore,
IReferralStorage referralStorage,
bytes32 positionKey,
MarketUtils.MarketPrices memory prices,
uint256 sizeDeltaUsd,
address uiFeeReceiver,
bool usePositionSizeAsSizeDeltaUsd
) public view returns (ReaderPositionUtils.PositionInfo memory)

Example (TypeScript / ethers):

const positionInfo = await reader.getPositionInfo(
dataStore.address,
referralStorage.address,
positionKey,
prices,
0,
ethers.constants.AddressZero,
false
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
referralStorageaddressAddress of the ReferralStorage contract
positionKeybytes32Key identifying the position
pricesMarketUtils.MarketPricesCurrent prices for the index, long, and short tokens
sizeDeltaUsduint256Used to calculate fees and execution price when simulating a position decrease. Pass 0 if not simulating a size change.
uiFeeReceiveraddressAddress of the UI fee receiver for fee calculations. Pass the zero address if not applicable.
usePositionSizeAsSizeDeltaUsdboolPass true to use the full position size as sizeDeltaUsd (for example, when simulating a full close)

Returns: ReaderPositionUtils.PositionInfo containing the position data, fees, execution price, and PnL.

Detailed position list by key

Use Reader.getPositionInfoList to retrieve detailed position data when you already have an array of position keys.

Reader.getPositionInfoList

function getPositionInfoList(
DataStore dataStore,
IReferralStorage referralStorage,
bytes32[] memory positionKeys,
MarketUtils.MarketPrices[] memory prices,
address uiFeeReceiver
) external view returns (ReaderPositionUtils.PositionInfo[] memory)

Example (TypeScript / ethers):

const positionInfoList = await reader.getPositionInfoList(
dataStore.address,
referralStorage.address,
positionKeys,
prices,
ethers.constants.AddressZero
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
referralStorageaddressAddress of the ReferralStorage contract
positionKeysbytes32[]Position keys to query
pricesMarketUtils.MarketPrices[]Current prices for the markets of the requested positions, in the same order as positionKeys
uiFeeReceiveraddressAddress of the UI fee receiver used in fee calculations. Pass the zero address if not applicable.

Returns: Array of ReaderPositionUtils.PositionInfo values in the same order as positionKeys.

Execution price for increasing or decreasing a position

Use Reader.getExecutionPrice to estimate the execution price for increasing or decreasing a position, accounting for price impact.

Reader.getExecutionPrice

function getExecutionPrice(
DataStore dataStore,
address marketKey,
MarketUtils.MarketPrices memory prices,
uint256 positionSizeInUsd,
uint256 positionSizeInTokens,
int256 sizeDeltaUsd,
int256 pendingImpactAmount,
bool isLong
) external view returns (ReaderPricingUtils.ExecutionPriceResult memory)

Example (TypeScript / ethers):

const executionPriceResult = await reader.getExecutionPrice(
dataStore.address,
marketKey,
prices,
positionSizeInUsd,
positionSizeInTokens,
sizeDeltaUsd,
pendingImpactAmount,
true
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketKeyaddressAddress of the market
pricesMarketUtils.MarketPricesCurrent prices for the index, long, and short tokens
positionSizeInUsduint256Current size of the open position in USD. Pass 0 if there is no existing position.
positionSizeInTokensuint256Current size of the open position in index tokens. Pass 0 if there is no existing position.
sizeDeltaUsdint256Size change in USD. Positive to increase, negative to decrease.
pendingImpactAmountint256Pending price impact amount already accumulated for this position
isLongboolPass true for a long position, false for short

Returns: ReaderPricingUtils.ExecutionPriceResult containing the execution price and price impact amounts.

Output amount for swaps

Use Reader.getSwapAmountOut to estimate how many tokens you receive for a swap, along with the price impact and swap fees.

Reader.getSwapAmountOut

function getSwapAmountOut(
DataStore dataStore,
Market.Props memory market,
MarketUtils.MarketPrices memory prices,
address tokenIn,
uint256 amountIn,
address uiFeeReceiver
) external view returns (uint256, int256, SwapPricingUtils.SwapFees memory fees)

Example (TypeScript / ethers):

const [amountOut, impactAmount, fees] = await reader.getSwapAmountOut(
dataStore.address,
market,
prices,
tokenIn,
amountIn,
ethers.constants.AddressZero
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsMarket to route the swap through
pricesMarketUtils.MarketPricesCurrent prices for the index, long, and short tokens
tokenInaddressAddress of the input token
amountInuint256Amount of the input token
uiFeeReceiveraddressAddress of the UI fee receiver. Pass the zero address if not applicable.

Returns: A tuple of (uint256 amountOut, int256 impactAmount, SwapPricingUtils.SwapFees fees) — the output token amount, price impact amount, and fee breakdown.

Output amount for deposits

Use Reader.getDepositAmountOut to estimate how many GM tokens you receive for a deposit.

Reader.getDepositAmountOut

function getDepositAmountOut(
DataStore dataStore,
Market.Props memory market,
MarketUtils.MarketPrices memory prices,
uint256 longTokenAmount,
uint256 shortTokenAmount,
address uiFeeReceiver,
ISwapPricingUtils.SwapPricingType swapPricingType,
bool includeVirtualInventoryImpact
) external view returns (uint256)

Example (TypeScript / ethers):

const marketTokensOut = await reader.getDepositAmountOut(
dataStore.address,
market,
prices,
longTokenAmount,
shortTokenAmount,
ethers.constants.AddressZero,
swapPricingType,
true
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsThe market to deposit into
pricesMarketUtils.MarketPricesCurrent prices for the index, long, and short tokens
longTokenAmountuint256Amount of the long token to deposit
shortTokenAmountuint256Amount of the short token to deposit
uiFeeReceiveraddressAddress of the UI fee receiver. Pass the zero address if not applicable.
swapPricingTypeISwapPricingUtils.SwapPricingTypePricing method to use for the internal swap component of the deposit
includeVirtualInventoryImpactboolPass true to include virtual inventory impact in the price impact calculation

Returns: uint256 — the estimated number of GM tokens minted.

Output amount for withdrawals

Use Reader.getWithdrawalAmountOut to estimate how many long and short tokens you receive when withdrawing GM tokens.

Reader.getWithdrawalAmountOut

function getWithdrawalAmountOut(
DataStore dataStore,
Market.Props memory market,
MarketUtils.MarketPrices memory prices,
uint256 marketTokenAmount,
address uiFeeReceiver,
ISwapPricingUtils.SwapPricingType swapPricingType
) external view returns (uint256, uint256)

Example (TypeScript / ethers):

const [longTokenAmountOut, shortTokenAmountOut] = await reader.getWithdrawalAmountOut(
dataStore.address,
market,
prices,
marketTokenAmount,
ethers.constants.AddressZero,
swapPricingType
);

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsThe market to withdraw from
pricesMarketUtils.MarketPricesCurrent prices for the index, long, and short tokens
marketTokenAmountuint256Amount of GM tokens to burn
uiFeeReceiveraddressAddress of the UI fee receiver. Pass the zero address if not applicable.
swapPricingTypeISwapPricingUtils.SwapPricingTypePricing method to use for the internal swap component of the withdrawal

Returns: A tuple of (uint256 longTokenAmount, uint256 shortTokenAmount) — the estimated amounts of long and short tokens returned.

Direct lookups

Use these methods when you already have a key, salt, or account and want the raw stored value without additional pricing or fee calculations.

Reader.getMarket

function getMarket(DataStore dataStore, address key)
external view returns (Market.Props memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
keyaddressMarket token address used as the market key

Returns: Market.Props for the requested market.

Reader.getMarketBySalt

function getMarketBySalt(DataStore dataStore, bytes32 salt)
external view returns (Market.Props memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
saltbytes32Deterministic market deployment salt

Returns: Market.Props for the requested market.

Reader.getDeposit

function getDeposit(DataStore dataStore, bytes32 key)
external view returns (Deposit.Props memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
keybytes32Deposit request key

Returns: Raw Deposit.Props for the requested deposit.

Reader.getWithdrawal

function getWithdrawal(DataStore dataStore, bytes32 key)
external view returns (Withdrawal.Props memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
keybytes32Withdrawal request key

Returns: Raw Withdrawal.Props for the requested withdrawal.

Reader.getShift

function getShift(DataStore dataStore, bytes32 key)
external view returns (Shift.Props memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
keybytes32Shift request key

Returns: Raw Shift.Props for the requested shift.

Reader.getPosition

function getPosition(DataStore dataStore, bytes32 key)
external view returns (Position.Props memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
keybytes32Position key

Returns: Raw Position.Props for the requested position.

Reader.getOrder

function getOrder(DataStore dataStore, bytes32 key)
external view returns (Order.Props memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
keybytes32Order key

Returns: Raw Order.Props for the requested order.

Account and position state helpers

Use these methods when you need raw account orders, PnL diagnostics, or liquidation checks rather than the higher-level position info helpers above.

Reader.getPositionPnlUsd

function getPositionPnlUsd(
DataStore dataStore,
Market.Props memory market,
MarketUtils.MarketPrices memory prices,
bytes32 positionKey,
uint256 sizeDeltaUsd
) external view returns (int256, int256, uint256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsMarket for the position
pricesMarketUtils.MarketPricesCurrent prices for the market
positionKeybytes32Position key
sizeDeltaUsduint256Position size delta to evaluate when computing PnL

Returns: A tuple of (positionPnlUsd, cappedPositionPnlUsd, sizeDeltaInTokens).

Reader.isPositionLiquidatable

function isPositionLiquidatable(
DataStore dataStore,
IReferralStorage referralStorage,
bytes32 positionKey,
Market.Props memory market,
MarketUtils.MarketPrices memory prices,
bool shouldValidateMinCollateralUsd,
bool forLiquidation
) public view returns (bool, string memory, PositionUtils.IsPositionLiquidatableInfo memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
referralStorageaddressAddress of the ReferralStorage contract
positionKeybytes32Position key
marketMarket.PropsMarket for the position
pricesMarketUtils.MarketPricesCurrent prices for the market
shouldValidateMinCollateralUsdboolWhether to enforce the minimum collateral USD checks
forLiquidationboolPass true when checking liquidation conditions, false for a soft check

Returns: A tuple of (isLiquidatable, reason, info) with the liquidation result, reason string, and detailed diagnostics.

Reader.getAccountOrders

function getAccountOrders(
DataStore dataStore,
address account,
uint256 start,
uint256 end
) external view returns (ReaderUtils.OrderInfo[] memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
accountaddressAccount to retrieve orders for
startuint256Start index for pagination
enduint256End index for pagination

Returns: Array of ReaderUtils.OrderInfo values for the account.

Market state and PnL helpers

Use these methods when you need one-market diagnostics without going through the paginated market list helpers.

Reader.getMarketInfo

function getMarketInfo(
DataStore dataStore,
MarketUtils.MarketPrices memory prices,
address marketKey
) public view returns (ReaderUtils.MarketInfo memory)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
pricesMarketUtils.MarketPricesCurrent prices for the market
marketKeyaddressMarket token address

Returns: ReaderUtils.MarketInfo for the requested market.

Reader.getPendingPositionImpactPoolDistributionAmount

function getPendingPositionImpactPoolDistributionAmount(
DataStore dataStore,
address market
) external view returns (uint256, uint256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketaddressMarket token address

Returns: A tuple of pending long-token and short-token impact pool distribution amounts.

Reader.getNetPnl

function getNetPnl(
DataStore dataStore,
Market.Props memory market,
Price.Props memory indexTokenPrice,
bool maximize
) external view returns (int256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsMarket to evaluate
indexTokenPricePrice.PropsIndex token price range
maximizeboolWhether to use the maximizing price path

Returns: Net market PnL as an int256.

Reader.getPnl

function getPnl(
DataStore dataStore,
Market.Props memory market,
Price.Props memory indexTokenPrice,
bool isLong,
bool maximize
) external view returns (int256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsMarket to evaluate
indexTokenPricePrice.PropsIndex token price range
isLongboolPass true for long-side PnL
maximizeboolWhether to use the maximizing price path

Returns: Long-side or short-side PnL as an int256.

Reader.getOpenInterestWithPnl

function getOpenInterestWithPnl(
DataStore dataStore,
Market.Props memory market,
Price.Props memory indexTokenPrice,
bool isLong,
bool maximize
) external view returns (int256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketMarket.PropsMarket to evaluate
indexTokenPricePrice.PropsIndex token price range
isLongboolPass true for long-side open interest
maximizeboolWhether to use the maximizing price path

Returns: Open interest with PnL included, as an int256.

Reader.getPnlToPoolFactor

function getPnlToPoolFactor(
DataStore dataStore,
address marketAddress,
MarketUtils.MarketPrices memory prices,
bool isLong,
bool maximize
) external view returns (int256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketAddressaddressMarket token address
pricesMarketUtils.MarketPricesCurrent prices for the market
isLongboolPass true for the long side
maximizeboolWhether to use the maximizing price path

Returns: Current PnL-to-pool factor as an int256.

Pricing and risk helpers

Use these methods when you need direct swap-impact or ADL diagnostics without calling the larger quote helpers.

Reader.getSwapPriceImpact

function getSwapPriceImpact(
DataStore dataStore,
address marketKey,
address tokenIn,
address tokenOut,
uint256 amountIn,
Price.Props memory tokenInPrice,
Price.Props memory tokenOutPrice
) external view returns (int256, int256, int256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketKeyaddressMarket token address
tokenInaddressInput token address
tokenOutaddressOutput token address
amountInuint256Input token amount
tokenInPricePrice.PropsInput token price range
tokenOutPricePrice.PropsOutput token price range

Returns: A tuple of swap price impact values as signed integers.

Reader.getAdlState

function getAdlState(
DataStore dataStore,
address market,
bool isLong,
MarketUtils.MarketPrices memory prices
) external view returns (uint256, bool, int256, uint256)

Parameters:

ParameterTypeDescription
dataStoreaddressAddress of the DataStore contract
marketaddressMarket token address
isLongboolPass true for the long side
pricesMarketUtils.MarketPricesCurrent prices for the market

Returns: A tuple describing the ADL state, including the ADL phase, whether ADL is enabled, the PnL factor, and the minimum PnL factor for ADL.