Run precommit

This commit is contained in:
Leo Arias 2021-10-11 17:51:38 +00:00
parent a67769cea3
commit ba73f58396
3 changed files with 40 additions and 26 deletions

View File

@ -1,6 +1,7 @@
import eth_utils.abi
from typing import Dict, Optional from typing import Dict, Optional
import eth_utils.abi
from hexbytes import HexBytes from hexbytes import HexBytes
from eth_abi import decode_abi from eth_abi import decode_abi
from eth_abi.exceptions import InsufficientDataBytes, NonEmptyPaddingBytes from eth_abi.exceptions import InsufficientDataBytes, NonEmptyPaddingBytes
@ -28,7 +29,8 @@ class ABIDecoder:
names = [input.name for input in func.inputs] names = [input.name for input in func.inputs]
types = [ types = [
input.type if input.type != 'tuple' input.type
if input.type != "tuple"
else eth_utils.abi.collapse_if_tuple(input.dict()) else eth_utils.abi.collapse_if_tuple(input.dict())
for input in func.inputs for input in func.inputs
] ]

View File

@ -48,9 +48,11 @@ class ABIFunctionDescription(BaseModel):
def get_signature(self) -> str: def get_signature(self) -> str:
joined_input_types = ",".join( joined_input_types = ",".join(
input.type if input.type != "tuple" input.type
if input.type != "tuple"
else eth_utils.abi.collapse_if_tuple(input.dict()) else eth_utils.abi.collapse_if_tuple(input.dict())
for input in self.inputs) for input in self.inputs
)
return f"{self.name}({joined_input_types})" return f"{self.name}({joined_input_types})"

View File

@ -7,14 +7,16 @@ from mev_inspect.schemas import abi
def test_decode_function_with_simple_argument(): def test_decode_function_with_simple_argument():
test_function_name = "testFunction" test_function_name = "testFunction"
test_parameter_name = "testParameter" test_parameter_name = "testParameter"
test_abi = pydantic.parse_obj_as(abi.ABI, [{ test_abi = pydantic.parse_obj_as(
abi.ABI,
[
{
"name": test_function_name, "name": test_function_name,
"type": "function", "type": "function",
"inputs": [{ "inputs": [{"name": test_parameter_name, "type": "uint256"}],
"name": test_parameter_name, }
"type": "uint256" ],
}] )
}])
# 4byte signature of the test function. # 4byte signature of the test function.
# https://www.4byte.directory/signatures/?bytes4_signature=0x350c530b # https://www.4byte.directory/signatures/?bytes4_signature=0x350c530b
test_function_selector = "350c530b" test_function_selector = "350c530b"
@ -23,7 +25,8 @@ def test_decode_function_with_simple_argument():
) )
abi_decoder = decode.ABIDecoder(test_abi) abi_decoder = decode.ABIDecoder(test_abi)
call_data = abi_decoder.decode( call_data = abi_decoder.decode(
"0x" + test_function_selector + test_function_argument) "0x" + test_function_selector + test_function_argument
)
assert call_data.function_name == test_function_name assert call_data.function_name == test_function_name
assert call_data.function_signature == "testFunction(uint256)" assert call_data.function_signature == "testFunction(uint256)"
assert call_data.inputs == {test_parameter_name: 1} assert call_data.inputs == {test_parameter_name: 1}
@ -33,18 +36,24 @@ def test_decode_function_with_tuple_argument():
test_function_name = "testFunction" test_function_name = "testFunction"
test_tuple_name = "testTuple" test_tuple_name = "testTuple"
test_parameter_name = "testParameter" test_parameter_name = "testParameter"
test_abi = pydantic.parse_obj_as(abi.ABI, [{ test_abi = pydantic.parse_obj_as(
abi.ABI,
[
{
"name": test_function_name, "name": test_function_name,
"type": "function", "type": "function",
"inputs": [{ "inputs": [
{
"name": test_tuple_name, "name": test_tuple_name,
"type": "tuple", "type": "tuple",
"components": [{ "components": [
"name": test_parameter_name, {"name": test_parameter_name, "type": "uint256"}
"type": "uint256" ],
}] }
}] ],
}]) }
],
)
# 4byte signature of the test function. # 4byte signature of the test function.
# https://www.4byte.directory/signatures/?bytes4_signature=0x98568079 # https://www.4byte.directory/signatures/?bytes4_signature=0x98568079
test_function_selector = "98568079" test_function_selector = "98568079"
@ -53,7 +62,8 @@ def test_decode_function_with_tuple_argument():
) )
abi_decoder = decode.ABIDecoder(test_abi) abi_decoder = decode.ABIDecoder(test_abi)
call_data = abi_decoder.decode( call_data = abi_decoder.decode(
"0x" + test_function_selector + test_function_argument) "0x" + test_function_selector + test_function_argument
)
assert call_data.function_name == test_function_name assert call_data.function_name == test_function_name
assert call_data.function_signature == "testFunction((uint256))" assert call_data.function_signature == "testFunction((uint256))"
assert call_data.inputs == {test_tuple_name: (1,)} assert call_data.inputs == {test_tuple_name: (1,)}