ExchangeUnion / xud

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

Make `tradinglimits` account for lightning chan reserve #1945

Closed kilrau closed 4 years ago

kilrau commented 4 years ago

EDIT: renamed this issue, new goal below.

LND 0.12 will return inbound balance with the channelbalance call. Since this is considerably more efficient and accurate than our current "iterate through lnd's listchannels" approach, we should move to this call too once lndbtc & lndltc 0.12 are released.

Also I just double checked the following:

Here is the but: lnd's local_balance and remote_balance subtract the commit_fee, but not the channel reserve chan_reserve_sat which is essentially non-usable balance. Question is: do we want to manually subtract channel reserves from our local and remote balances to give the user a correct picture of what balance is actually usable?

raladev commented 4 years ago

Question is: do we want to manually subtract channel reserves from our local and remote balances to give the user a correct picture of what balance is actually usable?

Im for providing this value, because it can be big enough.

sangaman commented 4 years ago

do we want to manually subtract channel reserves from our local and remote balances to give the user a correct picture of what balance is actually usable?

I think it makes sense to subtract channel reserves since it is non tradeable balance. However, to do so wouldn't we still need to iterate through the list of channels?

Also, do we actually know that this new approach would be more efficient or accurate than iterating through the list of channels? I don't know why our current approach wouldn't be accurate, and my guess would be it's at least roughly the same in terms of efficiency - either way it's not computationally expensive to iterate through a list of up to a couple dozen channels.

kilrau commented 4 years ago

I think it makes sense to subtract channel reserves since it is non tradeable balance. However, to do so wouldn't we still need to iterate through the list of channels?

Yes, that's why I brought it up. The new lnd channelbalance doesn't include something like total inbound/outbound chan reserve fields...

Also, do we actually know that this new approach would be more efficient or accurate than iterating through the list of channels? I don't know why our current approach wouldn't be accurate, and my guess would be it's at least roughly the same in terms of efficiency - either way it's not computationally expensive to iterate through a list of up to a couple dozen channels.

Yep, for us even more accurate since we'd exclude channel reserve.

Summary of the todo's:

rsercano commented 4 years ago

move tradinglimits' maxBuy & maxSell to iterating through channels (currently we are using lnd's channelbalance for maxSell I believe)

Currently we're already iterating through channels for lnd, correct me if I'm mistaken @sangaman.

subtract chan reserve from tradinglimits' maxBuy & maxSell

And currently that's how maxSell & maxBuy is being calculated:

const swapCapacities = await swapClient.swapCapacities(currency);
      const reservedOutbound = this.outboundReservedAmounts.get(currency) ?? 0;
      const reservedInbound = this.inboundReservedAmounts.get(currency) ?? 0;
      const availableOutboundCapacity = Math.max(0, swapCapacities.totalOutboundCapacity - reservedOutbound);
      const availableInboundCapacity = Math.max(0, swapCapacities.totalInboundCapacity - reservedInbound);

      return {
        reservedOutbound,
        reservedInbound,
        maxSell: Math.min(swapCapacities.maxOutboundChannelCapacity, availableOutboundCapacity),
        maxBuy: Math.min(swapCapacities.maxInboundChannelCapacity, availableInboundCapacity),
      };

In here while calculating available outbound & inbound capacities we're subtracting reserved amounts?

rsercano commented 4 years ago

And for fee calculation I'm not sure how to handle it, can somebody please guide me?

kilrau commented 4 years ago

Channel reserve is already accounted for by now (see https://github.com/ExchangeUnion/xud/pull/1988#issuecomment-724897010) and pre-calculating routing fees is hardly possible so I am closing here.