ABI 交互

在这里你可以找到如何直接与我们的协议互动的信息

简介

KTX.Finance 作为去中心化的永续合约交易所运营,所有功能都可通过相应的 RPC 公开访问 RPCs。 本指南介绍了如何直接与 KTX.Finance 上负责执行交易功能的智能合约进行交互。

开仓交易 - 市价订单和限价订单 (Arbitrum / Mantle)

在同一交易中开立头寸和或设置限价订单。

查找 ABI of ComplexOrderRouter.sol 在不同链:


  • Call createComplexOrder with following parameters:

    • _path(address[]):

      • [collateralToken] or [tokenIn, collateralToken] if a swap is needed

      • without swap:

        • for long orders, collateral token must be the same as index token

        • for short orders, collateral token must be stable token

        • e.g.

          • long BTC with BTC as collateral, _path = ["BTC address"]

          • long ETH with ETH as collateral, _path = ["ETH address"]

          • short BTC with USDT as collateral, _path = ["USDT address"]

      • with swap:

        • tokenIn will be swapped into collateralToken before creates position. This is not recommended as swap fee applies

      • required field

    • _amountIn(uint256):

      • collateral token amount

      • always fill in amount of token as in collateral token, e.g.

        • if _path = ["ETH address"], 3*10^18 means 3 ETH

        • if _path = ["USDT address", "ETH address"], 3*10^6 means 3 USDT because USDT has 6 decimals

      • required field

    • _minOut(uint256):

      • swap minimum out

      • 0 if no swap required

      • required field

    • _isLong(bool):

      • true: long order

      • false: short order

      • required field

    • _referralCode(bytes32):

      • depreciated, fill in 0x0000000000000000000000000000000000000000000000000000000000000000

      • required field

    • _sizeDelta(uint256[]):

      • _sizeDelta[0]: position size

      • _sizeDelta[1]:

        • decrease position size when take profit happens

        • normally _sizeDelta[1] = _sizeDelta[0], because you may want to close the entire position when take profit happens

        • 0 if you do not want to create take profit order

      • _sizeDelta[2]:

        • decrease position size when stop loss happens

        • normally _sizeDelta[2] = _sizeDelta[0], because you may want to close the entire position when stop loss happens

        • 0 if you do not want to create stop loss order

      • leverage = _sizeDelta[0]/_amountIn, and please keep leverage at 2 - 100 to avoid unexpected liquidation.

      • e.g.

        • [6**10^18, 0, 0]* for ETH means you simply want to open a position with size = 3ETH

        • [610^18, 610^18, 6*10^18] for ETH means you want to open a position with size = 3ETH and fully close it when take profit or stop loss order happens.

    • _price(uint256[]):

      • _price[0]: acceptable price for position increase

        • 30 decimal precision (1*10^30 means price at $1)

        • price is changing every moment and we use this price as your acceptable price, normally you can calculate this price using: current_mark_price * (1 - acceptable_slippage) for long order and current_mark_price * (1 + acceptable_slippage) for short order. KTX by default uses 0.3% as acceptable_slippage on our website.

        • _price[1]:

          • trigger price for take profit order

          • for long order, take profit price must be greater than _price[0]

          • for short order, take profit price must be smaller than _price[0]

          • 0 if you do not want to create take profit order

        • _price[2]:

          • trigger price for stop loss order

          • for long order, stop loss price must be smaller than _price[0]

          • for short order, stop loss price must be greater than _price[0]

          • 0 if you do not want to create stop loss order

    • _token(address[]):

      • _token[0]:

        • index token

      • _token[1]:

        • take-back token when take profit happens

      • _token[2]:

        • take-back token when stop loss happens

      • normally _token[0] = _token[1] = _token[2] to avoid swap fees

      • e.g.["ETH address", "ETH address", "ETH address"] for creating long ETH position

    • _executionFee(uint256[]):

      • _executionFee[0]:

        • 10^17 for mantle, 2*10^14 for arbitrum

      • _executionFee[1]:

        • for take profit order

        • 10^17 for mantle, 2*10^14 for arbitrum

        • 0 if you do not want to create take profit order

      • _executionFee[2]:

        • for stop loss order

        • 10^17 for mantle, 2*10^14 for arbitrum

        • 0 if you do not want to create stop loss order

    • value: value of this transaction

      • if order contains both take profit and stop loss order:

        • 310^17 for mantle, 610^14 for arbitrum

      • if order contains one of take profit or stop loss order:

        • 210^17 for mantle, 410^14 for arbitrum

      • if order has no take profit or stop loss order:

        • 10^17 for mantle, 2*10^14 for arbitrum

  • After this transaction is sent a keeper will execute the request, the request will either be executed or cancelled

  • If the position cannot be increased for reasons such as the _acceptablePrice not being fulfillable then the value will be refunded to user

