Skip to content

Commit

Permalink
adjust min/max length for bytes/string arrays in interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdefinitelyahuman committed Feb 3, 2020
1 parent cddf350 commit 186efc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
10 changes: 6 additions & 4 deletions tests/parser/functions/test_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from decimal import Decimal
from decimal import (
Decimal,
)

import pytest

Expand Down Expand Up @@ -453,13 +455,13 @@ def test_json_interface_calls(get_contract, type_str, value):
abi = compile_code(code, ['abi'])['abi']
c1 = get_contract(code)

code = """
code = f"""
import jsonabi as jsonabi
@public
@constant
def test_call(a: address, b: {0}) -> {0}:
def test_call(a: address, b: {type_str}) -> {type_str}:
return jsonabi(a).test_json(b)
""".format(type_str)
"""
c2 = get_contract(code, interface_codes={'jsonabi': {'type': 'json', 'code': abi}})
assert c2.test_call(c1.address, value) == value
28 changes: 12 additions & 16 deletions vyper/signatures/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
from vyper.signatures.function_signature import (
FunctionSignature,
)
from vyper.typing import (
InterfaceImports,
SourceCode,
)
from vyper.types.types import (
ByteArrayLike,
TupleLike,
)
from vyper.typing import (
InterfaceImports,
SourceCode,
)


# Populate built-in interfaces.
Expand All @@ -60,20 +60,16 @@ def render_return(sig):
return ""


def abi_type_to_ast(atype):
def abi_type_to_ast(atype, idx):
if atype in ('int128', 'uint256', 'bool', 'address', 'bytes32'):
return ast.Name(id=atype)
elif atype == 'fixed168x10':
return ast.Name(id='decimal')
elif atype == 'bytes':
return ast.Subscript(
value=ast.Name(id='bytes'),
slice=ast.Index(value=ast.Num(n=256))
)
elif atype == 'string':
elif atype in ('bytes', 'string'):
# idx is the maximum length for inputs, minimum length for outputs
return ast.Subscript(
value=ast.Name(id='string'),
slice=ast.Index(value=ast.Num(n=256))
value=ast.Name(id=atype),
slice=ast.Index(value=ast.Num(n=idx))
)
else:
raise ParserException(f'Type {atype} not supported by vyper.')
Expand All @@ -89,18 +85,18 @@ def mk_full_signature_from_json(abi):
for a in func['inputs']:
arg = ast.arg(
arg=a['name'],
annotation=abi_type_to_ast(a['type']),
annotation=abi_type_to_ast(a['type'], 1048576),
lineno=0,
col_offset=0
)
args.append(arg)

if len(func['outputs']) == 1:
returns = abi_type_to_ast(func['outputs'][0]['type'])
returns = abi_type_to_ast(func['outputs'][0]['type'], 1)
elif len(func['outputs']) > 1:
returns = ast.Tuple(
elts=[
abi_type_to_ast(a['type'])
abi_type_to_ast(a['type'], 1)
for a in func['outputs']
]
)
Expand Down

0 comments on commit 186efc0

Please sign in to comment.