def call_contract(greeter,account_addr,keypath,password,url):
    w3 = Web3(Web3.HTTPProvider(url))

    from_addr = w3.toChecksumAddress(account_addr)

    # Display the default greeting from the contract
    print('Default contract greeting: {}'.format(
        greeter.functions.greet().call()
    ))

    print('Setting the greeting to Nihao...')
    tx_hash = greeter.functions.setGreeting('Nihao').raw_transact({
        'gas': 300000,
        'from':from_addr,
        'value': 0,
    },keypath,password,42)

    # Wait for transaction to be mined...
    w3.cpc.waitForTransactionReceipt(tx_hash)

    # Display the new greeting value
    print('Updated contract greeting: {}'.format(
        greeter.functions.greet().call()
    ))

    # When issuing a lot of reads, try this more concise reader:
    reader = ConciseContract(greeter)
    assert reader.greet() == "Nihao"

    a = greeter.events.test.createFilter(fromBlock=0,toBlock='latest')
    eventlist = a.get_all_entries()
    print(eventlist)
def zero_address_contract(web3, WithConstructorAddressArgumentsContract, EMPTY_ADDR):
    deploy_txn = WithConstructorAddressArgumentsContract.constructor(
        EMPTY_ADDR,
    ).transact()
    deploy_receipt = web3.eth.waitForTransactionReceipt(deploy_txn)
    assert deploy_receipt is not None
    _address_contract = WithConstructorAddressArgumentsContract(
        address=deploy_receipt['contractAddress'],
    )
    return ConciseContract(_address_contract)
def test_conciscecontract_function_collision(
        web3,
        StringContract):

    contract = deploy(web3, StringContract, args=["blarg"])

    def getValue():
        assert 'getValue' in [
            item['name'] for item
            in StringContract.abi
            if 'name' in item]

    setattr(ConciseContract, 'getValue', getValue)

    concise_contract = ConciseContract(contract)

    assert isinstance(concise_contract, ConciseContract)

    with pytest.raises(AttributeError, match=r'Namespace collision .* with ConciseContract API.'):
        concise_contract.getValue()
def test_conciscecontract_keeps_custom_normalizers_on_base(web3):
    base_contract = web3.eth.contract()
    # give different normalizers to this base instance
    base_contract._return_data_normalizers = base_contract._return_data_normalizers + tuple([None])

    # create concisce contract with custom contract
    new_normalizers_size = len(base_contract._return_data_normalizers)
    concise = ConciseContract(base_contract)

    # check that concise contract includes the new normalizers
    concise_normalizers_size = len(concise._classic_contract._return_data_normalizers)
    assert concise_normalizers_size == new_normalizers_size + len(CONCISE_NORMALIZERS)
    assert concise._classic_contract._return_data_normalizers[0] is None
示例#5
0
def update(data, myaddr):
    # data=json.dumps(data)
    data = str(data)
    # Solidity source code
    contract_source_code = '''
    pragma solidity ^0.4.24;

    contract Dumper {
        string public dumping;
        event test(address who,string a);
        function Dumper() public {
            dumping = '';
        }

        function setDumping(string _dumping) public {
            emit test(msg.sender,_dumping);
            dumping = _dumping;
        }

        function dump() view public returns (string) {
            return dumping;
        }
    }
    '''
    # print(contract_source_code)

    compiled_sol = compile_source(contract_source_code)  # Compiled source code
    contract_interface = compiled_sol['<stdin>:Dumper']

    # web3.py instance
    w3 = Web3(Web3.HTTPProvider('http://3.1.81.79:8501'))

    # set pre-funded account as sender
    w3.cpc.defaultAccount = w3.cpc.accounts[0]

    # Instantiate and deploy contract
    Dumper = w3.cpc.contract(abi=contract_interface['abi'],
                             bytecode=contract_interface['bin'])

    # Submit the transaction that deploys the contract
    # your keypath
    # change the keypath to your keystore file
    keypath = "./UTC--2019-04-20T23-54-49.143706400Z--13af759d94420d097c63d0b4e521d01486b2a4fe"
    # your password
    password = "******"
    # your account address
    from_addr = w3.toChecksumAddress(myaddr)
    tx_hash = Dumper.constructor().raw_transact(
        {
            # Increase or decrease gas according to contract conditions
            'gas': 819776,
            'from': from_addr,
            'value': 0
        },
        keypath,
        password,
        42)
    # tx_hash = Dumper.constructor().transact()

    # print('*********',w3.cpc.getTransactionReceipt(tx_hash_raw))
    # print('*********', w3.cpc.getTransactionReceipt(tx_hash))

    # Wait for the transaction to be mined, and get the transaction receipt
    tx_receipt = w3.cpc.waitForTransactionReceipt(tx_hash)
    # tx_receipt1 = w3.cpc.waitForTransactionReceipt(tx_hash_raw)
    # print(tx_receipt)
    # print(tx_receipt1)

    # Create the contract instance with the newly-deployed address
    dumper = w3.cpc.contract(
        address=tx_receipt.contractAddress,
        abi=contract_interface['abi'],
    )

    # Display the default dumping from the contract
    print('Default contract dumping: {}'.format(
        dumper.functions.dump().call()))

    print('Setting the dumping ...')
    tx_hash = dumper.functions.setDumping(data).raw_transact(
        {
            'gas': 300000,
            'from': from_addr,
            'value': 0,
        }, keypath, password, 42)

    # Wait for transaction to be mined...
    w3.cpc.waitForTransactionReceipt(tx_hash)

    # Display the new dumping value
    print('Updated contract dumping: {}'.format(
        dumper.functions.dump().call()))

    # When issuing a lot of reads, try this more concise reader:
    reader = ConciseContract(dumper)
    assert reader.dump() == data
