File size: 8,215 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
213
214
215
216
217
218
219
"""
ERC1155 Transfer Single Validator

Validate ERC1155 single token ID transfer operation.
"""

from typing import Dict, Any


class ERC1155TransferSingleValidator:
    """Validate ERC1155 safeTransferFrom operation"""
    
    def __init__(
        self,
        nft_address: str,
        to_address: str,
        token_id: int,
        amount: int
    ):
        self.nft_address = nft_address.lower()
        self.to_address = to_address.lower()
        self.token_id = token_id
        self.expected_amount = amount
        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 ERC1155 transfer 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 executed successfully (30%)
        2. Correct contract called: ERC1155 contract (20%)
        3. Used function: safeTransferFrom (20%)
        4. Sender balance decreased, receiver balance increased (30%)
        """
        checks = []
        total_score = 0
        
        # 1. Validate transaction success (30 points)
        tx_status = receipt.get('status', 0)
        if tx_status == 1:
            checks.append({
                'name': 'Transaction Success',
                'passed': True,
                'message': 'Transaction executed successfully',
                'score': 30
            })
            total_score += 30
        else:
            checks.append({
                'name': 'Transaction Success',
                'passed': False,
                'message': f'Transaction failed with status: {tx_status}',
                'score': 30
            })
            # If transaction failed, return directly
            return {
                'passed': False,
                'score': 0,
                'max_score': self.max_score,
                'checks': checks,
                'details': {
                    'nft_address': self.nft_address,
                    'to_address': self.to_address,
                    'token_id': self.token_id,
                    'expected_amount': self.expected_amount,
                    'transaction_status': tx_status
                }
            }
        
        # 2. ValidateCorrect contract called: ERC1155 contract (20 points)
        tx_to = tx.get('to', '').lower()
        if tx_to == self.nft_address:
            checks.append({
                'name': 'Contract Address',
                'passed': True,
                'message': f'Correct ERC1155 contract: {tx_to}',
                'score': 20
            })
            total_score += 20
        else:
            checks.append({
                'name': 'Contract Address',
                'passed': False,
                'message': f'Wrong contract. Expected: {self.nft_address}, Got: {tx_to}',
                'score': 20
            })
        
        # 3. Validate Used function: safeTransferFrom (20 points)
        tx_data = tx.get('data', '') or tx.get('input', '')
        
        if isinstance(tx_data, bytes):
            tx_data = tx_data.hex()
        if isinstance(tx_data, str) and tx_data.startswith('0x'):
            tx_data = tx_data[2:]
        
        # safeTransferFrom(address,address,uint256,uint256,bytes) function selector
        expected_selector = '0xf242432a'
        actual_selector = 'N/A'
        
        if len(tx_data) >= 8:
            actual_selector = '0x' + tx_data[:8]
            
            if actual_selector.lower() == expected_selector.lower():
                checks.append({
                    'name': 'Function Signature',
                    'passed': True,
                    'message': f'Correct safeTransferFrom selector: {actual_selector}',
                    'score': 20,
                    'details': {
                        'expected': expected_selector,
                        'actual': actual_selector
                    }
                })
                total_score += 20
            else:
                checks.append({
                    'name': 'Function Signature',
                    'passed': False,
                    'message': f'Incorrect function selector. Expected safeTransferFrom ({expected_selector}), got {actual_selector}',
                    'score': 20,
                    'details': {
                        'expected': expected_selector,
                        'actual': actual_selector
                    }
                })
        else:
            checks.append({
                'name': 'Function Signature',
                'passed': False,
                'message': 'Transaction data too short or missing',
                'score': 20
            })
        
        # 4. Validate balance change (30 points)
        sender_balance_before = state_before.get('erc1155_balance', 0)
        sender_balance_after = state_after.get('erc1155_balance', 0)
        recipient_balance_before = state_before.get('target_erc1155_balance', 0)
        recipient_balance_after = state_after.get('target_erc1155_balance', 0)
        
        sender_decrease = sender_balance_before - sender_balance_after
        recipient_increase = recipient_balance_after - recipient_balance_before
        
        balance_check_passed = (
            sender_decrease == self.expected_amount and
            recipient_increase == self.expected_amount
        )
        
        if balance_check_passed:
            checks.append({
                'name': 'Balance Transfer',
                'passed': True,
                'message': f'Balances changed correctly: -{self.expected_amount} from sender, +{self.expected_amount} to recipient',
                'score': 30,
                'details': {
                    'sender_balance_before': sender_balance_before,
                    'sender_balance_after': sender_balance_after,
                    'sender_decrease': sender_decrease,
                    'recipient_balance_before': recipient_balance_before,
                    'recipient_balance_after': recipient_balance_after,
                    'recipient_increase': recipient_increase,
                    'expected_amount': self.expected_amount
                }
            })
            total_score += 30
        else:
            checks.append({
                'name': 'Balance Transfer',
                'passed': False,
                'message': f'Balance mismatch. Expected: {self.expected_amount}, Sender decrease: {sender_decrease}, Recipient increase: {recipient_increase}',
                'score': 30,
                'details': {
                    'sender_balance_before': sender_balance_before,
                    'sender_balance_after': sender_balance_after,
                    'sender_decrease': sender_decrease,
                    'recipient_balance_before': recipient_balance_before,
                    'recipient_balance_after': recipient_balance_after,
                    'recipient_increase': recipient_increase,
                    'expected_amount': self.expected_amount
                }
            })
        
        # Aggregate results
        all_passed = all(check['passed'] for check in checks)
        
        return {
            'passed': all_passed,
            'score': total_score,
            'max_score': self.max_score,
            'checks': checks,
            'details': {
                'nft_address': self.nft_address,
                'to_address': self.to_address,
                'token_id': self.token_id,
                'expected_amount': self.expected_amount,
                'sender_balance_before': sender_balance_before,
                'sender_balance_after': sender_balance_after,
                'recipient_balance_before': recipient_balance_before,
                'recipient_balance_after': recipient_balance_after,
                'actual_selector': actual_selector
            }
        }