This repository contains the smart contracts for an automated trading system, designed to execute orders as they come within range. There are two primary types of orders supported by the system: Bracket Orders and Stop Limit Orders.
The Automation Master contract is designed to be monitored by Chainlink Automation-type systems. Anyone can fill any of the orders as long as the order is eligible and they provide the necessary assets to satisfy the slippage requirements. The token-out assets are sent to the user as part of the upkeep function.
A Bracket Order executes an automated swap when either the takeProfit
or stopPrice
conditions are met. The purpose of a Bracket Order is to allow traders to define both a profit target (takeProfit
) and a stop loss (stopPrice
) in a single transaction. The order is filled when either of these price conditions is reached, swapping the input token (tokenIn
) for the output token (tokenOut
).
takeProfit
: The execution price at which a profit target is reached.stopPrice
: The price at which the order is closed to limit losses.
A Stop Limit Order is used to trigger the creation of a new Bracket Order when the stopLimitPrice
condition is met. Once the stop limit price is reached, a Bracket Order is automatically created using the same unique orderId
and parameters such as takeProfit
and stopPrice
.
- Shared Order ID: Both the Stop Limit Order and the resulting Bracket Order share the same
orderId
for easy tracking and management.
By manipulating the stopPrice
or the takeProfit
in a Bracket Order, two more order types can be functionally replicated.
-
Limit Order: By setting the
stopPrice
to 0, the system will have functionally created a standard limit order. This order type will only execute when thetakeProfit
is reached. -
Stop Loss Order: By setting the
takeProfit
to the maximum possible value (2 ** 256 - 1
), the system will have functionally created a stop loss order. This order type executes when thestopPrice
is reached to minimize potential losses.
For all examples, assume WETH
price is $3000
- User holds
1 WETH
and creates a Bracket Order, with atakeProfit
set to3200
and astopPrice
set to2500
. - If either of these are reached, the user's
1 WETH
will be automaticly swapped to how ever muchUSDC
can be bought at that price
- User holds
1 WETH
and creates a Bracket Order, with atakeProfit
set to3200
and astopPrice
set to0
. - In this scenario, the user will never sell their
WETH
until thetakeProfit
is reached
- User holds
1 WETH
and creates a Bracket Order, with atakeProfit
set to(2^256) - 1
and astopPrice
set to2800
. - In this scenario, the user will hold their
WETH
until the price has dropped to thestopPrice
, at which point they will sell forUSDC
- User holds
3000 USDC
and creates a Stop Limit Order with astopLimitPrice
set to2800
- Once this price is reached, the Stop Limit Order is filled, creating a new Bracket Order. This new Bracket Order will share the same
orderId
as the Stop Limit Order - Suppose this new Bracket Order has a
stopPrice
at2500
, andWETH
continues to fall to this price. - Once this price is reached, the Bracket Order will be filled, and the user's
USDC
will be swapped toWETH
- User holds
2800 USDC
and creates a Stop Limit Order with astopLimitPrice
set to2800
andswapOnFill
set totrue
- Once this price is reached, the Stop Limit Order is filled, swapping the
2800 USDC
for1 WETH
and creating a new Bracket Order. This new Bracket Order will share the sameorderId
as the Stop Limit Order - Suppose this new Bracket Order has a
takeProfit
at3000
, andWETH
bounces back to this price. - Once this price is reached, the Bracket Order will be filled, and the user's
1 WETH
will be swapped back to3000 USDC
, and the user has profited ~200 USDC
-
Bracket Order Creation:
function createOrder( bytes calldata swapPayload, // Optional data for executing a swap when the Stop Limit order is filled uint256 takeProfit, // Price to trigger take-profit. uint256 stopPrice, // Price to trigger stop-loss. uint256 amountIn, // Amount of tokenIn to sell when conditions are met. IERC20 tokenIn, // Token to sell IERC20 tokenOut, // Token to buy. address recipient, // Address to receive tokenOut once the order is filled. uint16 takeProfitSlippage, // Slippage tolerance for take-profit price, defined simply in basis points. uint16 stopSlippage, // Slippage tolerance for stop-loss price, defined simply in basis points. bool permit, // Indicates whether Permit2 is used for token approvals. bytes calldata permitPayload // Permit2 signature payload for approval-less token transfers. ) external;
-
Stop Limit Order Creation:
function createOrder( uint256 stopLimitPrice, // Price to trigger the Stop Limit order. uint256 takeProfit, // Target price for the resulting Bracket Order to take profit. uint256 stopPrice, // Stop-loss price for the resulting Bracket Order. uint256 amountIn, // Amount of tokenIn to sell when conditions are met. IERC20 tokenIn, // Token to sell. IERC20 tokenOut, // Token to buy. address recipient, // Address to receive tokenOut once the order is filled. uint16 takeProfitSlippage, // Slippage tolerance for the take-profit price in the Bracket Order. uint16 stopSlippage, // Slippage tolerance for the stop-loss price in the Bracket Order. uint16 swapSlippage, // Slippage tolerance for the initial swap when the Stop Limit order is filled. bool swapOnFill, // Determines if the tokens should be swapped immediately after the Stop Limit order is filled. bool permit, // Indicates whether Permit2 is used for token approvals. bytes calldata permitPayload // Permit2 signature payload for approval-less token transfers. ) external;
Oracles are expected to return a USD price in 1e8 terms, so the price of USDC should be returned as ~1e8 or ~100000000
In order to run the tests, create a .env file and add a MAINNET_URL and ARB_URL and assign these to the appropriate RPC addresses. Here is an example .env file:
MAINNET_URL="https://rpc.ankr.com/eth"
ARB_URL="https://rpc.ankr.com/arbitrum"
Then the tests can then be run by npm run test