Comments (7)
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:
So it's only two additional divisions by 997/1000 (so multiplications by 1000/997).
from v1-contracts.
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.
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.
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.
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.
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.
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.
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)
- contracts-vyper instruction HOT 1
- getTokenToTokenPrice HOT 2
- Tokens that do no return bools do not work HOT 1
- vNext Feature Request: Moving Average Price Feed Data HOT 3
- Unknown list type HOT 1
- How important is the `min_liquidity` argument when adding liquidity
- UIP 1: Optional Permissioned Liquidity Pools HOT 2
- Add support for EIP1155
- ERC-777 token support HOT 2
- Doesn’t woks with ᴇʀᴄ20 tokens which always returns nothing. HOT 1
- What will happen in case of REVERT inside DELEGATE_CALL? HOT 4
- Support for latest Vyper compiler HOT 4
- End to End encryption of Uniswap to prevent front running HOT 3
- Bytecode: deployedBytecode or constructor?
- ABI removeLiquidity return values name collision
- Can't compile contracts on master branch, are other branch contracts okay to test on local testnet? HOT 1
- Add ERC-20 compatibility check when creating a new exchange HOT 1
- Missing ETH after failed attempt to add additional funds to UBT/ETH Liquidity pool HOT 2
- 如何将一个地址中的代币转移到另外一个地址中?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v1-contracts.