Skip to main content



Trading contract used to arb uniswap pairs to a desired "true" price. Intended use is to arb UMA perpetual synthetics that trade off peg. This implementation can ber used in conjunction with a DSProxy contract to atomically swap and move a uniswap market.


swapToPrice(bool tradingAsEOA, address uniswapRouter, address uniswapFactory, address[2] swappedTokens, uint256[2] truePriceTokens, uint256[2] maxSpendTokens, address to, uint256 deadline) (public)

Swaps an amount of either token such that the trade results in the uniswap pair's price being as close as possible to the truePrice.

True price is expressed in the ratio of token A to token B. The caller must approve this contract to spend whichever token is intended to be swapped.


  • tradingAsEOA: bool to indicate if the UniswapV2Broker is being called by a DSProxy or an EOA.
  • uniswapRouter: address of the uniswap router used to facilitate trades.
  • uniswapFactory: address of the uniswap factory used to fetch current pair reserves.
  • swappedTokens: array of addresses which are to be swapped. The order does not matter as the function will figure out which tokens need to be exchanged to move the market to the desired "true" price.
  • truePriceTokens: array of unit used to represent the true price. 0th value is the numerator of the true price and the 1st value is the the denominator of the true price.
  • maxSpendTokens: array of unit to represent the max to spend in the two tokens.
  • to: recipient of the trade proceeds.
  • deadline: to limit when the trade can execute. If the tx is mined after this timestamp then revert.
computeTradeToMoveMarket(uint256 truePriceTokenA, uint256 truePriceTokenB, uint256 reserveA, uint256 reserveB) โ†’ bool aToB, uint256 amountIn (public)

Given the "true" price a token (represented by truePriceTokenA/truePriceTokenB) and the reservers in the uniswap pair, calculate: a) the direction of trade (aToB) and b) the amount needed to trade (amountIn) to move the pool price to be equal to the true price.

Note that this method uses the Babylonian square root method which has a small margin of error which will result in a small over or under estimation on the size of the trade needed.


  • truePriceTokenA: the nominator of the true price.
  • truePriceTokenB: the denominator of the true price.
  • reserveA: number of token A in the pair reserves
  • reserveB: number of token B in the pair reserves
getReserves(address factory, address tokenA, address tokenB) โ†’ uint256 reserveA, uint256 reserveB (public)
sortTokens(address tokenA, address tokenB) โ†’ address token0, address token1 (internal)
pairFor(address factory, address tokenA, address tokenB) โ†’ address pair (internal)