GMX 백서

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

is non-empty and limits oracle-mispricing exposure while converting the fee token. Exceeding it reverts with `MaxRelayFeeSwapExceeded`. Subaccount relay calls are checked against both caps. After the global check passes, the relay router also compares the fee value against `MAX_RELAY_FEE_SWAP_USD_FOR_SUBACCOUNT` and reverts with `MaxRelayFeeSwapForSubaccountExceeded` when the subaccount cap is exceeded. This limits a malicious subaccount's ability to pair a large negative-impact fee swap with a personal positive-impact swap. Read both values from the `DataStore` rather than assuming the subaccount cap equals the global one, and size delegated relay-fee swaps against the lower of the two. ## Atomic operation parameters These parameters control behavior for atomic (synchronous) operations that use on-chain price feeds. | Key | Description | | --- | --- | | `MAX_ATOMIC_ORACLE_PRICE_AGE` | Maximum acceptable age for oracle prices in atomic operations | ## Advanced parameters These parameters control specialized protocol behavior. | Key | Description | | --- | --- | | `DATA_STREAM_SPREAD_REDUCTION_FACTOR` | Factor applied to reduce the bid-ask spread from data stream oracle prices | | `GLV_SHIFT_MAX_LOSS_FACTOR` | Maximum acceptable loss factor when executing GLV shifts | | `BUYBACK_GMX_FACTOR` | Factor controlling GMX token buyback from protocol fees | | `BUYBACK_MAX_PRICE_IMPACT_FACTOR` | Maximum price impact allowed for buyback operations | --- ## GlvReader The `GlvReader` contract provides read-only functions for querying GMX Liquidity Vault (GLV) data from on-chain storage. For GM market reader functions, see [Reader](./reader.md). This page focuses on the most commonly used GLV read helpers. For the full public surface, see [`GlvReader.sol`](https://github.com/gmx-io/gmx-synthetics/blob/updates/contracts/reader/GlvReader.sol). Examples below assume you already have contract instances such as `glvReader` and `dataStore`, plus any required price structs. ## Getting a list of GLVs Use `getGlvs` to retrieve all registered GLV vaults. ### GlvReader.getGlvs ```solidity function getGlvs(DataStore dataStore, uint256 start, uint256 end) external view returns (Glv.Props[] memory) ``` **Example (TypeScript / ethers):** ```typescript const glvs = await glvReader.getGlvs(dataStore.address, 0, 20); ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End index for pagination | **Returns:** Array of `Glv.Props`. ### Glv.Props | Field | Type | Description | | ------------ | --------- | -------------------------- | | `glvToken` | `address` | Address of the GLV token | | `longToken` | `address` | Address of the long token | | `shortToken` | `address` | Address of the short token | ## Getting GLV info with market list Use `getGlvInfoList` to retrieve GLV vaults together with the markets each vault is exposed to. ### GlvReader.getGlvInfoList ```solidity function getGlvInfoList(DataStore dataStore, uint256 start, uint256 end) external view returns (GlvInfo[] memory) ``` **Example (TypeScript / ethers):** ```typescript const glvInfoList = await glvReader.getGlvInfoList(dataStore.address, 0, 20); ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End index for pagination | **Returns:** Array of `GlvInfo` elements, each containing a `Glv.Props` and a list of market addresses. ## Getting GLV value Use `getGlvValue` to retrieve the total GLV vault value before converting it into a token price. ### GlvReader.getGlvValue ```solidity function getGlvValue( DataStore dataStore, address[] memory marketAddresses, Price.Props[] memory indexTokenPrices, Price.Props memory longTokenPrice, Price.Props memory shortTokenPrice, address glv, bool maximize ) external view returns (uint256) ``` **Example (TypeScript / ethers):** ```typescript const glvValue = await glvReader.getGlvValue( dataStore.address, marketAddresses, indexTokenPrices, longTokenPrice, shortTokenPrice, glvAddress, true ); ``` **Parameters:** | Parameter | Type | Description | | ------------------ | --------------- | --------------------------------------------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `marketAddresses` | `address[]` | Addresses of the markets inside the GLV | | `indexTokenPrices` | `Price.Props[]` | Prices for the index tokens of each GLV market. The order must match `marketAddresses`. | | `longTokenPrice` | `Price.Props` | Price of the long token | | `shortTokenPrice` | `Price.Props` | Price of the short token | | `glv` | `address` | Address of the GLV vault | | `maximize` | `bool` | When `true`, uses maximum token prices. When `false`, uses minimum prices. | **Returns:** Total GLV vault value in 30-decimal USD precision. ## Getting GLV token price Use `getGlvTokenPrice` to retrieve a GLV token price for the supplied market list, token prices, and min/max direction. The call also returns total vault value and total supply. Use this method when you need direct GMX Reader valuation for a specific block and supplied price context, or when a Chainlink Data Feed is not available. For external protocol integrations that only need a standard oracle interface, prefer Chainlink Data Feeds where available. For the integration decision tree, see [Getting GM and GLV token prices](../gm-glv-prices.md). ### GlvReader.getGlvTokenPrice ```solidity function getGlvTokenPrice( DataStore dataStore, address[] memory marketAddresses, Price.Props[] memory indexTokenPrices, Price.Props memory longTokenPrice, Price.Props memory shortTokenPrice, address glv, bool maximize ) external view returns (uint256, uint256, uint256) ``` **Example (TypeScript / ethers):** ```typescript const [glvPrice, glvValue, totalSupply] = await glvReader.getGlvTokenPrice( dataStore.address, marketAddresses, indexTokenPrices, longTokenPrice, shortTokenPrice, glvAddress, true ); ``` **Parameters:** | Parameter | Type | Description | | ------------------ | --------------- | --------------------------------------------------------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `marketAddresses` | `address[]` | Addresses of the markets inside the GLV | | `indexTokenPrices` | `Price.Props[]` | Prices for the index tokens of each GLV market. The order must match `marketAddresses`. | | `longTokenPrice` | `Price.Props` | Price of the long token | | `shortTokenPrice` | `Price.Props` | Price of the short token | | `glv` | `address` | Address of the GLV vault | | `maximize` | `bool` | When `true`, uses maximum token prices. When `false`, uses minimum prices. | **Returns:** A tuple of `(uint256 price, uint256 glvValue, uint256 totalSupply)`. ## Direct GLV lookups Use these methods when you already have a GLV address or deployment salt and want the raw stored object. ### GlvReader.getGlv ```solidity function getGlv(DataStore dataStore, address glv) external view returns (Glv.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `glv` | `address` | GLV token address | **Returns:** `Glv.Props` for the requested GLV. ### GlvReader.getGlvInfo ```solidity function getGlvInfo(DataStore dataStore, address glv) public view returns (GlvInfo memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `glv` | `address` | GLV token address | **Returns:** `GlvInfo`, containing the GLV props plus its supported market list. ### GlvReader.getGlvBySalt ```solidity function getGlvBySalt(DataStore dataStore, bytes32 salt) external view returns (Glv.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `salt` | `bytes32` | Deterministic GLV deployment salt | **Returns:** `Glv.Props` for the requested GLV. ## GLV deposit requests Use these methods to inspect GLV deposit requests by key, globally, or for one account. ### GlvReader.getGlvDeposit ```solidity function getGlvDeposit(DataStore dataStore, bytes32 key) external view returns (GlvDeposit.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | GLV deposit request key | **Returns:** Raw `GlvDeposit.Props` for the request. ### GlvReader.getGlvDeposits ```solidity function getGlvDeposits( DataStore dataStore, uint256 start, uint256 end ) external view returns (GlvDeposit.Props[] memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End index for pagination | **Returns:** Array of `GlvDeposit.Props` values. ### GlvReader.getAccountGlvDeposits ```solidity function getAccountGlvDeposits( DataStore dataStore, address account, uint256 start, uint256 end ) external view returns (GlvDeposit.Props[] memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | ------------------------------------ | | `dataStore` | `address` | Address of the DataStore contract | | `account` | `address` | Account to retrieve GLV deposits for | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End index for pagination | **Returns:** Array of `GlvDeposit.Props` values for the account. ## GLV withdrawal requests Use these methods to inspect GLV withdrawal requests by key, globally, or for one account. ### GlvReader.getGlvWithdrawal ```solidity function getGlvWithdrawal(DataStore dataStore, bytes32 key) external view returns (GlvWithdrawal.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | GLV withdrawal request key | **Returns:** Raw `GlvWithdrawal.Props` for the request. ### GlvReader.getGlvWithdrawals ```solidity function getGlvWithdrawals( DataStore dataStore, uint256 start, uint256 end ) external view returns (GlvWithdrawal.Props[] memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End index for pagination | **Returns:** Array of `GlvWithdrawal.Props` values. ### GlvReader.getAccountGlvWithdrawals ```solidity function getAccountGlvWithdrawals( DataStore dataStore, address account, uint256 start, uint256 end ) external view returns (GlvWithdrawal.Props[] memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `account` | `address` | Account to retrieve GLV withdrawals for | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End index for pagination | **Returns:** Array of `GlvWithdrawal.Props` values for the account. ## GLV shift requests Use these methods to inspect GLV shift requests by key or through paginated lists. ### GlvReader.getGlvShift ```solidity function getGlvShift(DataStore dataStore, bytes32 key) external view returns (GlvShift.Props memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `key` | `bytes32` | GLV shift request key | **Returns:** Raw `GlvShift.Props` for the request. ### GlvReader.getGlvShifts ```solidity function getGlvShifts( DataStore dataStore, uint256 start, uint256 end ) external view returns (GlvShift.Props[] memory) ``` **Parameters:** | Parameter | Type | Description | | ----------- | --------- | --------------------------------- | | `dataStore` | `address` | Address of the DataStore contract | | `start` | `uint256` | Start index for pagination | | `end` | `uint256` | End index for pagination | **Returns:** Array of `GlvShift.Props` values. --- ## GlvRouter The `GlvRouter` contract is the main entry point for creating and cancelling GLV (GMX Liquidity Vault) deposits and withdrawals. It works similarly to the [ExchangeRouter](./exchange-router.md) but targets GLV vaults instead of individual GM markets. For an overview of the contract architecture and execution model, see [Architecture](./architecture.md). ## Creating a GLV deposit To create a GLV deposit, you must first transfer the required tokens to the `GlvVault`, then call `GlvRouter.createGlvDeposit` in the same transaction. :::warning The token transfer and the `GlvRouter.createGlvDeposit` call must occur in a single transaction. If they are separated, other users may withdraw the transferred tokens before your deposit is created. ::: You can deposit in two ways: - **Underlying tokens** -- Transfer long and short tokens to the `GlvVault`. The protocol mints GM tokens internally, then mints GLV tokens to the receiver. - **GM market tokens** -- Transfer GM tokens directly to the `GlvVault` by setting `isMarketTokenDeposit` to `true`. The protocol skips the GM minting step and mints GLV tokens to the receiver. If this transaction reverts, the token transfer also reverts, so no funds remain in the `GlvVault`. If the deposit request is created successfully and is later cancelled, the initial deposit tokens are returned to the account that created the deposit, and any unused execution fee is refunded separately. ### GlvRouter.createGlvDeposit ```solidity function createGlvDeposit( IGlvDepositUtils.CreateGlvDepositParams calldata params ) external payable returns (bytes32) ``` Use this to add liquidity to a GLV vault. Transfer the execution fee and any long / short input tokens (or GM tokens) to the `GlvVault` in the same transaction, usually via `multicall`. **Example (TypeScript / ethers):** ```typescript await usdc.approve(router.address, shortTokenAmount); await glvRouter.multicall( [ glvRouter.interface.encodeFunctionData("sendWnt", [glvVault.address, executionFee]), glvRouter.interface.encodeFunctionData("sendTokens", [usdc.address, glvVault.address, shortTokenAmount]), glvRouter.interface.encodeFunctionData("createGlvDeposit", [ { addresses: { glv: glvToken.address, market: marketToken, receiver: liquidityProvider.address, callbackContract: ethers.constants.AddressZero, uiFeeReceiver: ethers.constants.AddressZero, initialLongToken: longToken, initialShortToken: usdc.address, longTokenSwapPath: [], shortTokenSwapPath: [], }, minGlvTokens, executionFee, callbackGasLimit: 0, shouldUnwrapNativeToken: false, isMarketTokenDeposit: false, dataList: [], }, ]), ], { value: executionFee } ); ``` **Parameters:** | Parameter | Type | Description | | --------- | --------------------------------------------- | ------------------------------------------- | | `params` | `IGlvDepositUtils.CreateGlvDepositParams` | Struct containing GLV deposit configuration | **Returns:** `bytes32` -- the key that identifies this GLV deposit request. ### CreateGlvDepositParams The deposit struct is split into address fields and a set of numeric fields and flags. | Field | Description | | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `addresses` | Group of address fields listed in [CreateGlvDepositParamsAddresses](#createglvdepositparamsaddresses). | | `minGlvTokens` | Minimum acceptable amount of GLV tokens to receive. | | `executionFee` | Amount of native token (for example, ETH on Arbitrum) included as the execution fee. This is the maximum fee keepers can use to execute the deposit. Any excess is returned to the deposit account. See [Execution Fee](./fees.md#execution-fee) for details. | | `callbackGasLimit` | Gas limit passed to the callback contract on deposit execution or cancellation. | | `shouldUnwrapNativeToken` | Whether to unwrap the native token if the deposit is cancelled. For example, if `initialLongToken` is WETH and this is `true`, the contract converts WETH to ETH before refunding. | | `isMarketTokenDeposit` | Set to `true` when depositing GM market tokens directly instead of underlying long/short tokens. When `true`, `initialLongToken` and `initialShortToken` must be the zero address, and both swap paths must be empty. | | `dataList` | Array of additional `bytes32` data. Pass an empty array if no extra data is needed. | ### CreateGlvDepositParamsAddresses | Field | Description | | -------------------- | --------------------------------------------------------------------------------------- | | `glv` | Address of the GLV vault to deposit into. | | `market` | GM market to deposit into. Must be a market that the GLV vault supports. | | `receiver` | Address that receives the GLV tokens. | | `callbackContract` | Contract to call on deposit execution or cancellation. | | `uiFeeReceiver` | Address that receives the UI fee. | | `initialLongToken` | Long token transferred into the contract. Set to the zero address for GM token deposits.| | `initialShortToken` | Short token transferred into the contract. Set to the zero address for GM token deposits.| | `longTokenSwapPath` | Array of market addresses to swap `initialLongToken` through before depositing. | | `shortTokenSwapPath` | Array of market addresses to swap `initialShortToken` through before depositing. | ## Creating a GLV withdrawal To create a GLV withdrawal, send the GLV tokens and execution fee to the `GlvVault` and call `GlvRouter.createGlvWithdrawal` in the same transaction, usually via `multicall`. :::warning The token transfer and the `GlvRouter.createGlvWithdrawal` call must occur in a single transaction. If they are separated, other users may withdraw the transferred tokens before your withdrawal is created. ::: ### GlvRouter.createGlvWithdrawal ```solidity function createGlvWithdrawal( IGlvWithdrawalUtils.CreateGlvWithdrawalParams calldata params ) external payable returns (bytes32) ``` Use this to create an asynchronous GLV withdrawal request. Transfer the GLV tokens and execution fee to the `GlvVault` in the same transaction, usually via `multicall`. The withdrawal size is determined by the GLV token amount sent to the `GlvVault` with `sendTokens`. **Example (TypeScript / ethers):** ```typescript await glvToken.approve(router.address, glvTokenAmount); await glvRouter.multicall( [ glvRouter.interface.encodeFunctionData("sendWnt", [glvVault.address, executionFee]), glvRouter.interface.encodeFunctionData("sendTokens", [ glvToken.address, glvVault.address, glvTokenAmount, ]), glvRouter.interface.encodeFunctionData("createGlvWithdrawal", [ { addresses: { receiver: trader.address, callbackContract: ethers.constants.AddressZero, uiFeeReceiver: ethers.constants.Addres
프로젝트로 돌아가기