Skip to main content

OpenMevRouter

Sandy Bradley <sandy@commoditystream.com>, Sam Bacha <sam@commoditystream.com>, ControlCandP

OpenMevRouter

Optimal MEV router contract (IUniswapV2Router compatible)

Methods

acceptOwnership

function acceptOwnership() external nonpayable

Transfers ownership of the contract to the caller. Can only be called by a new potential owner set by the current owner.

addLiquidity

function addLiquidity(address tokenA, address tokenB, uint256 amountADesired, uint256 amountBDesired, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) external nonpayable returns (uint256 amountA, uint256 amountB, uint256 liquidity)

Adds liquidity to an ERC-20⇄ERC-20 pool. msg.sender should have already given the router an allowance of at least amountADesired/amountBDesired on tokenA/tokenB

Parameters

NameTypeDescription
tokenAaddressToken in pool
tokenBaddressToken in pool
amountADesireduint256Amount of token A desired to add to pool
amountBDesireduint256Amount of token B desired to add to pool
amountAMinuint256Minimum amount of token A, can be 0
amountBMinuint256Minimum amount of token B, can be 0
toaddressAddress to receive liquidity token
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountAuint256exact amount of token A added to pool
amountBuint256exact amount of token B added to pool
liquidityuint256amount of liquidity token received

addLiquidityETH

function addLiquidityETH(address token, uint256 amountTokenDesired, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) external payable returns (uint256 amountToken, uint256 amountETH, uint256 liquidity)

Adds liquidity to an ERC-20⇄WETH pool with ETH. msg.sender should have already given the router an allowance of at least amountTokenDesired on token. msg.value is treated as a amountETHDesired. Leftover ETH, if any, is returned to msg.sender

Parameters

NameTypeDescription
tokenaddressToken in pool
amountTokenDesireduint256Amount of token desired to add to pool
amountTokenMinuint256Minimum amount of token, can be 0
amountETHMinuint256Minimum amount of ETH, can be 0
toaddressAddress to receive liquidity token
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountTokenuint256exact amount of token added to pool
amountETHuint256exact amount of ETH added to pool
liquidityuint256amount of liquidity token received

cancelOwnershipTransfer

function cancelOwnershipTransfer() external payable

Cancel a transfer of ownership to a new account. Can only be called by the current owner.

executeOperation

function executeOperation(address[] assets, uint256[] amounts, uint256[] premiums, address initiator, bytes params) external nonpayable returns (bool)

Called from Aave Lending pool after contract has received the flash loaned amount (https://docs.aave.com/developers/v/2.0/guides/flash-loans)

Reverts if not profitable.

Parameters

NameTypeDescription
assetsaddress[]Array of tokens to loan
amountsuint256[]Array of amounts to loan
premiumsuint256[]Array of premiums to repay on loan amounts
initiatoraddressAddress of flashloan initiator
paramsbytesEncoded factories and tokens

Returns

NameTypeDescription
_0boolsuccess indicating success

getAmountIn

function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut) external pure returns (uint256 amountIn)

Parameters

NameTypeDescription
amountOutuint256undefined
reserveInuint256undefined
reserveOutuint256undefined

Returns

NameTypeDescription
amountInuint256undefined

getAmountOut

function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut) external pure returns (uint256 amountOut)

Parameters

NameTypeDescription
amountInuint256undefined
reserveInuint256undefined
reserveOutuint256undefined

Returns

NameTypeDescription
amountOutuint256undefined

getAmountsIn

function getAmountsIn(uint256 amountOut, address[] path) external view returns (uint256[] amounts)

Parameters

NameTypeDescription
amountOutuint256undefined
pathaddress[]undefined

Returns

NameTypeDescription
amountsuint256[]undefined

getAmountsOut

function getAmountsOut(uint256 amountIn, address[] path) external view returns (uint256[] amounts)

Parameters

NameTypeDescription
amountInuint256undefined
pathaddress[]undefined

Returns

NameTypeDescription
amountsuint256[]undefined

harvest

function harvest(uint256 percentage, address[] tokens, address[] receivers) external payable

