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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
from collections import (
abc,
)
import copy
import itertools
import re
from typing import (
Any,
Dict,
Iterable,
List,
Literal,
Mapping,
Optional,
Sequence,
Tuple,
Union,
cast,
overload,
)
from eth_typing import (
ABI,
ABIComponent,
ABIConstructor,
ABIElement,
ABIError,
ABIEvent,
ABIFallback,
ABIFunction,
ABIReceive,
)
from eth_utils.types import (
is_list_like,
)
from .crypto import (
keccak,
)
def _align_abi_input(
arg_abi: ABIComponent, normalized_arg: Any
) -> Union[Any, Tuple[Any, ...]]:
"""
Aligns the values of any mapping at any level of nesting in ``normalized_arg``
according to the layout of the corresponding abi spec.
"""
tuple_parts = _get_tuple_type_str_and_dims(arg_abi.get("type", ""))
if tuple_parts is None:
# normalized_arg is non-tuple. Just return value.
return normalized_arg
tuple_prefix, tuple_dims = tuple_parts
if tuple_dims is None:
# normalized_arg is non-list tuple. Each sub arg in `normalized_arg` will be
# aligned according to its corresponding abi.
sub_abis = cast(Iterable[ABIComponent], arg_abi.get("components", []))
else:
num_dims = tuple_dims.count("[")
# normalized_arg is list tuple. A non-list version of its abi will be used to
# align each element in `normalized_arg`.
new_abi = copy.copy(arg_abi)
new_abi["type"] = tuple_prefix + "[]" * (num_dims - 1)
sub_abis = itertools.repeat(new_abi)
if isinstance(normalized_arg, abc.Mapping):
# normalized_arg is mapping. Align values according to abi order.
aligned_arg = tuple(normalized_arg[abi["name"]] for abi in sub_abis)
else:
aligned_arg = normalized_arg
if not is_list_like(aligned_arg):
raise TypeError(
f'Expected non-string sequence for "{arg_abi.get("type")}" '
f"component type: got {aligned_arg}"
)
# convert NamedTuple to regular tuple
typing = tuple if isinstance(aligned_arg, tuple) else type(aligned_arg)
return typing(
_align_abi_input(sub_abi, sub_arg)
for sub_abi, sub_arg in zip(sub_abis, aligned_arg)
)
def _get_tuple_type_str_and_dims(s: str) -> Optional[Tuple[str, Optional[str]]]:
"""
Takes a JSON ABI type string. For tuple type strings, returns the separated
prefix and array dimension parts. For all other strings, returns ``None``.
"""
tuple_type_str_re = "^(tuple)((\\[([1-9]\\d*\b)?])*)??$"
match = re.compile(tuple_type_str_re).match(s)
if match is not None:
tuple_prefix = match.group(1)
tuple_dims = match.group(2)
return tuple_prefix, tuple_dims
return None
def _raise_if_not_function_abi(abi_element: ABIElement) -> None:
if abi_element["type"] != "function":
raise ValueError(
f"Outputs only supported for ABI type `function`. Provided"
f" ABI type was `{abi_element.get('type')}` and outputs were "
f"`{abi_element.get('outputs')}`."
)
def _raise_if_fallback_or_receive_abi(abi_element: ABIElement) -> None:
if abi_element["type"] == "fallback" or abi_element["type"] == "receive":
raise ValueError(
f"Inputs not supported for function types `fallback` or `receive`. Provided"
f" ABI type was `{abi_element.get('type')}` with inputs "
f"`{abi_element.get('inputs')}`."
)
def collapse_if_tuple(abi: Union[ABIComponent, Dict[str, Any], str]) -> str:
"""
Extract argument types from a function or event ABI parameter.
With tuple argument types, return a Tuple of each type.
Returns the param if `abi` is an instance of str or another non-tuple
type.
:param abi: A Function or Event ABI component or a string with type info.
:type abi: `Union[ABIComponent, Dict[str, Any], str]`
:return: Type(s) for the function or event ABI param.
:rtype: `str`
.. doctest::
>>> from eth_utils.abi import collapse_if_tuple
>>> abi = {
... 'components': [
... {'name': 'anAddress', 'type': 'address'},
... {'name': 'anInt', 'type': 'uint256'},
... {'name': 'someBytes', 'type': 'bytes'},
... ],
... 'type': 'tuple',
... }
>>> collapse_if_tuple(abi)
'(address,uint256,bytes)'
"""
if isinstance(abi, str):
return abi
element_type = abi.get("type")
if not isinstance(element_type, str):
raise TypeError(
f"The 'type' must be a string, but got {repr(element_type)} of type "
f"{type(element_type)}"
)
elif not element_type.startswith("tuple"):
return element_type
delimited = ",".join(collapse_if_tuple(c) for c in abi["components"])
# Whatever comes after "tuple" is the array dims. The ABI spec states that
# this will have the form "", "[]", or "[k]".
array_dim = element_type[5:]
collapsed = f"({delimited}){array_dim}"
return collapsed
def abi_to_signature(abi_element: ABIElement) -> str:
"""
Returns a string signature representation of the function or event ABI
and arguments.
Signatures consist of the name followed by a list of arguments.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:return: Stringified ABI signature
:rtype: `str`
.. doctest::
>>> from eth_utils import abi_to_signature
>>> abi_element = {
... 'constant': False,
... 'inputs': [
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'name': 'f',
... 'outputs': [],
... 'payable': False,
... 'stateMutability': 'nonpayable',
... 'type': 'function'
... }
>>> abi_to_signature(abi_element)
'f(uint256)'
"""
signature = "{name}({input_types})"
abi_type = str(abi_element.get("type", ""))
if abi_type == "fallback" or abi_type == "receive":
return signature.format(name=abi_type, input_types="")
if abi_type == "constructor":
fn_name = abi_type
else:
fn_name = str(abi_element.get("name", abi_type))
return signature.format(
name=fn_name, input_types=",".join(get_abi_input_types(abi_element))
)
def filter_abi_by_name(abi_name: str, contract_abi: ABI) -> Sequence[ABIElement]:
"""
Get one or more function and event ABIs by name.
:param abi_name: Name of the function, event or error.
:type abi_name: `str`
:param contract_abi: Contract ABI.
:type contract_abi: `ABI`
:return: Function or event ABIs with matching name.
:rtype: `Sequence[ABIElement]`
.. doctest::
>>> from eth_utils.abi import filter_abi_by_name
>>> abi = [
... {
... "constant": False,
... "inputs": [],
... "name": "func_1",
... "outputs": [],
... "type": "function",
... },
... {
... "constant": False,
... "inputs": [
... {"name": "a", "type": "uint256"},
... ],
... "name": "func_2",
... "outputs": [],
... "type": "function",
... },
... {
... "constant": False,
... "inputs": [
... {"name": "a", "type": "uint256"},
... {"name": "b", "type": "uint256"},
... ],
... "name": "func_3",
... "outputs": [],
... "type": "function",
... },
... {
... "constant": False,
... "inputs": [
... {"name": "a", "type": "uint256"},
... {"name": "b", "type": "uint256"},
... {"name": "c", "type": "uint256"},
... ],
... "name": "func_4",
... "outputs": [],
... "type": "function",
... },
... ]
>>> filter_abi_by_name("func_1", abi)
[{'constant': False, 'inputs': [], 'name': 'func_1', 'outputs': [], \
'type': 'function'}]
"""
return [
abi
for abi in contract_abi
if (
(
abi["type"] == "function"
or abi["type"] == "event"
or abi["type"] == "error"
)
and abi["name"] == abi_name
)
]
@overload
def filter_abi_by_type(
abi_type: Literal["function"],
contract_abi: ABI,
) -> Sequence[ABIFunction]:
pass
@overload
def filter_abi_by_type(
abi_type: Literal["constructor"],
contract_abi: ABI,
) -> Sequence[ABIConstructor]:
pass
@overload
def filter_abi_by_type(
abi_type: Literal["fallback"],
contract_abi: ABI,
) -> Sequence[ABIFallback]:
pass
@overload
def filter_abi_by_type(
abi_type: Literal["receive"],
contract_abi: ABI,
) -> Sequence[ABIReceive]:
pass
@overload
def filter_abi_by_type(
abi_type: Literal["event"],
contract_abi: ABI,
) -> Sequence[ABIEvent]:
pass
@overload
def filter_abi_by_type(
abi_type: Literal["error"],
contract_abi: ABI,
) -> Sequence[ABIError]:
pass
def filter_abi_by_type(
abi_type: Literal[
"function", "constructor", "fallback", "receive", "event", "error"
],
contract_abi: ABI,
) -> Sequence[
Union[ABIFunction, ABIConstructor, ABIFallback, ABIReceive, ABIEvent, ABIError]
]:
"""
Return a list of each ``ABIElement`` that is of type ``abi_type``.
For mypy, function overloads ensures the correct type is returned based on the
``abi_type``. For example, if ``abi_type`` is "function", the return type will be
``Sequence[ABIFunction]``.
:param abi_type: Type of ABI element to filter by.
:type abi_type: `str`
:param contract_abi: Contract ABI.
:type contract_abi: `ABI`
:return: List of ABI elements of the specified type.
:rtype: `Sequence[Union[ABIFunction, ABIConstructor, ABIFallback, ABIReceive, \
ABIEvent, ABIError]]`
.. doctest::
>>> from eth_utils import filter_abi_by_type
>>> abi = [
... {"type": "function", "name": "myFunction", "inputs": [], "outputs": []},
... {"type": "function", "name": "myFunction2", "inputs": [], "outputs": []},
... {"type": "event", "name": "MyEvent", "inputs": []}
... ]
>>> filter_abi_by_type("function", abi)
[{'type': 'function', 'name': 'myFunction', 'inputs': [], 'outputs': []}, \
{'type': 'function', 'name': 'myFunction2', 'inputs': [], 'outputs': []}]
"""
if abi_type == Literal["function"] or abi_type == "function":
return [abi for abi in contract_abi if abi["type"] == "function"]
elif abi_type == Literal["constructor"] or abi_type == "constructor":
return [abi for abi in contract_abi if abi["type"] == "constructor"]
elif abi_type == Literal["fallback"] or abi_type == "fallback":
return [abi for abi in contract_abi if abi["type"] == "fallback"]
elif abi_type == Literal["receive"] or abi_type == "receive":
return [abi for abi in contract_abi if abi["type"] == "receive"]
elif abi_type == Literal["event"] or abi_type == "event":
return [abi for abi in contract_abi if abi["type"] == "event"]
elif abi_type == Literal["error"] or abi_type == "error":
return [abi for abi in contract_abi if abi["type"] == "error"]
else:
raise ValueError(f"Unsupported ABI type: {abi_type}")
def get_all_function_abis(contract_abi: ABI) -> Sequence[ABIFunction]:
"""
Return interfaces for each function in the contract ABI.
:param contract_abi: Contract ABI.
:type contract_abi: `ABI`
:return: List of ABIs for each function interface.
:rtype: `Sequence[ABIFunction]`
.. doctest::
>>> from eth_utils import get_all_function_abis
>>> contract_abi = [
... {"type": "function", "name": "myFunction", "inputs": [], "outputs": []},
... {"type": "function", "name": "myFunction2", "inputs": [], "outputs": []},
... {"type": "event", "name": "MyEvent", "inputs": []}
... ]
>>> get_all_function_abis(contract_abi)
[{'type': 'function', 'name': 'myFunction', 'inputs': [], 'outputs': []}, \
{'type': 'function', 'name': 'myFunction2', 'inputs': [], 'outputs': []}]
"""
return filter_abi_by_type("function", contract_abi)
def get_all_event_abis(contract_abi: ABI) -> Sequence[ABIEvent]:
"""
Return interfaces for each event in the contract ABI.
:param contract_abi: Contract ABI.
:type contract_abi: `ABI`
:return: List of ABIs for each event interface.
:rtype: `Sequence[ABIEvent]`
.. doctest::
>>> from eth_utils import get_all_event_abis
>>> contract_abi = [
... {"type": "function", "name": "myFunction", "inputs": [], "outputs": []},
... {"type": "function", "name": "myFunction2", "inputs": [], "outputs": []},
... {"type": "event", "name": "MyEvent", "inputs": []}
... ]
>>> get_all_event_abis(contract_abi)
[{'type': 'event', 'name': 'MyEvent', 'inputs': []}]
"""
return filter_abi_by_type("event", contract_abi)
def get_normalized_abi_inputs(
abi_element: ABIElement,
*args: Optional[Sequence[Any]],
**kwargs: Optional[Dict[str, Any]],
) -> Tuple[Any, ...]:
r"""
Flattens positional args (``args``) and keyword args (``kwargs``) into a Tuple and
uses the ``abi_element`` for validation.
Checks to ensure that the correct number of args were given, no duplicate args were
given, and no unknown args were given. Returns a list of argument values aligned
to the order of inputs defined in ``abi_element``.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:param args: Positional arguments for the function.
:type args: `Optional[Sequence[Any]]`
:param kwargs: Keyword arguments for the function.
:type kwargs: `Optional[Dict[str, Any]]`
:return: Arguments list.
:rtype: `Tuple[Any, ...]`
.. doctest::
>>> from eth_utils import get_normalized_abi_inputs
>>> abi = {
... 'constant': False,
... 'inputs': [
... {
... 'name': 'name',
... 'type': 'string'
... },
... {
... 'name': 's',
... 'type': 'uint256'
... },
... {
... 'name': 't',
... 'components': [
... {'name': 'anAddress', 'type': 'address'},
... {'name': 'anInt', 'type': 'uint256'},
... {'name': 'someBytes', 'type': 'bytes'},
... ],
... 'type': 'tuple'
... }
... ],
... 'name': 'f',
... 'outputs': [],
... 'payable': False,
... 'stateMutability': 'nonpayable',
... 'type': 'function'
... }
>>> get_normalized_abi_inputs(
... abi, *('myName', 123), **{'t': ('0x1', 1, b'\x01')}
... )
('myName', 123, ('0x1', 1, b'\x01'))
"""
_raise_if_fallback_or_receive_abi(abi_element)
function_inputs = cast(Sequence[ABIComponent], abi_element.get("inputs", []))
if len(args) + len(kwargs) != len(function_inputs):
raise TypeError(
f"Incorrect argument count. Expected '{len(function_inputs)}'"
f", got '{len(args) + len(kwargs)}'."
)
# If no keyword args were given, we don't need to align them
if not kwargs:
return cast(Tuple[Any, ...], args)
kwarg_names = set(kwargs.keys())
sorted_arg_names = tuple(arg_abi["name"] for arg_abi in function_inputs)
args_as_kwargs = dict(zip(sorted_arg_names, args))
# Check for duplicate args
duplicate_args = kwarg_names.intersection(args_as_kwargs.keys())
if duplicate_args:
raise TypeError(
f"{abi_element.get('name')}() got multiple values for argument(s) "
f"'{', '.join(duplicate_args)}'."
)
# Check for unknown args
# Arg names sorted to raise consistent error messages
unknown_args = tuple(sorted(kwarg_names.difference(sorted_arg_names)))
if unknown_args:
message = "{} got unexpected keyword argument(s) '{}'."
if abi_element.get("name"):
raise TypeError(
message.format(f"{abi_element.get('name')}()", ", ".join(unknown_args))
)
raise TypeError(
message.format(
f"Type: '{abi_element.get('type')}'", ", ".join(unknown_args)
)
)
# Sort args according to their position in the ABI and unzip them from their
# names
sorted_args = tuple(
zip(
*sorted(
itertools.chain(kwargs.items(), args_as_kwargs.items()),
key=lambda kv: sorted_arg_names.index(kv[0]),
)
)
)
if len(sorted_args) > 0:
return tuple(sorted_args[1])
else:
return tuple()
def get_aligned_abi_inputs(
abi_element: ABIElement,
normalized_args: Union[Tuple[Any, ...], Mapping[Any, Any]],
) -> Tuple[Tuple[str, ...], Tuple[Any, ...]]:
"""
Returns a pair of nested Tuples containing a list of types and a list of input
values sorted by the order specified by the ``abi``.
``normalized_args`` can be obtained by using
:py:meth:`eth_utils.abi.get_normalized_abi_inputs`, which returns nested mappings
or sequences corresponding to tuple-encoded values in ``abi``.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:param normalized_args: Normalized arguments for the function.
:type normalized_args: `Union[Tuple[Any, ...], Mapping[Any, Any]]`
:return: Tuple of types and aligned arguments.
:rtype: `Tuple[Tuple[str, ...], Tuple[Any, ...]]`
.. doctest::
>>> from eth_utils import get_aligned_abi_inputs
>>> abi = {
... 'constant': False,
... 'inputs': [
... {
... 'name': 'name',
... 'type': 'string'
... },
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'name': 'f',
... 'outputs': [],
... 'payable': False,
... 'stateMutability': 'nonpayable',
... 'type': 'function'
... }
>>> get_aligned_abi_inputs(abi, ('myName', 123))
(('string', 'uint256'), ('myName', 123))
"""
_raise_if_fallback_or_receive_abi(abi_element)
abi_element_inputs = cast(Sequence[ABIComponent], abi_element.get("inputs", []))
if isinstance(normalized_args, abc.Mapping):
# `args` is mapping. Align values according to abi order.
normalized_args = tuple(
normalized_args[abi["name"]] for abi in abi_element_inputs
)
return (
tuple(collapse_if_tuple(abi) for abi in abi_element_inputs),
type(normalized_args)(
_align_abi_input(abi, arg)
for abi, arg in zip(abi_element_inputs, normalized_args)
),
)
def get_abi_input_names(abi_element: ABIElement) -> List[Optional[str]]:
"""
Return names for each input from the function or event ABI.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:return: Names for each input in the function or event ABI.
:rtype: `List[Optional[str]]`
.. doctest::
>>> from eth_utils import get_abi_input_names
>>> abi = {
... 'constant': False,
... 'inputs': [
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'name': 'f',
... 'outputs': [],
... 'payable': False,
... 'stateMutability': 'nonpayable',
... 'type': 'function'
... }
>>> get_abi_input_names(abi)
['s']
"""
_raise_if_fallback_or_receive_abi(abi_element)
return [
arg.get("name", None)
for arg in cast(Sequence[ABIComponent], abi_element.get("inputs", []))
]
def get_abi_input_types(abi_element: ABIElement) -> List[str]:
"""
Return types for each input from the function or event ABI.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:return: Types for each input in the function or event ABI.
:rtype: `List[str]`
.. doctest::
>>> from eth_utils import get_abi_input_types
>>> abi = {
... 'constant': False,
... 'inputs': [
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'name': 'f',
... 'outputs': [],
... 'payable': False,
... 'stateMutability': 'nonpayable',
... 'type': 'function'
... }
>>> get_abi_input_types(abi)
['uint256']
"""
_raise_if_fallback_or_receive_abi(abi_element)
return [
collapse_if_tuple(arg)
for arg in cast(Sequence[ABIComponent], abi_element.get("inputs", []))
]
def get_abi_output_names(abi_element: ABIElement) -> List[Optional[str]]:
"""
Return names for each output from the ABI element.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:return: Names for each function output in the function ABI.
:rtype: `List[Optional[str]]`
.. doctest::
>>> from eth_utils import get_abi_output_names
>>> abi = {
... 'constant': False,
... 'inputs': [
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'name': 'f',
... 'outputs': [
... {
... 'name': 'name',
... 'type': 'string'
... },
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'payable': False,
... 'stateMutability': 'nonpayable',
... 'type': 'function'
... }
>>> get_abi_output_names(abi)
['name', 's']
"""
_raise_if_not_function_abi(abi_element)
return [
arg.get("name", None)
for arg in cast(Sequence[ABIComponent], abi_element.get("outputs", []))
]
def get_abi_output_types(abi_element: ABIElement) -> List[str]:
"""
Return types for each output from the function ABI.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:return: Types for each function output in the function ABI.
:rtype: `List[str]`
.. doctest::
>>> from eth_utils import get_abi_output_types
>>> abi = {
... 'constant': False,
... 'inputs': [
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'name': 'f',
... 'outputs': [
... {
... 'name': 'name',
... 'type': 'string'
... },
... {
... 'name': 's',
... 'type': 'uint256'
... }
... ],
... 'payable': False,
... 'stateMutability': 'nonpayable',
... 'type': 'function'
... }
>>> get_abi_output_types(abi)
['string', 'uint256']
"""
_raise_if_not_function_abi(abi_element)
return [
collapse_if_tuple(arg)
for arg in cast(Sequence[ABIComponent], abi_element.get("outputs", []))
]
def function_signature_to_4byte_selector(function_signature: str) -> bytes:
r"""
Return the 4-byte function selector from a function signature string.
:param function_signature: String representation of the function name and arguments.
:type function_signature: `str`
:return: 4-byte function selector.
:rtype: `bytes`
.. doctest::
>>> from eth_utils import function_signature_to_4byte_selector
>>> function_signature_to_4byte_selector('myFunction()')
b'\xc3x\n:'
"""
return keccak(text=function_signature.replace(" ", ""))[:4]
def function_abi_to_4byte_selector(abi_element: ABIElement) -> bytes:
r"""
Return the 4-byte function signature of the provided function ABI.
:param abi_element: ABI element.
:type abi_element: `ABIElement`
:return: 4-byte function signature.
:rtype: `bytes`
.. doctest::
>>> from eth_utils import function_abi_to_4byte_selector
>>> abi_element = {
... 'type': 'function',
... 'name': 'myFunction',
... 'inputs': [],
... 'outputs': []
... }
>>> function_abi_to_4byte_selector(abi_element)
b'\xc3x\n:'
"""
function_signature = abi_to_signature(abi_element)
return function_signature_to_4byte_selector(function_signature)
def event_signature_to_log_topic(event_signature: str) -> bytes:
r"""
Return the 32-byte keccak signature of the log topic for an event signature.
:param event_signature: String representation of the event name and arguments.
:type event_signature: `str`
:return: Log topic bytes.
:rtype: `bytes`
.. doctest::
>>> from eth_utils import event_signature_to_log_topic
>>> event_signature_to_log_topic('MyEvent()')
b'M\xbf\xb6\x8bC\xdd\xdf\xa1+Q\xeb\xe9\x9a\xb8\xfd\xedb\x0f\x9a\n\xc21B\x87\x9aO\x19*\x1byR\xd2'
"""
return keccak(text=event_signature.replace(" ", ""))
def event_abi_to_log_topic(event_abi: ABIEvent) -> bytes:
r"""
Return the 32-byte keccak signature of the log topic from an event ABI.
:param event_abi: Event ABI.
:type event_abi: `ABIEvent`
:return: Log topic bytes.
:rtype: `bytes`
.. doctest::
>>> from eth_utils import event_abi_to_log_topic
>>> abi = {
... 'type': 'event',
... 'anonymous': False,
... 'name': 'MyEvent',
... 'inputs': []
... }
>>> event_abi_to_log_topic(abi)
b'M\xbf\xb6\x8bC\xdd\xdf\xa1+Q\xeb\xe9\x9a\xb8\xfd\xedb\x0f\x9a\n\xc21B\x87\x9aO\x19*\x1byR\xd2'
"""
event_signature = abi_to_signature(event_abi)
return event_signature_to_log_topic(event_signature)
|