示例#6
0
def main():
    # Solidity source code
    contract_source_code = '''
    pragma solidity ^0.4.24;

    contract Greeter {
        string public greeting;
        event test(address who,string a);
        function Greeter() public {
            greeting = 'Hello';
        }

        function setGreeting(string _greeting) public {
            emit test(msg.sender,_greeting);
            greeting = _greeting;
        }

        function greet() view public returns (string) {
            return greeting;
        }
    }
    '''
    print(contract_source_code)

    compiled_sol = compile_source(contract_source_code)  # Compiled source code
    contract_interface = compiled_sol['<stdin>:Greeter']

    # web3.py instance
    w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8501'))

    # set pre-funded account as sender
    w3.cpc.defaultAccount = w3.cpc.accounts[0]

    # Instantiate and deploy contract
    Greeter = w3.cpc.contract(abi=contract_interface['abi'],
                              bytecode=contract_interface['bin'])

    # Submit the transaction that deploys the contract
    # your keypath
    # change the keypath to your keystore file
    keypath = "//home/shi/chain/workspace/src/bitbucket.org/cpchain/chain/examples/cpchain/data/data21/keystore/key21"
    # your password
    password = "******"
    # your account address
    from_addr = w3.toChecksumAddress(
        '0xb3801b8743dea10c30b0c21cae8b1923d9625f84')
    tx_hash = Greeter.constructor().raw_transact(
        {
            # Increase or decrease gas according to contract conditions
            'gas': 819776,
            'from': from_addr,
            'value': 0
        },
        keypath,
        password,
        42)
    # tx_hash = Greeter.constructor().transact()

    # print('*********',w3.cpc.getTransactionReceipt(tx_hash_raw))
    print('*********', w3.cpc.getTransactionReceipt(tx_hash))

    # Wait for the transaction to be mined, and get the transaction receipt
    tx_receipt = w3.cpc.waitForTransactionReceipt(tx_hash)
    # tx_receipt1 = w3.cpc.waitForTransactionReceipt(tx_hash_raw)
    print(tx_receipt)
    # print(tx_receipt1)

    # Create the contract instance with the newly-deployed address
    greeter = w3.cpc.contract(
        address=tx_receipt.contractAddress,
        abi=contract_interface['abi'],
    )

    # Display the default greeting from the contract
    print('Default contract greeting: {}'.format(
        greeter.functions.greet().call()))

    print('Setting the greeting to Nihao...')
    tx_hash = greeter.functions.setGreeting('Nihao').raw_transact(
        {
            'gas': 300000,
            'from': from_addr,
            'value': 0,
        }, keypath, password, 42)

    # Wait for transaction to be mined...
    w3.cpc.waitForTransactionReceipt(tx_hash)

    # Display the new greeting value
    print('Updated contract greeting: {}'.format(
        greeter.functions.greet().call()))

    # When issuing a lot of reads, try this more concise reader:
    reader = ConciseContract(greeter)
    assert reader.greet() == "Nihao"