Uniswap / v1-contracts

🐍Uniswap V1 smart contracts
GNU General Public License v3.0
495 stars 318 forks source link

Exchange rate from block-to-block #9

Closed destenson closed 5 years ago

destenson commented 5 years ago

I came across Uniswap & the smart contracts that implement them a couple days ago & so far, I must say it seems to work great. I really like it.

However, looking through the source code, at least one flaw seemed to appear that I'd like to bring to your attention. I have not reviewed them thoroughly, and I'm not a vyper expert, so I definitely could be mistaken.

It looks like the exchange rate can change with each block as new swaps occur. But when you add or remove liquidity, the exchange rate may change between the block that the transaction was created and the mined block that it appears in. Since the arguments for these calls must be calculated exactly from the exchange rate, I think this could make it very difficult to use once activity increases.

Increasing the gas provided with the transaction would decrease the number of blocks between signing a transaction & it being mined, mitigating this somewhat. However, a very active market will be nearly impossible to add or remove liquidity, and when these calls fail, I think they will consume all of the gas provided for them. I think this will be quite hostile for people trying to participate in providing liquidity.

I think this is not an issue if the functions are always called by another contract so that calculations are always correct for the block the transaction is mined in.

I'm currently writing a wrapper contract in solidity to do that, among other things. I might switch to vyper if it always compiles code as efficient as yours. I've been away from coding smart contracts for a bit & vyper was new & untested then, so I've always written them in solidity.

Any thoughts?

gundas commented 5 years ago

@destenson I do not think this would be a big problem - in a very a active market the price should not move too much (except for some rare cases). The Liquidity Provider has an ability to specify his tolerance level by adjusting min_liquidity and max_tokens parameters when adding liquidity.