0

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 use Test.functions.updateval(b).transact({'from': mywalletaddress}) – carver Aug 21 '18 at 17:54
  • Thanks,but my issue is to pass a custom message with transaction.When I use transact({}),data field is overwritten by input parameters instead of showing custom message 'Hello World'. – angelmaria Aug 22 '18 at 4:22
1
txnHash = Test.functions.updateval(int(b)).sendTransaction(
  {'from' : mywalletaddress, 'data' : msg }
)

There are a few problems with this:

  1. sendTransaction isn't one of the ContractFunction methods. You're probably looking for transact()
  2. The data field of a transaction is used to invoke the function on the smart contract. There is no good reason to override it when building the function this way. Overriding will cause the contract function invocation to fail.

It's not clear why you:

need to pass message 'Hello world' with transaction.

You can't do that with the current contract. Perhaps you could add a string argument to the function you are calling, and then pass the string in, like:

function updateval(uint b, string message) public {
    a = a + b;
    // do something with message
}

Then you could send the transaction with:

txnHash = Test.functions.updateval(b, msg).sendTransaction(
  {'from' : mywalletaddress}
)
  • I need to view the custom message under txid in block explorer. Eg: If user purchases certain item. I'm storing those details in blockchain.But I need to view the purchased details when txid is filtered in block explorer...When I used js, the following code was working for me: web3.eth.sendTransaction({ from: fromaddress, to: shopaddress, value: web3.toWei(totalpay, "ether"), data : web3.toHex(pur) }); where 'pur' holds purchased details in json format...I need to implement same functionality in python script. – angelmaria Aug 28 '18 at 10:05
  • In that line of js code, you don't appear to be invoking a contract function at all then (unlike the question that tries to invoke updateval). In that case, you don't need to use a contract object at all, and can basically reuse the same syntax that you used in web3.js. – carver Aug 28 '18 at 16:38

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.