コード例 #1
0
def test_contract_add_function_signature():
    add = Function(
        "add",
        inputs=[{'type': 'int256', 'name': 'a'}, {'type': 'int256', 'name': 'b'}],
        outputs=[{'type': 'int256', 'name': 'result'}],
    )
    assert add.abi_signature == 2784215611
    assert add.encoded_abi_signature == '\xa5\xf3\xc2;'
    assert add.get_call_data((3, 4)) == 'a5f3c23b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004'
コード例 #2
0
def test_contract_multiply7_function_signature():
    multiply7 = Function(
        'multiply7',
        inputs=[{'type': 'int256', 'name': 'a'}],
        outputs=[{'type': 'int256', 'name': 'result'}],
    )
    assert multiply7.abi_signature == 3707058097
    assert multiply7.encoded_abi_signature == '\xdc\xf57\xb1'
    assert multiply7.get_call_data((3,)) == 'dcf537b10000000000000000000000000000000000000000000000000000000000000003'
コード例 #3
0
def test_contract_return13_function_signature():
    return13 = Function("return13",
                        inputs=[],
                        outputs=[{
                            'type': 'int256',
                            'name': 'result'
                        }])
    assert return13.abi_signature == 371289913
    assert return13.encoded_abi_signature == b'\x16!o9'
    assert return13.get_call_data([]) == b'16216f39'
コード例 #4
0
def test_abi_args_signature_with_0x_prefixed_address():
    func = Function("multiply", [{
        'type': 'address',
        'name': 'to'
    }, {
        'type': 'uint256',
        'name': 'deposit'
    }])
    args_signature = func.abi_args_signature(
        ('0xd3cda913deb6f67967b99d67acdfa1712c293601', 12345))
    assert args_signature == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd3\xcd\xa9\x13\xde\xb6\xf6yg\xb9\x9dg\xac\xdf\xa1q,)6\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0009'
コード例 #5
0
def test_contract_multiply7_function_signature():
    multiply7 = Function(
        'multiply7',
        inputs=[{
            'type': 'int256',
            'name': 'a'
        }],
        outputs=[{
            'type': 'int256',
            'name': 'result'
        }],
    )
    assert multiply7.abi_signature == 3707058097
    assert multiply7.encoded_abi_signature == b'\xdc\xf57\xb1'
    assert multiply7.get_call_data(
        (3, )
    ) == b'dcf537b10000000000000000000000000000000000000000000000000000000000000003'
コード例 #6
0
def test_signature_multiple_args():
    func = Function("multiply", [{
        'type': 'int256',
        'name': 'a'
    }, {
        'type': 'int256',
        'name': 'b'
    }])
    assert str(func) == "multiply(int256 a, int256 b)"
コード例 #7
0
def test_abi_signature_multiple_args():
    func = Function("multiply", [{
        'type': 'int256',
        'name': 'a'
    }, {
        'type': 'int256',
        'name': 'b'
    }])
    assert encode_hex(func.encoded_abi_signature) == b"3c4308a8"
コード例 #8
0
def test_contract_add_function_signature():
    add = Function(
        "add",
        inputs=[{
            'type': 'int256',
            'name': 'a'
        }, {
            'type': 'int256',
            'name': 'b'
        }],
        outputs=[{
            'type': 'int256',
            'name': 'result'
        }],
    )
    assert add.abi_signature == 2784215611
    assert add.encoded_abi_signature == b'\xa5\xf3\xc2;'
    assert add.get_call_data(
        (3, 4)
    ) == b'a5f3c23b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004'
コード例 #9
0
def test_abi_signature_no_args():
    func = Function("doit", [])
    assert hex(func.abi_signature) == "0x4d536fe3"
コード例 #10
0
def test_contract_return13_function_signature():
    return13 = Function("return13", inputs=[], outputs=[{'type': 'int256', 'name': 'result'}])
    assert return13.abi_signature == 371289913
    assert return13.encoded_abi_signature == '\x16!o9'
    assert return13.get_call_data([]) == '16216f39'
コード例 #11
0
def test_abi_args_signature_with_0x_prefixed_address():
    func = Function("multiply", [{'type': 'address', 'name': 'to'}, {'type': 'uint256', 'name': 'deposit'}])
    args_signature = func.abi_args_signature(('0xd3cda913deb6f67967b99d67acdfa1712c293601', 12345))
    assert args_signature == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd3\xcd\xa9\x13\xde\xb6\xf6yg\xb9\x9dg\xac\xdf\xa1q,)6\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0009'
