Skip to content

Commit

Permalink
rpc: batch on-chain fee estimate calc includes account version
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Jan 19, 2023
1 parent e0623e7 commit 0e7a18b
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,9 @@ func (s *rpcServer) prepareLeasesResponse(ctx context.Context,
// paid.
totalNumChannels := 0

// Keep a tally of the total number of channels per account key.
accountChannelsCount := make(map[[33]byte]uint32)

// Keep track of the index for the first lease found for this
// batch. We'll use this to know where to start from when
// populating the ChainFeeSat field for each lease.
Expand All @@ -2282,7 +2285,8 @@ func (s *rpcServer) prepareLeasesResponse(ctx context.Context,
}

// Filter out the account if required.
_, ok = accounts[ourOrder.Details().AcctKey]
accountKey := ourOrder.Details().AcctKey
_, ok = accounts[accountKey]
if len(accounts) != 0 && !ok {
continue
}
Expand Down Expand Up @@ -2418,20 +2422,29 @@ func (s *rpcServer) prepareLeasesResponse(ctx context.Context,

// Count the total number of channels.
totalNumChannels++

// Count the total number of channels per
// account.
accountChannelsCount[accountKey]++
}

// Estimate the chain fees paid for the number of
// channels created in this batch and tally them.
//
// TODO(guggero): This is just an approximation! We
// should properly calculate the fees _per account_ as
// that's what we do on the server side. Then we can
// also take a look at the actual account version at the
// time of the batch.
chainFee := order.EstimateTraderFee(
uint32(totalNumChannels), batch.BatchTxFeeRate,
account.VersionInitialNoVersion,
)
chainFee := btcutil.Amount(0)
for accountKey, numChannels := range accountChannelsCount {
// Calculate and sum the account specific chain
// fee estimate.
//
// The account version influences the chain fee.
// It is important to use the account version
// that was used when the batch was created.
accountInBatch := batch.Accounts[accountKey]

chainFee += order.EstimateTraderFee(
numChannels, batch.BatchTxFeeRate,
accountInBatch.Version,
)
}

// We'll need to compute the chain fee paid for each
// lease. Since multiple leases can be created within a
Expand Down

0 comments on commit 0e7a18b

Please sign in to comment.