关闭 / 减少仓位

Find ABI of PositionRouter.sol on corresponding chains:

  • Call PositionRouter.createDecreasePosition with parameters:

    • _path(address[]):

      • [collateralToken] or [collateralToken, tokenOut] if a swap is needed

    • _indexToken:

      • the index token of the position

    • _collateralDelta:

      • the amount of collateral in USD value to withdraw

    • _sizeDelta:

      • the USD value of the change in position size

    • _isLong:

      • whether the position is a long or short

    • _receiver:

      • the address to receive the withdrawn tokens

    • _acceptablePrice:

      • the USD value of the min (for longs) or max (for shorts) index price acceptable when executing the request

    • _minOut:

      • the min output token amount

      • set to 0 if no swap is required

    • _executionFee:

      • 10^17 for mantle, 2*10^14 for arbitrum

    • _withdrawETH:

      • only applicable if WETH will be withdrawn, the WETH will be unwrapped to ETH if this is set to true

    • _callbackTarget:

      • an optional callback contract, this contract will be called on request execution or cancellation

  • After this transaction is sent a keeper will execute the request, the request will either be executed or cancelled

  • If the position cannot be decreased for reasons such as the _acceptablePrice not being fulfillable then the request will be cancelled and there will be no change to the position

查找您的订单

通过 subgraph 查询

Request URL:

Request body:

query MyQuery {

activePositions(

orderBy: timestamp

orderDirection: desc

where: {status: "active", account: "your_address_in_lower_case"}

) {

		account
		
		collateral
		
		collateralToken
		
		isLong
		
		size
		
		timestamp
		
		indexToken
		
		averagePrice
	
	}

}

查找您的限价订单

通过 subgraph 查询

Request URL:

Request body:

query MyQuery {

orders(

where: {status: open, account: "your_address_in_lower_case"}

orderBy: createdTimestamp

orderDirection: desc

) {

		account
		
		status
		
		type
		
		triggerPrice
		
		triggerAboveThreshold
		
		size
		
		isLong
		
		indexToken
		
		index
		
		id
		
		createdTimestamp
		
		}

}

兑换

查找 ABI of Router.sol 在不同链:

执行兑换:

  • Approve the Router contract for the token and amount you would like to swap

  • Call Router.swap with parameters:

    • _path: [tokenIn, tokenOut]

    • _amountIn: amount of tokenIn to swap

    • _minOut: minimum expected output amount

    • _receiver: address of the receiver of tokenOut

  • The function will revert if the amount of tokenOut sent to the receiver is less than _minOut

To get swap amounts before execution:

  • Call Reader.getMaxAmountIn with parameters:

    • _vault: address of the vault

    • _tokenIn: address of token that will be given

    • _tokenOut: address of token to be received

  • The max amount of tokenIn that can be swapped will be returned

  • Call Reader.getAmountOut with parameters:

    • _vault: address of the vault

    • _tokenIn: address of token that will be given

    • _tokenOut: address of token to be received

    • _amountIn: amount of tokenIn to swap

  • Two values will be returned, the first is the amount out after fees, and the second is the fee amount

  • The fee amount will be in terms of tokenOut

Last updated