Skip to content

Commit 3549fde

Browse files
committed
Test passing a tuple into a contract call
1 parent 2b8d47e commit 3549fde

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

tests/core/contracts/conftest.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,110 @@ def FallballFunctionContract(web3, FALLBACK_FUNCTION_CONTRACT):
527527
return web3.eth.contract(**FALLBACK_FUNCTION_CONTRACT)
528528

529529

530+
CONTRACT_TUPLE_SOURCE = """
531+
pragma experimental ABIEncoderV3;
532+
533+
contract Tuple {
534+
struct Struct {
535+
int anInt;
536+
bool aBool;
537+
address anAddress;
538+
}
539+
540+
function method(Struct memory m)
541+
public
542+
pure
543+
returns (Struct memory)
544+
{
545+
return m;
546+
}
547+
}"""
548+
549+
CONTRACT_TUPLE_CODE = "608060405234801561001057600080fd5b506102ed806100206000396000f3fe608060405234801561001057600080fd5b5060043610610048576000357c0100000000000000000000000000000000000000000000000000000000900480634e3269761461004d575b600080fd5b61006760048036036100629190810190610163565b61007d565b60405161007491906101fb565b60405180910390f35b61008561008d565b819050919050565b60606040519081016040528060008152602001600015158152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b60006100d3823561028b565b905092915050565b60006100e7823561029d565b905092915050565b60006100fb82356102a9565b905092915050565b60006060828403121561011557600080fd5b61011f6060610216565b9050600061012f848285016100ef565b6000830152506020610143848285016100db565b6020830152506040610157848285016100c7565b60408301525092915050565b60006060828403121561017557600080fd5b600061018384828501610103565b91505092915050565b61019581610243565b82525050565b6101a481610255565b82525050565b6101b381610261565b82525050565b6060820160008201516101cf60008501826101aa565b5060208201516101e2602085018261019b565b5060408201516101f5604085018261018c565b50505050565b600060608201905061021060008301846101b9565b92915050565b6000604051905081810181811067ffffffffffffffff8211171561023957600080fd5b8060405250919050565b600061024e8261026b565b9050919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006102968261026b565b9050919050565b60008115159050919050565b600081905091905056fea265627a7a723058203f779dc401e40b86d552e03af453e4f759bd8e4f8f0ca5ffb05909e2af7567b76c6578706572696d656e74616cf50037" # noqa: E501
550+
551+
CONTRACT_TUPLE_RUNTIME = "608060405234801561001057600080fd5b5060043610610048576000357c0100000000000000000000000000000000000000000000000000000000900480634e3269761461004d575b600080fd5b61006760048036036100629190810190610163565b61007d565b60405161007491906101fb565b60405180910390f35b61008561008d565b819050919050565b60606040519081016040528060008152602001600015158152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b60006100d3823561028b565b905092915050565b60006100e7823561029d565b905092915050565b60006100fb82356102a9565b905092915050565b60006060828403121561011557600080fd5b61011f6060610216565b9050600061012f848285016100ef565b6000830152506020610143848285016100db565b6020830152506040610157848285016100c7565b60408301525092915050565b60006060828403121561017557600080fd5b600061018384828501610103565b91505092915050565b61019581610243565b82525050565b6101a481610255565b82525050565b6101b381610261565b82525050565b6060820160008201516101cf60008501826101aa565b5060208201516101e2602085018261019b565b5060408201516101f5604085018261018c565b50505050565b600060608201905061021060008301846101b9565b92915050565b6000604051905081810181811067ffffffffffffffff8211171561023957600080fd5b8060405250919050565b600061024e8261026b565b9050919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006102968261026b565b9050919050565b60008115159050919050565b600081905091905056fea265627a7a723058203f779dc401e40b86d552e03af453e4f759bd8e4f8f0ca5ffb05909e2af7567b76c6578706572696d656e74616cf50037" # noqa: E501
552+
553+
CONTRACT_TUPLE_ABI = json.loads("""
554+
[
555+
{
556+
"constant": true,
557+
"inputs": [
558+
{
559+
"components": [
560+
{
561+
"name": "anInt",
562+
"type": "int256"
563+
},
564+
{
565+
"name": "aBool",
566+
"type": "bool"
567+
},
568+
{
569+
"name": "anAddress",
570+
"type": "address"
571+
}
572+
],
573+
"name": "m",
574+
"type": "tuple"
575+
}
576+
],
577+
"name": "method",
578+
"outputs": [
579+
{
580+
"components": [
581+
{
582+
"name": "anInt",
583+
"type": "int256"
584+
},
585+
{
586+
"name": "aBool",
587+
"type": "bool"
588+
},
589+
{
590+
"name": "anAddress",
591+
"type": "address"
592+
}
593+
],
594+
"name": "",
595+
"type": "tuple"
596+
}
597+
],
598+
"payable": false,
599+
"stateMutability": "pure",
600+
"type": "function"
601+
}
602+
]""")
603+
604+
605+
@pytest.fixture()
606+
def TUPLE_CODE():
607+
return CONTRACT_TUPLE_CODE
608+
609+
610+
@pytest.fixture()
611+
def TUPLE_RUNTIME():
612+
return CONTRACT_TUPLE_RUNTIME
613+
614+
615+
@pytest.fixture()
616+
def TUPLE_ABI():
617+
return CONTRACT_TUPLE_ABI
618+
619+
620+
@pytest.fixture()
621+
def TUPLE_CONTRACT(TUPLE_CODE, TUPLE_RUNTIME, TUPLE_ABI):
622+
return {
623+
'bytecode': TUPLE_CODE,
624+
'bytecode_runtime': TUPLE_RUNTIME,
625+
'abi': TUPLE_ABI,
626+
}
627+
628+
629+
@pytest.fixture()
630+
def TupleContract(web3, TUPLE_CONTRACT):
631+
return web3.eth.contract(**TUPLE_CONTRACT)
632+
633+
530634
class LogFunctions:
531635
LogAnonymous = 0
532636
LogNoArguments = 1

tests/core/contracts/test_contract_call_interface.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ def fallback_function_contract(web3, FallballFunctionContract, address_conversio
134134
return deploy(web3, FallballFunctionContract, address_conversion_func)
135135

136136

137+
@pytest.fixture()
138+
def tuple_contract(web3, TupleContract, address_conversion_func):
139+
return deploy(web3, TupleContract, address_conversion_func)
140+
141+
137142
def test_invalid_address_in_deploy_arg(web3, WithConstructorAddressArgumentsContract):
138143
with pytest.raises(InvalidAddress):
139144
WithConstructorAddressArgumentsContract.constructor(
@@ -611,3 +616,21 @@ def test_invalid_fixed_value_reflections(web3, fixed_reflection_contract, functi
611616
contract_func = fixed_reflection_contract.functions[function]
612617
with pytest.raises(ValidationError, match=error):
613618
contract_func(value).call({'gas': 420000})
619+
620+
621+
@pytest.mark.parametrize(
622+
'method_input, expected',
623+
(
624+
(
625+
{'anInt': 0, 'aBool': True, 'anAddress': '0x' + 'f' * 40},
626+
(0, True, '0x' + 'f' * 40)
627+
),
628+
(
629+
(0, True, '0x' + 'f' * 40),
630+
(0, True, '0x' + 'f' * 40),
631+
),
632+
)
633+
)
634+
def test_call_tuple_contract(tuple_contract, method_input, expected):
635+
result = tuple_contract.functions.method(method_input).call()
636+
assert result == expected

0 commit comments

Comments
 (0)