Datasets:
Tasks:
Text Generation
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
ArXiv:
Tags:
benchmark
code-generation
transaction-code-generation
execution-grounded-evaluation
blockchain
evm
License:
File size: 8,334 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 220 | """
ERC20 FlashLoan Validator
Validate flashloan operation.
"""
from typing import Dict, Any
class ERC20FlashLoanValidator:
"""Validate ERC20 flashloan operation"""
def __init__(
self,
flashloan_contract_address: str,
token_address: str,
amount: float,
token_decimals: int,
fee_percentage: float
):
self.flashloan_contract = flashloan_contract_address.lower()
self.token_address = token_address.lower()
self.amount = amount
self.token_decimals = token_decimals
self.fee_percentage = fee_percentage
# Calculate fee (unit: token smallest unit)
amount_smallest = int(amount * (10 ** token_decimals))
self.expected_fee = int(amount_smallest * fee_percentage / 100)
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 flashloan 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: Flashloan contract (20%)
3. Used function: executeFlashLoan function (20%)
4. Paid correct fee (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': {
'flashloan_contract': self.flashloan_contract,
'token_address': self.token_address,
'amount': self.amount,
'expected_fee': self.expected_fee / (10 ** self.token_decimals),
'transaction_status': tx_status
}
}
# 2. ValidateCorrect contract called: Flashloan contract (20 points)
tx_to = tx.get('to', '').lower()
if tx_to == self.flashloan_contract:
checks.append({
'name': 'Contract Address',
'passed': True,
'message': f'Correct flash loan contract: {tx_to}',
'score': 20
})
total_score += 20
else:
checks.append({
'name': 'Contract Address',
'passed': False,
'message': f'Wrong contract. Expected: {self.flashloan_contract}, Got: {tx_to}',
'score': 20
})
# 3. ValidateUsed function: executeFlashLoan function (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:]
# executeFlashLoan(address,uint256) function selector
# First 4 bytes of keccak256("executeFlashLoan(address,uint256)")
expected_selector = '0x6065c245'
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 executeFlashLoan 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 executeFlashLoan ({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 fee payment (30 points)
# Flashloan causes user balance to decrease by fee amount
balance_before = state_before.get('token_balance', 0)
balance_after = state_after.get('token_balance', 0)
balance_decrease = balance_before - balance_after
# Allow some error (due to precision issues)
fee_tolerance = max(1, self.expected_fee // 100) # 1% tolerance
fee_check_passed = abs(balance_decrease - self.expected_fee) <= fee_tolerance
if fee_check_passed:
checks.append({
'name': 'Fee Payment',
'passed': True,
'message': f'Flash loan fee paid correctly: {balance_decrease / (10 ** self.token_decimals):.6f} tokens',
'score': 30,
'details': {
'balance_before': balance_before / (10 ** self.token_decimals),
'balance_after': balance_after / (10 ** self.token_decimals),
'balance_decrease': balance_decrease / (10 ** self.token_decimals),
'expected_fee': self.expected_fee / (10 ** self.token_decimals),
'fee_percentage': self.fee_percentage
}
})
total_score += 30
else:
checks.append({
'name': 'Fee Payment',
'passed': False,
'message': f'Fee mismatch. Expected: {self.expected_fee / (10 ** self.token_decimals):.6f}, Actual: {balance_decrease / (10 ** self.token_decimals):.6f}',
'score': 30,
'details': {
'balance_before': balance_before / (10 ** self.token_decimals),
'balance_after': balance_after / (10 ** self.token_decimals),
'balance_decrease': balance_decrease / (10 ** self.token_decimals),
'expected_fee': self.expected_fee / (10 ** self.token_decimals),
'fee_percentage': self.fee_percentage
}
})
# 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': {
'flashloan_contract': self.flashloan_contract,
'token_address': self.token_address,
'amount': self.amount,
'expected_fee': self.expected_fee / (10 ** self.token_decimals),
'balance_before': balance_before / (10 ** self.token_decimals),
'balance_after': balance_after / (10 ** self.token_decimals),
'balance_decrease': balance_decrease / (10 ** self.token_decimals),
'actual_selector': actual_selector
}
}
|