I have contract Test.sol migrated to privatenet running on port '8545' which updates value of 'a'.
Test.sol
pragma solidity ^0.4.20;
contract Test{
uint public a=2;
function updateval(uint b) public {
a = a+ b;
}
function geta() public view returns(uint c){
c=a;
return c;
}
}
I am using python3.6 to call function updateval() from Test.sol.
contractcall.py
import json
import web3
import binascii
from web3 import Web3,HTTPProvider
from solc import compile_source
from web3.contract import ConciseContract
w3 = Web3(HTTPProvider('http://127.0.0.1:8545'))
abi= '''[
{
"constant": false,
"inputs": [
{
"name": "b",
"type": "uint256"
}
],
"name": "updateval",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "a",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "geta",
"outputs": [
{
"name": "c",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
'''
address = Web3.toChecksumAddress("0x09f2e386111647ff986439f488583f39e153c940") //ContractAddress
mywalletaddress = w3.eth.accounts[0]
Test = w3.eth.contract(address, abi=abi)
b=4
msg = w3.toHex(text='Hello World')
txnHash= Test.functions.updateval(int(b)).sendTransaction({'from' : mywalletaddress,'data' : msg })
print(txnHash)
I need to pass message 'Hello world' with transaction. I tried with the following code:
txnHash = Test.functions.updateval(b).buildTransaction({'from':mywalletaddress})
print(txnHash)
tx= w3.eth.sendTransaction(txnHash)//How to pass data field here??
print('tx',tx)
What is the correct method of implementing it. Help will be appreciated.
.buildTransaction()
outputs an unsigned transaction, not a hash of a transaction. If you want to send it immediately, you can useTest.functions.updateval(b).transact({'from': mywalletaddress})
– carver Aug 21 '18 at 17:54