Market Aid
Contract Source Code (opens in a new tab)
Overview
MarketAid.sol is a helper contract for advanced trading and market making on RubiconMarket.sol. Think of it as a middle-layer contract that allows traders to make larger, more efficient, and more complex transactions on the Rubicon order books.
Users need to create a MarketAid.sol by using the instance of MarketAid, If you have any questions or trouble with MarketAid, join our Discord server (opens in a new tab), our team is happy to help you get started with automated trading on Rubicon!
Disclaimer
MarketAid.sol is in beta and for sophisticated developers and users only. Use it at your own risk.
Quick Start
To get started, create your own MarketAid.sol instance, fund the contract with ERC-20 tokens, and send your first batch of orders to RubiconMarket.sol
! You can use ethers.js
to integrate this into an existing bot, and we are working on tutorials to make it even easier! Let's get started:
-
Create a new instance of the
MarketAid
contract- Deploy a
MarketAid
instance by callingcreateMarketAidInstance()
on theMarketAidFactory
contract. This will spawn a new instance ofMarketAid
that is permissioned to the caller ofcreateMarketAidInstance()
and automatically approve them as a strategist with access to the contract.
- Deploy a
-
Fund your MarketAid instance with the ERC-20s you want to trade (WETH,USDC, DAI, etc.)
- Your MarketAid instance can be queried via
getUserMarketAid()
onMarketAidFactory
or by reading the event emitted from thecreateMarketAidInstance()
call. - Verify you control the contract first via Etherscan by checking the
admin
variable is your address and that you are also anapprovedStrategist
. - Verify you are sending funds to the right address!
- Your MarketAid instance can be queried via
-
Send batches of Rubicon orders from your MarketAid contract using the functions described below
Note: The owner of a MarketAid
instance can approve additional addresses to use and access the contract (multiple addresses can clear the onlyApprovedStrategist
check)
Troubleshooting
Make sure to check the following:
- Where is my MarketAid instance?
- Query with
getUserMarketAid()
-> returns an array of all instances you have created
- Query with
- Your MarketAid instance has approved the contracts you want to interact with
- Otherwise, transactions will. Using
placeMarketMakingTrades()
will automatically approve theRubiconMarket
contract
- Otherwise, transactions will. Using
- You are the admin of your Market Aid instance
- Query with
admin()
on your instance -> returns the EOA that owns the instance
- Query with
- Any EOA you are trying to manage an ERC20 pair with, via MarketAid is an approved strategist
- Main security check in the contract, admin is auto-approved as a strategist
Functions (In Progress)
Generally, each of the inputs is the exact same as one would place on the low-level RubiconMarket: the raw uint values of the ERC20s you want to bid and/or ask for when placing offers ( offer()
). In order to batch, pass in the values for all the offers as arrays, with all like-variables (e.g. ask numerators) paired together. The arrays passed in must match in legth - this can be helpful when placing a number of offers at once with different prices. Note the use of formatUnits()
and parseUnits()
through ethers
helps with numbers and ERC-20 values when dealing with human number =
wei conversions.
An important concept is that each order placed through MarketAid (via placeMarketMakingTrades
) creates a unique ID that can identify a market maker's outstanding orders. The outstanding book can be queried at any time for any given EOA via getOutstandingStrategistTrades()
- this is helpful to manage an outstanding book and get the list of unique IDs or strategistTrades
a given user controls. A market maker can query their book, then requote ( batchRequoteOffers
) or cancel ( scrubStrategistTrades
) them as needed. Note that fill automatically accrues to the MarketAid
instance that a user controls.
- placeMarketMakingTrades
- Place a BID and/or an ASK in a single unique "strategistTrade"
- batchMarketMakingTrades
- Optionally place many placeMarketMakingTrades calls in a batch fashion to deploy an entire liquidity curve (e.g. 10 bids and 10 asks at differing prices and size depending on market conditions)
- getOutstandingStrategistTrades view
- Returns the unique IDs of a user's outstanding (batch) offers
- getStrategistTotalLiquidity view
- Returns the total ERC-20 book value amount a user controls across the market AND the value sitting on the MarketAid instance
- requote
- Single requote of a strategistTrade (bid and/or ask pair) to a new pair that generates a new unique id
- batchRequoteOffers
- Call requote() in batch to update an entire liquidity curve in a single go. This cancels all ids identified and replaces them with the new offer payloads specified
- batchRequoteAllOffers
- Grabs the user's outstanding book IDs via
getOutstandingStrategistTrades()
then updates them all viabatchRequoteOffers()
- Grabs the user's outstanding book IDs via
- scrubStrategistTrade
- Cancels a
strategistTrade
and returns funds that haven't been filled/taken to Market Aid
- Cancels a
- scrubStrategistTrades
- Cancel multiple
strategistTrade
s in a single transaction
- Cancel multiple
- adminPullAllFunds
- Helpful function for the admin to get all their funds back by specifying the ERC-20s they want
- strategistRebalanceFunds
- Optional function to use an external liquidity venue to arbitrage against or rebalance funds through