def test_load_contract(address):
    print('contract:', address)
    # contract_factory = contract.construct_contract_factory(web3, contract_data['abi'])
    # my_contract = contract_factory(address=address)
    result = my_contract.call().add(1, 2)
    print('add result:', result)

    print('proof timestamp:', my_contract.call().getProofName("aaaaa"))

    gas_limit = int(eth.getBlock('latest')['gasLimit'])
    # tx = my_contract.transact().proof("aaa", "bb", "cc")
    tx = my_contract.transact({'gas': int(gas_limit*0.9)}).proof("aaaaa", "bbb", "cc")
    print(tx)

    while True:
        receipt = eth.getTransactionReceipt(tx)
        if receipt is not None:
            break
        sleep(1)
    print('receipt:', receipt)

    from datetime import datetime

    now = datetime.now()
    print(now)
    new_data = str(now)
    print('try to set new data:', new_data)
    tx_list = []
    for i in range(1):
        tx = my_contract.transact().setData(new_data)
        # tx = my_contract.transact({'gas': 210000}).setData(new_data)
        tx_list.append(tx)
        print('tx hash:', tx)
    while len(tx_list) > 0:
        for tx in tx_list:
            receipt = eth.getTransactionReceipt(tx)
            if receipt is not None:
                tx_list.remove(tx)
                # print('tx receive:', receipt)
                print('tx %s receipt: blockNumber: %s, transactionIndex: %s' % (tx, receipt['blockNumber'], receipt['transactionIndex']))
        data = my_contract.call().getData()
        print('contract data:', data)

        if len(tx_list) > 0:
            sleep(2)
    print(datetime.now())
def authorize(from_user, to_user, file):
    from_address = from_user.wallets[0].address
    to_address = to_user.wallets[0].address
    file_hash = bytearray(unhexlify(file.hash))

    # unlock
    web3.personal.unlockAccount(from_address, from_user.wallets[0].key_origin, 0)

    gas_limit = web3.eth.getBlock('latest')['gasLimit']
    gas_estimate = my_contract.estimateGas({'from': from_address}).authorize(file_hash, to_address)

    if gas_estimate > gas_limit * 9 / 10:
        raise EthereumException()

    if get_balance(from_address) < gas_estimate * eth.gasPrice:
        raise BalanceException

    return my_contract.transact({'from': from_address}).authorize(file_hash, to_address)
def purchase(user, file):

    address = user.wallets[0].address
    price = to_wei(file.price)
    file_hash = bytearray(unhexlify(file.hash))

    # unlock
    web3.personal.unlockAccount(address, user.wallets[0].key_origin, 0)

    gas_limit = web3.eth.getBlock('latest')['gasLimit']
    gas_estimate = my_contract.estimateGas({'from': address, 'value': price}).purchase(file_hash)

    if gas_estimate > gas_limit * 9 / 10:
        raise EthereumException()

    if get_balance(address) < gas_estimate * eth.gasPrice:
        raise BalanceException

    return my_contract.transact({'from': address, 'value': price}).purchase(file_hash)
def submit_file(user, file):
    address = user.wallets[0].address
    price = web3.toWei(file.price, "ether")
    file_hash = bytearray(unhexlify(file.hash))
    owner = "0x0000000000000000000000000000000000000000"

    # unlock
    web3.personal.unlockAccount(address, user.wallets[0].key_origin, 0)

    gas_limit = web3.eth.getBlock('latest')['gasLimit']
    gas_estimate = my_contract.estimateGas({'from': address}).proof(file_hash, file.filename, file.description, file.for_sell, price, owner)

    if gas_estimate > gas_limit * 9 / 10:
        raise EthereumException()

    balance = get_balance(address)
    value = gas_estimate * eth.gasPrice
    if balance < value:
        raise BalanceException(to_ether(value), to_ether(balance))

    return my_contract.transact({'from': address}).proof(file_hash, file.filename, file.description, file.for_sell, price, owner)