0

I am able to get the transaction with a block number while calling method of a contract however the state variables don't change unless I mention the "address" and "gas" parameters on web3^0.20.7

HelloWorld.sol

contract HelloWorld {
  string message;
  event evntGetMessage(string message);

  constructor(string mymessage) public {
    message =  mymessage;
  emit evntGetMessage(message);
  }

  function getMessage() external view returns(string) {
    return message;
  }

  function welcomeMssg(string name) external returns (string){
     message =  string(abi.encodePacked(message, " ", name));
     emit evntGetMessage(message);
     return string(abi.encodePacked(message, " ", name));
  } 
}

txn.js

//txn.js file to call the contract instance from web3. I have deployed the contract using web3 JS:

contractInstance.welcomeMssg('Hi',{gas: 500000,from:txnAccount1})   //returning me the expected results and appending the state variable - "message"

contractInstance.welcomeMssg('Hi')    //returning me transaction block number but not updating the "message" variable

Is it the expected behavior in [email protected]?

Thanks

1
  • In web3.js versions v0.20 will use a default gas of 90k gas if none is specified. Since some operations on strings are complex it will cause out of gas. It is expected you supply the correct amount of gas.
    – Ismael
    Oct 31, 2018 at 20:29

1 Answer 1

0

In Web3 JS here's a way to make transaction.

In case Python WEB3

Using Web3 version 4.6.0

State variables are actually transactions. So you must use buidTransaction or transact to sign transactions.

w3 = //HTTPProvider
txn = contractInstance.functions.welcomeMssg("VALUE").
buildTransaction({'from': txnAccount1,
                  'gas': 90000,
                  'value': 0,
                  'gasPrice': 4100000000,
                  'nonce': w3.eth.getTransactionCount(txnAccount1)})

 raw_tx = {'nonce': txn['nonce'],
      'gas': txn['gas'],
      'gasPrice': txn['gasPrice'],
      'to': txn['to'],
      'data': txn['data'],
      'from': txn['from']}

 signed_txn = w3.eth.account.signTransaction(dict(raw_tx), txnAccount1KEY )
 w3.eth.sendRawTransaction(signed_txn.rawTransaction)
 tx_hash = Web3.toHex(Web3.sha3(signed_txn.rawTransaction))
 w3.eth.waitForTransactionReceipt(tx_hash)
 print("Transaction For HelloWorld Successfully Mined, Hash : {0}".format(tx_hash))
4
  • You are not answering the question about gas usage and strings.
    – Ismael
    Oct 31, 2018 at 20:27
  • actually state changing functions can not be set as VIEW in solidity. Such functions are actually transactions and transactions cannot be executed using functionName.CALL(). This is why I posted complete way to make a transaction and change variable state using sendTransaction/sendRawTransaction after BUILDTransaction/Transact() function. Nov 2, 2018 at 5:47
  • 1
    The question is why calling the same function with same arguments succeed whe gas is specified and fail otherwise.
    – Ismael
    Nov 2, 2018 at 17:03
  • @Ismael my bad. -_- Nov 3, 2018 at 5:19

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.