Skip to content
This repository was archived by the owner on Sep 17, 2021. It is now read-only.

Market limit orders for Uniswap #33

@OliverNChalk

Description

@OliverNChalk

Reasoning & Overview

I'm proposing market limit orders that would allow users to purchase all the tokens up to a certain price point. This can be implemented as a wrapper contract for V1 and might make sense to be a core function for V2.

Currently, Uniswap lets users trade based on volume and enforce minimum returns on the volume of their trade. This trade type would be the counterpart and have the volume dynamically set based on the price Uniswap is offering to the user.

Implementation Without Fees

Eth to Token Market Limit:

ethToTokenSwapLimit(price_limit: uint256) -> uint256:
    price_accuracy: uint256 = 10**9  #9 decimals of price precision
    start_input_reserve: uint256 = self.balance
    start_output_reserve: uint256 = self.token.balanceOf(self)
    invariant: uint256 = start_input_reserve * start_output_reserve
    
    target_input_reserve: uint256 = sqrt(invariant * target_price / price_accuracy)
    trade_volume: uint256 = target_input_reserve - start_input_reserve
    self.ethToTokenInput(trade_volume, 1, -1, msg.sender, msg.sender)

This method will purchase all the available tokens such that the cost of the last wei of token is <= price_limit.

Dealing with Uniswap's Fees

This works perfectly fine before you introduce fees to the system. Due to the nature of how Uniswap's pricing mechanism works, I am having a hard time predicting token cost with the introduction of fees. For example, simply setting price_limit to price_limit * 997/1000 is too conservative a strategy, resulting in the cost of the last wei of token being well under the price_limit.

It is my hope that someone smarter than me will be able to make this formula work with fees in the way Uniswap has implemented them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions