Example #1
0
def buy(id):

    contract = Contract.get_by_id(id) #instancio contrato 
    user = User.get_by_id(current_user.id) #instancio comprador
    if (contract.owner_id == current_user.id):
        flash('Usted es el dueƱo de este contrato')
        return render_template('newContract.html') 

    walletV =  Wallet.get_by_idownerunico(contract.owner_id) 
    acctV = w3.eth.account.privateKeyToAccount(walletV.key) #direccion vendedor

    walletC = Wallet.get_by_idownerunico(current_user.id)
    acctC = w3.eth.account.privateKeyToAccount(walletC.key) #direccion comprador

    signed_txn = w3.eth.account.signTransaction(dict(
    nonce=w3.eth.getTransactionCount(str(acctC.address)),
    gasPrice=w3.eth.gasPrice,
    gas=100000,
    to=str(acctV.address),  
    value=int(contract.price),
    data=b'',
    ),
    str(walletC.key),
    )
    tx_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    hash = w3.toHex(tx_receipt['transactionHash'])
    Contract.update_idowner(contract, current_user.id)
    Contract.onSale_False(contract)
    #contract_instance = w3.eth.contract(address= contract.address, abi = abi)
    #tx_hash2 = contract_instance.functions.setOwner(acctC.address).transact()

    #tx_receipt2 = w3.eth.waitForTransactionReceipt(tx_hash2)
    #hash2 = w3.toHex(tx_receipt2['transactionHash'])

    flash('Contract adquired')
    return render_template('newContract.html', hash=hash) #hash2 = hash2