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