Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
abi-gen/Py: name tuples w/internalType, not hash
Browse files Browse the repository at this point in the history
Use the new `internalType` field on the `DataItem`s in the contract
artifact to give generated tuple classes a better name than just hashing
their component field names.
  • Loading branch information
feuGeneA committed Nov 15, 2019
1 parent d81bb79 commit 17b70cf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 50 deletions.
21 changes: 14 additions & 7 deletions packages/abi-gen/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,20 @@ export const utils = {
* concatenation into PascalCase to conform to Python convention.
*/
makePythonTupleName(tuple: DataItem): string {
const tupleComponents = tuple.components;
const lengthOfHashSuffix = 8;
return `Tuple0x${createHash('MD5')
.update(_.map(tupleComponents, component => component.name).join('_'))
.digest()
.toString('hex')
.substring(0, lengthOfHashSuffix)}`;
if (tuple.internalType !== undefined) {
return tuple.internalType
.replace('struct ', '')
.replace('.', '')
.replace('[]', '');
} else {
const tupleComponents = tuple.components;
const lengthOfHashSuffix = 8;
return `Tuple0x${createHash('MD5')
.update(_.map(tupleComponents, component => component.name).join('_'))
.digest()
.toString('hex')
.substring(0, lengthOfHashSuffix)}`;
}
},
/**
* @returns a string that is a Python code snippet that's intended to be
Expand Down
64 changes: 36 additions & 28 deletions packages/abi-gen/test-cli/output/python/abi_gen_dummy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AbiGenDummyValidator( # type: ignore
pass


class Tuple0xf95128ef(TypedDict):
class AbiGenDummyComplexInput(TypedDict):
"""Python representation of a tuple or struct.
Solidity compiler output does not include the names of structs that appear
Expand All @@ -69,7 +69,7 @@ class Tuple0xf95128ef(TypedDict):
car: str


class Tuple0xa057bf41(TypedDict):
class AbiGenDummyComplexOutput(TypedDict):
"""Python representation of a tuple or struct.
Solidity compiler output does not include the names of structs that appear
Expand All @@ -85,7 +85,7 @@ class Tuple0xa057bf41(TypedDict):
accomplished via `str.encode("utf_8")`:code:
"""

input: Tuple0xf95128ef
input: AbiGenDummyComplexInput

lorem: Union[bytes, str]

Expand All @@ -94,7 +94,7 @@ class Tuple0xa057bf41(TypedDict):
dolor: str


class Tuple0x246f9407(TypedDict):
class AbiGenDummyStructNotDirectlyUsedAnywhere(TypedDict):
"""Python representation of a tuple or struct.
Solidity compiler output does not include the names of structs that appear
Expand All @@ -113,7 +113,7 @@ class Tuple0x246f9407(TypedDict):
aField: int


class Tuple0x1b9da225(TypedDict):
class AbiGenDummyNestedStructWithInnerStructNotUsedElsewhere(TypedDict):
"""Python representation of a tuple or struct.
Solidity compiler output does not include the names of structs that appear
Expand All @@ -129,10 +129,10 @@ class Tuple0x1b9da225(TypedDict):
accomplished via `str.encode("utf_8")`:code:
"""

innerStruct: Tuple0x246f9407
innerStruct: AbiGenDummyStructNotDirectlyUsedAnywhere


class Tuple0xcf8ad995(TypedDict):
class AbiGenDummyStruct(TypedDict):
"""Python representation of a tuple or struct.
Solidity compiler output does not include the names of structs that appear
Expand All @@ -157,7 +157,7 @@ class Tuple0xcf8ad995(TypedDict):
aString: str


class Tuple0xc9bdd2d5(TypedDict):
class AbiGenDummyNestedStruct(TypedDict):
"""Python representation of a tuple or struct.
Solidity compiler output does not include the names of structs that appear
Expand All @@ -173,7 +173,7 @@ class Tuple0xc9bdd2d5(TypedDict):
accomplished via `str.encode("utf_8")`:code:
"""

innerStruct: Tuple0xcf8ad995
innerStruct: AbiGenDummyStruct

description: str

Expand Down Expand Up @@ -281,7 +281,9 @@ def __init__(
super().__init__(web3_or_provider, contract_address, validator)
self.underlying_method = contract_function

def validate_and_normalize_inputs(self, complex_input: Tuple0xf95128ef):
def validate_and_normalize_inputs(
self, complex_input: AbiGenDummyComplexInput
):
"""Validate the inputs to the complexInputComplexOutput method."""
self.validator.assert_valid(
method_name="complexInputComplexOutput",
Expand All @@ -292,9 +294,9 @@ def validate_and_normalize_inputs(self, complex_input: Tuple0xf95128ef):

def call(
self,
complex_input: Tuple0xf95128ef,
complex_input: AbiGenDummyComplexInput,
tx_params: Optional[TxParams] = None,
) -> Tuple0xa057bf41:
) -> AbiGenDummyComplexOutput:
"""Execute underlying contract method via eth_call.
Tests decoding when the input and output are complex.
Expand All @@ -307,7 +309,7 @@ def call(
returned = self.underlying_method(complex_input).call(
tx_params.as_dict()
)
return Tuple0xa057bf41(
return AbiGenDummyComplexOutput(
input=returned[0],
lorem=returned[1],
ipsum=returned[2],
Expand All @@ -316,7 +318,7 @@ def call(

def estimate_gas(
self,
complex_input: Tuple0xf95128ef,
complex_input: AbiGenDummyComplexInput,
tx_params: Optional[TxParams] = None,
) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -470,7 +472,7 @@ def __init__(

def call(
self, tx_params: Optional[TxParams] = None
) -> List[Tuple0xcf8ad995]:
) -> List[AbiGenDummyStruct]:
"""Execute underlying contract method via eth_call.
:param tx_params: transaction parameters
Expand All @@ -479,7 +481,7 @@ def call(
tx_params = super().normalize_tx_params(tx_params)
returned = self.underlying_method().call(tx_params.as_dict())
return [
Tuple0xcf8ad995(
AbiGenDummyStruct(
someBytes=element[0],
anInteger=element[1],
aDynamicArrayOfBytes=element[2],
Expand Down Expand Up @@ -541,15 +543,19 @@ def __init__(
super().__init__(web3_or_provider, contract_address)
self.underlying_method = contract_function

def call(self, tx_params: Optional[TxParams] = None) -> Tuple0x1b9da225:
def call(
self, tx_params: Optional[TxParams] = None
) -> AbiGenDummyNestedStructWithInnerStructNotUsedElsewhere:
"""Execute underlying contract method via eth_call.
:param tx_params: transaction parameters
"""
tx_params = super().normalize_tx_params(tx_params)
returned = self.underlying_method().call(tx_params.as_dict())
return Tuple0x1b9da225(innerStruct=returned[0],)
return AbiGenDummyNestedStructWithInnerStructNotUsedElsewhere(
innerStruct=returned[0],
)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -653,7 +659,7 @@ def __init__(
super().__init__(web3_or_provider, contract_address, validator)
self.underlying_method = contract_function

def validate_and_normalize_inputs(self, n: Tuple0xc9bdd2d5):
def validate_and_normalize_inputs(self, n: AbiGenDummyNestedStruct):
"""Validate the inputs to the nestedStructInput method."""
self.validator.assert_valid(
method_name="nestedStructInput",
Expand All @@ -663,7 +669,7 @@ def validate_and_normalize_inputs(self, n: Tuple0xc9bdd2d5):
return n

def call(
self, n: Tuple0xc9bdd2d5, tx_params: Optional[TxParams] = None
self, n: AbiGenDummyNestedStruct, tx_params: Optional[TxParams] = None
) -> None:
"""Execute underlying contract method via eth_call.
Expand All @@ -675,7 +681,7 @@ def call(
self.underlying_method(n).call(tx_params.as_dict())

def estimate_gas(
self, n: Tuple0xc9bdd2d5, tx_params: Optional[TxParams] = None
self, n: AbiGenDummyNestedStruct, tx_params: Optional[TxParams] = None
) -> int:
"""Estimate gas consumption of method call."""
(n) = self.validate_and_normalize_inputs(n)
Expand All @@ -696,15 +702,17 @@ def __init__(
super().__init__(web3_or_provider, contract_address)
self.underlying_method = contract_function

def call(self, tx_params: Optional[TxParams] = None) -> Tuple0xc9bdd2d5:
def call(
self, tx_params: Optional[TxParams] = None
) -> AbiGenDummyNestedStruct:
"""Execute underlying contract method via eth_call.
:param tx_params: transaction parameters
"""
tx_params = super().normalize_tx_params(tx_params)
returned = self.underlying_method().call(tx_params.as_dict())
return Tuple0xc9bdd2d5(
return AbiGenDummyNestedStruct(
innerStruct=returned[0], description=returned[1],
)

Expand Down Expand Up @@ -1268,15 +1276,15 @@ def __init__(
super().__init__(web3_or_provider, contract_address, validator)
self.underlying_method = contract_function

def validate_and_normalize_inputs(self, s: Tuple0xcf8ad995):
def validate_and_normalize_inputs(self, s: AbiGenDummyStruct):
"""Validate the inputs to the structInput method."""
self.validator.assert_valid(
method_name="structInput", parameter_name="s", argument_value=s,
)
return s

def call(
self, s: Tuple0xcf8ad995, tx_params: Optional[TxParams] = None
self, s: AbiGenDummyStruct, tx_params: Optional[TxParams] = None
) -> None:
"""Execute underlying contract method via eth_call.
Expand All @@ -1288,7 +1296,7 @@ def call(
self.underlying_method(s).call(tx_params.as_dict())

def estimate_gas(
self, s: Tuple0xcf8ad995, tx_params: Optional[TxParams] = None
self, s: AbiGenDummyStruct, tx_params: Optional[TxParams] = None
) -> int:
"""Estimate gas consumption of method call."""
(s) = self.validate_and_normalize_inputs(s)
Expand All @@ -1309,7 +1317,7 @@ def __init__(
super().__init__(web3_or_provider, contract_address)
self.underlying_method = contract_function

def call(self, tx_params: Optional[TxParams] = None) -> Tuple0xcf8ad995:
def call(self, tx_params: Optional[TxParams] = None) -> AbiGenDummyStruct:
"""Execute underlying contract method via eth_call.
a method that returns a struct
Expand All @@ -1319,7 +1327,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> Tuple0xcf8ad995:
"""
tx_params = super().normalize_tx_params(tx_params)
returned = self.underlying_method().call(tx_params.as_dict())
return Tuple0xcf8ad995(
return AbiGenDummyStruct(
someBytes=returned[0],
anInteger=returned[1],
aDynamicArrayOfBytes=returned[2],
Expand Down
1 change: 1 addition & 0 deletions packages/ethereum-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export interface EventAbi {
export interface DataItem {
name: string;
type: string;
internalType?: string;
components?: DataItem[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from enum import auto, Enum

from . import (
Tuple0x735c43e3,
Tuple0x6ca34a6f,
Tuple0x4c5ca29b,
Tuple0xdabc15fe,
Tuple0xb1e4a1ae,
LibFillResultsFillResults,
LibOrderOrder,
LibFillResultsMatchedFillResults,
LibZeroExTransactionZeroExTransaction,
LibOrderOrderInfo,
)


Expand All @@ -25,43 +25,43 @@
# of each of these classes.


class FillResults(Tuple0x735c43e3):
class FillResults(LibFillResultsFillResults):
"""The `FillResults`:code: Solidity struct.
Also known as
`zero_ex.contract_wrappers.exchange.Tuple0x735c43e3`:py:class:.
`zero_ex.contract_wrappers.exchange.LibFillResultsFillResults`:py:class:.
"""


class Order(Tuple0x6ca34a6f):
class Order(LibOrderOrder):
"""The `Order`:code: Solidity struct.
Also known as
`zero_ex.contract_wrappers.exchange.Tuple0x6ca34a6f`:py:class:.
`zero_ex.contract_wrappers.exchange.LibOrderOrder`:py:class:.
"""


class MatchedFillResults(Tuple0x4c5ca29b):
class MatchedFillResults(LibFillResultsMatchedFillResults):
"""The `MatchedFillResults`:code: Solidity struct.
Also known as
`zero_ex.contract_wrappers.exchange.Tuple0x4c5ca29b`:py:class:.
`zero_ex.contract_wrappers.exchange.LibFillResultsMatchedFillResults`:py:class:.
"""


class ZeroExTransaction(Tuple0xdabc15fe):
class ZeroExTransaction(LibZeroExTransactionZeroExTransaction):
"""The `ZeroExTransaction`:code: Solidity struct.
Also known as
`zero_ex.contract_wrappers.exchange.Tuple0xdabc15fe`:py:class:.
`zero_ex.contract_wrappers.exchange.LibZeroExTransactionZeroExTransaction`:py:class:.
"""


class OrderInfo(Tuple0xb1e4a1ae):
class OrderInfo(LibOrderOrderInfo):
"""The `OrderInfo`:code: Solidity struct.
Also known as
`zero_ex.contract_wrappers.exchange.Tuple0xb1e4a1ae`:py:class:.
`zero_ex.contract_wrappers.exchange.LibOrderOrderInfo`:py:class:.
"""


Expand Down

0 comments on commit 17b70cf

Please sign in to comment.