abi.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. Types for Contract ABIs and related components.
  3. """
  4. from typing import (
  5. Any,
  6. Literal,
  7. Sequence,
  8. Tuple,
  9. TypedDict,
  10. Union,
  11. )
  12. from typing_extensions import (
  13. NotRequired,
  14. )
  15. from eth_typing.encoding import (
  16. HexStr,
  17. )
  18. TypeStr = str
  19. """String representation of a data type."""
  20. Decodable = Union[bytes, bytearray]
  21. """Binary data to be decoded."""
  22. class ABIComponent(TypedDict):
  23. """
  24. TypedDict representing an `ABIElement` component.
  25. """
  26. type: str
  27. """Type of the component."""
  28. name: NotRequired[str]
  29. """Name of the component."""
  30. components: NotRequired[Sequence["ABIComponent"]]
  31. """List of nested `ABI` components for ABI types."""
  32. class ABIComponentIndexed(ABIComponent):
  33. """
  34. TypedDict representing an indexed `ABIElement` component.
  35. """
  36. indexed: bool
  37. """If True, component can be used as a topic filter."""
  38. class ABIEvent(TypedDict):
  39. """
  40. TypedDict to represent the `ABI` for an event.
  41. """
  42. name: str
  43. """Event name identifier."""
  44. type: Literal["event"]
  45. """Event ABI type."""
  46. anonymous: NotRequired[bool]
  47. """If True, event is anonymous. Cannot filter the event by name."""
  48. inputs: NotRequired[Sequence["ABIComponentIndexed"]]
  49. """Input components for the event."""
  50. class ABIFunctionType(TypedDict, total=False):
  51. """
  52. TypedDict representing the `ABI` for all function types.
  53. This is the base type for functions.
  54. Please use ABIFunction, ABIConstructor, ABIFallback or ABIReceive instead.
  55. """
  56. stateMutability: Literal["pure", "view", "nonpayable", "payable"]
  57. """State mutability of the constructor."""
  58. payable: bool
  59. """
  60. Contract is payable to receive ether on deployment.
  61. Deprecated in favor of stateMutability payable and nonpayable.
  62. """
  63. constant: bool
  64. """
  65. Function is constant and does not change state.
  66. Deprecated in favor of stateMutability pure and view.
  67. """
  68. class ABIFunction(ABIFunctionType):
  69. """
  70. TypedDict representing the `ABI` for a function.
  71. """
  72. type: Literal["function"]
  73. """Type of the function."""
  74. name: str
  75. """Name of the function."""
  76. inputs: NotRequired[Sequence["ABIComponent"]]
  77. """Function input components."""
  78. outputs: NotRequired[Sequence["ABIComponent"]]
  79. """Function output components."""
  80. class ABIConstructor(ABIFunctionType):
  81. """
  82. TypedDict representing the `ABI` for a constructor function.
  83. """
  84. type: Literal["constructor"]
  85. """Type of the constructor function."""
  86. inputs: NotRequired[Sequence["ABIComponent"]]
  87. """Function input components."""
  88. class ABIFallback(ABIFunctionType):
  89. """
  90. TypedDict representing the `ABI` for a fallback function.
  91. """
  92. type: Literal["fallback"]
  93. """Type of the fallback function."""
  94. class ABIReceive(ABIFunctionType):
  95. """
  96. TypedDict representing the `ABI` for a receive function.
  97. """
  98. type: Literal["receive"]
  99. """Type of the receive function."""
  100. class ABIError(TypedDict):
  101. """
  102. TypedDict representing the `ABI` for an error.
  103. """
  104. type: Literal["error"]
  105. """Type of the error."""
  106. name: str
  107. """Name of the error."""
  108. inputs: NotRequired[Sequence["ABIComponent"]]
  109. """Error input components."""
  110. ABICallable = Union[ABIFunction, ABIConstructor, ABIFallback, ABIReceive]
  111. """
  112. A `Union` type consisting of `ABIFunction`, `ABIConstructor`, `ABIFallback` and
  113. `ABIReceive` types.
  114. """
  115. ABIElement = Union[ABICallable, ABIEvent, ABIError]
  116. """A `Union` type consisting of `ABICallable`, `ABIEvent`, and `ABIError` types."""
  117. class ABIElementInfo(TypedDict):
  118. """
  119. TypedDict to represent properties of an `ABIElement`, including the abi,
  120. selector and arguments.
  121. """
  122. abi: ABIElement
  123. """ABI for any `ABIElement` type."""
  124. selector: HexStr
  125. """Solidity `ABIElement` selector sighash."""
  126. arguments: Tuple[Any, ...]
  127. """`ABIElement` input components."""
  128. ABI = Sequence[ABIElement]
  129. """
  130. List of components representing function and event interfaces
  131. (elements of an ABI).
  132. """