Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHIA-1069] Use smarter coin selection algorithm for DAO wallet select_coins_for_asset_type #18579

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions chia/wallet/dao_wallet/dao_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,21 +390,24 @@ async def get_balance_by_asset_type(self, asset_id: Optional[bytes32] = None) ->
return uint128(sum(cr.coin.amount for cr in records if not cr.spent))

# if asset_id == None: then we get normal XCH
async def select_coins_for_asset_type(self, amount: uint64, asset_id: Optional[bytes32] = None) -> List[Coin]:
async def select_coins_for_asset_type(
self, amount: uint64, action_scope: WalletActionScope, asset_id: Optional[bytes32] = None
) -> List[Coin]:
puzhash = get_p2_singleton_puzhash(self.dao_info.treasury_id, asset_id=asset_id)
records = await self.wallet_state_manager.coin_store.get_coin_records_by_puzzle_hash(puzhash)
# TODO: smarter coin selection algorithm
total = 0
coins = []
for record in records:
if not record.spent:
total += record.coin.amount
coins.append(record.coin)
if total >= amount:
break
if total < amount: # pragma: no cover
raise ValueError(f"Not enough of asset {asset_id}: {total} < {amount}")
return coins
unspent_records = [r for r in records if not r.spent]
spendable_amount = uint128(sum(r.coin.amount for r in unspent_records))
async with action_scope.use() as interface:
return list(
await select_coins(
spendable_amount,
action_scope.config.adjust_for_side_effects(interface.side_effects).tx_config.coin_selection_config,
unspent_records,
{},
self.log,
uint128(amount),
)
)

async def coin_added(self, coin: Coin, height: uint32, peer: WSChiaConnection, coin_data: Optional[Any]) -> None:
"""
Expand Down Expand Up @@ -1326,7 +1329,7 @@ async def create_proposal_close_spend(
if condition_statement.first().as_int() == 51:
sum += condition_statement.rest().rest().first().as_int()
if sum > 0:
xch_coins = await self.select_coins_for_asset_type(uint64(sum))
xch_coins = await self.select_coins_for_asset_type(uint64(sum), action_scope)
for xch_coin in xch_coins:
xch_parent_amount_list.append([xch_coin.parent_coin_info, xch_coin.amount])
solution = Program.to(
Expand All @@ -1349,7 +1352,9 @@ async def create_proposal_close_spend(
for condition in conditions.as_iter():
if condition.first().as_int() == 51:
sum_of_conditions += condition.rest().rest().first().as_int()
cat_coins = await self.select_coins_for_asset_type(uint64(sum_of_conditions), tail_hash)
cat_coins = await self.select_coins_for_asset_type(
uint64(sum_of_conditions), action_scope, tail_hash
)
parent_amount_list = []
for cat_coin in cat_coins:
sum_of_coins += cat_coin.amount
Expand Down
Loading