def is_encodable(typ: TypeStr, arg: Any) -> bool: """ Determines if the python value ``arg`` is encodable as a value of the ABI type ``typ``. :param typ: A string representation for the ABI type against which the python value ``arg`` will be checked e.g. ``'uint256'``, ``'bytes[]'``, ``'(int,int)'``, etc. :param arg: The python value whose encodability should be checked. :returns: ``True`` if ``arg`` is encodable as a value of the ABI type ``typ``. Otherwise, ``False``. """ encoder = registry.get_encoder(typ) try: encoder.validate_value(arg) except EncodingError: return False except AttributeError: try: encoder(arg) except EncodingError: return False return True
def encode_single(typ, arg): if isinstance(typ, str): type_str = typ else: type_str = collapse_type(*typ) encoder = registry.get_encoder(type_str) return encoder(arg)
def encode_single(typ: TypeStr, arg: Any) -> bytes: """ Encodes the python value ``arg`` as a binary value of the ABI type ``typ``. :param typ: The string representation of the ABI type that will be used for encoding e.g. ``'uint256'``, ``'bytes[]'``, ``'(int,int)'``, etc. :param arg: The python value to be encoded. :returns: The binary representation of the python value ``arg`` as a value of the ABI type ``typ``. """ encoder = registry.get_encoder(typ) return encoder(arg)
def encode_abi(types: Iterable[TypeStr], args: Iterable[Any]) -> bytes: """ Encodes the python values in ``args`` as a sequence of binary values of the ABI types in ``types`` via the head-tail mechanism. :param types: An iterable of string representations of the ABI types that will be used for encoding e.g. ``('uint256', 'bytes[]', '(int,int)')`` :param args: An iterable of python values to be encoded. :returns: The head-tail encoded binary representation of the python values in ``args`` as values of the ABI types in ``types``. """ encoders = [registry.get_encoder(type_str) for type_str in types] encoder = TupleEncoder(encoders=encoders) return encoder(args)
def encode_single_packed(_type, value): import codecs from eth_abi import ( grammar as abi_type_parser, ) from eth_abi.registry import has_arrlist, registry abi_type = abi_type_parser.parse(_type) if has_arrlist(_type): item_encoder = registry.get_encoder(abi_type.item_type.to_type_str()) if abi_type.arrlist[-1] != 1: return DynamicArrayPackedEncoder(item_encoder=item_encoder).encode(value) else: raise NotImplementedError( "Fixed arrays are not implemented in this packed encoder prototype") elif abi_type.base == "string": return codecs.encode(value, 'utf8') elif abi_type.base == "bytes": return value
def is_encodable(typ, arg): """ Determines if the given python value ``arg`` can be encoded as a value of abi type ``typ``. """ if isinstance(typ, str): type_str = typ else: type_str = collapse_type(*typ) encoder = registry.get_encoder(type_str) try: encoder.validate_value(arg) except EncodingError: return False else: return True
def test_default_registry_gives_correct_basic_coders(type_str, encoder_class, decoder_class): assert isinstance(default_registry.get_encoder(type_str), encoder_class) assert isinstance(default_registry.get_decoder(type_str), decoder_class)
def encode_abi(types, args): encoders = [registry.get_encoder(type_str) for type_str in types] encoder = TupleEncoder(encoders=encoders) return encoder(args)