Multi-sig consensus call to distribute a given percentage of specified tokens to specified receivers.

Parameters

NameTypeDescription
percentageuint256Percentage of balance to distribute
tokensaddress[]Array of token addresses to distribute
receiversaddress[]Array of addresses for receiving distribution

isOwner

function isOwner() external view returns (bool)

Returns true if the caller is the current owner.

Returns

NameTypeDescription
_0boolundefined

onFlashLoan

function onFlashLoan(address sender, address token, uint256 amount, uint256 fee, bytes data) external nonpayable

Called from BentoBox Lending pool after contract has received the flash loaned amount

Reverts if not profitable.

Parameters

NameTypeDescription
senderaddressAddress of flashloan initiator
tokenaddressToken to loan
amountuint256Amount to loan
feeuint256Fee to repay on loan amount
databytesEncoded factories and tokens

owner

function owner() external view returns (address)

Returns the address of the current owner.

Returns

NameTypeDescription
_0addressundefined

quote

function quote(uint256 amountA, uint256 reserveA, uint256 reserveB) external pure returns (uint256 amountB)

Parameters

NameTypeDescription
amountAuint256undefined
reserveAuint256undefined
reserveBuint256undefined

Returns

NameTypeDescription
amountBuint256undefined

removeLiquidity

function removeLiquidity(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) external nonpayable returns (uint256 amountA, uint256 amountB)

Removes liquidity from an ERC-20⇄ERC-20 pool. msg.sender should have already given the router an allowance of at least liquidity on the pool.

Parameters

NameTypeDescription
tokenAaddressToken in pool
tokenBaddressToken in pool
liquidityuint256Amount of liquidity tokens to remove
amountAMinuint256Minimum amount of token A, can be 0
amountBMinuint256Minimum amount of token B, can be 0
toaddressAddress to receive pool tokens
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountAuint256Amount of token A received
amountBuint256Amount of token B received

removeLiquidityETH

function removeLiquidityETH(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) external nonpayable returns (uint256 amountToken, uint256 amountETH)

Removes liquidity from an ERC-20⇄WETH pool and receive ETH. msg.sender should have already given the router an allowance of at least liquidity on the pool.

Parameters

NameTypeDescription
tokenaddressToken in pool
liquidityuint256Amount of liquidity tokens to remove
amountTokenMinuint256Minimum amount of token, can be 0
amountETHMinuint256Minimum amount of ETH, can be 0
toaddressAddress to receive pool tokens
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountTokenuint256Amount of token received
amountETHuint256Amount of ETH received

removeLiquidityETHSupportingFeeOnTransferTokens

function removeLiquidityETHSupportingFeeOnTransferTokens(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) external nonpayable returns (uint256 amountETH)

Identical to removeLiquidityETH, but succeeds for tokens that take a fee on transfer. msg.sender should have already given the router an allowance of at least liquidity on the pool.

Parameters

NameTypeDescription
tokenaddressToken in pool
liquidityuint256Amount of liquidity tokens to remove
amountTokenMinuint256Minimum amount of token, can be 0
amountETHMinuint256Minimum amount of ETH, can be 0
toaddressAddress to receive pool tokens
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountETHuint256Amount of ETH received

removeLiquidityETHWithPermit

function removeLiquidityETHWithPermit(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) external nonpayable returns (uint256 amountToken, uint256 amountETH)

Removes liquidity from an ERC-20⇄WETTH pool and receive ETH without pre-approval, thanks to permit

Parameters

NameTypeDescription
tokenaddressToken in pool
liquidityuint256Amount of liquidity tokens to remove
amountTokenMinuint256Minimum amount of token, can be 0
amountETHMinuint256Minimum amount of ETH, can be 0
toaddressAddress to receive pool tokens
deadlineuint256Unix timestamp in seconds after which the transaction will revert
approveMaxboolWhether or not the approval amount in the signature is for liquidity or uint(-1)
vuint8The v component of the permit signature
rbytes32The r component of the permit signature
sbytes32The s component of the permit signature

Returns

NameTypeDescription
amountTokenuint256Amount of token received
amountETHuint256Amount of ETH received

