defichain-wallet
defichain-wallet copied to clipboard
dex swap price not accurate
If someone wants to swap on illiquid markets or (dependent on the pool size) huge amounts, the slippage have to be set really high, because the price estimation is wrong.
You have to check how much tokens will be received if a certain amount of tokens will be added to the pool.
Currently it is more like:
estimated = (reserveA / reserveB) * amountA
But it should be:
q = reserveA * reserveB
//Calculate the output of amount of token B if token A will be put into the pool:
amountB = reserveB - (q / (reserveA + amountA))
//Calculate the output of amount of token A if token B will be put into the pool:
amountA = reserveA - (q / (reserveB + amountB))
//Calculate the prices
priceA = amountB / amountA
priceB = amountA / amountB
This have to be done for each pool of the composite swap and for single pool swaps.
A small example based on dPG-dUSD by selling 100 dPG:
reserveA = 2833.534
reserveB = 352347.23
q = reserveA * reserveB //998 387 856.01082
//Current formula
estimated = (352347.23 / 2833.534) * 100 //price = 124.349
= 12434.90 dUSD
Correct calculation:
//amountB = reserveB - (q / (reserveA + amountA))
amountB = 352347.23 - (998387856.01082 / (2833.534 + 100)) //340336.214
= 12011.01573
price = 12011.01 / 100 = 120.11
//Estimated price (124.349) is roughly 3.5% above the real price of 120.11
Negative effects for users are they can either sell only in small chunks or have to increase the slippage which allows bots to take advantage if the user is increasing the slippage too much. Slippage should only cover regular fees and price changes from other swaps which will be made between current state and users swap.
https://github.com/DeFiCh/wallet/blob/92e74e438a8f2fb4ce4a92ba183aa88f2cb040b7/mobile-app/app/screens/AppNavigator/screens/Portfolio/hooks/TokenBestPath.ts#L106-L111
@mbochmann: Thanks for opening an issue, it is currently awaiting triage.
The triage/accepted label can be added by foundation members by writing /triage accepted in a comment.
Details
I am a bot created to help the DeFiCh developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.
@mbochmann: There are no 'area' labels on this issue. Adding an appropriate label will greatly expedite the process for us. You can add as many area as you see fit. If you are unsure what to do you can ignore this!
You can add area labels by leaving a /area
comment.
Details
I am a bot created to help the DeFiCh developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.
@mbochmann: Thanks for opening an issue, an appropriate priority will be added soon.
The priority labels can be added by foundation members by writing /priority [type] in a comment.
Details
I am a bot created to help the DeFiCh developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.
Hi @mbochmann, thanks for raising. We are aware of this and something that we will improve in future releases.