DeFiCh / ain

DeFi Blockchain - enabling decentralized finance on Bitcoin
https://defichain.com
MIT License
408 stars 122 forks source link

Cannot reproduce listaccounthistory amounts from calculated amounts #1508

Closed BenTStark closed 2 years ago

BenTStark commented 2 years ago

What happened:

According to my research, transaction 13077be5d2e9226337bd8775cdca7955ca9e9ca0196c0677dc614232db17534d is the first addpoolliquidity to BTC-DFI. Resulting in 40667.00000000 DFI as reserveB and 1.00000000 BTC as reserveA

According to my research first poolswap is transaction 75c0e497f8b9032f481f9f6cb5a97230ec7b8d5c80bc93f6afb87cabe65f779d swapping 9.00000000 DFI.

listaccounthistory output for this transaction: CLI input: listaccounthistory 8eG9Pe1wQnWZuXD5NRr3QaxDex9RJ99fd5 '{"maxBlockHeight":469104,"depth":1,"limit":100,"no_rewards":true}' Output: [ { "owner": "8eG9Pe1wQnWZuXD5NRr3QaxDex9RJ99fd5", "blockHeight": 469103, "blockHash": "a130c06b1a810b4a6d7f910d9f5acc3f38d6481599c276eccc140976347e685f", "blockTime": 1606727765, "type": "PoolSwap", "txn": 1, "txid": "75c0e497f8b9032f481f9f6cb5a97230ec7b8d5c80bc93f6afb87cabe65f779d", "amounts": [ "0.00022086@BTC" ] } ]

What you expected to happen:

According to the formula in https://github.com/DeFiCh/ain/blob/a7d914f15f762d121ac2c5d07d38d30cf7e09d4d/src/masternodes/poolpairs.cpp of the method Res CPoolPair::Swap I calculate and amount of 0.00022042 BTC

How to reproduce it (as minimally and precisely as possible):

With the values provided above this can be done in a simple Excel sheet. The behaviour of listaccounthistory is not accessable for me (lack of C++ skill :) )

Anything else we need to know?:

I encounter this behavior in several other poolswaps following. Interestingly, the difference is in the range of the two times the dexFeeOut.

defichain-bot commented 2 years ago

@BenTStark: 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](https://github.com/DeFiCh) developers manage community feedback and contributions. You can check out my [manifest file](https://github.com/DeFiCh/ain/blob/master/.github/governance.yml) to understand my behavior and what I can do. If you want to use this for your project, you can check out the [DeFiCh/oss-governance-bot](https://github.com/DeFiCh/oss-governance-bot) repository.
BenTStark commented 2 years ago

I did some code review into old cpp files. Before Fort Canning Hill, there was no DexFee implemented in the PoolSwap. That explains the difference

shohamc1 commented 2 years ago

Hey, thanks for opening this issue. Could you share your calculations here?

By my calculations (shortened version below) nothing seems to be wrong. Do note that this is before BayfrontGardensHeight and DakotaHeight, and at that point in time the pool just had a 0.2% commision and no DEX fee.

$in = 9 (1 - 0.002) = 8.982$ $poolF = 40667$ $poolT = 1$ $stepTo = \frac{poolTo in}{poolF} = \frac{1 * 8.982}{40667} = 0.00022086$ (truncated to 8 decimals)

Edit: seems like you got it as well 😃

BenTStark commented 2 years ago

Thanks a lot @shohamc1 for taking your time. I really appreciate this.