File size: 7,808 Bytes
edbc049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
Query ERC20 Balance Validator

Validate correctness of ERC20 token balance query operation
"""

from typing import Dict, Any, List


class QueryERC20BalanceValidator:
    """Query ERC20 Balance Validator"""
    
    def __init__(self, token_address: str, query_address: str, token_decimals: int = 18, expected_balance: float = None, **kwargs):
        """
        Initialize validator
        
        Args:
            token_address: ERC20 token contract address
            query_address: Address to query balance for
            token_decimals: Token decimal places (default: 18)
            expected_balance: Expected balance (not used in validation, set via Anvil)
        """
        self.token_address = token_address.lower()
        self.query_address = query_address.lower()
        self.token_decimals = token_decimals
        self.max_score = 100
    
    def validate(
        self,
        tx: Dict[str, Any],
        receipt: Dict[str, Any],
        state_before: Dict[str, Any],
        state_after: Dict[str, Any]
    ) -> Dict[str, Any]:
        """
        Validate query result
        
        Args:
            tx: Transaction object (for queries, this contains the query result)
            receipt: Transaction receipt (not used for queries)
            state_before: State before query (contains actual token balance)
            state_after: State after query
            
        Returns:
            Validation result dictionary
        """
        checks = []
        score = 0
        
        # For query operations, the result is stored in the tx object
        query_result = tx.get('query_result', {})
        
        # Check 1: Query executed successfully (30 points)
        query_success = query_result.get('success', False)
        error_msg = query_result.get('error', '')
        
        checks.append({
            'name': 'Query Execution Success',
            'passed': query_success,
            'message': 'Query executed successfully' if query_success else f'Query failed: {error_msg}',
            'score': 30 if query_success else 0
        })
        if query_success:
            score += 30
        else:
            # If query failed, return early
            return {
                'passed': False,
                'score': score,
                'max_score': self.max_score,
                'checks': checks,
                'feedback': self._generate_feedback(checks, score, False),
                'details': {
                    'token_address': self.token_address,
                    'query_address': self.query_address,
                    'error': error_msg
                }
            }
        
        # Check 2: Return format correct (30 points)
        # Expected format: { balance_raw: string, balance_formatted: string }
        returned_data = query_result.get('data', {})
        has_balance_raw = 'balance_raw' in returned_data
        
        format_correct = has_balance_raw
        format_score = 30 if format_correct else 0
        
        checks.append({
            'name': 'Return Format Correct',
            'passed': format_correct,
            'message': f"Expected 'balance_raw' field in result. Got: {list(returned_data.keys())}",
            'score': format_score
        })
        if format_correct:
            score += format_score
        else:
            # If format is incorrect, can't validate correctness
            return {
                'passed': False,
                'score': score,
                'max_score': self.max_score,
                'checks': checks,
                'feedback': self._generate_feedback(checks, score, False),
                'details': {
                    'token_address': self.token_address,
                    'query_address': self.query_address,
                    'returned_keys': list(returned_data.keys())
                }
            }
        
        # Check 3: Balance value correctness (40 points)
        # Compare returned balance with actual chain state
        returned_balance_raw = returned_data.get('balance_raw', '')
        
        # Get actual token balance from state_before
        actual_balance = state_before.get('token_balance', 0)
        
        try:
            # Convert returned balance to int
            if isinstance(returned_balance_raw, str):
                if returned_balance_raw.startswith('0x'):
                    returned_balance = int(returned_balance_raw, 16)
                else:
                    returned_balance = int(returned_balance_raw)
            else:
                returned_balance = int(returned_balance_raw)
            
            # Balance should match exactly
            balance_correct = returned_balance == actual_balance
            
            # Calculate human-readable values
            actual_balance_formatted = actual_balance / (10 ** self.token_decimals)
            returned_balance_formatted = returned_balance / (10 ** self.token_decimals)
            
            checks.append({
                'name': 'Balance Correctness',
                'passed': balance_correct,
                'message': f"Expected: {actual_balance} ({actual_balance_formatted:.6f} tokens), "
                          f"Got: {returned_balance} ({returned_balance_formatted:.6f} tokens)",
                'score': 40 if balance_correct else 0
            })
            if balance_correct:
                score += 40
            
            balance_value_for_details = returned_balance
            
        except (ValueError, TypeError) as e:
            checks.append({
                'name': 'Balance Correctness',
                'passed': False,
                'message': f"Failed to parse balance value: {e}",
                'score': 0
            })
            balance_value_for_details = None
            actual_balance_formatted = 0
            returned_balance_formatted = 0
        
        # Determine if passed
        passed = all(check['passed'] for check in checks)
        
        # Generate feedback
        feedback = self._generate_feedback(checks, score, passed)
        
        return {
            'passed': passed,
            'score': score,
            'max_score': self.max_score,
            'checks': checks,
            'feedback': feedback,
            'details': {
                'token_address': self.token_address,
                'query_address': self.query_address,
                'token_decimals': self.token_decimals,
                'expected_balance_raw': actual_balance,
                'expected_balance_formatted': actual_balance_formatted,
                'returned_balance_raw': balance_value_for_details,
                'returned_balance_formatted': returned_balance_formatted if balance_value_for_details else None
            }
        }
    
    def _generate_feedback(
        self,
        checks: List[Dict[str, Any]],
        score: int,
        passed: bool
    ) -> str:
        """
        Generate feedback message
        
        Args:
            checks: List of check items
            score: Total score
            passed: Whether validation passed
            
        Returns:
            Feedback string
        """
        lines = []
        
        if passed:
            lines.append("🎉 Congratulations! ERC20 balance query executed correctly!")
            lines.append(f"Final Score: {score}/{self.max_score}")
        else:
            lines.append("❌ Query validation failed, please check the following issues:")
            failed_checks = [c for c in checks if not c['passed']]
            for check in failed_checks:
                lines.append(f"  - {check['name']}: {check['message']}")
            lines.append(f"\nCurrent Score: {score}/{self.max_score}")
        
        return '\n'.join(lines)