bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 45쪽 중 27쪽
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `receiver` | Address that receives any output amounts. |
| `cancellationReceiver` | If the order is cancelled, collateral and the network fee gas are sent to this address if it is not the zero address. |
| `callbackContract` | Contract to call on order execution or cancellation. |
| `uiFeeReceiver` | Address that receives the UI fee. |
| `market` | Market to trade in. |
| `initialCollateralToken` | Initial collateral token transferred into the contract. |
| `swapPath` | Array of market addresses to swap `initialCollateralToken` through. For increase and swap orders, the token is swapped before entering the position. For decrease orders, the output is swapped through these markets. |
### CreateOrderParamsNumbers
Direct `ExchangeRouter` calls expect position order price fields in contract price format, not the 30-decimal SDK/UI price. For `triggerPrice` and `acceptablePrice`, encode the value as:
```solidity
contractPrice = price30 / 10 ** indexTokenDecimals
```
This is equivalent to `usdPrice * 10 ** (30 - indexTokenDecimals)`. For example, DOGE has 8 decimals, so a `$0.04` trigger price is `40000000000000000000000000000` in 30-decimal SDK format and `400000000000000000000` in raw contract/order data. The SDK performs this conversion before sending contract calldata.
| Field | Description |
| ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `sizeDeltaUsd` | Position size to increase or decrease. |
| `initialCollateralDeltaAmount` | Amount of input collateral sent to the `OrderVault` for swap and increase orders, or collateral amount to withdraw for decrease orders. |
| `triggerPrice` | Trigger price for `LimitIncrease`, `LimitDecrease`, and `StopLossDecrease` orders, encoded in contract price format. The keeper attempts execution when price reaches this level. |
| `acceptablePrice` | Price at which the order can execute, encoded in contract price format. For market orders, the order is cancelled if it cannot execute at this price. For limit and stop-loss orders, execution is skipped if the trigger price is reached but the acceptable price cannot be filled. |
| `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 order. Any excess is returned to the order account. See [Execution Fee](./fees.md#execution-fee) for details. |
| `callbackGasLimit` | Gas limit passed to the callback contract on order execution or cancellation. |
| `minOutputAmount` | For swap orders, the minimum token output amount. For increase orders, the minimum token amount after `initialCollateralDeltaAmount` is swapped through `swapPath`. For decrease orders, this is the minimum USD value, because decrease orders can produce two output tokens: the profit token and the withdrawn collateral token. |
| `validFromTime` | Timestamp from which the order becomes valid for execution. The keeper skips the order until `block.timestamp` reaches this value. Pass `0` for immediate validity. |
### OrderType
| Value | Description |
| ------------------ | ---------------------------------------------------------------------------------------------------------------- |
| `MarketSwap` | Execute a swap at the current market price. |
| `LimitSwap` | Execute a swap when `minOutputAmount` can be filled. |
| `MarketIncrease` | Open or increase a long or short position at market price. |
| `LimitIncrease` | Open or increase a position when the trigger price is reached. |
| `MarketDecrease` | Close or reduce a position at market price. |
| `LimitDecrease` | Close or reduce a position when the trigger price is reached. |
| `StopLossDecrease` | Close or reduce a position when price falls to the trigger level. |
| `Liquidation` | Forced position closure triggered by the protocol when margin requirements are no longer met. Not user-callable. |
| `StopIncrease` | Open or increase a position when the stop price is reached. |
### DecreasePositionSwapType
| Value | Description |
| ------------------------------- | --------------------------------------------------------------------- |
| `NoSwap` | No swap is performed. |
| `SwapPnlTokenToCollateralToken` | The profit token is swapped to the collateral token, if possible. |
| `SwapCollateralTokenToPnlToken` | The withdrawn collateral is swapped to the profit token, if possible. |
## Creating a deposit
To create a deposit, you must first transfer tokens to the `DepositVault`, then call `ExchangeRouter.createDeposit` in the same transaction.
:::warning
The token transfer and the `ExchangeRouter.createDeposit` call must occur in a single transaction. If they are separated, other users may withdraw the transferred tokens before your deposit is created.
:::
If this transaction reverts, the token transfer also reverts, so no funds remain in the `DepositVault`. 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.
See the [ExchangeRouter tests](https://github.com/gmx-io/gmx-synthetics/blob/updates/test/router/ExchangeRouter.ts) for a complete example.
### ExchangeRouter.createDeposit
```solidity
function createDeposit(IDepositUtils.CreateDepositParams calldata params) returns (bytes32)
```
Use this to add liquidity to a GM market. Transfer the execution fee and any long / short input tokens to the `DepositVault` in the same transaction, usually via `multicall`.
**Example (TypeScript / ethers):**
```typescript
await usdc.approve(router.address, shortTokenAmount);
await exchangeRouter.multicall(
[
exchangeRouter.interface.encodeFunctionData("sendWnt", [depositVault.address, executionFee]),
exchangeRouter.interface.encodeFunctionData("sendTokens", [usdc.address, depositVault.address, shortTokenAmount]),
exchangeRouter.interface.encodeFunctionData("createDeposit", [
{
addresses: {
receiver: liquidityProvider.address,
callbackContract: ethers.constants.AddressZero,
uiFeeReceiver: ethers.constants.AddressZero,
market: marketToken,
initialLongToken: longToken,
initialShortToken: usdc.address,
longTokenSwapPath: [],
shortTokenSwapPath: [],
},
minMarketTokens,
shouldUnwrapNativeToken: false,
executionFee,
callbackGasLimit: 0,
dataList: [],
},
]),
],
{ value: executionFee }
);
```
**Parameters:**
| Parameter | Type | Description |
| --------- | ----------------------------------- | --------------------------------------- |
| `params` | `IDepositUtils.CreateDepositParams` | Struct containing deposit configuration |
### CreateDepositParams
| Field | Description |
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `receiver` | Address that receives the GM tokens. |
| `callbackContract` | Contract to call on deposit execution or cancellation. |
| `uiFeeReceiver` | Address that receives the UI fee. |
| `market` | Market to deposit into. |
| `initialLongToken` | Long token transferred into the contract. |
| `initialShortToken` | Short token transferred into the contract. |
| `longTokenSwapPath` | Array of market addresses to swap `initialLongToken` through before depositing. |
| `shortTokenSwapPath` | Array of market addresses to swap `initialShortToken` through before depositing. |
| `minMarketTokens` | Minimum acceptable amount of GM tokens to receive. |
| `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. |
| `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. |
| `dataList` | Array of additional `bytes32` data. Pass an empty array if no extra data is needed. |
## Creating a withdrawal
To create a withdrawal through `ExchangeRouter`, send the GM market tokens and execution fee to the `WithdrawalVault` and call `ExchangeRouter.createWithdrawal` in the same transaction, usually via `multicall`.
### ExchangeRouter.createWithdrawal
```solidity
function createWithdrawal(IWithdrawalUtils.CreateWithdrawalParams calldata params) returns (bytes32)
```
Use this to create an asynchronous withdrawal request. Transfer the GM market tokens and execution fee to the `WithdrawalVault` in the same transaction, usually via `multicall`. The withdrawal size is determined by the GM market token amount sent to `WithdrawalVault` with `sendTokens`.
**Example (TypeScript / ethers):**
```typescript
await marketToken.approve(router.address, marketTokenAmount);
await exchangeRouter.multicall(
[
exchangeRouter.interface.encodeFunctionData("sendWnt", [withdrawalVault.address, executionFee]),
exchangeRouter.interface.encodeFunctionData("sendTokens", [
marketToken.address,
withdrawalVault.address,
marketTokenAmount,
]),
exchangeRouter.interface.encodeFunctionData("createWithdrawal", [
{
addresses: {
receiver: trader.address,
callbackContract: ethers.constants.AddressZero,
uiFeeReceiver: ethers.constants.AddressZero,
market: marketToken.address,
longTokenSwapPath: [],
shortTokenSwapPath: [],
},
minLongTokenAmount,
minShortTokenAmount,
shouldUnwrapNativeToken: false,
executionFee,
callbackGasLimit: 0,
dataList: [],
},
]),
],
{ value: executionFee }
);
```
**Parameters:**
| Parameter | Type | Description |
| --------- | ----------------------------------------- | ------------------------------------------ |
| `params` | `IWithdrawalUtils.CreateWithdrawalParams` | Struct containing withdrawal configuration |
### CreateWithdrawalParams
The GM market token amount to withdraw is not part of `CreateWithdrawalParams`. It is set by the amount sent to `WithdrawalVault` in the preceding `sendTokens` call.
| Field | Description |
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `receiver` | Address that receives the withdrawn tokens. |
| `callbackContract` | Contract to call on withdrawal execution or cancellation. |
| `uiFeeReceiver` | Address that receives the UI fee. |
| `market` | Market to withdraw from. |
| `longTokenSwapPath` | Array of market addresses to swap the withdrawn long token through. |
| `shortTokenSwapPath` | Array of market addresses to swap the withdrawn short token through. |
| `minLongTokenAmount` | Minimum output amount of long token after swapping through `longTokenSwapPath`. For example, if WETH is swapped to BTC, this specifies the minimum BTC amount. |
| `minShortTokenAmount` | Minimum output amount of short token after swapping through `shortTokenSwapPath`. |
| `shouldUnwrapNativeToken` | Whether to unwrap the native token on output. For example, if the output token is WETH and this is `true`, the contract converts WETH to ETH before sending. |
| `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 withdrawal. Any excess is returned to the withdrawal account. See [Execution Fee](./fees.md#execution-fee) for details. |
| `callbackGasLimit` | Gas limit passed to the callback contract on withdrawal execution or cancellation. |
| `dataList` | Array of additional `bytes32` data. Pass an empty array if no extra data is needed. |
## Creating an atomic withdrawal
An atomic withdrawal executes synchronously without waiting for a keeper. It uses on-chain Chainlink price feeds rather than off-chain oracle data, so only tokens with a registered `ChainlinkPriceFeedProvider` are supported.
### ExchangeRouter.executeAtomicWithdrawal
```solidity
function executeAtomicWithdrawal(
IWithdrawalUtils.CreateWithdrawalParams calldata params,
OracleUtils.SetPricesParams calldata oracleParams
)
```
Use this to withdraw synchronously using on-chain Chainlink prices instead of the normal keeper flow. Swaps are not supported, so both swap paths must be empty. As with standard withdrawals, the withdrawal size is determined by the GM market token amount sent to `WithdrawalVault`.
**Example (TypeScript / ethers):**
```typescript
await marketToken.approve(router.address, marketTokenAmount);
await exchangeRouter.multicall(
[
exchangeRouter.interface.encodeFunctionData("sendWnt", [withdrawalVault.address, executionFee]),
exchangeRouter.interface.encodeFunctionData("sendTokens", [
marketToken.address,
withdrawalVault.address,
marketTokenAmount,
]),
exchangeRouter.interface.encodeFunctionData("executeAtomicWithdrawal", [
{
addresses: {
receiver: trader.address,
callbackContract: ethers.constants.AddressZero,
uiFeeReceiver: ethers.constants.AddressZero,
market: marketToken.address,
longTokenSwapPath: [],
shortTokenSwapPath: [],
},
minLongTokenAmount,
minShortTokenAmount,
shouldUnwrapNativeToken: false,
executionFee,
callbackGasLimit: 0,
dataList: [],
},
{
tokens: [indexToken.address, longToken.address, shortToken.address],
providers: [
chainlinkPriceFeedProvider.address,
chainlinkPriceFeedProvider.address,
chainlinkPriceFeedProvider.address,
],
data: ["0x", "0x", "0x"],
},
]),
],
{ value: executionFee }
);
```
**Parameters:**
| Parameter | Type | Description |
| -------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `params` | `IWithdrawalUtils.CreateWithdrawalParams` | Withdrawal configuration, using the same fields as a standard withdrawal with the constraints listed in [CreateWithdrawalParams for atomic withdrawal](#createwithdrawalparams-for-atomic-withdrawal). |
| `oracleParams` | `OracleUtils.SetPricesParams` | Oracle price inputs required for atomic execution. |
### CreateWithdrawalParams for atomic withdrawal
| Field | Description |
| -------------------- | -------------------------------------------------------------- |
| `longTokenSwapPath` | Must be empty. Swaps are not supported for atomic withdrawals. |
| `shortTokenSwapPath` | Must be empty. Swaps are not supported for atomic withdrawals. |
| All other fields | Same as [CreateWithdrawalParams](#createwithdrawalparams). |
### OracleUtils.SetPricesParams
| Field | Description |
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tokens` | Array of token addresses: the index token, long token, and short token of the market. |
| `providers` | Array of price providers for each token. Use the `ChainlinkPriceFeedProvider` address found in the `deployments` folder. To check whether a token is supported, call `dataStore.getAddress(Keys.priceFeedKey(token))` and verify the result is not the zero address. |
| `data` | Array of provider data. Pass an array of `0x` empty bytes for Chainlink feeds. |
## Creating a shift
A shift moves liquidity from one GM market to another in a single request. Instead of withdrawing from the source market and depositing into the destination market separately, you can use `createShift` to do both atomically. Transfer the GM market tokens from the source market and the execution fee to the `ShiftVault` before calling `ExchangeRouter.createShift` in the same transaction.
:::warning
The token transfer and the `ExchangeRouter.createShift` call must occur in a single transaction. If they are separated, other users may withdraw the transferred tokens before your shift is created.
:::
### ExchangeRouter.createShift
```solidity
function createShift(IShiftUtils.CreateShiftParams calldata params) returns (bytes32)
```
Use this to move GM liquidity from one market to another. Transfer the source market's GM tokens and the execution fee to the `ShiftVault` in the same transaction, usually via `multicall`. The shift amount is determined by the GM token amount sent to `ShiftVault` with `sendTokens`.
**Example (TypeScript / ethers):**
```typescript
await sourceMarketToken.approve(router.address, marketTokenAmount);
await exchangeRouter.multicall(
[
exchangeRouter.interface.encodeFunctionData("sendWnt", [shiftVault.address, executionFee]),
exchangeRouter.interface.encodeFunctionData("sendTokens", [
sourceMarketToken.address,
shiftVault.address,
marketTokenAmount,
]),
exchangeRouter.interface.encodeFunctionData("createShift", [
{
addresses: {
receiver: account.address,
callbackContract: ethers.constants.AddressZero,
uiFeeReceiver: ethers.constants.AddressZero,
fromMarket: sourceMarketToken.address,
toMarket: destinationMarketToken.address,
},
minMarketTokens,
executionFee,
callbackGasLimit: 0,
dataList: [],
},
]),
],
{ value: executionFee }
);
```
**Parameters:**
| Parameter | Type | Description |
| --------- | --------------------------------- | ------------------------------------- |
| `params` | `IShiftUtils.CreateShiftParams` | Struct containing shift configuration |
**Returns:** `bytes32` -- the unique key identifying the shift request.
### CreateShiftParams
The GM market token amount to shift is not part of `CreateShiftParams`. It is set by the amount sent to `ShiftVault` in the preceding `sendTokens` call.
| Field | Description |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `receiver` | Address that receives the destination market's GM tokens. |
| `callbackContract` | Contract to call on shift execution or cancellation. |
| `uiFeeReceiver` | Address that receives the UI fee. |
| `fromMarket` | Source GM market to withdraw liquidity from. |
| `toMarket` | Destination GM market to deposit liquidity into. |
| `minMarketTokens` | Minimum acceptable amount of destination GM 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 shift. Any excess is returned to the shift account. See [Execution Fee](./fees.md#execution-fee) for details. |
| `callbackGasLimit` | Gas limit passed to the callback contract on shift execution or cancellation. |
| `dataList` | Array of additional `bytes32` data. Pass an empty array if no extra data is needed. |
## Updating an order
You can modify the parameters of an existing pending order by calling `updateOrder`. Only the order's creator can update it, and market orders can't be updated. Any additional WNT trans프로젝트로 돌아가기