File size: 4,214 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
"""
ERC20 Permit Validator (EIP-2612)

Validates that the permit function correctly sets token allowance using signature-based approval.
"""

from typing import Dict, Any
from decimal import Decimal


class ERC20PermitValidator:
    """Validator for EIP-2612 permit operations"""
    
    def __init__(
        self,
        token_address: str,
        owner_address: str,
        spender_address: str,
        value: float,
        token_decimals: int = 18
    ):
        self.token_address = token_address.lower()
        self.owner_address = owner_address.lower()
        self.spender_address = spender_address.lower()
        
        # Use Decimal for precise calculation
        self.expected_allowance = int(Decimal(str(value)) * Decimal(10 ** token_decimals))
        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 EIP-2612 permit transaction
        
        Args:
            tx: Transaction object
            receipt: Transaction receipt
            state_before: State before transaction
            state_after: State after transaction
            
        Returns:
            Validation result dictionary
            
        Checks:
        1. Transaction success (40%)
        2. Allowance correctly set (60%)
        """
        
        checks = []
        score = 0
        details = {
            'token_address': self.token_address,
            'owner_address': self.owner_address,
            'spender_address': self.spender_address,
            'expected_allowance': self.expected_allowance
        }
        
        # Check 1: Transaction success (40 points)
        tx_success = receipt.get('status') == 1
        if tx_success:
            score += 40
            checks.append({
                'name': 'Transaction Success',
                'passed': True,
                'points': 40,
                'message': 'Permit transaction executed successfully'
            })
        else:
            checks.append({
                'name': 'Transaction Success',
                'passed': False,
                'points': 0,
                'message': f"Transaction failed with status: {receipt.get('status')}"
            })
            # Early return on transaction failure
            return {
                'score': score,
                'max_score': self.max_score,
                'passed': False,
                'checks': checks,
                'details': details
            }
        
        # Check 2: Allowance correctly set (60 points)
        # Get allowance from state_after (should be queried by executor)
        actual_allowance = state_after.get('allowance', 0)
        details['allowance_before'] = state_before.get('allowance', 0)
        details['allowance_after'] = actual_allowance
        
        if actual_allowance == self.expected_allowance:
            score += 60
            checks.append({
                'name': 'Allowance Set Correctly',
                'passed': True,
                'points': 60,
                'message': f'Allowance correctly set to {self.expected_allowance} units',
                'details': {
                    'expected': self.expected_allowance,
                    'actual': actual_allowance
                }
            })
        else:
            checks.append({
                'name': 'Allowance Set Correctly',
                'passed': False,
                'points': 0,
                'message': f'Allowance mismatch. Expected: {self.expected_allowance}, Got: {actual_allowance}',
                'details': {
                    'expected': self.expected_allowance,
                    'actual': actual_allowance
                }
            })
        
        # Determine overall pass/fail
        all_passed = all(check['passed'] for check in checks)
        
        return {
            'score': score,
            'max_score': self.max_score,
            'passed': all_passed,
            'checks': checks,
            'details': details
        }