I have a contract that looks like this -

contract Calculator {
    uint public result;
    event Added(address caller, uint a, uint b, uint res);

    constructor() public {
        result = 777;

    function add(uint a, uint b) public returns (uint, address) {
        result = a + b;
        emit Added(msg.sender, a, b, result);
        return (result, msg.sender);

So I expected that invoking add function will add supplied two numbers and replace the old value persisted in result variable. But my invocation produces -

Before:  777
Transaction hash: 0xeef135d7838a2f6093918d3b055fe5ea2ce4c3c3c54c8a8c8328def95465f8e4
After:  777

Above transaction hash is valid and you can see details here.

Here is my code -

async function addWithTx() {
    return new Promise(async (resolve, reject) => {
        const senderAccount = rtUtil.getFirstAccount();
        const contract = await rtUtil.getCalculatorContract(senderAccount.calculatorContractAddress);

        let result = await contract.methods.result().call();
        console.log("Before: ", result);

        const accountPkBuffer = Buffer.from(senderAccount.accountPk, "hex");
        const data = await contract.methods.add(5, 74).encodeABI();

        const web3 = rtUtil.getWeb3();
        const taxCount = await web3.eth.getTransactionCount(senderAccount.accountAddress);

        const txData = {
            nonce: web3.utils.toHex(taxCount),
            to: rtUtil.getSecondAccount().calculatorContractAddress,
            value: web3.utils.toHex(web3.utils.toWei('0', 'ether')),
            gasLimit: web3.utils.toHex(2100000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('6', 'gwei')),
            data: data

        const tx = new EthereumTx(txData, {chain: 'ropsten'});
        const serializedTx = tx.serialize();
        const raw = web3.utils.toHex(serializedTx);

        const transaction = await web3.eth.sendSignedTransaction(raw);

        result = await contract.methods.result().call();
        console.log("After: ", result);


    .then(receipt => {
        console.log("Transaction hash: ", receipt.transactionHash);
    .catch(error => {

My questions are -

  1. Why the value is not persisted? Even though its a valid transaction and mined.
  2. If i don't supply the to field then a new contract creation transaction is created. Why the transaction is creating a new contract? What is the content of the contract?
  • Likely you are not sending transaction to a deployed contract. Either the contract is deployed or you are sending the transaction to a wrong address. Commented Dec 3, 2020 at 9:45
  • 1
    I doubt if that is the case. I am sending the transaction to 0xF5247E59cbB11FD3a5F377E608f25c641E84f37A and you can find it on Ropsten. Also by sending the transaction to wrong address if you mean the account address, then I don't think that is correct either, because here is the address I am using 0x136930AdF29C6425Fa19eA660b5A22c26bdaAFE8, and you can see that transaction on that account has been created as well. Of course the transaction signature is being verified which is signed by account private key. So I don't think any of those are wrong. Commented Dec 3, 2020 at 11:01
  • Thank you Jahid. Looks good. Can you verify your contract source code on EtherScan? In this case, you would get symbolic output from each transaction and you will see exactly what happens within your contract. Here is an example of symbolic output on Etherscan: etherscan.io/tx/… Commented Dec 3, 2020 at 11:22
  • I am bit confused. The link you gave is a transaction details, but not contract. Secondly, I actually dont know how to verify contract. I mean I just invoked the getter function result method, and which returns the default value. So isn't that enough to say that the contract is deployed and function is executed? Commented Dec 3, 2020 at 11:29
  • @JahidShohel Querying the contract result changed to 79 = 74 + 5 as expected, so the scripts is querying before the transaction is mined.
    – Ismael
    Commented Dec 3, 2020 at 20:46

1 Answer 1

  1. My hunch is that the code on ropsten may not match what you've shown, or the ABI being used by web3's Contract method isn't up to date.
  2. the contract "code" that is stored is just 0x (so: nothing), and its address is generated from the from account and its nonce. It's just what happens when you don't have a to address.
  • I just executed let result = await contract.methods.result().call(); and it is still giving me 777 (i mean the default value). So according to your loging, isn't that should return 79 now? Commented Dec 4, 2020 at 4:55
  • Hm, in that case I'm not sure what the issue is. Perhaps your contract code on ropsten isn't exactly as you expect for some reason. Commented Dec 4, 2020 at 16:20

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.