1

In my contract, I expect that my_contract.a() will not make any updates to values in the blockchain, but my_contract.change_value(new_val) should. However, change_value() is not making the change I expect.

How do I get the value in my contract to change?

My full code for interacting with the contract is below:

web3 = Web3(HTTPProvider('http://localhost:8545'))
print('Last block is ' + str(web3.eth.blockNumber))
print('Accounts: ' + str(web3.eth.accounts))
print('Unlock account: ' + str(web3.personal.unlockAccount(web3.eth.accounts[0], '123', 10000)))

abi_raw='''[{"constant":true,"inputs":[],"name":"a","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getValue","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newValue","type":"int256"}],"name":"change_value","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_newValue","type":"int256"}],"name":"NewValue","type":"event"}]'''
abi = json.loads(abi_raw)
bytecode = '6060604052341561000f57600080fd5b5b60646000555b5b61013a806100266000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630dbe671f811461005357806320965255146100785780635e21b8601461009d575b600080fd5b341561005e57600080fd5b6100666100c5565b60405190815260200160405180910390f35b341561008357600080fd5b6100666100cb565b60405190815260200160405180910390f35b34156100a857600080fd5b6100666004356100d2565b60405190815260200160405180910390f35b60005481565b6000545b90565b6000818155817fd9c5893d0867e0ea2c1c5251cb2bc831b6121f6db93f76fa1db9cca46e2f99b960405160405180910390a2506000545b9190505600a165627a7a723058204820bc0702b61905d788684d628adc4cc3a320161ead0a1a3268b1c715fbaa740029'
tokenContract = web3.eth.contract(abi, bytecode=bytecode)
tx_deploy_hash = tokenContract.deploy(transaction={"from":web3.eth.accounts[0]})

# wait for contract deployment transaction to be mined
txn_receipt = web3.eth.getTransactionReceipt(tx_deploy_hash)
while txn_receipt is None:
    time.sleep(1)
    txn_receipt = web3.eth.getTransactionReceipt(tx_deploy_hash)

contract_address = txn_receipt['contractAddress']
my_contract = web3.eth.contract(abi=abi,
                                address=contract_address,
                                ContractFactoryClass=ConciseContract)

result = my_contract.a()
print('a = ' + str(result))

b = my_contract.change_value(20)
print('return from function - ' + str(b))

result2 = my_contract.a()
print('a = ' + str(result2))

Running the above code prints the following:

Contract address - 0xbf71e4662e3fc46f0522ea4a1bb425505b072a90
a = 100
return from function - 20
a = 100
result from func getValue - 100

Why can't I change the value?

The Solidity code for this contract is in this screenshot

0

In order to change a value, you want to issue a transaction instead of making a call.

The ConciseContract Way

To issue a transaction using ConciseContract, add the transact keyword argument with a dictionary. You probably want to specify some transaction variables, like gasPrice. Strictly speaking, you don't need to specify any variables, meaning you can pass in an empty {}.

In the question's example, that means changing the line from:

b = my_contract.change_value(20)

to:

b = my_contract.change_value(20, transact={})

Two more notes:

  • When issuing a transaction, the method call will not return the value returned by your Solidity function. It will return the transaction hash instead.
  • You will have to wait for the transaction to be mined, before the call will reflect the value.

The classic Contract Way

If you prefer the default contract API, it requires a little extra code during calls, but about the same amount for transactions.

It looks like this:

result = my_contract.call().a()
b = my_contract.transact().change_value(20)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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