removeLiquidityETHWithPermitSupportingFeeOnTransferTokens

function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) external nonpayable returns (uint256 amountETH)

Identical to removeLiquidityETHWithPermit, but succeeds for tokens that take a fee on transfer.

Parameters

NameTypeDescription
tokenaddressToken in pool
liquidityuint256Amount of liquidity tokens to remove
amountTokenMinuint256Minimum amount of token, can be 0
amountETHMinuint256Minimum amount of ETH, can be 0
toaddressAddress to receive pool tokens
deadlineuint256Unix timestamp in seconds after which the transaction will revert
approveMaxboolWhether or not the approval amount in the signature is for liquidity or uint(-1)
vuint8The v component of the permit signature
rbytes32The r component of the permit signature
sbytes32The s component of the permit signature

Returns

NameTypeDescription
amountETHuint256Amount of ETH received

removeLiquidityWithPermit

function removeLiquidityWithPermit(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) external nonpayable returns (uint256 amountA, uint256 amountB)

Removes liquidity from an ERC-20⇄ERC-20 pool without pre-approval, thanks to permit.

Parameters

NameTypeDescription
tokenAaddressToken in pool
tokenBaddressToken in pool
liquidityuint256Amount of liquidity tokens to remove
amountAMinuint256Minimum amount of token A, can be 0
amountBMinuint256Minimum amount of token B, can be 0
toaddressAddress to receive pool tokens
deadlineuint256Unix timestamp in seconds after which the transaction will revert
approveMaxboolWhether or not the approval amount in the signature is for liquidity or uint(-1)
vuint8The v component of the permit signature
rbytes32The r component of the permit signature
sbytes32The s component of the permit signature

Returns

NameTypeDescription
amountAuint256Amount of token A received
amountBuint256Amount of token B received

swapETHForExactTokens

function swapETHForExactTokens(uint256 amountOut, address[] path, address to, uint256 deadline) external payable returns (uint256[] amounts)

Receive an exact amount of tokens for as little ETH as possible, along the route determined by the path. The first element of path must be WETH9. Leftover ETH, if any, is returned to msg.sender. amountInMax = msg.value

Require has been replaced with revert for gas optimization. Fallback alternate router check for insufficient output amount. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountOutuint256Amount of output tokens that must be received
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountsuint256[]Array of input token amount and all subsequent output token amounts

swapExactETHForTokens

function swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline) external payable returns (uint256[] amounts)

Swaps an exact amount of ETH for as many output tokens as possible, along the route determined by the path. The first element of path must be WETH9, the last is the output token. amountIn = msg.value

Require has been replaced with revert for gas optimization. Fallback alternate router check for insufficient output amount. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountOutMinuint256Minimum amount of output tokens that must be received
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountsuint256[]Array of input token amount and all subsequent output token amounts

swapExactETHForTokensSupportingFeeOnTransferTokens

function swapExactETHForTokensSupportingFeeOnTransferTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline) external payable

Identical to swapExactETHForTokens, but succeeds for tokens that take a fee on transfer. amountIn = msg.value

Require has been replaced with revert for gas optimization. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountOutMinuint256Minimum amount of output tokens that must be received
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

swapExactTokensForETH

function swapExactTokensForETH(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline) external nonpayable returns (uint256[] amounts)

Swaps an exact amount of tokens for as much ETH as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH9.

Require has been replaced with revert for gas optimization. Fallback alternate router check for insufficient output amount. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountInuint256Amount of input tokens to send.
amountOutMinuint256Minimum amount of ETH that must be received
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountsuint256[]Array of input token amount and all subsequent output token amounts

swapExactTokensForETHSupportingFeeOnTransferTokens

function swapExactTokensForETHSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline) external nonpayable

Identical to swapExactTokensForETH, but succeeds for tokens that take a fee on transfer.

Require has been replaced with revert for gas optimization. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountInuint256Amount of input tokens to send.
amountOutMinuint256Minimum amount of ETH that must be received
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

swapExactTokensForTokens

function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline) external nonpayable returns (uint256[] amounts)

Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through. msg.sender should have already given the router an allowance of at least amountIn on the input token.

