Injective | Documentation
InjectiveGithub
Injective | Documentation
Injective | Documentation
  • About Injective
  • Getting Started
    • Wallet
      • Create a wallet
      • Accounts
      • Staking
      • Governance
      • Auction
    • Token Standards
      • INJ coin
      • Token Factory
      • CW20 Standard
    • Transactions
      • Gas and Fees
  • Guides
    • Create a Wallet
    • Bridge
      • From Ethereum
      • Using Wormhole
      • Using IBC
      • From Solana
    • Launch a Token
    • Launch a Market
    • Denom Metadata
    • Get INJ
  • Toolkits
    • injectived
      • Install injectived
      • Using injectived
      • Commands
    • Injective TS SDK
    • Injective Go SDK
    • Injective Python SDK
    • Injective CW SDK
    • Injective Rust
    • The Graph
  • References
  • Glossary
  • Developers
    • Getting Started
      • Guides
        • Testnet Proposals
        • Convert addresses
        • Calculations
          • Min Price Tick Size
          • Min Quantity Tick Size
        • Testnet Faucet Integration
    • Exchange Developers
      • Build a DEX
      • Provider Oracle
    • Cosmwasm Developers
      • Your First Smart Contract
      • Guides
        • Local Development
        • Mainnet Deployment
        • Whitelisting deployment address
        • Create your Swap Contract
        • Creating UIs
      • Using Injective Queries and Messages
      • CW20 Adapter
      • Injective Test Tube
    • Modules
      • Injective
        • Auction
          • State
          • Messages
          • EndBlock
          • Events
          • Params
          • Errors
        • Exchange
          • Derivative Markets Concepts
          • Spot Markets Concepts
          • Binary Option Markets Concepts
          • Other Concepts
          • State
          • State Transitions
          • Messages
          • Proposals
          • BeginBlock
          • EndBlock
          • Events
          • Params
          • MsgPrivilegedExecuteContract
          • Errors
        • Insurance
          • State
          • State Transitions
          • Messages
          • EndBlock
          • Events
          • Params
          • Improvements
          • Errors
        • OCR
          • Concepts
          • State
          • Messages
          • Proposals
          • BeginBlock
          • Hooks
          • Events
          • Params
          • Errors
        • Oracle
          • State
          • Keeper
          • Messages
          • Proposals
          • Events
          • Improvements
          • Errors
        • Peggy
          • Definitions
          • Workflow
          • State
          • Messages
          • Slashing
          • EndBlock
          • Events
          • Params
          • Relay Semantics
          • Improvements
          • Errors
        • Permissions
          • Concepts
          • State
          • State Transition
          • Errors
        • TokenFactory
          • Concepts
          • State
          • Messages
          • Events
          • Params
          • Errors
        • WasmX
          • Concepts
          • Data
          • Proposals
          • Messages
          • Params
          • Errors
        • Lanes
        • TxFees
      • Core
        • Auth
        • AuthZ
        • Bank
        • Consensus
        • Crisis
        • Distribution
        • Evidence
        • Feegrant
        • Gov
        • Group
        • Mint
        • NFT
        • Params
        • Slashing
        • Staking
        • Upgrade
        • Circuit
        • Genutils
    • dApps Documentation
  • Nodes
    • Getting Started
      • Interact with a node
      • Running a node
        • Setting up the keyring
        • Join a network
        • Cosmovisor
        • Upgrade your node
    • Validators
      • Mainnet
        • Peggo
        • Canonical Chain Upgrades
          • Upgrade to 10002-rc1
          • Upgrade to 10002-rc2
          • Upgrade to 10003-rc1
          • Upgrade to 10004-rc1
          • Upgrade to 10004-rc1-patch
          • Upgrade to 10005-rc1
          • Upgrade to 10006-rc1
          • Upgrade to 10007-rc1
          • Upgrade to 10008 - Camelot
          • Upgrade to 10009
          • Upgrade to v1.10
          • Upgrade to v1.11
          • Upgrade to v1.12.0 - Volan
          • Upgrade to v1.12.1
          • Upgrade to v1.13.0 - Altaris
          • Upgrade to v1.13.2
          • Upgrade to v1.13.3
          • Upgrade to v1.14.0
          • Upgrade to v1.14.1
          • Upgrade to v1.15.0
      • Testnet
        • Testnet Peggo
    • Public Endpoints
    • Premium Endpoints
    • Injective Indexer Setup
  • Traders
    • Getting Started
    • Documentation
    • API Reference
  • Useful Links
    • Injective 101
    • Injective Hub
    • Injective Explorer
    • Chain API Reference
    • Indexer API Reference
    • Testnet Faucet
