An Introduction for Developers

# Prerequisites * Basic Familiarity * **Blockchain Technology** — Distributed conensus * **Ethereum** — Smart Contracts * **Tokens** — Limited assets * General Programming Concepts

0x — Asset Trading Protocol

## 0x Terminology * **Order** — Offer to trade assets * **Maker** — The creator of an order * **Maker Asset** — Asset provided by maker * **Taker** — Entity filling an order * **Taker Asset** — Asset provided by the taker * **Relayer** — Service to connect Makers with Takers * **Relayer Fee** — Paid by Maker & Taker as order fills
## 0x Technology * **0x Exchange Contracts** — Ethereum Contracts to execute trades * **Standard Relayer API** (SRA) — HTTP & WS API * **0x.js** — JavaScript Bindings for 0x * **0x Connect** — JavaScript Bindings for SRA * **ZRX** — Ethereum Token Used to Pay Fees

OpenRelay

Open Source 0x Relayer

## OpenRelay * Provides Open Order Book SRA * Microservices Architecture * Written in Go * Deployed with Docker * Built to scale
## OpenRelay Differentiators * Open Source! * Developer oriented service * Allows trading any tokens * Flat fees — not based on value of order * Flexible fee structure
## Affiliate Program * Anyone can sign up * Affiliate gets their own fee recipient contract * Orders get filled, the affiliate contract gets the fee * Contract splits fees 80% to affiliate, 20% to us

High Level Implementation

## Ingest * Public facing API * Validates basic order attributes * Order format * Supported Exchange * Order Expiration * Signature * Fees * **Does not** validate on-chain properties * Has order been filled / cancelled? * Does maker have required tokens?
## Fill Updater * Receives orders on channel * Checks blockchain for fill or cancel * Optimized with a bloom filter to avoid RPC requests * Publishes updated order
## Fund Validation * Checks blockchain for maker balances * Discards orders if funds or allowances are inadequate * Forwards valid orders to next stage * First Pass: Delay Relay * Second Pass: Indexer
## Delay Relay * OpenRelay will sell limited access to new orders * Orders will be public 2 blocks after receipt * Delay relay holds orders for 2 blocks, then sends them to have funds rechecked
## Order Indexer * Shoves orders into a database
## MySQL * Holds orders * Also supports PostgreSQL * We use AWS Aurora in production
## Search API * Public Facing API * Provides order search API defined in SRA * Retrieves orders from MySQL * Blockhash parameter for caching * Behind AWS CloudFront in production
## Monitoring Services * React to blockchain events * Designed to minimize RPC calls * Block Monitor publishes new blocks * Other monitors watch block monitor
## Block Monitor * Queries the blockchain for new blocks periodically * Publishes messages containing: * Block Number * Block Hash * Block Bloom Filter
## Fill Monitor * Checks block bloom filter for 0x fills * Makes RPC call for filled orders * Publishes FillRecords
## Fill Recorder * Watches for fill records * Updates fill amount on orders in MySQL
## Allowance Monitor * Checks block bloom filter for token allowances for the 0x exchange * Makes RPC call for complete allowances * Publishes SpendRecords * Token Spent / Approved * Spender * Balance / Allowance after transaction
## Spend Monitor * Checks block bloom filter for token transfers * Makes RPC call for complete transfers * Makes RPC calls for new user balances * Publishes SpendRecords
## Spend Recorder * Watches for SpendRecords * Updates order status for orders effected by spend * Maker & Maker Asset correspond to spend * Order's remaining amount exceeds spender balance * Orders no longer fillable are marked as unfunded
## Next Time * OpenRelay and Docker * The OpenRelay Development Environment * If you want to follow along: * Have Docker Installed * Check out OpenRelay from https://github.com/notegio/openrelay