def test_base_types_as_arrays(type_str): primitive = get_primitive_types()[type_str] type_definition = get_type_from_abi({"type": f"{type_str}[3]"}) assert isinstance(type_definition, ArrayDefinition) assert type_definition.length == 3 assert isinstance(type_definition.value_type, primitive._type)
def from_abi(cls, abi: Dict) -> "ContractFunction": """ Generate a `ContractFunction` object from an ABI interface. Arguments --------- abi : dict An object from a JSON ABI interface, representing a function. Returns ------- ContractFunction object. """ arguments = OrderedDict() for item in abi["inputs"]: arguments[item["name"]] = get_type_from_abi( item, location=DataLocation.CALLDATA, is_immutable=True ) return_type = None if len(abi["outputs"]) == 1: return_type = get_type_from_abi( abi["outputs"][0], location=DataLocation.CALLDATA, is_immutable=True ) elif len(abi["outputs"]) > 1: return_type = TupleDefinition( tuple( get_type_from_abi(i, location=DataLocation.CALLDATA, is_immutable=True) for i in abi["outputs"] ) ) return cls( abi["name"], arguments, len(arguments), len(arguments), return_type, function_visibility=FunctionVisibility.EXTERNAL, state_mutability=StateMutability.from_abi(abi), )
def from_abi(cls, abi: Dict) -> "Event": """ Generate an `Event` object from an ABI interface. Arguments --------- abi : dict An object from a JSON ABI interface, representing an event. Returns ------- Event object. """ members: OrderedDict = OrderedDict() indexed: List = [i["indexed"] for i in abi["inputs"]] for item in abi["inputs"]: members[item["name"]] = get_type_from_abi(item) return Event(abi["name"], members, indexed)
def test_invalid_index(idx): with pytest.raises(UnknownType): get_type_from_abi({"type": f"int128[{idx}]"})
def test_base_types(type_str): primitive = get_primitive_types()[type_str] type_definition = get_type_from_abi({"type": type_str}) assert isinstance(type_definition, primitive._type)