0x Protocol Documentation

repository·development·Indexed 19 days ago

https://github.com/0xproject/protocol

An open-source monorepo providing smart contracts and developer tools for the trustless exchange of Ethereum-based assets. Includes packages for ERC20 token implementations (@0x/contracts-erc20), governance architecture (@0x/contracts-treasury), test utilities (@0x/contracts-test-utils), and extensible contract architecture (@0x/contracts-zero-ex).

Tokens
92.3K
Snippets
271
Records
449
Agent score
62%

What's inside 0x Protocol

  1. Overview of 0x Protocol Packages

    development

    The 0x protocol is an open protocol for the trustless exchange of Ethereum-based assets. This monorepo contains smart contracts and developer tools, with each public sub-package published independently to NPM.

    Solidity Packages

    These are primarily used for on-chain logic:

    • @0x/contracts-zero-ex: Contracts for settling trades within the protocol.
    • @0x/contracts-erc20: Implementations of various ERC20 tokens.
    • @0x/contracts-test-utils: TypeScript/Javascript utilities for testing contracts.
    • @0x/contracts-utils: Generic libraries and utilities used across all contracts.

    TypeScript/Javascript Packages

    These are used for off-chain logic and interaction:

    • @0x/protocol-utils: Utilities for generating, parsing, signing, and validating 0x orders.
    • @0x/contract-addresses: Utility for retrieving known deployed contract addresses for specific networks.
    • @0x/contract-wrappers: JS/TS wrappers for interacting with 0x smart contracts.
    • @0x/contract-artifacts: 0x smart contract compilation artifacts.
  2. Protocol Emergency Playbook Summary

    development

    The protocol follows a tiered response strategy for emergencies:

    ScenarioAction
    Release is brokenRollback the entire deployment.
    Feature is brokenRollback the Feature to a working version or NIL.
    Transformer is brokenUpdate 0x API to stop routing trades through it.
    Funds are at risk(If 1-3 are not possible) Temporarily disable the entire protocol.
  3. Scope of the 0x Smart Contract Bug Bounty Program

    development

    The 0x bug bounty program is strictly limited to vulnerabilities found in the 0x smart contracts deployed on the following EVM-compatible networks:

    • Ethereum mainnet
    • Binance Smart Chain
    • Polygon
    • Avalanche
    • Fantom
    • Celo
    • Optimism
    • Future EVM-compatible networks announced via official 0x communication channels.

    Ineligible Reports

    • 0x API and Web Interfaces: Reports regarding UI/UX, servers, or infrastructure for 0x API or web interfaces (such as Matcha or 0x.org) are not eligible.
    • Duplicate Findings: Only the first reporter of a specific contract vulnerability is eligible for a reward.
    • Audit Findings: Vulnerabilities already identified during formal audits (conducted by Consensys Diligence, Trail of Bits, or ABDK) are ineligible.
  4. Overview of 0x Governance Architecture

    development

    The 0x governance system is designed to fully decentralize the management of the 0x Protocol and the Treasury. It utilizes a wrapped ZRX token (ZRXWrappedToken) and a Compound-like governor design.

    Governance is split into two distinct domains:

    1. Protocol Governance: Managed by ZeroExProtocolGovernor and a dedicated ZeroExTimelock instance.
    2. Treasury Governance: Managed by ZeroExTreasuryGovernor and a separate ZeroExTimelock instance.

    While the governors themselves are non-upgradable, the underlying voting implementation (ZeroExVotes) is upgradable via an ERC1967Proxy.

  5. What is the Flash Wallet and how does it work?

    development

    The Flash Wallet is a sandboxed escrow contract designed to hold funds for Transformers to operate on. It acts as a trustless intermediary: a Feature contract transfers tokens to the Flash Wallet, which then uses delegatecall to allow a Transformer to perform operations on those escrowed funds.

    Key characteristics:

    • Sandboxed Security: Transformers only have access to the funds deposited into the Flash Wallet; they do not have access to user allowances.
    • Current Scope: It is currently used by the TransformERC20 feature for ERC20 tokens, though it is architected to support other standards like ERC1155 or ERC223 via fallback implementations.
    • Deployment: It is deployed via the createTransformWallet() function on the feature contract, which is restricted to the owner/governor.
  6. What is the ZeroExGovernor and how does it work?

    development

    The ZeroExGovernor is a time-locked multi-signature wallet designed for Exchange V4. It holds administrative permissions to perform critical functions within the 0x Protocol.

    Key characteristics include:

    • Multi-signature: Requires 2/3 signatures to execute functions.
    • Timelocks: Most administrative actions are subject to a delay (timelock) before they become active, allowing users to react to changes. However, emergency functions (e.g., those used to mitigate vulnerabilities) may have a 0-day timelock to allow for immediate action.
    • Ownership & Authorization: It can transfer ownership of contracts and manage authorized addresses in the Exchange and Staking systems.
  7. Understand the impact of Tokens with Fees on Transfer

    development

    Tokens that implement fees on transfer do not strictly follow the ERC20 specification. The protocol expects a transfer to move the exact specified amount or revert. Because fee-on-transfer tokens result in the recipient receiving less than the specified amount, the protocol's behavior is unspecified.

    In most scenarios, attempting to use these tokens will result in an overall transaction failure triggered by the protocol's slippage protections.

  8. How to choose an order nonce

    development

    The nonce is a unique identifier for an order.

    Warning: If two orders signed by the same maker have the same nonce, filling or cancelling one can result in the other becoming unfillable. Note that while two ERC721 orders with the same nonce cannot both be filled, two ERC1155 orders with the same nonce can both be filled as long as the orders are not identical.

    Best Practices:

    • Use the most significant 128 bits of the nonce as an application/marketplace identifier.
    • Use the least significant 128 bits of the nonce to increment from 0 for each order created by a particular maker.
    • You can also use a pseudorandom value or the current timestamp.
  9. How Fee Collectors work in the 0x Protocol

    development

    Fee Collectors are specialized contracts that act as repositories for protocol fees. Each Staking Pool has exactly one dedicated Fee Collector.

    Key Lifecycle & Mechanics:

    1. Fee Accrual: When a Limit Order is filled, the protocol fee is automatically sent to the Fee Collector corresponding to the order's pool (order.pool).
    2. Aggregation: The Fee Collector automatically approve()s the staking contract, allowing fees to be aggregated from the collector into the Staking System.
    3. Conversion: Fee Collectors include built-in functionality to convert ETH to WETH.
    4. Transfer to Staking: Fees are moved from the Fee Collectors to the Staking System using the transferProtocolFeesForPools() function. While this can be called at any time, it is most optimal to execute during finalization to balance the cost of finalizing an epoch against the transaction costs for takers.

    Deployment Note: Fee Collectors are deployed using CREATE2, ensuring their addresses are predictable.

  10. How the ZeroEx Proxy pattern works

    development

    The ZeroEx contract (also known as the Exchange Proxy or EP) acts as a central router for asset exchanges. It uses a per-function proxy pattern where the main contract maintains a mapping of function selectors to specific implementation contracts (called "features").

    When a function is called on the ZeroEx contract, it uses a fallback mechanism to delegatecall the request to the corresponding feature implementation. This allows the protocol to upgrade individual functions independently without redeploying the entire system.

    /* The ZeroEx contract's sole responsibility is to maintain a mapping of "features" to implementation contracts and route (delegate) calls to per-function implementation contracts through its fallback mechanism. */
  11. What are Transformers in the 0x Protocol

    development

    Transformers are trustless extensions to the core protocol used to perform specialized operations during order fulfillment. They are permissioned by the Transformer Deployer and are identified by the specific nonce of that deployer.

    Crucially, Transformers are executed via delegatecall within the context of the Flash Wallet. This means they can operate on the state and funds held by the Flash Wallet during the transaction lifecycle.