Limit Orders on Uniswap v2.
Limit Orders is a protocol for automated and decentralized orders exchange powered by Ethereum.
Limit orders give traders complete control over the rate at which their orders will be executed, enabling traders to automate transactions at a specific rate.
It continues the base commitment to free and decentralized exchange.
Every token combination is available. There isn't a limitation on how you can select what to buy and what to sell (Of course if it is a token living on the Ethereum network).
An order at Limit Orders can only be canceled by its creator, and it can be executed if the creator receives the desired amount, making the system trustless and independent of a central entity.
The smart contract is validated and can be reviewed by anyone. The code hasn't been audited by a reputable third party yet, and we advise to proceed with caution.
Imagine the current rate of DAI -> ETH is 0.003
. So, based on the market price if the user trades 400 DAI
will receive 1.2 ETH
. BUT, a user wants to sell 400 DAI
in exchange for 1.6 ETH
(desired_output
).
The user creates a limit order at limit.orders by sending a transaction with the following values:
- Input: 400 DAI
- Rate: 0.004 DAI-ETH
- Output: 1.6 ETH
Once the transaction is confirmed, relayers will start checking if the order can be fulfilled. Reyalers can have their own strategy on how to execute an order. Limit Orders, has a minimum of two relayers running 24/7 with a basic strategy.
The first thing they do is to check how much output, in this case ETH, they will get in exchange for 400 DAI
(trade_output
). Then, if it is higher or equal than 1.6 ETH
(which is what the user set as the minimum output) they check how much will cost to send the transaction to do the trade (execution cost
). Once relayers get the execution_cost
, they check if they can still achieve the output defined by the user:
desired_output <= (trade_output - execution_cost)
execution_cost
depends on the Gas Price. Higher gas prices, higher execution_cost
. You can read more about Gas Price here.
Finally, relayers can charge a fee for executing the order (relayer_fee
). The final formula will be:
desired_output <= (trade_output - execution_cost - relayer_fee)
Even the math can match, have in mind that if the amount to trade is high, a price impact will occur depending on the liquidity of the pool used.
To continue with real numbers, if the execution_cost
is 0.04 ETH and the relayers_fee
is 0.006:
1.6 ETH <= trade_output - 0.04 ETH - 0.006 ETH
1.6 ETH <= trade_output - 0.046 ETH
1.6 ETH + 0.046 ETH <= trade_output
1.646 ETH <= trade_output // Final rate 0.004115 for execution
If you want to add your token reach out us.
Repo forked and modified from Pine.