Powered by GitBook
On this page
  • Order Matching: Frequent Batch Auction (FBA)
  • Single Trade Calculations
Edit on GitHub
Export as PDF
  1. Developers
  2. Modules
  3. Injective
  4. Exchange

EndBlock

The exchange EndBlocker runs at the end of every block in our defined order after governance and staking modules, and before the peggy, auction and insurance modules. It is particularly important that the governance module's EndBlocker runs before the exchange module's.

  • Stage 0: Determine the fee discounts for all the accounts that have placed an order in a fee-discount supported market in the current block.

  • Stage 1: Process all market orders in parallel - spot market and derivative market orders

    • Markets orders are executed against the resting orderbook at the time of the beginning of the block.

    • Note that market orders may be invalidated in the EndBlocker due to subsequently incoming oracle updates or limit order cancels.

  • Stage 2: Persist market order execution to store

    • Spot Markets

      • Persist Spot market order execution data

      • Emit relevant events

        • EventBatchSpotExecution

    • Derivative Markets

      • Persist Derivative market order execution data

      • Emit relevant events

        • EventBatchDerivativeExecution

        • EventCancelDerivativeOrder

  • Stage 3: Process all limit orders in parallel - spot and derivative limit orders that are matching

    • Limit orders are executed in a frequent batch auction mode to ensure fair matching prices, see below for details.

    • Note that vanilla limit orders may be invalidated in the EndBlocker due to subsequently incoming oracle updates and reduce-only limit orders may be invalidated in the EndBlocker due to subsequently incoming orders which flip a position.

  • Stage 4: Persist limit order matching execution + new limit orders to store

    • Spot Markets

      • Persist Spot Matching execution data

      • Emit relevant events

        • EventNewSpotOrders

        • EventBatchSpotExecution

    • Derivative Markets

      • Persist Derivative Matching execution data

      • Emit relevant events

        • EventNewDerivativeOrders

        • EventBatchDerivativeExecution

        • EventCancelDerivativeOrder

  • Stage 5: Persist perpetual market funding info

  • Stage 6: Persist trading rewards total and account points.

  • Stage 7: Persist new fee discount data, i.e., new fees paid additions and new account tiers.

  • Stage 8: Process Spot Market Param Updates if any

  • Stage 9: Process Derivative Market Param Updates if any

  • Stage 10: Emit Deposit and Position Update Events

Order Matching: Frequent Batch Auction (FBA)

  1. Likewise limit orders are filled at a uniform clearing price. New limit orders are combined with the resting orderbook and orders are matched as long as there is still negative spread. The clearing price is either

a. the best buy/sell order in case the last matched order crosses the spread in that direction, the, b. the mark price in case of derivative markets and the mark price is between the last matched orders or c. the mid price.

Single Trade Calculations

  • For a qualifying market compute the fee discounts:

    • Fee discounts are applied as refunds and the fee paid contribution is recorded.

    • Relayer fees are applied AFTER the fee discount is taken.

  • For a qualifying market compute the trade reward point contribution:

    • Obtain the FeePaidMultiplier for maker and taker.

    • Compute the trade reward point contribution.

    • Trade reward points are based on the discounted trading fee.

  • Calculate fee refunds (or charges). There are several reasons why an order might get a fee refund after matching:

    1. It's a limit order which is not matched or only partially matched which means it will become a resting limit order and switch from a taker to maker fee. The refund is UnmatchedQuantity * (TakerFeeRate - MakerFeeRate). Note that for negative maker fees, we refund the UnmatchedQuantity * TakerFeeRate instead.

    2. Fee discounts are applied. We refund the difference between the original fee paid and the fee paid after the discount.

    3. The order is matched at a better price resulting in a different fee.

      • For buy orders a better price means a lower price and thus a lower fee. We refund the fee price delta.

      • For sell orders a better price means a higher price and thus a higher fee. We charge the fee price delta.

PreviousBeginBlockNextEvents

Last updated 2 months ago

The goal of FBA is to prevent any . This is achieved by calculating a single clearing price for all matched orders in a given block.

Market orders are filled first against the resting orderbook at the time of the beginning of the block. While the resting orders are filled at their respective order prices, the market orders are all filled at a uniform clearing price with the same mechanism as limit orders. For an example for the market order matching in FBA fashion, look at the API docs .

For an example for the limit order matching in FBA fashion, look at the API docs .

You can find the respective code with an example . Please check the master branch for the latest chain code.

Front-Running
here
here
here