GMX 백서

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 45쪽 중 32쪽

ositions for | | `markets` | `address[]` | Array of perp market addresses covering every position you expect to retrieve. Must contain only perpetual markets (not swap-only markets). | | `marketPrices` | `MarketUtils.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. | | `uiFeeReceiver` | `address` | Address of the UI fee receiver used in fee calculations. Pass the zero address if not applicable. | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End 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 ```solidity 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):** ```typescript const positionInfo = await reader.getPositionInfo( dataStore.address, referralStorage.address, positionKey, prices, 0, ethers.constants.AddressZero, false ); ``` **Parameters:** | Parameter | Type | Description | | ------------------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `referralStorage` | `address` | Address of the ReferralStorage contract | | `positionKey` | `bytes32` | Key identifying the position | | `prices` | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens | | `sizeDeltaUsd` | `uint256` | Used to calculate fees and execution price when simulating a position decrease. Pass `0` if not simulating a size change. | | `uiFeeReceiver` | `address` | Address of the UI fee receiver for fee calculations. Pass the zero address if not applicable. | | `usePositionSizeAsSizeDeltaUsd` | `bool` | Pass `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. Two aggregate fields are useful when presenting position state: | Field | Composition | | --- | --- | | `pnlAfterPriceImpactUsd` | `basePnlUsd + executionPriceResult.priceImpactUsd` | | `positionValueInUsd` | Collateral valued at its minimum price, plus claimable long-token and short-token funding valued at their minimum prices, plus `basePnlUsd` and `executionPriceResult.totalImpactUsd`, minus total costs valued at the collateral token's maximum price | Both values are signed USD amounts. `positionValueInUsd` uses conservative price bounds for assets and costs, and can be negative. ## Detailed position list by key Use `Reader.getPositionInfoList` to retrieve detailed position data when you already have an array of position keys. ### Reader.getPositionInfoList ```solidity function getPositionInfoList( DataStore dataStore, IReferralStorage referralStorage, bytes32[] memory positionKeys, MarketUtils.MarketPrices[] memory prices, address uiFeeReceiver ) external view returns (ReaderPositionUtils.PositionInfo[] memory) ``` **Example (TypeScript / ethers):** ```typescript const positionInfoList = await reader.getPositionInfoList( dataStore.address, referralStorage.address, positionKeys, prices, ethers.constants.AddressZero ); ``` **Parameters:** | Parameter | Type | Description | | ----------------- | ---------------------------- | ------------------------------------------------------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `referralStorage` | `address` | Address of the ReferralStorage contract | | `positionKeys` | `bytes32[]` | Position keys to query | | `prices` | `MarketUtils.MarketPrices[]` | Current prices for the markets of the requested positions, in the same order as `positionKeys` | | `uiFeeReceiver` | `address` | Address 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 ```solidity 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):** ```typescript const executionPriceResult = await reader.getExecutionPrice( dataStore.address, marketKey, prices, positionSizeInUsd, positionSizeInTokens, sizeDeltaUsd, pendingImpactAmount, true ); ``` **Parameters:** | Parameter | Type | Description | | ---------------------- | -------------------------- | --------------------------------------------------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `marketKey` | `address` | Address of the market | | `prices` | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens | | `positionSizeInUsd` | `uint256` | Current size of the open position in USD. Pass `0` if there is no existing position. | | `positionSizeInTokens` | `uint256` | Current size of the open position in index tokens. Pass `0` if there is no existing position. | | `sizeDeltaUsd` | `int256` | Size change in USD. Positive to increase, negative to decrease. | | `pendingImpactAmount` | `int256` | Pending price impact amount already accumulated for this position | | `isLong` | `bool` | Pass `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 ```solidity 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):** ```typescript const [amountOut, impactAmount, fees] = await reader.getSwapAmountOut( dataStore.address, market, prices, tokenIn, amountIn, ethers.constants.AddressZero ); ``` **Parameters:** | Parameter | Type | Description | | --------------- | -------------------------- | ------------------------------------------------------------------------ | | `dataStore` | `address` | Address of the DataStore contract | | `market` | `Market.Props` | Market to route the swap through | | `prices` | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens | | `tokenIn` | `address` | Address of the input token | | `amountIn` | `uint256` | Amount of the input token | | `uiFeeReceiver` | `address` | Address 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 ```solidity 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):** ```typescript const marketTokensOut = await reader.getDepositAmountOut( dataStore.address, market, prices, longTokenAmount, shortTokenAmount, ethers.constants.AddressZero, swapPricingType, true ); ``` **Parameters:** | Parameter | Type | Description | | ------------------------------- | ----------------------------------- | ------------------------------------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `market` | `Market.Props` | The market to deposit into | | `prices` | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens | | `longTokenAmount` | `uint256` | Amount of the long token to deposit | | `shortTokenAmount` | `uint256` | Amount of the short token to deposit | | `uiFeeReceiver` | `address` | Address of the UI fee receiver. Pass the zero address if not applicable. | | `swapPricingType` | `ISwapPricingUtils.SwapPricingType` | Pricing method to use for the internal swap component of the deposit | | `includeVirtualInventoryImpact` | `bool` | Pass `true` to include virtual inventory impact in the price impact calculation | **Returns:** `uint256` — the estimated number of GM tokens minted, at 18 decimals. :::note Pass `longTokenAmount` and `shortTokenAmount` in each collateral token's **native** decimals (not the index token's), and encode the entries in `prices` as `usdPrice * 10^(30 - tokenDecimals)`. See [Price and amount encoding](#price-and-amount-encoding) for the full convention and a worked example. The returned GM amount is at 18 decimals — divide by `10^18` for display. ::: ## Output amount for withdrawals Use `Reader.getWithdrawalAmountOut` to estimate how many long and short tokens you receive when withdrawing GM tokens. ### Reader.getWithdrawalAmountOut ```solidity 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):** ```typescript const [longTokenAmountOut, shortTokenAmountOut] = await reader.getWithdrawalAmountOut( dataStore.address, market, prices, marketTokenAmount, ethers.constants.AddressZero, swapPricingType ); ``` **Parameters:** | Parameter | Type | Description | | ------------------- | ----------------------------------- | ------------------------------------------------------------------------ | | `dataStore` | `address` | Address of the DataStore contract | | `market` | `Market.Props` | The market to withdraw from | | `prices` | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens | | `marketTokenAmount` | `uint256` | Amount of GM tokens to burn | | `uiFeeReceiver` | `address` | Address of the UI fee receiver. Pass the zero address if not applicable. | | `swapPricingType` | `ISwapPricingUtils.SwapPricingType` | Pricing 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 ```solidity function getMarket(DataStore dataStore, address key) external view returns (Market.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | ------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `address` | Market token address used as the market key | **Returns:** `Market.Props` for the requested market. ### Reader.getMarketBySalt ```solidity function getMarketBySalt(DataStore dataStore, bytes32 salt) external view returns (Market.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | ------------------------------------ | | `dataStore` | `address` | Address of the DataStore contract | | `salt` | `bytes32` | Deterministic market deployment salt | **Returns:** `Market.Props` for the requested market. ### Reader.getDeposit ```solidity function getDeposit(DataStore dataStore, bytes32 key) external view returns (Deposit.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | Deposit request key | **Returns:** Raw `Deposit.Props` for the requested deposit. ### Reader.getWithdrawal ```solidity function getWithdrawal(DataStore dataStore, bytes32 key) external view returns (Withdrawal.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | Withdrawal request key | **Returns:** Raw `Withdrawal.Props` for the requested withdrawal. ### Reader.getShift ```solidity function getShift(DataStore dataStore, bytes32 key) external view returns (Shift.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | Shift request key | **Returns:** Raw `Shift.Props` for the requested shift. ### Reader.getPosition ```solidity function getPosition(DataStore dataStore, bytes32 key) external view returns (Position.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | Position key | **Returns:** Raw `Position.Props` for the requested position. ### Reader.getOrder ```solidity function getOrder(DataStore dataStore, bytes32 key) external view returns (Order.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | Order 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 ```solidity function getPositionPnlUsd( DataStore dataStore, Market.Props memory market, MarketUtils.MarketPrices memory prices, bytes32 positionKey, uint256 sizeDeltaUsd ) external view returns (int256, int256, uint256) ``` **Parameters:** | Parameter | Type | Description | | -------------- | -------------------------- | -------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `market` | `Market.Props` | Market for the position | | `prices` | `MarketUtils.MarketPrices` | Current prices for the market | | `positionKey` | `bytes32` | Position key | | `sizeDeltaUsd` | `uint256` | Position size delta to evaluate when computing PnL | **Returns:** A tuple of `(positionPnlUsd, cappedPositionPnlUsd, sizeDeltaInTokens)`. ### Reader.isPositionLiquidatable ```solidity 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:** | Parameter | Type | Description | | -------------------------------- | -------------------------- | -------------------------------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `referralStorage` | `address` | Address of the ReferralStorage contract | | `positionKey` | `bytes32` | Position key | | `market` | `Market.Props` | Market for the position | | `prices` | `MarketUtils.MarketPrices` | Current prices for the market | | `shouldValidateMinCollateralUsd` | `bool` | Whether to enforce the minimum collateral USD checks | | `forLiquidation` | `bool` | Pass `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 ```solidity function getAccountOrders( DataStore dataStore, address account, uint256 start, uint256 end ) external view returns (ReaderUtils.OrderInfo[] memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `account` | `address` | Account to retrieve orders for | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End 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 ```solidity function getMarketInfo( DataStore dataStore, MarketUtils.MarketPrices memory prices, address marketKey ) public view returns (ReaderUtils.MarketInfo memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | -------------------------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `prices` | `MarketUtils.MarketPrices` | Current prices for the market | | `marketKey` | `address` | Market token address | **Returns:** `ReaderUtils.MarketInfo` for the requested market. ### Reader.getPendingPositionImpactPoolDistributionAmount ```solidity function getPendingPositionImpactPoolDistributionAmount( DataStore dataStore, address market ) external view returns (uint256, uint256) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `market` | `address` | Market token address | **Returns:** A tuple of pending long-token and short-token impact pool distribution amounts. ### Reader.getNetPnl ```solidity function getNetPnl( DataStore dataStore, Market.Props memory market, Price.Props memory indexTokenPrice, bool maximize ) external view returns (int256) ``` **Parameters:** | Parameter | Type | Description | | ----------------- | -------------- | ---------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `market` | `Market.Props` | Market to evaluate | | `indexTokenPrice` | `Price.Props` | Index token price range | | `maximize` | `bool` | Whether to use the maximizing price path | **Returns:** Net market PnL as an `int256`. ### Reader.getPnl ```solidity function getPnl( DataStore dataStore, Market.Props memory market, Price.Props memory indexTokenPrice, bool isLong, bool maximize ) external view returns (int256) ``` **Parameters:** | Parameter | Type | Description | | ----------------- | -------------- | ---------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `market` | `Market.Props` | Market to evaluat
프로젝트로 돌아가기