GithubHelp home page GithubHelp logo

Comments (7)

danrobinson avatar danrobinson commented on July 22, 2024 1

Good news: the formula I derived above was wrong, and the true formula is much simpler! (The error is that I was assuming the marginal price of adding to the trade was the same as the marginal price at the start of the next trade—it actually isn't).

Here's the actual formula:

Screen Shot 2019-06-11 at 2 05 35 PM

So it's only two additional divisions by 997/1000 (so multiplications by 1000/997).

from v1-contracts.

danrobinson avatar danrobinson commented on July 22, 2024

I think this is a very good idea (and one that I believe Hayden's thought about as well).

I believe this is the formula you're looking for (incorporating fees): EDIT: this formula was incorrect; see corrected one below.

Deriving Formula

It is admittedly quite gnarly. I suspect it might be simpler if fees were charged on the way out rather than the way in, although I haven't done the math yet.

from v1-contracts.

OliverNChalk avatar OliverNChalk commented on July 22, 2024

Hmm, what are your thoughts on having the user do this maths off chain then send a trade on chain with the result and some checks on the state of the market?

from v1-contracts.

danrobinson avatar danrobinson commented on July 22, 2024

You can already do that by doing the calculation and placing a normal market order that fails if it gets worse execution than expected. The usefulness of adding this function, I would think, is its different way of handling the race condition when other transactions have changed the price between when the user submitted the tx and when it is executed on chain.

To be clear, the computation is inelegant but it’s not burdensome—it could probably be done in only a few hundred gas more than your computation.

from v1-contracts.

OliverNChalk avatar OliverNChalk commented on July 22, 2024

Well, without fees you can pass the sqrt of the invariant to the contract and save a good portion of the gas costs. That said, I don't think the same approach would work as well when fees get involved due to the ever growing invariant. That said, you could have the trade fallback on calculating the sqrt if a trade occurs before yours.

I'm interested in your thoughts on front-running this type of transaction. I imagine that frontrunners would be incentivized to buy the first half of your order before you, then you purchase the second half of your order, then they sell into the new price you created.

This would mean in the worst case, these types of orders return slightly more than 50% of their "true" volume. Does this seem accurate?

from v1-contracts.

danrobinson avatar danrobinson commented on July 22, 2024

I'm interested in your thoughts on front-running this type of transaction. I imagine that frontrunners would be incentivized to buy the first half of your order before you, then you purchase the second half of your order, then they sell into the new price you created.

I believe this is correct (ignoring fees) if there is only one front-runner and Uniswap is the only way they can buy or sell the asset.

  • The result changes if there are multiple front-runners. Assuming (unrealistically) that they queue up rather than competing in a gas auction, the first would trade the asset halfway, the second would push it half of the rest of the way (so to 75% of the original order), and so on in accordance with Zeno's paradox.
  • The optimal strategy also changes if the front-runner is able to combine the front-running with some arbitrage. If, for example, the front-runner can buy and sell arbitrary amounts of the token on Coinbase at the starting price, then they should let your entire buy order execute, buy that same quantity on Coinbase, and sell the full amount back on Uniswap (pushing the price back to the starting price).

This would mean in the worst case, these types of orders return slightly more than 50% of their "true" volume. Does this seem accurate?

If you have fees, then yes, I think that if someone optimally front-runs you (and the caveats above don't apply), a little more than half of your order will execute (although you will receive less than half of what you would have received, because the half that does execute is executing at a worse price).

from v1-contracts.

OliverNChalk avatar OliverNChalk commented on July 22, 2024

I implemented this and tested i with variable fees, it appears holds true for all fees < 49.99%. I will probably implement a wrapper contract for Uniswap and do some tests relating to gas cost. Will also experiment with computing the sqrt off-chain. Thanks a tonne for your help!

from v1-contracts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.