File size: 6,154 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
"""
BNB Transfer to Contract Validator

Validates that a BNB transfer is sent to a valid contract address
"""

from typing import Dict, Any


class BNBTransferToContractValidator:
    """Validator for BNB transfers to smart contract addresses"""
    
    def __init__(self, contract_address: str, amount: float):
        """
        Initialize validator
        
        Args:
            contract_address: Expected contract address
            amount: Expected transfer amount in BNB (float)
        """
        from decimal import Decimal
        
        self.expected_to = contract_address.lower()
        # Use Decimal for precise BNB to wei conversion
        self.expected_amount = int(Decimal(str(amount)) * Decimal(10**18))
        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 is a contract address (20 points)
        actual_to = tx.get('to', '').lower()
        to_correct = actual_to == self.expected_to
        
        # Check if target is a contract (has code)
        target_code_size = state_before.get('contract_code_size', 0)
        is_contract = target_code_size > 0
        
        if to_correct and is_contract:
            score += 20
            checks.append({
                'name': 'Recipient is Contract',
                'passed': True,
                'points': 20,
                'message': f'Correct contract address: {self.expected_to} (code size: {target_code_size} bytes)'
            })
        else:
            # Failed: either wrong address or not a contract
            if to_correct and not is_contract:
                error_msg = f'Address correct but target is not a contract (code size: 0)'
            else:
                error_msg = f'Expected contract: {self.expected_to}, Got: {actual_to}'
            
            checks.append({
                'name': 'Recipient is Contract',
                'passed': False,
                'points': 0,
                'message': error_msg
            })
        
        details['expected_to'] = self.expected_to
        details['actual_to'] = actual_to
        details['is_contract'] = is_contract
        details['contract_code_size'] = target_code_size
        
        # Check 3: Transfer amount (30 points)
        actual_value = int(tx.get('value', 0))
        amount_correct = actual_value == self.expected_amount
        
        if amount_correct:
            score += 30
            checks.append({
                'name': 'Transfer Amount',
                'passed': True,
                'points': 30,
                'message': f'Correct amount: {self.expected_amount} wei'
            })
        else:
            checks.append({
                'name': 'Transfer Amount',
                'passed': False,
                'points': 0,
                'message': f'Expected: {self.expected_amount} wei, Got: {actual_value} wei'
            })
        
        details['expected_amount'] = self.expected_amount
        details['actual_amount'] = actual_value
        
        # Check 4: Contract balance change (20 points)
        contract_balance_before = state_before.get('target_balance', 0)
        contract_balance_after = state_after.get('target_balance', 0)
        
        expected_contract_balance = contract_balance_before + self.expected_amount
        balance_diff = abs(contract_balance_after - expected_contract_balance)
        
        # Allow 0.1% tolerance for rounding errors
        tolerance = max(int(self.expected_amount * 0.001), 1)
        balance_correct = balance_diff <= tolerance
        
        if balance_correct:
            score += 20
            checks.append({
                'name': 'Contract Balance Change',
                'passed': True,
                'points': 20,
                'message': f'Contract balance increased by {self.expected_amount} wei'
            })
        else:
            checks.append({
                'name': 'Contract Balance Change',
                'passed': False,
                'points': 0,
                'message': f'Expected increase: {self.expected_amount} wei, Actual: {contract_balance_after - contract_balance_before} wei'
            })
        
        details['contract_balance_before'] = contract_balance_before
        details['contract_balance_after'] = contract_balance_after
        details['contract_balance_change'] = contract_balance_after - contract_balance_before
        
        # 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
        }