Skip to content

Commit

Permalink
fix: support bytes array argument in solidity and vyper contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
slush committed Aug 30, 2024
1 parent b1bbc23 commit 8c84af3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/ape/managers/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def convert(self, value: Any) -> int:
return to_int(HexBytes(value))


class HexListConverter(ConverterAPI):
"""
Convert list of hex values to single concatenated hex value.
"""

def is_convertible(self, value: Any) -> bool:
return isinstance(value, list) and all(is_hex(v) or isinstance(v, bytes) for v in value)

def convert(self, value: Any) -> bytes:
return HexBytes(b"".join([HexBytes(v) for v in value]))


class StringIntConverter(ConverterAPI):
def is_convertible(self, value: Any) -> bool:
return isinstance(value, str) and not is_0x_prefixed(value) and value.isnumeric()
Expand Down Expand Up @@ -263,7 +275,10 @@ def _converters(self) -> dict[type, list[ConverterAPI]]:
HexAddressConverter(),
IntAddressConverter(),
],
bytes: [HexConverter()],
bytes: [
HexConverter(),
HexListConverter(),
],
int: [
TimestampConverter(),
HexIntConverter(),
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tests/functional/data/sources/SolidityContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ contract SolidityContract {

}

function functionWithCalldata(bytes calldata data) public {

}

function setStruct(MyStruct memory _my_struct) public pure {

}
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/data/sources/VyperContract.vy
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ def functionWithUniqueAmountOfArguments(
):
pass

@external
def functionWithCalldata(data: Bytes[1_024]=b""):
pass

@pure
@external
def setStruct(_my_struct: MyStruct):
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/test_contract_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,9 @@ def test_sending_funds_to_non_payable_constructor_by_accountDeploy(
def test_as_transaction(tx_type, vyper_contract_instance, owner, eth_tester_provider):
tx = vyper_contract_instance.setNumber.as_transaction(987, sender=owner, type=tx_type.value)
assert tx.gas_limit == eth_tester_provider.max_gas


@pytest.mark.parametrize("calldata", ("0x123456", ["0x123456", "0xabcd"]))
def test_calldata_arg(calldata, contract_instance, owner):
tx = contract_instance.functionWithCalldata(calldata, sender=owner)
assert not tx.failed

0 comments on commit 8c84af3

Please sign in to comment.