Skip to content

Commit

Permalink
remove duplicate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jack60612 committed Mar 31, 2022
1 parent 9f79b6f commit 6c8c2e4
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions chia/wallet/coin_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ async def select_coins(
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
smallest_coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins, amount)
log.debug(f"Selected closest greater coin: {smallest_coin.name()}")
return {smallest_coin}
elif smaller_coin_sum > amount:
Expand All @@ -88,21 +87,11 @@ async def select_coins(
if coin_set is None:
raise ValueError("Knapsack algorithm failed to find a solution.")
if len(coin_set) > max_num_coins:
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."
)
coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins, amount)
coin_set = {coin}
return coin_set
else:
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."
)
coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins, amount)
log.debug(f"Resorted to selecting smallest coin over target due to dust.: {coin}")
return {coin}

Expand All @@ -118,12 +107,20 @@ def check_for_exact_match(coin_list: List[Coin], target: uint64) -> Optional[Coi
return None


def select_smallest_coin_over_target(smaller_coin_list: List[Coin], valid_spendable_coin_list: List[Coin]) -> Coin:
def select_smallest_coin_over_target(
smaller_coin_list: List[Coin], valid_spendable_coin_list: List[Coin], target_amount: uint128
) -> Coin:
if len(smaller_coin_list) > 0: # in case we only have bigger coins.
greater_coins = valid_spendable_coin_list[: -len(smaller_coin_list)]
else:
greater_coins = valid_spendable_coin_list
return greater_coins[len(greater_coins) - 1] # select the coin with the least value.
coin = greater_coins[len(greater_coins) - 1] # select the coin with the least value.
if coin is None or coin.amount < target_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."
)
return coin


# we use this to find the set of coins which have total value closest to the target, but at least the target.
Expand Down

0 comments on commit 6c8c2e4

Please sign in to comment.