Require has been replaced with revert for gas optimization. Fallback alternate router check for insufficient output amount. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountInuint256Amount of input tokens to send.
amountOutMinuint256Minimum amount of output tokens that must be received
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountsuint256[]Array of input token amount and all subsequent output token amounts

swapExactTokensForTokensSupportingFeeOnTransferTokens

function swapExactTokensForTokensSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline) external nonpayable

Identical to swapExactTokensForTokens, but succeeds for tokens that take a fee on transfer. msg.sender should have already given the router an allowance of at least amountIn on the input token.

Require has been replaced with revert for gas optimization. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountInuint256Amount of input tokens to send.
amountOutMinuint256Minimum amount of output tokens that must be received
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

swapTokensForExactETH

function swapTokensForExactETH(uint256 amountOut, uint256 amountInMax, address[] path, address to, uint256 deadline) external nonpayable returns (uint256[] amounts)

Receive an exact amount of ETH for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH9. msg.sender should have already given the router an allowance of at least amountInMax on the input token.

Require has been replaced with revert for gas optimization. Fallback alternate router check for insufficient output amount. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountOutuint256Amount of ETH to receive
amountInMaxuint256Maximum amount of input tokens
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountsuint256[]Array of input token amount and all subsequent output token amounts

swapTokensForExactTokens

function swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, address[] path, address to, uint256 deadline) external nonpayable returns (uint256[] amounts)

Receive an exact amount of output tokens for as few input tokens as possible, along the route determined by the path. msg.sender should have already given the router an allowance of at least amountInMax on the input token.

Require has been replaced with revert for gas optimization. Fallback alternate router check for insufficient output amount. Attempt to back-run swaps.

Parameters

NameTypeDescription
amountOutuint256Amount of output tokens to receive
amountInMaxuint256Maximum amount of input tokens
pathaddress[]Array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity
toaddressAddress of receiver
deadlineuint256Unix timestamp in seconds after which the transaction will revert

Returns

NameTypeDescription
amountsuint256[]Array of input token amount and all subsequent output token amounts

transferOwnership

function transferOwnership(address newOwner) external payable

Allows a new account (newOwner) to accept ownership. Can only be called by the current owner.

Parameters

NameTypeDescription
newOwneraddressundefined

updateAaveAsset

function updateAaveAsset(bool isActive, address asset) external payable

Update internal Aave asset flag

Parameters

NameTypeDescription
isActiveboolBoolean flagging whether to use the asset for Aave flashloans
assetaddressAddress of asset

updateAllAaveAssets

function updateAllAaveAssets() external payable

Update all internal Aave assets

Events

LoanError

event LoanError(address indexed token, uint256 amountIn)

Parameters

NameTypeDescription
token indexedaddressundefined
amountInuint256undefined

MEV

event MEV(address indexed user, address indexed token, uint256 value)

Parameters

NameTypeDescription
user indexedaddressundefined
token indexedaddressundefined
valueuint256undefined

OwnershipTransferred

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)

Parameters

NameTypeDescription
previousOwner indexedaddressundefined
newOwner indexedaddressundefined

Errors

ExcessiveInputAmount

error ExcessiveInputAmount()

ExecuteNotAuthorized

error ExecuteNotAuthorized()

Expired

error Expired()

IdenticalAddresses

error IdenticalAddresses()

InsufficientAAmount

error InsufficientAAmount()

InsufficientAllowance

error InsufficientAllowance()

InsufficientBAmount

error InsufficientBAmount()

InsufficientLiquidity

error InsufficientLiquidity()

InsufficientOutputAmount

error InsufficientOutputAmount()

InvalidPath

error InvalidPath()

NoReceivers

error NoReceivers()

NoTokens

error NoTokens()

NotPercent

error NotPercent()

Overflow

error Overflow()

TokenIsFeeOnTransfer

error TokenIsFeeOnTransfer()

TransferFailed

error TransferFailed()

Unauthorized

error Unauthorized()

ZeroAddress

error ZeroAddress()

ZeroAmount

error ZeroAmount()