@@ -405,6 +405,18 @@ def _sanitize_string(self, input_string: Optional[str]) -> Optional[str]:
405405
406406 return sanitized
407407
408+ def _to_safe_int (self , value : Optional [str ]) -> int :
409+ """Safely convert string to int, handling float strings via int(float()) with fallback to 0."""
410+ if value is None or value .strip () == "" :
411+ return 0
412+ try :
413+ return int (float (value .strip ()))
414+ except (ValueError , TypeError ) as e :
415+ self .logger .warning (
416+ f"Failed to parse '{ value } ' as int: { e } , defaulting to 0"
417+ )
418+ return 0
419+
408420 def _get_agent_token_holders (self , dao_id : UUID ) -> List [AgentWithWalletTokenDTO ]:
409421 """Get agents that hold tokens for the given DAO.
410422
@@ -447,13 +459,7 @@ def _conduct_quorum_lottery(
447459 Returns:
448460 LotterySelection: Complete lottery results with quorum tracking
449461 """
450- try :
451- _ = int (proposal_liquid_tokens or "0" )
452- except ValueError as e :
453- self .logger .warning (
454- f"Invalid proposal_liquid_tokens '{ proposal_liquid_tokens } ': { e } - returning empty selection"
455- )
456- return LotterySelection ()
462+ self ._to_safe_int (proposal_liquid_tokens )
457463
458464 selection = LotterySelection ()
459465
@@ -466,7 +472,7 @@ def _conduct_quorum_lottery(
466472 filtered_agents = [
467473 agent
468474 for agent in agents_with_tokens
469- if int (agent .token_amount or "0" ) >= min_threshold
475+ if self . _to_safe_int (agent .token_amount ) >= min_threshold
470476 ]
471477 total_filtered = len (filtered_agents )
472478 total_original = len (agents_with_tokens )
@@ -535,7 +541,7 @@ def _conduct_quorum_lottery(
535541 )
536542 selection .selected_wallets .append (wallet_dict )
537543
538- selected_tokens += Decimal (selected_agent .token_amount )
544+ selected_tokens += Decimal (self . _to_safe_int ( selected_agent .token_amount ) )
539545
540546 self .logger .debug (
541547 f"Round { round_number } : Selected agent { selected_agent .agent_id } "
@@ -566,7 +572,7 @@ def _conduct_quorum_lottery(
566572 )
567573 selection .selected_wallets .append (wallet_dict )
568574
569- selected_tokens += Decimal (selected_agent .token_amount )
575+ selected_tokens += Decimal (self . _to_safe_int ( selected_agent .token_amount ) )
570576
571577 # Update final totals
572578 selection .total_selected_tokens = str (selected_tokens )
@@ -586,7 +592,7 @@ def _perform_weighted_selection(
586592 self , remaining_agents : List [AgentWithWalletTokenDTO ]
587593 ) -> AgentWithWalletTokenDTO :
588594 """Perform a single round of weighted random agent selection (assumes random is seeded)."""
589- weights = [int (agent .token_amount or "0" ) for agent in remaining_agents ]
595+ weights = [self . _to_safe_int (agent .token_amount ) for agent in remaining_agents ]
590596 if not weights or all (w == 0 for w in weights ):
591597 self .logger .warning ("All remaining weights are zero, using equal weights" )
592598 weights = [1 ] * len (remaining_agents )
0 commit comments