Add support for decoding ABIs
This commit is contained in:
parent
de3c6bf849
commit
2c709b9e0d
36
mev_inspect/decode.py
Normal file
36
mev_inspect/decode.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
from typing import Dict, Optional
|
||||||
|
|
||||||
|
from hexbytes import HexBytes
|
||||||
|
from eth_abi import decode_abi
|
||||||
|
|
||||||
|
from mev_inspect.schemas.abi import ABI, ABIFunctionDescription
|
||||||
|
from mev_inspect.schemas.call_data import CallData
|
||||||
|
|
||||||
|
|
||||||
|
class ABIDecoder:
|
||||||
|
def __init__(self, abi: ABI):
|
||||||
|
self._functions_by_selector: Dict[str, ABIFunctionDescription] = {
|
||||||
|
description.get_selector(): description
|
||||||
|
for description in abi
|
||||||
|
if isinstance(description, ABIFunctionDescription)
|
||||||
|
}
|
||||||
|
|
||||||
|
def decode(self, data: str) -> Optional[CallData]:
|
||||||
|
hex_data = HexBytes(data)
|
||||||
|
selector, params = hex_data[:4], hex_data[4:]
|
||||||
|
|
||||||
|
func = self._functions_by_selector.get(selector)
|
||||||
|
|
||||||
|
if func is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
names = [input.name for input in func.inputs]
|
||||||
|
types = [input.type for input in func.inputs]
|
||||||
|
|
||||||
|
decoded = decode_abi(types, params)
|
||||||
|
|
||||||
|
return CallData(
|
||||||
|
function_name=func.name,
|
||||||
|
function_signature=func.get_signature(),
|
||||||
|
inputs={name: value for name, value in zip(names, decoded)},
|
||||||
|
)
|
@ -24,6 +24,7 @@ NON_FUNCTION_DESCRIPTION_TYPES = Union[
|
|||||||
|
|
||||||
|
|
||||||
class ABIDescriptionInput(BaseModel):
|
class ABIDescriptionInput(BaseModel):
|
||||||
|
name: str
|
||||||
type: str
|
type: str
|
||||||
|
|
||||||
|
|
||||||
|
9
mev_inspect/schemas/call_data.py
Normal file
9
mev_inspect/schemas/call_data.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class CallData(BaseModel):
|
||||||
|
function_name: str
|
||||||
|
function_signature: str
|
||||||
|
inputs: Dict[str, Any]
|
Loading…
x
Reference in New Issue
Block a user