2

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 ?

2
  • could you please share all your code to try to reproduce the results?, are you using infura?
    – Jaime
    Commented Jan 18, 2019 at 15:15
  • now i shared the whole code for this contract and the usage in web3 Commented Jan 18, 2019 at 15:25

3 Answers 3

2

Instead of :

proof = identitiescontract.functions.proofProduction(account2)

Use :

proof = identitiescontract.proofProduction(account2)

This should invoke the function and return value.

4
  • proof = identitiescontract.proofProduction(account2) AttributeError: type object 'Contract' has no attribute 'proofProduction' Commented Jan 19, 2019 at 14:39
  • You mentioned in answer that your are getting False from the function. Does that mean call is successful (though the logic is not working as you expect) ? Commented Jan 20, 2019 at 14:53
  • the call is succesfull but not what i aspect to get because it should be True Commented Jan 20, 2019 at 21:51
  • Add a public variables for debugging. I.e. A counter that increases every time you add a key + value pair to production. Apart from that comment line for "existProduction" in "proofProduction" function. Does that change anything ? Commented Jan 21, 2019 at 3:36
1

So guys finally i have the answer to solve the task:

The Output was False because i have to edit this line

tx_setProduction = contract_instance_identities.setProduction(account2, True)

And then add this line:

w3.eth.waitForTransactionReceipt(tx_setProduction)

Now i get the right answer for the proofProduction Function which should be True and thats how it looks like :)

Thank you for help

0

i get my function worked so that i get an output which is a boolean!!!

I increased the Gas for the contract to this:

tx_hash_identities = identitiescontract.deploy(transaction={'from': w3.eth.accounts[0], 'gas': 4100000})

and i added this to my code:

tx_receipt_identities = w3.eth.getTransactionReceipt(tx_hash_identities)
contract_address_identities = tx_receipt_identities['contractAddress']
contract_instance_identities = w3.eth.contract(abi=contract_interface_identities['abi'],
                                           address=contract_address_identities,
                                           ContractFactoryClass=ConciseContract)

After that i call the functions with:

contract_instance_identities.setProduction(account2, True)
proof = contract_instance_identities.proofProduction(account2)

But my output is the whole time False!!!

Why is my setProduction function not working as i expect?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.