コード例 #12
0
ファイル: core.py プロジェクト: movermeyer/ethereum-contract
def Contract(contract_meta, contract_name=None):
    _abi = contract_meta['info']['abiDefinition']
    code = contract_meta['code']
    source = contract_meta['info']['source']

    if contract_name is None:
        contract_name = "Unknown-{0}".format(hashlib.md5(code).hexdigest())

    functions = []
    events = []
    constructor = None

    _dict = {}
    _functions = collections.defaultdict(list)

    for signature_item in _abi:
        if signature_item['type'] == 'constructor':
            # Constructors don't need to be part of a contract's methods
            if signature_item.get('inputs'):
                constructor = Function(
                    name='constructor',
                    inputs=signature_item['inputs'],
                )
            continue

        if signature_item['type'] == 'function':
            # make sure we're not overwriting a signature

            func = Function(
                name=signature_item['name'],
                inputs=signature_item['inputs'],
                outputs=signature_item['outputs'],
                constant=signature_item['constant'],
            )
            _functions[signature_item['name']].append(func)
        elif signature_item['type'] == 'event':
            if signature_item['name'] in _dict:
                # TODO: handle namespace conflicts
                raise ValueError(
                    "About to overwrite a function signature for duplicate function name {0}"
                    .format(signature_item['name']))  # NOQA
            event = Event(
                name=signature_item['name'],
                inputs=signature_item['inputs'],
                anonymous=signature_item['anonymous'],
            )
            _dict[signature_item['name']] = event
            events.append(event)
        else:
            raise ValueError(
                "Unknown signature item '{0}'".format(signature_item))

    # Now process all the functions
    for fn_name, fn_list in _functions.items():
        if len(fn_list) == 1:
            _dict[fn_name] = fn_list[0]
            functions.append(fn_list[0])
        else:
            fn_group = FunctionGroup(fn_list)
            _dict[fn_name] = fn_group
            functions.append(fn_group)

    docstring = """
    contract {contract_name} {{
    // Events
    {events}

    // Functions
    {functions}
    }}
    """.format(
        contract_name=contract_name,
        functions='\n'.join(str(f) for f in functions),
        events='\n'.join(str(e) for e in events),
    )

    _dict['__doc__'] = docstring
    _dict['_config'] = Config(
        code,
        source,
        _abi,
        functions,
        events,
        constructor,
        contract_name,
    )

    return type(str(contract_name), (ContractBase, ), _dict)
コード例 #13
0
int8_b = {'type': 'int8', 'name': 'b'}
int8_c = {'type': 'int8', 'name': 'c'}
uint8_a = {'type': 'uint8', 'name': 'a'}
uint8_b = {'type': 'uint8', 'name': 'b'}
int256_a = {'type': 'int256', 'name': 'a'}
int256_b = {'type': 'int256', 'name': 'b'}
uint256_a = {'type': 'uint256', 'name': 'a'}
uint256_b = {'type': 'uint256', 'name': 'b'}
bytes8_x = {'type': 'bytes8', 'name': 'x'}
bytes8_y = {'type': 'bytes8', 'name': 'y'}
bytes32_x = {'type': 'bytes32', 'name': 'x'}
bytes32_y = {'type': 'bytes32', 'name': 'y'}
address_m = {'type': 'address', 'name': 'm'}
address_n = {'type': 'address', 'name': 'n'}

f_1a = Function('doit', [int8_a, uint8_a, int256_a, uint256_a])
f_1b = Function('doit', [int8_a, uint8_a, int8_b, int8_c])

f_group_1 = FunctionGroup([f_1a, f_1b])


def test_matching_with_similar_integers():
    assert f_group_1.get_function_for_call_signature((1, 2, 500, 500)) == f_1a
    with pytest.raises(TypeError):
        assert f_group_1.get_function_for_call_signature((1, 2, 100, 100))
    assert f_group_1.get_function_for_call_signature((1, 2, 100, -100)) == f_1b


f_2a = Function('doit', [uint256_a, bytes32_x, address_m])
f_2b = Function('doit', [uint256_a, address_n, bytes8_x])
コード例 #14
0
def test_abi_signature_single_arg():
    func = Function("register", [{'type': 'bytes32', 'name': 'name'}])
    assert encode_hex(func.encoded_abi_signature) == b"e1fa8e84"
コード例 #15
0
def test_abi_signature_single_arg():
    func = Function("register", [{'type': 'bytes32', 'name': 'name'}])
    assert hex(func.abi_signature) == "0xe1fa8e84"
コード例 #16
0
def test_abi_signature_no_args():
    func = Function("doit", [])
    assert encode_hex(func.encoded_abi_signature) == b"4d536fe3"
コード例 #17
0
def test_signature_single_arg():
    func = Function("register", [{'type': 'bytes32', 'name': 'name'}])
    assert str(func) == "register(bytes32 name)"
コード例 #18
0
def test_signature_no_args():
    func = Function("doit", [])
    assert str(func) == "doit()"