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: fix incorrect method return types
Browse files Browse the repository at this point in the history
Fixes #2298 .
  • Loading branch information
feuGeneA committed Nov 15, 2019
1 parent c506b8d commit c7d2477
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 43 deletions.
2 changes: 2 additions & 0 deletions packages/abi-gen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as mkdirp from 'mkdirp';
import toposort = require('toposort');
import * as yargs from 'yargs';

import { pythonHandlebarsHelpers } from './python_handlebars_helpers';
import { ContextData, ContractsBackend, ParamKind } from './types';
import { utils } from './utils';

Expand Down Expand Up @@ -293,6 +294,7 @@ function registerPythonHelpers(): void {
'toPythonClassname',
(sourceName: string) => new Handlebars.SafeString(changeCase.pascal(sourceName)),
);
Handlebars.registerHelper('makeOutputsValue', pythonHandlebarsHelpers.makeOutputsValue);
}
if (args.language === 'TypeScript') {
registerTypeScriptHelpers();
Expand Down
50 changes: 50 additions & 0 deletions packages/abi-gen/src/python_handlebars_helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as Handlebars from 'handlebars';

import { DataItem } from 'ethereum-types';

import { utils } from './utils';

export const pythonHandlebarsHelpers = {
/**
* Produces a Python expression representing the return value from a
* Solidity function.
* @param pythonVariable the name of the Python variable holding the value
* to be used to populate the output expression.
* @param abiOutputs the "outputs" object of the function's ABI.
*/
makeOutputsValue: (pythonVariable: string, abiOutputs: DataItem[]) => {
if (abiOutputs.length === 1) {
return new Handlebars.SafeString(solValueToPyValue(pythonVariable, abiOutputs[0]));
} else {
let tupleValue = '(';
for (let i = 0; i < abiOutputs.length; i++) {
tupleValue += `${pythonVariable}[${i}],`;
}
tupleValue += ')';
return new Handlebars.SafeString(tupleValue);
}
},
};

function solValueToPyValue(pythonVariable: string, abiItem: DataItem): string {
const pythonTypeName = utils.solTypeToPyType(abiItem.type, abiItem.components);
if (pythonTypeName.match(/List\[.*\]/) !== null) {
return `[${solValueToPyValue('element', {
...abiItem,
type: abiItem.type.replace('[]', ''),
})} for element in ${pythonVariable}]`;
} else {
let pyValue = `${pythonTypeName}(`;
if (abiItem.components) {
let i = 0;
for (const component of abiItem.components) {
pyValue += `${component.name}=${pythonVariable}[${i}],`;
i++;
}
} else {
pyValue += pythonVariable;
}
pyValue += ')';
return pyValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class {{toPythonClassname this.languageSpecificName}}Method(ContractMethod):
({{> params }}) = self.validate_and_normalize_inputs({{> params}})
{{/if}}
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method({{> params}}).call(tx_params.as_dict())
{{#hasReturnValue}}returned = {{/hasReturnValue}}self.underlying_method({{> params}}).call(tx_params.as_dict())
{{#hasReturnValue}}
return {{makeOutputsValue 'returned' outputs}}
{{/hasReturnValue}}

{{^if this.constant}}
def send_transaction(self, {{#if inputs}}{{> typed_params inputs=inputs}}, {{/if}}tx_params: Optional[TxParams] = None) -> Union[HexBytes, bytes]:
Expand Down
108 changes: 78 additions & 30 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 @@ -214,7 +214,7 @@ def call(
"""
(a) = self.validate_and_normalize_inputs(a)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(a).call(tx_params.as_dict())
self.underlying_method(a).call(tx_params.as_dict())

def estimate_gas(
self, a: List[Union[bytes, str]], tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -256,7 +256,7 @@ def call(
"""
(a) = self.validate_and_normalize_inputs(a)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(a).call(tx_params.as_dict())
self.underlying_method(a).call(tx_params.as_dict())

def estimate_gas(
self, a: Union[bytes, str], tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -304,7 +304,15 @@ def call(
"""
(complex_input) = self.validate_and_normalize_inputs(complex_input)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(complex_input).call(tx_params.as_dict())
returned = self.underlying_method(complex_input).call(
tx_params.as_dict()
)
return Tuple0xa057bf41(
input=returned[0],
lorem=returned[1],
ipsum=returned[2],
dolor=returned[3],
)

def estimate_gas(
self,
Expand Down Expand Up @@ -383,7 +391,10 @@ def call(
"""
(_hash, v, r, s) = self.validate_and_normalize_inputs(_hash, v, r, s)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(_hash, v, r, s).call(tx_params.as_dict())
returned = self.underlying_method(_hash, v, r, s).call(
tx_params.as_dict()
)
return str(returned)

def estimate_gas(
self,
Expand Down Expand Up @@ -422,7 +433,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> None:
:returns: the return value of the underlying method.
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
self.underlying_method().call(tx_params.as_dict())

def send_transaction(
self, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -468,7 +479,16 @@ def call(
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return [
Tuple0xcf8ad995(
someBytes=element[0],
anInteger=element[1],
aDynamicArrayOfBytes=element[2],
aString=element[3],
)
for element in returned
]

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -497,7 +517,11 @@ def call(self, tx_params: Optional[TxParams] = None) -> Tuple[int, str]:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return (
returned[0],
returned[1],
)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -528,7 +552,8 @@ def call(self, tx_params: Optional[TxParams] = None) -> Tuple0x1b9da225:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return Tuple0x1b9da225(innerStruct=returned[0],)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -592,9 +617,14 @@ def call(
index_0, index_1, index_2
)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(index_0, index_1, index_2).call(
returned = self.underlying_method(index_0, index_1, index_2).call(
tx_params.as_dict()
)
return (
returned[0],
returned[1],
returned[2],
)

def estimate_gas(
self,
Expand Down Expand Up @@ -646,7 +676,7 @@ def call(
"""
(n) = self.validate_and_normalize_inputs(n)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(n).call(tx_params.as_dict())
self.underlying_method(n).call(tx_params.as_dict())

def estimate_gas(
self, n: Tuple0xc9bdd2d5, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -678,7 +708,10 @@ def call(self, tx_params: Optional[TxParams] = None) -> Tuple0xc9bdd2d5:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return Tuple0xc9bdd2d5(
innerStruct=returned[0], description=returned[1],
)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -709,7 +742,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> None:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
self.underlying_method().call(tx_params.as_dict())

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -740,7 +773,8 @@ def call(self, tx_params: Optional[TxParams] = None) -> int:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return int(returned)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -769,7 +803,8 @@ def call(self, tx_params: Optional[TxParams] = None) -> int:
:returns: the return value of the underlying method.
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return int(returned)

def send_transaction(
self, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -813,7 +848,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> None:
:returns: the return value of the underlying method.
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
self.underlying_method().call(tx_params.as_dict())

def send_transaction(
self, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -867,7 +902,7 @@ def call(self, a: str, tx_params: Optional[TxParams] = None) -> None:
"""
(a) = self.validate_and_normalize_inputs(a)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(a).call(tx_params.as_dict())
self.underlying_method(a).call(tx_params.as_dict())

def estimate_gas(
self, a: str, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -909,7 +944,7 @@ def call(self, a: int, tx_params: Optional[TxParams] = None) -> None:
"""
(a) = self.validate_and_normalize_inputs(a)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(a).call(tx_params.as_dict())
self.underlying_method(a).call(tx_params.as_dict())

def estimate_gas(
self, a: int, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -941,7 +976,8 @@ def call(self, tx_params: Optional[TxParams] = None) -> int:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return int(returned)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -970,7 +1006,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> None:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
self.underlying_method().call(tx_params.as_dict())

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -999,7 +1035,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> None:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
self.underlying_method().call(tx_params.as_dict())

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -1042,7 +1078,7 @@ def call(self, index_0: int, tx_params: Optional[TxParams] = None) -> None:
"""
(index_0) = self.validate_and_normalize_inputs(index_0)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(index_0).call(tx_params.as_dict())
self.underlying_method(index_0).call(tx_params.as_dict())

def estimate_gas(
self, index_0: int, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -1088,7 +1124,8 @@ def call(self, index_0: int, tx_params: Optional[TxParams] = None) -> int:
"""
(index_0) = self.validate_and_normalize_inputs(index_0)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(index_0).call(tx_params.as_dict())
returned = self.underlying_method(index_0).call(tx_params.as_dict())
return int(returned)

def estimate_gas(
self, index_0: int, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -1120,7 +1157,8 @@ def call(self, tx_params: Optional[TxParams] = None) -> int:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return int(returned)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -1161,7 +1199,8 @@ def call(self, x: int, tx_params: Optional[TxParams] = None) -> int:
"""
(x) = self.validate_and_normalize_inputs(x)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(x).call(tx_params.as_dict())
returned = self.underlying_method(x).call(tx_params.as_dict())
return int(returned)

def estimate_gas(
self, x: int, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -1193,7 +1232,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> None:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
self.underlying_method().call(tx_params.as_dict())

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -1222,7 +1261,7 @@ def call(self, tx_params: Optional[TxParams] = None) -> None:
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
self.underlying_method().call(tx_params.as_dict())

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -1261,7 +1300,7 @@ def call(
"""
(s) = self.validate_and_normalize_inputs(s)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(s).call(tx_params.as_dict())
self.underlying_method(s).call(tx_params.as_dict())

def estimate_gas(
self, s: Tuple0xcf8ad995, tx_params: Optional[TxParams] = None
Expand Down Expand Up @@ -1295,7 +1334,13 @@ def call(self, tx_params: Optional[TxParams] = None) -> Tuple0xcf8ad995:
:returns: a Struct struct
"""
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method().call(tx_params.as_dict())
returned = self.underlying_method().call(tx_params.as_dict())
return Tuple0xcf8ad995(
someBytes=returned[0],
anInteger=returned[1],
aDynamicArrayOfBytes=returned[2],
aString=returned[3],
)

def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
"""Estimate gas consumption of method call."""
Expand Down Expand Up @@ -1372,7 +1417,10 @@ def call(
"""
(x, a, b, y, c) = self.validate_and_normalize_inputs(x, a, b, y, c)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(x, a, b, y, c).call(tx_params.as_dict())
returned = self.underlying_method(x, a, b, y, c).call(
tx_params.as_dict()
)
return str(returned)

def estimate_gas(
self,
Expand Down Expand Up @@ -1422,7 +1470,7 @@ def call(self, wad: int, tx_params: Optional[TxParams] = None) -> None:
"""
(wad) = self.validate_and_normalize_inputs(wad)
tx_params = super().normalize_tx_params(tx_params)
return self.underlying_method(wad).call(tx_params.as_dict())
self.underlying_method(wad).call(tx_params.as_dict())

def send_transaction(
self, wad: int, tx_params: Optional[TxParams] = None
Expand Down
Loading

0 comments on commit c7d2477

Please sign in to comment.