Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

TxEVM CoreRead

CoreRead is a set of precompiles on TxEVM that return TxCore market data. A contract staticcalls a fixed address and gets the same mark price, oracle price, and best bid/ask that TxCore's order books and risk engine use, with no external oracle in between.

Reads

ReadAddressInputOutput
markPx0x0000000000000000000000000000000000000806uint32 perpuint64 mark price
oraclePx0x0000000000000000000000000000000000000807uint32 perpuint64 oracle (index) price
l1BlockNumber0x0000000000000000000000000000000000000809noneuint64 TxCore height
bbo0x000000000000000000000000000000000000080euint32 perp(uint64 bid, uint64 ask)

Inputs and outputs are ABI-encoded. Only live perpetual markets are served. Spot and outcome markets, and delisted perps, have no reads.

Perp index

perp is the market's index from perpMeta, the same id used across the Info API.

Price encoding

Prices are integers with 6 − szDecimals decimal places, where szDecimals is the market's size decimals from perpMeta. Extra digits are truncated, not rounded.

price = onchain_value / 10^(6 − szDecimals)

Example: on testnet, BTC-USDC has szDecimals = 4 and keeps 2 decimal places, so a mark price of 65000.50 reads as 6500050.

Zero means no quote

A read succeeds and returns 0 when there is no usable value: the oracle has not published yet, a side of the book is empty, or the price cannot be encoded in uint64.

When the data is taken

Every TxCore commit that runs EVM work takes a snapshot of TxCore state after that commit. A transaction reads the snapshot of the commit it executes in.

  • One EVM block spans 5 TxCore commits, so two transactions in the same EVM block can see different prices.
  • Within one transaction, every read comes from the same snapshot.
  • l1BlockNumber() returns the TxCore height of that snapshot. To check that several values come from one TxCore state, compare l1BlockNumber(), not block.number.

Gas and failure

A successful read costs 2000 + 65 × (input bytes + output bytes):

ReadGas
markPx, oraclePx6,160
bbo8,240
l1BlockNumber4,080

A read fails (success == false, no return data) for an unknown or non-perp index, a malformed argument, or any other address in 0x0800–0x0813, which is reserved. A failed read consumes all gas forwarded to it. If your contract must keep running after a failed read, forward a bounded amount, for example staticcall{gas: 20_000}.

Example

The library below wraps the reads with the checks above. Each function reverts on a failed read or a missing quote.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
 
library CoreRead {
    address constant MARK_PX = 0x0000000000000000000000000000000000000806;
    address constant ORACLE_PX = 0x0000000000000000000000000000000000000807;
    address constant L1_BLOCK_NUMBER = 0x0000000000000000000000000000000000000809;
    address constant BBO = 0x000000000000000000000000000000000000080e;
    uint256 constant READ_GAS = 20_000;
 
    function markPx(uint32 perp) internal view returns (uint64 px) {
        px = _readPrice(MARK_PX, perp);
    }
 
    function oraclePx(uint32 perp) internal view returns (uint64 px) {
        px = _readPrice(ORACLE_PX, perp);
    }
 
    function bbo(uint32 perp) internal view returns (uint64 bid, uint64 ask) {
        (bool ok, bytes memory out) = BBO.staticcall{gas: READ_GAS}(abi.encode(perp));
        require(ok, "CoreRead: bbo failed");
        (bid, ask) = abi.decode(out, (uint64, uint64));
        require(bid > 0 && ask > 0 && bid <= ask, "CoreRead: no quote");
    }
 
    function l1BlockNumber() internal view returns (uint64 height) {
        (bool ok, bytes memory out) = L1_BLOCK_NUMBER.staticcall{gas: READ_GAS}("");
        require(ok, "CoreRead: l1BlockNumber failed");
        height = abi.decode(out, (uint64));
    }
 
    function _readPrice(address precompile, uint32 perp) private view returns (uint64 px) {
        (bool ok, bytes memory out) = precompile.staticcall{gas: READ_GAS}(abi.encode(perp));
        require(ok, "CoreRead: read failed");
        px = abi.decode(out, (uint64));
        require(px > 0, "CoreRead: no quote");
    }
}

A contract that reads the BTC-USDC mark price on testnet and returns it with 6 decimals, the same precision as USDC. Multiplying by 10^szDecimals turns any perp price into a 6-decimal value.

contract BtcPriceReader {
    // BTC-USDC on testnet: perpMeta index 1, szDecimals 4.
    uint32 constant BTC_USDC = 1;
    uint256 constant SZ_DECIMALS = 4;
 
    function btcMarkPrice() external view returns (uint256) {
        uint64 px = CoreRead.markPx(BTC_USDC); // 65000.50 reads as 6_500_050
        return uint256(px) * 10 ** SZ_DECIMALS; // 65_000_500_000 = 65000.50 with 6 decimals
    }
}

Look up index and szDecimals in perpMeta on the network you deploy to; they are not guaranteed to match across networks.

Next

PageFor
OverviewAccess requirements, testnet parameters, roadmap
JSON-RPCCalling contracts with eth_call
perpMetaPerp indexes and szDecimals