ExchangeUnion / xud

Exchange Union Daemon 🔁 ⚡️
https://exchangeunion.com
GNU Affero General Public License v3.0
115 stars 44 forks source link

SWAP calculations #604

Closed kilrau closed 6 years ago

kilrau commented 6 years ago

@moshababo discovered:

I was looking at the swaps code for something, and there’s something in the currency/amount calculations that I don’t understand. Considering i’m on pair LTC/BTC and the price is 0.01. we have 2 scenarios:

1)

maker order: buy quantity 5 taker order: sell quantity 5

swap amounts: maker: 5 LTC taker: 0.05 BTC

2)

maker order: sell quantity 5 taker order: buy quantity 5

swap amounts maker: 5 BTC taker: 0.05 LTC

--

My calculations are based on: https://github.com/ExchangeUnion/xud/blob/master/lib/swaps/Swaps.ts#L91 https://github.com/ExchangeUnion/xud/blob/master/lib/swaps/Swaps.ts#L241

sangaman commented 6 years ago

Thanks for catching and describing this, it's indeed a bug and it's possible for the amounts to get switched. I opened a PR to fix this and also a similar bug in the logic for deriving the SwapResult from a completed swap.