File size: 5,878 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
"""
BNB Transfer Max Amount Validator

Validates that maximum BNB amount (balance - gas) was transferred
"""

from typing import Dict, Any


class BNBTransferMaxAmountValidator:
    """Validator for BNB maximum amount transfer"""
    
    def __init__(self, to_address: str):
        """
        Initialize validator
        
        Args:
            to_address: Expected recipient address
        """
        self.expected_recipient = to_address.lower()
        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 the transaction execution results
        
        Args:
            tx: Transaction object
            receipt: Transaction receipt
            state_before: Blockchain state before transaction
            state_after: Blockchain state after transaction
            
        Returns:
            Validation results including score and details
        """
        score = 0
        details = {}
        checks = []
        
        # Check 1: Transaction success (30 points)
        tx_success = receipt.get('status') == 1
        if tx_success:
            score += 30
            checks.append({
                'name': 'Transaction Success',
                'passed': True,
                'points': 30,
                'message': 'Transaction executed successfully'
            })
        else:
            checks.append({
                'name': 'Transaction Success',
                'passed': False,
                'points': 0,
                'message': f"Transaction failed with status: {receipt.get('status')}"
            })
            return {
                'score': score,
                'max_score': self.max_score,
                'passed': False,
                'checks': checks,
                'details': details
            }
        
        # Check 2: Recipient address correct (20 points)
        actual_to = tx.get('to', '').lower()
        recipient_correct = actual_to == self.expected_recipient
        
        if recipient_correct:
            score += 20
            checks.append({
                'name': 'Recipient Address',
                'passed': True,
                'points': 20,
                'message': f'Correct recipient: {self.expected_recipient}'
            })
        else:
            checks.append({
                'name': 'Recipient Address',
                'passed': False,
                'points': 0,
                'message': f'Expected: {self.expected_recipient}, Got: {actual_to}'
            })
        
        details['expected_recipient'] = self.expected_recipient
        details['actual_to'] = actual_to
        
        # Check 3: Maximum amount transferred (30 points)
        # Max amount = balance_before - gas_cost
        balance_before = state_before.get('balance', 0)
        balance_after = state_after.get('balance', 0)
        
        gas_used = receipt.get('gasUsed', 0)
        gas_price = receipt.get('effectiveGasPrice', 0)
        gas_cost = gas_used * gas_price
        
        # The actual transferred amount
        actual_transferred = tx.get('value', 0)
        
        # Expected max transfer = balance - gas cost
        expected_max_transfer = balance_before - gas_cost
        
        # Allow small tolerance (0.1% or 1000 wei, whichever is larger)
        tolerance = max(int(expected_max_transfer * 0.001), 1000)
        transfer_correct = abs(actual_transferred - expected_max_transfer) <= tolerance
        
        if transfer_correct:
            score += 30
            checks.append({
                'name': 'Maximum Amount Transferred',
                'passed': True,
                'points': 30,
                'message': f'Transferred max amount: {actual_transferred / 10**18:.6f} BNB (balance: {balance_before / 10**18:.6f} BNB, gas: {gas_cost / 10**18:.6f} BNB)'
            })
        else:
            checks.append({
                'name': 'Maximum Amount Transferred',
                'passed': False,
                'points': 0,
                'message': f'Expected: {expected_max_transfer / 10**18:.6f} BNB, Transferred: {actual_transferred / 10**18:.6f} BNB'
            })
        
        details['balance_before'] = balance_before
        details['gas_cost'] = gas_cost
        details['expected_max_transfer'] = expected_max_transfer
        details['actual_transferred'] = actual_transferred
        
        # Check 4: Sender balance minimal (20 points)
        # After transferring max amount, balance should be close to 0
        # Only dust should remain (less than 0.0001 BNB = 100000000000000 wei)
        max_remaining = 100000000000000  # 0.0001 BNB
        
        balance_minimal = balance_after <= max_remaining
        
        if balance_minimal:
            score += 20
            checks.append({
                'name': 'Sender Balance Minimal',
                'passed': True,
                'points': 20,
                'message': f'Sender balance minimal: {balance_after / 10**18:.10f} BNB (< 0.0001 BNB)'
            })
        else:
            checks.append({
                'name': 'Sender Balance Minimal',
                'passed': False,
                'points': 0,
                'message': f'Sender balance too high: {balance_after / 10**18:.6f} BNB (should be < 0.0001 BNB)'
            })
        
        details['balance_after'] = balance_after
        details['max_remaining_allowed'] = max_remaining
        
        # Final result
        passed = score >= self.max_score * 0.8  # 80% threshold
        
        return {
            'score': score,
            'max_score': self.max_score,
            'passed': passed,
            'checks': checks,
            'details': details
        }