Protocol
Rubicon Market

Rubicon Market

Contract source code (opens in a new tab)

RubiconMarket is the low-level order book and matching engine for Rubicon Classic. It stores ERC-20/ERC-20 order books as sorted lists, escrows maker liquidity, matches takers directly, and returns funds when orders are canceled.

Core Functions

offer()

function offer(
    uint256 pay_amt,
    IERC20 pay_gem,
    uint256 buy_amt,
    IERC20 buy_gem
) public returns (uint256)

Places a limit order. pay_amt of pay_gem is transferred into escrow until the offer is filled or canceled. Advanced overloads support a sorted-list position and a custom recipient.

ParameterTypeDescription
pay_amtuint256Quantity of ERC-20 tokens the maker is selling.
pay_gemIERC20ERC-20 token the maker is selling.
buy_amtuint256Quantity of ERC-20 tokens the maker wants to buy.
buy_gemIERC20ERC-20 token the maker wants to buy.

cancel()

function cancel(uint256 id) public returns (bool success)

Cancels an active offer and returns the escrowed tokens to the offer owner.

buyAllAmount()

function buyAllAmount(
    ERC20 buy_gem,
    uint256 buy_amt,
    ERC20 pay_gem,
    uint256 max_fill_amount
) external returns (uint256 fill_amt)

Attempts to buy an exact amount of buy_gem while spending at most max_fill_amount of pay_gem.

sellAllAmount()

function sellAllAmount(
    ERC20 pay_gem,
    uint256 pay_amt,
    ERC20 buy_gem,
    uint256 min_fill_amount
) external returns (uint256 fill_amt)

Attempts to sell an exact amount of pay_gem while receiving at least min_fill_amount of buy_gem.

Batch Orders

Batch orders let traders place, cancel, or update multiple limit orders in a single transaction, limited by the network's block and transaction gas limits. This is most useful for market makers that frequently update books and want to reduce cost per order.

Batch orders can span multiple token pairs in one transaction.

batchOffer()

function batchOffer(
    uint256[] calldata payAmts,
    address[] calldata payGems,
    uint256[] calldata buyAmts,
    address[] calldata buyGems
) external

Places multiple offers. All arrays must be the same length and each index represents one offer.

batchCancel()

function batchCancel(uint256[] calldata ids) external

Cancels multiple offers by ID.

batchRequote()

function batchRequote(
    uint256[] calldata ids,
    uint256[] calldata payAmts,
    address[] calldata payGems,
    uint256[] calldata buyAmts,
    address[] calldata buyGems
) external

Cancels and replaces multiple offers in one transaction.

MarketAid

MarketAid source code (opens in a new tab)

MarketAid is a helper contract for advanced trading and market making on RubiconMarket. It acts as a middle layer that lets market makers manage larger, more efficient, and more complex order-book strategies.

Market makers can create a MarketAid instance, fund it with ERC-20 tokens, and use it to place, requote, and cancel batches of Classic orders.

MarketAid is intended for sophisticated developers and market makers. Use it carefully and verify permissions, strategist access, and funded token balances before running automated strategies.

MarketAid Workflow

  1. Deploy a MarketAid instance by calling createMarketAidInstance() on MarketAidFactory.
  2. Confirm the instance address with getUserMarketAid() or the emitted creation event.
  3. Verify the instance admin and approvedStrategist values.
  4. Fund the instance with the ERC-20 tokens you want to trade.
  5. Use MarketAid functions to place, requote, cancel, and withdraw funds.

MarketAid Functions

FunctionPurpose
placeMarketMakingTradesPlaces a bid, ask, or bid/ask pair as a single strategist trade.
batchMarketMakingTradesPlaces many market-making trades in one transaction.
getOutstandingStrategistTradesReturns outstanding strategist trade IDs.
getStrategistTotalLiquidityReturns the total book value controlled by a strategist.
requoteRequotes one strategist trade.
batchRequoteOffersRequotes many strategist trades.
batchRequoteAllOffersReads outstanding strategist trades and requotes them together.
scrubStrategistTradeCancels one strategist trade.
scrubStrategistTradesCancels many strategist trades.
adminPullAllFundsLets the admin withdraw specified ERC-20 funds.
strategistRebalanceFundsSupports rebalancing funds through external liquidity venues.

Minimum Order Sizes

RubiconMarket enforces minimum order sizes for some ERC-20 tokens to prevent dust offers.

TokenMinimum Order SizeMinimum Order Size (uint256)
WETH0.0022 ETH2200000000000000
WBTC0.00015 WBTC15000
USDC5 USDC5000000
USDT5 USDT5000000
DAI5 DAI5000000000000000000
OP3 OP3000000000000000000
ARB4 ARB4000000000000000000

Use getMinSell() to query a token's minimum order size.

function getMinSell(ERC20 pay_gem) external view returns (uint256)

View Functions

getBuyAmountWithFee()

function getBuyAmountWithFee(
    IERC20 buy_gem,
    IERC20 pay_gem,
    uint256 pay_amt
) external view returns (uint256 buy_amt, uint256 approvalAmount)

Returns the amount of buy_gem received for a specified pay_amt, plus the approval amount required after accounting for fees.

getPayAmountWithFee()

function getPayAmountWithFee(
    IERC20 pay_gem,
    IERC20 buy_gem,
    uint256 buy_amt
) public view returns (uint256 pay_amt, uint256 approvalAmount)

Returns the amount of pay_gem needed to receive a specified buy_amt, plus the approval amount required after accounting for fees.

getBestOffer()

function getBestOffer(ERC20 pay_gem, ERC20 buy_gem) public view returns (uint256)

Returns the offer ID at the top of the specified order book side.

getOfferCount()

function getOfferCount(ERC20 sell_gem, ERC20 buy_gem) public view returns (uint256)

Returns the number of offers in the specified book.