Ethereum Asked by anibis on August 26, 2021
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
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))
Answered by faran ahmed on August 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP