> ## Documentation Index
> Fetch the complete documentation index at: https://docs.injective.network/llms.txt
> Use this file to discover all available pages before exploring further.

# Swap Precompile

> Quote and execute native spot swaps with the EVM precompile at 0x68.

Injective’s Swap Precompile exposes the native spot exchange to EVM applications at `0x0000000000000000000000000000000000000068`. Use it to read an order-book quote, set a minimum output and deadline, and execute a swap in one call.

> [!NOTE]
>
> V1 exact-input methods and `quoteExactOutputV1` are available from v1.20.4. The V2 methods, exact-output execution, and EVM-native MTS token resolution ship in v1.21, including testnet v1.21.0-beta.

## Walkthrough

Follow the [swap precompile demo](https://github.com/InjectiveLabs/inj-examples/blob/main/examples/swap-precompile/README.md) to check the testnet allowlist, quote and simulate both swap types, submit a transaction, or run the full flow on a fresh local chain.

## How it works

1. Select a network and a supported spot market.
2. Resolve the input token and express the amount in its native integer units.
3. Read a quote, then derive a minimum output and a deadline.
4. Simulate the swap from the funded caller, submit it, and wait for a successful receipt.

```mermaid theme={null}
%%{init: {"theme": "base", "themeVariables": {"background": "#FFFFFF", "primaryColor": "#FFFFFF", "lineColor": "#4D3DFF", "textColor": "#14151F"}}}%%
flowchart LR
    subgraph EVM["Injective EVM"]
        CALLER["dApp contract<br/>or EOA"]
        SWAP["Swap Precompile<br/>0x68"]
    end
    subgraph NATIVE["Native modules"]
        PARAMS["SwapParams<br/>market allowlist"]
        EXCH["x/exchange<br/>orderbook"]
        BANK["x/bank balances<br/>MultiVM Token Standard"]
    end
    CALLER -->|"1 · quoteExactInputV2<br/>request spent input and output"| SWAP
    SWAP -.->|"2 · quoted input spent and output<br/>used to derive minOut"| CALLER
    CALLER -->|"3 · swapExactInputV2<br/>with minOut and deadline"| SWAP
    SWAP -->|"4 · verify market<br/>is allowlisted"| PARAMS
    SWAP -->|"5 · execute on<br/>default subaccount"| EXCH
    EXCH -->|"6 · fill, or revert<br/>below minOut"| SWAP
    SWAP -->|"7 · credit amountOut<br/>to recipient"| BANK
    BANK -.->|"same balance readable as ERC20"| CALLER

    classDef caller fill:#EEF0FF,stroke:#4D3DFF,color:#14151F
    classDef precompile fill:#4D3DFF,stroke:#4D3DFF,color:#FFFFFF
    classDef native fill:#222530,stroke:#222530,color:#FFFFFF
    class CALLER caller
    class SWAP precompile
    class PARAMS,EXCH,BANK native
    style EVM fill:#FFFFFF,stroke:#4D3DFF,stroke-dasharray:4
    style NATIVE fill:#FFFFFF,stroke:#8B94A7,stroke-dasharray:4
```

Quotes read the current order book without changing state. Execution places an atomic spot market order on the caller’s default subaccount and credits the output to the recipient. If the minimum output or deadline check fails, the swap’s state changes revert. Transaction gas can still be charged.

For exact input, `amountIn` is an input budget. Thin liquidity or quantity ticks can leave part of it unspent. The remainder stays with the caller and `minOut` sets the minimum output for the fill. For exact output, execution must deliver at least `amountOut` within `maxAmountIn` or revert. Quantity ticks can cause slightly more output to be delivered.

Orders can change between quoting and execution. Quotes use the undiscounted taker fee, and execution applies the caller’s fee discount and skips paused or frozen makers. Use a fresh quote to set your limits. Execution enforces `minOut` for exact input or `maxAmountIn` for exact output, along with any deadline you set.

## Swap Precompile Interface

```solidity theme={null}
interface ISwapModule {
    /// Quote the input consumed and output received for an input budget.
    function quoteExactInputV2(
        address tokenIn,
        string calldata marketId,
        uint256 amountIn
    ) external view returns (uint256 amountInSpent, uint256 amountOut);

    /// Execute within minOut and deadline; return spent input and output.
    function swapExactInputV2(
        address tokenIn,
        string calldata marketId,
        uint256 amountIn,
        uint256 minOut,
        address recipient,
        uint256 deadline
    ) external returns (uint256 amountInSpent, uint256 amountOut);

    /// Legacy exact-input quote; returns only output.
    function quoteExactInputV1(
        address tokenIn,
        string calldata marketId,
        uint256 amountIn
    ) external view returns (uint256 amountOut);

    /// Legacy exact-input swap; returns only output.
    function swapExactInputV1(
        address tokenIn,
        string calldata marketId,
        uint256 amountIn,
        uint256 minOut,
        address recipient,
        uint256 deadline
    ) external returns (uint256 amountOut);

    /// Quote the input required to receive the target output.
    function quoteExactOutputV1(
        address tokenOut,
        string calldata marketId,
        uint256 amountOut
    ) external view returns (uint256 amountIn);

    /// Receive at least amountOut while spending at most maxAmountIn.
    function swapExactOutputV1(
        address tokenOut,
        string calldata marketId,
        uint256 amountOut,
        uint256 maxAmountIn,
        address recipient,
        uint256 deadline
    ) external returns (uint256 amountInSpent, uint256 amountOutDelivered);
}
```

* `marketId` is a `0x`-prefixed, 32-byte spot market ID passed as an ABI `string`.
* `tokenIn` selects the input side for exact input. `tokenOut` selects the asset to receive for exact output. Use the zero address for native INJ. Other tokens resolve through a [registered ERC20 pair](https://docs.injective.network/developers-evm/erc20-module.md) or an EVM-native MTS `erc20:` denom on v1.21.
* Amounts use the token’s smallest units. For example, 10 USDC is `10000000`. Use output units for `minOut` and input units for `maxAmountIn`.
* `recipient` defaults to the caller when set to the zero address. The precompile spends the immediate caller’s bank balance, including when a contract calls it. Invoke it with zero transaction value. ERC20 approval is unnecessary.
* `deadline` is a Unix timestamp in seconds. Set it to zero to disable the time check.

The V1 and V2 suffixes refer to the precompile’s ABI versions. The exchange module versions its protobuf types independently.

When checking a settled swap against the amounts returned by the precompile, allow for a difference of one smallest token unit. Fractional amounts accumulate in the caller’s default subaccount and can affect when a whole unit moves into or out of the bank balance.

## Market allowlist

Swappable markets are governed by `SwapParams`, an enable flag plus a market allowlist inside the exchange module parameters. Calling the precompile on a market that is not allowlisted reverts with:

```text theme={null}
market <id> is not allowlisted for swaps: invalid swap route
```

### Checking the current allowlist

Choose your network to query the exchange module’s v2 swap parameters.

> [tab] Testnet
>
> ```bash theme={null}
> curl -fsS https://testnet.sentry.lcd.injective.network/injective/exchange/v2/exchangeParams \
>   | jq '.params.swap_params'
> ```

> [tab] Mainnet
>
> ```bash theme={null}
> curl -fsS https://sentry.lcd.injective.network/injective/exchange/v2/exchangeParams \
>   | jq '.params.swap_params'
> ```

Use `jq '.params.exchange_admins'` to list the exchange admins on the selected network.

### Requesting a market

If you are building on the swap precompile and need a market allowlisted, reach out in the `#developers` channel of the [Injective Discord](https://discord.gg/injective) with the spot market id, or raise it with your Injective partner contact. Market ids for active spot markets are listed at `/injective/exchange/v2/spot/markets?status=Active`.

### Updating the allowlist (exchange admins)

`MsgUpdateSwapParams` is callable by the governance authority or any address in the `exchange_admins` parameter. It takes effect immediately, with no governance vote required when sent by an admin.

The message **fully replaces** the swap parameter group. Always include every market that should remain allowlisted and keep `enabled: true`, because omitting an existing market removes it and omitting the flag pauses all swaps.

```json theme={null}
{
  "body": {
    "messages": [
      {
        "@type": "/injective.exchange.v2.MsgUpdateSwapParams",
        "sender": "<admin inj1... address>",
        "swap_params": {
          "enabled": true,
          "allowed_markets": ["0x<existing market id>", "0x<new market id>"]
        }
      }
    ]
  },
  "auth_info": {
    "fee": {
      "amount": [{ "denom": "inj", "amount": "200000000000000" }],
      "gas_limit": "400000"
    }
  }
}
```

Sign and broadcast the transaction document with `injectived tx sign` and `injectived tx broadcast` from the admin key.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.