simplify and de duplicate code

This commit is contained in:
Jack Nelson
2022-03-31 11:47:52 -04:00
parent 6ab1cc79bb
commit 2d19a53245
+16 -16
View File
@@ -73,28 +73,28 @@ async def select_coins(
if coin.amount < amount:
smaller_coin_sum += coin.amount
smaller_coins.append(coin)
if smaller_coin_sum == amount:
if len(smaller_coins) < 500:
log.debug(
f"Selected all smaller coins because they equate to an exact match of the target.: {smaller_coins}"
)
return set(smaller_coins)
else:
smallest_coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins)
assert smallest_coin is not None
log.debug(f"Selected closest greater coin due to the large amount of coins. {smallest_coin.name()}")
return {smallest_coin}
if smaller_coin_sum == amount and len(smaller_coins) < 500:
log.debug(f"Selected all smaller coins because they equate to an exact match of the target.: {smaller_coins}")
return set(smaller_coins)
elif smaller_coin_sum < amount:
smallest_coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins)
assert smallest_coin is not None
log.debug(f"Selected closest greater coin: {smallest_coin.name()}")
return {smallest_coin}
else:
knapsack_coin_set = knapsack_coin_algorithm(smaller_coins, amount, max_coin_amount)
assert knapsack_coin_set is not None
log.debug(f"Selected coins from knapsack algorithm: {knapsack_coin_set}")
return knapsack_coin_set
coin_set = knapsack_coin_algorithm(smaller_coins, amount, max_coin_amount)
log.debug(f"Selected coins from knapsack algorithm: {coin_set}")
if coin_set is None or len(coin_set) > 500: # lots of dust
coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins)
if coin is None or coin.amount < amount:
raise ValueError(
"Can't make this transaction because the transaction would use over 500 "
"coins which is unlikely to get confirmed. Try making a smaller transaction to condense the dust."
)
else:
coin_set = {coin}
log.debug(f"Resorted to selecting smallest coin over target due to dust.: {coin_set}")
return coin_set
# These algorithms were based off of the algorithms in: