Skip to content

Commit

Permalink
Dev (#5)
Browse files Browse the repository at this point in the history
fix fee calculation for compulsory list
  • Loading branch information
vladmelnyk authored Feb 7, 2019
1 parent a45b44b commit 136b9a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = 'com.coinselection'
version = '0.0.4'
version = '0.0.5'
buildscript {
ext.kotlin_version = '1.3.11'
ext.spring_boot_version = '2.0.6.RELEASE'
Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/com.coinselection/BtcCoinSelectionProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class BtcCoinSelectionProvider(
compulsoryUtxoList: List<UnspentOutput>): UtxoSumCalculationData? {
val cumulativeHolder = CumulativeHolder.defaultInit()
cumulativeHolder.appendFee(costCalculator.getBaseFee())
val selectedCompulsoryUtxoList = compulsoryUtxoList
.asSequence()
.take(maxNumberOfInputs)
.onEach { appendSumAndFee(cumulativeHolder, costCalculator, sum = it.amount) }
.toList()
val selectedUtxoList = utxoListRearanged
.asSequence()
.takeWhile { sumIsLessThanTarget(cumulativeHolder, targetValue) }
Expand All @@ -86,7 +91,7 @@ class BtcCoinSelectionProvider(
if (sumIsLessThanTarget(cumulativeHolder, targetValue)) {
return null
}
return UtxoSumCalculationData(compulsoryUtxoList + selectedUtxoList, cumulativeHolder)
return UtxoSumCalculationData(selectedCompulsoryUtxoList + selectedUtxoList, cumulativeHolder)
}

private fun fallbackLargestFirstSelection(utxoListRearanged: List<UnspentOutput>,
Expand All @@ -99,7 +104,7 @@ class BtcCoinSelectionProvider(
} else {
val cumulativeHolder = dataPair.cumulativeHolder
cumulativeHolder.appendFee(costCalculator.getBaseFee())
UtxoSumCalculationData(compulsoryUtxoList + dataPair.utxoList, cumulativeHolder)
UtxoSumCalculationData(dataPair.utxoList, cumulativeHolder)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class BtcCoinSelectionProviderTest {
val coinSelectionResult = coinSelectionProvider.provide(utxoList, targetValue, smartFeePerByte, compulsoryUtxoList = compulsoryUtxoList)
Assertions.assertNotNull(coinSelectionResult!!.selectedUtxos)
Assertions.assertTrue(coinSelectionResult.selectedUtxos!!.containsAll(compulsoryUtxoList))
val totalFeeExpected = calculateTransactionFee(coinSelectionResult.selectedUtxos!!.size, 2, smartFeePerByte)
val totalFee = coinSelectionResult.totalFee
Assertions.assertEquals(totalFeeExpected, totalFee)

}

private fun createUnspentOutput(value: Double): UnspentOutput {
Expand Down

0 comments on commit 136b9a9

Please sign in to comment.