[Ethereum] Use function return variable of smart contract in web3.py

contract-debuggingcontract-designcontract-developmentsolidityweb3.py

since im new to solidity programming i have some problem with executing my Contract.

My Contract looks like this:

# Solidity source code
contract_source_code = '''
pragma solidity ^0.4.1;

contract Identities {

event existProduction(bool);
address private owner;
mapping(address => bool) private production;

function Identities() public {
    owner = msg.sender;
}

function setProduction(address id, bool include) public {
    require(msg.sender == owner);
    production[id] = include;
}

function proofProduction(address id) public constant returns (bool) {
    existProduction(production[id]);
    return production[id];
}

I want to save an address of a production and want to set it 'TRUE' or 'FALSE' from another instance.

My web3.py code looks like this where i try to find out the boolean variable of an existing or not existing member:

compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol[':Greeter']
contract_interface_identities = compiled_sol[':Identities']

# web3.py instance
provider = HTTPProvider('http://127.0.0.1:8000')
w3 = Web3(provider)
w3.middleware_stack.inject(geth_poa_middleware, layer=0)
identitiescontract = w3.eth.contract(abi=contract_interface_identities['abi'],
                                     bytecode=contract_interface_identities['bin'])
tx_hash_identities = identitiescontract.deploy(transaction={'from': w3.eth.accounts[0]
w3.eth.waitForTransactionReceipt(tx_hash_identities)
account2 = w3.toChecksumAddress('0x1200f5a866b0889a816d808a82b4f3be30ba6ba2')
identitiescontract.functions.setProduction(account2, True)
proof = identitiescontract.functions.proofProduction(account2)
exist = identitiescontract.eventFilter('existProduction', {'fromBlock': 0, 'toBlock': 'latest'})

My EventFilter is empty and i cannot read out the events and also the proof variable dont give me some information like bool as i want to

Output is like this

Function proofProduction(address) bound to
('0x1200F5A866b0889A816D808A82B4f3bE30bA6bA2',)

Can someone help me how i get the return value out of a smart contract function ?

Best Answer

Instead of :

proof = identitiescontract.functions.proofProduction(account2)

Use :

proof = identitiescontract.proofProduction(account2)

This should invoke the function and return value.

Related Topic